- Author
- Charles F. F. Karney (charl.nosp@m.es@k.nosp@m.arney.nosp@m..com)
- Version
- 1.44
Abstract
This is a C implementation of the geodesic algorithms from GeographicLib. This is a self-contained library (requiring only the standard C math library) which makes it easy to do geodesic computations for an ellipsoid of revolution in a C program. It uses ANSI C as described in B. W. Kernigan and D. M. Ritchie, The C Programming Language, 2nd Ed. (Prentice Hall, 1988), and so should compile correctly with just about any C compiler.
Downloading the source
The C library is part of GeographicLib which available for download at
as either a compressed tar file (tar.gz) or a zip file. After unpacking the source, the C library can be found in GeographicLib-1.44/legacy/C. The library consists of two files geodesic.c and geodesic.h.
The library is also included as part of proj.4 starting with version 4.9.0, where it is used as the computational backend for geod(1). Instructions for how to use the library via proj.4 are given below.
Library documentation
The interface to the library is documented via doxygen in the header file. To access this, see geodesic.h.
Sample programs
Also included are 3 small test programs:
- direct.c is a simple command line utility for solving the direct geodesic problem;
- inverse.c is a simple command line utility for solving the inverse geodesic problem;
- planimeter.c is a simple command line utility for computing the area of a geodesic polygon given its vertices.
Here, for example, is inverse.c
#include <stdio.h>
#if defined(_MSC_VER)
# pragma warning (disable: 4996)
#endif
double a = 6378137,
f = 1/298.257223563;
double lat1, lon1, azi1, lat2, lon2, azi2, s12;
while (scanf("%lf %lf %lf %lf", &lat1, &lon1, &lat2, &lon2) == 4) {
geod_inverse(&g, lat1, lon1, lat2, lon2, &s12, &azi1, &azi2);
printf("%.15f %.15f %.10f\n", azi1, azi2, s12);
}
return 0;
}
To compile, link, and run this, you would typically use
cc -o inverse inverse.c geodesic.c -lm
echo 30 0 29.5 179.5 | ./inverse
These sample programs can also be built with the supplied cmake file, CMakeLists.txt, as follows
mkdir BUILD
cd BUILD
cmake ..
make
echo 30 0 29.5 179.5 | ./inverse
Alternatively, if you have proj.4 installed, you can compile and link with
cc -c inverse.c
cc -o inverse inverse.o -lproj
echo 30 0 29.5 179.5 | ./inverse
If proj.4 is installed, e.g., in /usr/local, you might have to use
cc -c -I/usr/local/include inverse.c
cc -o inverse inverse.o -lproj -L/usr/local/lib -Wl,-rpath=/usr/local/lib
echo 30 0 29.5 179.5 | ./inverse
Using the library
- Put in your source code. If you are using the library via proj.4, change this to
- Make calls to the geodesic routines from your code. The interface to the library is documented in geodesic.h.
- Compile and link as described above.
- If linking with proj.4, you might want to check that the version of proj.4 contains the geodesic routines. You can do this with
#include <proj_api.h>
#if PJ_VERSION >= 490
#endif
...
- You can check the version of the geodesic library with, e.g.,
#if GEODESIC_VERSION >= GEODESIC_VERSION_NUM(1,40,0)
...
#endif
External links
Change log
- Version 1.44 (released 2015-08-14)
- Improve accuracy of calculations by evaluating trigonometric functions more carefully and replacing the series for the reduced length with one with a smaller truncation error.
- The allowed ranges for longitudes and azimuths is now unlimited; it used to be [−540°, 540°).
- Enforce the restriction of latitude to [−90°, 90°] by returning NaNs if the latitude is outside this range.
- The inverse calculation sets s12 to zero for coincident points at pole (instead of returning a tiny quantity).