tempomax
tempomax

Reputation: 793

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)

Upvotes: 3

Views: 1155

Answers (1)

James
James

Reputation: 2711

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.

Upvotes: 4

Related Questions