Search code examples
pythondjangogeodjango

GeoDjango: Move a coordinate a fixed distance in a given direction (e.g. move a point east by one mile)


Given a Point (django.contrib.gis.geos.Point) how do I find another point 1 mile east from that Point object?

(A) ---[ 1 mile ]---> (B)

My failed attempts:

  • I've considered trying to go hardcode the 1 mile east point but due to the curvature of the earth. The miles per degree change.

  • Copius googling on SO and Geo Info Exchange. I assume I cannot find the terminology?


  • Python 3.6
  • Django 1.11

Solution

  • You can accomplish this using the Geod class of pyproj.

    from pyproj import Geod
    geoid = Geod(ellps='WGS84')
    
    def add_distance(lat, lng, az, dist):
        lng_new, lat_new, return_az = geoid.fwd(lon, lat, az, dist)
        return lat_new, lng_new
    
    • lng: starting longitude
    • lat: starting latitude
    • az: azimuth, the direction of movement
    • dist: distance in meters

    So for your specific question, you could do the following:

    from django.contrib.gis.geos import Point
    
    def move_point_mile_east(point):
        dist = 1609.34
        lat, lng = add_distance(point.y, point.x, 90, dist)
        return Point(lng, lat)