GeographicLib  1.37
Implementations in other languages
Back to Code organization. Forward to Geoid height. Up to Contents.

Implementations of subsets of GeographicLib are available in other languages

# C and Fortran implementation

The directories legacy/C and legacy/Fortran contain implementations of Geodesic, GeodesicLine, and PolygonArea in C and Fortran respectively. These are intended for use in old codes written in these languages and should work any reasonably modern compiler. These implementations are entirely self-contained and do not depend on the rest of GeographicLib. Sample main programs to solve the direct and inverse geodesic problems and to compute polygonal areas are provided.

For documentation, see

# Java implementation

The directory java contains implementations of Geodesic, GeodesicLine, and PolygonArea in Java. This implementation is entirely self-contained and does not depend on the rest of GeographicLib. Sample main programs to solve the direct and inverse geodesic problems and to compute polygonal areas are provided.

For documentation, see

# JavaScript implementation

The directory doc/scripts/GeographicLib contains the classes

translated into JavaScript. See Interface.js for a simple JavaScript interface to these routines (documented near the top of the file). Examples of using this interface are

• a geodesic calculator showing the solution of direct and inverse geodesic problem, finding intermediate points on a geodesic line, and computing the area of a geodesic polygon.
• displaying geodesics in Google Maps which shows the geodesic, the geodesic circle, and various geodesic envelopes.

These examples include a "stripped" version of the JavaScript code,

<script type="text/javascript"
src="http://geographiclib.sf.net/scripts/geographiclib.js">
</script>

# Python implementation

A python implementation of the geodesic routines from GeographicLib are provided in the python/geographiclib directory (which is installed as PREFIX/lib/python/site-packages/geographiclib, if COMMON_INSTALL_PATH is ON, and as PREFIX/python/geographiclib, otherwise). This contains implementations of the classes

You can also download the python interface independent of the rest of GeographicLib from

and then unpack the .tar.gz or .zip file.

You can "install" these routines, so that they are in python's default path with, for example

  cd geographiclib-1.16
python setup.py install


(this will require root privileges). Or else you can set the path within python using

>>> import sys
>>> sys.path.append("/usr/local/lib/python/site-packages")

An example of using this interface is

>>> from geographiclib.geodesic import Geodesic
>>> # The geodesic inverse problem
... Geodesic.WGS84.Inverse(-41.32, 174.81, 40.96, -5.50)
>>> # The geodesic direct problem
... Geodesic.WGS84.Direct(40.6, -73.8, 45, 10000e3)
>>> # How to obtain several points along a geodesic
... line = Geodesic.WGS84.Line(40.6, -73.8, 45)
>>> line.Position( 5000e3)
>>> line.Position(10000e3)
>>> # Computing the area of a geodesic polygon
... def p(lat,lon): return {'lat': lat, 'lon': lon}
...
>>> Geodesic.WGS84.Area([p(0, 0), p(0, 90), p(90, 0)])
>>> # Introductory help
... help(Geodesic)

Another illustrative exercise is finding the point midway between JFK Airport to Singapore Changi Airport

from geographiclib.geodesic import Geodesic
# Coordinates of airports
lat1, lon1 = 40.640, -73.779 # JFK
lat2, lon2 = 1.359, 103.989 # SIN
# Compute path from 1 to 2
g = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
# Compute midpoint starting at 1
h = Geodesic.WGS84.Direct(lat1, lon1, g['azi1'], g['s12']/2)
print(h['lat2'], h['lon2']);

(Note: The initial version of setup.py was provided by Andrew MacIntyre of the Australian Communications and Media Authority.)

# Matlab and Octave implementations

The matlab directory contains

• Native Matlab implementations of the geodesic routines. To use these, start Matlab or Octave and run one of (for example)
   addpath /usr/local/libexec/GeographicLib/matlab
addpath 'C:/pkg-vc10-x64/GeographicLib-1.37/libexec/GeographicLib/matlab'
The available functions are:
• geoddoc: briefly descibe the routines
• geodreckon: solve the direct geodesic problem
• geoddistance: solve the inverse geodesic problem
• geodarea: compute the area of ellipsoidal polygons
Use the help function to get documentation, e.g.,
help geoddistance
to obtain documentation. These functions are also available as a standalone package from Matlab File Exchange using the link
• Native Matlab implementations of projections which are related to geodesics. These are
• geodproj: briefly descibe the routines
• eqdazim_fwd, eqdazim_inv: azimuthal equidistant
• cassini_fwd, cassini_inv: Cassini-Soldner
• tranmerc_fwd, tranmerc_inv: transverse Mercator
• gnomonic_fwd, gnomonic_inv: ellipsoidal gnomonic
• utm_fwd, utm_inv: universal transverse Mercator
These functions are also available as a package from Matlab File Exchange using the link (This requires that the previous package also be installed.)
• Interface code so that some GeographicLib classes can be accessed from Matlab or Octave. The rest of this section describes how to compile and use these interfaces.

There are two ways of compiling the interface code: (1) using cmake and (2) invoking the compiler from Matlab.

• Using cmake: Before running cmake, configure MATLAB on Windows to use the same compiler that you're going to use for compiling GeographicLib. For example
   mex.bat -setup

Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Microsoft Visual C++ 2012 in C:\Program Files (x86)\Microsoft Visual Studio 11.0
[2] Microsoft Visual C++ 2010 in C:\Program Files (x86)\Microsoft Visual Studio 10.0

[0] None

Compiler: 2
etc. 
(This will require that mex.bat is in your PATH. With Linux, use mex -setup.) Then configure cmake with, for example
   cmake -G "Visual Studio 10" -D MATLAB_COMPILER=mex ..
cmake --build . --config Release --target matlabinterface 
(Note that only the Release configuration is supported for Matlab.) If you are running a 64-bit version of Matlab, be sure to select a 64-bit generator with cmake, e.g., "Visual Studio 10 Win64". Finally compile GeographicLib with Visual Studio. (The binary installer for 64-bit Windows includes the compiled interface built with Visual Studio 10 and Matlab R2013a 64-bit).
On Linux systems, you can compile the interface for use with octave instead by using
   cmake -D MATLAB_COMPILER=mkoctfile ..
make matlabinterface 
• Invoking the compiler from Matlab or Octave: Start Matlab or Octave and run, e.g.,
mex -setup
cd 'C:/pkg-vc10-x64/GeographicLib-1.37/matlab'
help geographiclibinterface
geographiclibinterface('C:/pkg-vc10/GeographicLib-1.37');
The first command allows you to select the compiler to use (which should be the same as that used to compile GeographicLib).

To use the interface routines for GeographicLib, run one of (for example)

  addpath /usr/local/libexec/GeographicLib/matlab


in Octave or Matlab. The available functions are:

• geodesicdirect: solve direct geodesic problem (see Geodesic::Direct)
• geodesicinverse: solve inverse geodesic problem (see Geodesic::Inverse)
• geodesicline: compute points along a geodesic (see GeodesicLine::Position)
• polygonarea: compute area of a geodesic polygon (see PolygonArea)
• utmupsforward: convert geographic coordinates to UTM/UPS (see UTMUPS::Forward)
• utmupsreverse: convert UTM/UPS coordinates to geographic (see UTMUPS::Reverse)
• mgrsforward: convert UTM/UPS coordinates to MGRS (see MGRS::Forward)
• mgrsreverse: convert MGRS coordinates to UTM/UPS (see MGRS::Reverse)
• geoidheight: compute geoid height (see Geoid::operator()())
• geocentricforward: convert geographic coordinates to geocentric (see Geocentric::Forward)
• geocentricreverse: convert geocentric coordinates to geographic (see Geocentric::Reverse)
• localcartesianforward: convert geographic coordinates to local cartesian (see LocalCartesian::Forward)
• localcartesianreverse: convert local cartesian coordinates to geographic (see LocalCartesian::Reverse)

These routines just offer a simple interface to the corresponding C++ class. Use the help function to get documentation, e.g.,

help geodesicdirect

Unfortunately, the help function does not work for compiled functions in Octave; in this case, just list the .m file, e.g.,

type geodesicdirect

Other useful functions, e.g., to convert from geographic coordinates to MGRS can easily be written with Matlab code.

Note that geoidheight, when compiled with Visual Studio 2008 causes Matlab to crash. (The problem does not occur with Visual Studio 2005 or Visual Studio 2010.)

# Maxima routines

Maxima is a free computer algebra system which can be downloaded from http://maxima.sf.net. Maxima was used to generate the series used by TransverseMercator (tmseries.mac) and Geodesic (geod.mac) and to generate accurate data for testing (tm.mac and geodesic.mac). The latter uses Maxima's bigfloat arithmetic together with series extended to high order or solutions in terms of elliptic integrals (ellint.mac). These files contain brief instructions on how to use them.

# .NET wrapper

This is a comprehensive wrapper library, written and maintained by Scott Heiman, which exposes all of the functionality of GeographicLib to the .NET family of languages. For documentation, see

Back to Code organization. Forward to Geoid height. Up to Contents.