Search code examples
pythonpython-2.7gpscoordinatesgeopy

How to find nearby points given a gps origin?


How does one find a nearby point given a gps coordinate? The new point can be randomly chosen, but must be within 100 meters of the origin. Collisions are fine as well.

ie,

origin = (latitude, longitude)

# prints one nearby point in (lat, lon)
print "%s, %s" % get_nearby_point(origin)

# prints another nearby point in (lat, lon)
print "%s, %s" % get_nearby_point(origin)

Solution

  • import geopy.distance
    from random import random
    
    def get_nearby_point(origin):
        dist = geopy.distance.VincentyDistance(kilometers = .1)
        pt = dist.destination(point=geopy.Point(origin), bearing=random()*360)
        return pt[0],pt[1]
    
    origin = (34.2234, 14.252) # I picked some mostly random numbers
    
    # prints one nearby point in (lat, lon)                                         
    print "%s, %s" % get_nearby_point(origin)
    
    # prints another nearby point in (lat, lon)                                     
    print "%s, %s" % get_nearby_point(origin)
    

    Results:

    $ python nearby.py 
    34.2225717618, 14.2524285475
    34.2225807815, 14.2524529774
    

    I learned how to do this from here. It's not an exact duplicate, but it's enough to format this answer. You define a distance using the distance function then you move off a point, the input to the function, in a random direction.