Django Calculate Latitude Longitude Distance with Native (Pure) Python Library

  • Posted on: 15 November 2017
  • By: oon
Django Calculate Latitude Longitude Distance with Native (Pure) Python Library

If you try to find library on Google about how python (or Django, Flask) can easily calculate latitude longitude points distance, you probably got geopy or pyproj.

Yes, previously I used pyproj [1], because years ago I have quite alot experience with Proj4 library [2]. And it's quite hard to use pyproj in windows, you have to install SDK an so on.

What a great project (pyproj) because of the precision of its calculation.

don't belive me? it's ok, you can visit this geodesic calculation [3] to prove it. Or follow this discussion [6].

But then I need to bring my application to Google App Engine (standard environment), which basically I need pure python implementation (no C or C++ extensions) of the library, because pyproj is interface to Proj4 library which built on C programming language.

After read more careful on the geodesic calculation library [3], I found there's native python implementation for geodesic calculation, the library called geographiclib [4].

But how hard to implement latitude longitude points calculation? quite easy, just follow itis example [5].

Example: Distance from Jakarta (6.1751° S, 106.8650° E) to Makassar (5.1477° S, 119.4327° E), location by google search.

from geographiclib.geodesic import Geodesic

geod = Geodesic.WGS84  # define the WGS84 ellipsoid
g = geod.Inverse(-6.1751, 106.8650, -5.1477, 119.4327)  # JKT-MKS

print 'JKT-MKS distance: {:.3f} m.'.format(g['s12'])
# the output will be: JKT-MKS distance: 1396833.076 m.

and yes, I have tested it running well on Google App Engine standard environment. :-)

oh btw, will update the article about the Django implementation. As simple as, pip install geographiclib, then put it on the views.

[1]https://github.com/jswhit/pyproj
[2]http://proj4.org/
[3]http://proj4.org/geodesic.html
[4]https://geographiclib.sourceforge.io/
[5]https://geographiclib.sourceforge.io/html/python/examples.html
[6]https://gis.stackexchange.com/questions/84885/whats-the-difference-betwe...

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.