Feature request

4 views
Skip to first unread message

Lu Jik

unread,
Aug 1, 2008, 11:37:48 AM8/1/08
to geopy
First of all thanks for all the hard work you have put into geopy.

In the project I am working on at the moment I need to find all the
towns within eg 10km of a geographical point. I have a database of all
the towns in the country in question. But obviously going through them
all every time I need to find the towns in the area is VERY slow. So
I'm wanting to get a rough squire around the point in co-ordinates so
I can fetch a small number of entries from the database to run the
geopy.distance.distance method on. With the formula I am using the
higher up the country I go the smaller the difference becomes (I am in
the northern hemisphere). So the squire becomes squashed from the
sides.

So the feature request if for a method in geopy that given a co-
ordinate and distance will return two co-ordinates that make a squire
around the point.

This is the faulty method I have at the moment.

def nearby(self, co_lat, co_lng, km_distance):
earth_radius = float(6378) #km

lng_diff = math.asin(math.sin(km_distance / earth_radius) /
math.cos(co_lat))
max_lng = float(co_lng + lng_diff)
min_lng = float(co_lng - lng_diff)

lat_diff = (180 / math.pi) * (km_distance / earth_radius)
max_lat = float(co_lat + lat_diff)
min_lat = float(co_lat - lat_diff)

print co_lat, co_lng
print min_lat, min_lng
print max_lat, max_lng

Thank you
Simon

Lu Jik

unread,
Aug 2, 2008, 8:44:42 AM8/2/08
to geopy
A friend has informed me that the maths is wrong in the method I sent.

You should replace
lng_diff = math.asin(math.sin(km_distance / earth_radius) /
math.cos(co_lat)) #Returns radians :(
with
lng_diff = (180 / math.pi) * (km_distance / earth_radius) /
math.cos(co_lat * math.pi / 180) #Returns degrees :)

gnig

unread,
Aug 8, 2008, 5:01:57 PM8/8/08
to geopy
A related feature that would be very interesting is to implement a
function that takes the set of (lat, long, city name) and a point
(lat, long), and find the nearest city to this point. I have looked at
kdtrees but do not know how I can use a kdtree with lat/long
coordinates. If someone can provide an algorithmic explanation I'd be
happy to come up with a first cut of the implementation.
Reply all
Reply to author
Forward
0 new messages