C library for Geodesics  2.1
Use as a standalone library

The PROJ library (especially as provided as part of a Linux distribution) may lag behind the code documented here by about a year.

Download the source code from as a tarball from

or check out the source code from

Build the code with cmake

cmake -B BUILD -S .
cd BUILD
make
make test

possibly including some options via -D:

  • CONVERT_WARNINGS_TO_ERRORS warnings are fatal (default ON)
  • BUILD_DOCUMENTATION look for doxgen and build documentation (default ON)
  • BUILD_SHARED_LIBS make a shared (instead of static) library (default ON)

CMake code to install the library is not provided.

The library consists of two files geodesic.c and geodesic.h.

Licensed under the MIT/X11 License; see LICENSE.txt.

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

/**
* @file inverse.c
* @brief A test program for geod_inverse()
**********************************************************************/
#include <stdio.h>
#include "geodesic.h"
#if defined(_MSC_VER)
/* Squelch warnings about scanf */
# pragma warning (disable: 4996)
#endif
/**
* A simple program to solve the inverse geodesic problem.
*
* This program reads in lines with lat1, lon1, lat2, lon2 and prints out lines
* with azi1, azi2, s12 (for the WGS84 ellipsoid).
**********************************************************************/
int main() {
double a = 6378137, f = 1/298.257223563; /* WGS84 */
double lat1, lon1, azi1, lat2, lon2, azi2, s12;
struct geod_geodesic g;
geod_init(&g, a, f);
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;
}
int main()
Definition: direct.c:21
API for the geodesic routines in C.
void GEOD_DLL geod_init(struct geod_geodesic *g, double a, double f)
void GEOD_DLL geod_inverse(const struct geod_geodesic *g, double lat1, double lon1, double lat2, double lon2, double *ps12, double *pazi1, double *pazi2)
double f
Definition: geodesic.h:88
double a
Definition: geodesic.h:87

Use inverse, for example, with

echo -30 0 29.5 179.5 | ./tools/inverse