If you're interested in making both forward and reverse geocoding better, please consider paying attention to a project I started and help maintain called OpenAddresses: http://openaddresses.io The goal is to collect address datasets so that forward and reverse geocoding is an easier problem to solve. A contributor wrote an excellent overview of the project the other day: https://medium.com/colemanm/creating-an-open-…
Very cool project. Out of curiosity, how do you test the validity of the addresses?
A fast, offline reverse geocoder in Python
21–30 of 46 posts
Re: A fast, offline reverse geocoder in Python
#22Re: A fast, offline reverse geocoder in Python
#23One question - is it OK to put an MIT license on something that is based on LGPL code? I don't know enough about how the LGPL works (I do know it is less "infective" than plain GPL).
Well two questions: python2, or python3?
Re: A fast, offline reverse geocoder in Python
#24Kudos for a very well done README (and it's not just cribbed from the original project, it explains the new stuff very well and tells what the project is, and gives credit back). So many projects neglect the README. One question - is it OK to put an MIT license on something that is based on LGPL code? I don't know enough about how the LGPL works (I do know it is less "infective" than plain GPL). Well two questions: p…
Good question regarding the license. I'm not too sure about that. I'd appreciate it if someone could shed some light on it.
Regarding the version, I've only tested it on python2. I should add that in the README. Thanks!
Re: A fast, offline reverse geocoder in Python
#25If you're interested in making both forward and reverse geocoding better, please consider paying attention to a project I started and help maintain called OpenAddresses: http://openaddresses.io The goal is to collect address datasets so that forward and reverse geocoding is an easier problem to solve. A contributor wrote an excellent overview of the project the other day: https://medium.com/colemanm/creating-an-open-…
Re: A fast, offline reverse geocoder in Python
#26Kudos for a very well done README (and it's not just cribbed from the original project, it explains the new stuff very well and tells what the project is, and gives credit back). So many projects neglect the README. One question - is it OK to put an MIT license on something that is based on LGPL code? I don't know enough about how the LGPL works (I do know it is less "infective" than plain GPL). Well two questions: p…
Thanks for that comment! Good question regarding the license. I'm not too sure about that. I'd appreciate it if someone could shed some light on it. Regarding the version, I've only tested it on python2. I should add that in the README. Thanks!
Re: A fast, offline reverse geocoder in Python
#27First, longitude wraps from -180 to +180 at antimeridian, meaning distance calculations will fail there; second, and I'd say more importantly, one degree longitude length in meters differs a lot depending on latitude; meaning this library will be heavily biased towards longitudal neighbors when using it for locations far from equator.
Re: A fast, offline reverse geocoder in Python
#28You can't really use KD trees with lat/lon coordinates, at least you can't use euclidean distance there for nearest neighbor search. First, longitude wraps from -180 to +180 at antimeridian, meaning distance calculations will fail there; second, and I'd say more importantly, one degree longitude length in meters differs a lot depending on latitude; meaning this library will be heavily biased towards longitudal neighb…
On it!
Re: A fast, offline reverse geocoder in Python
#29Re: A fast, offline reverse geocoder in Python
#30You can't really use KD trees with lat/lon coordinates, at least you can't use euclidean distance there for nearest neighbor search. First, longitude wraps from -180 to +180 at antimeridian, meaning distance calculations will fail there; second, and I'd say more importantly, one degree longitude length in meters differs a lot depending on latitude; meaning this library will be heavily biased towards longitudal neighb…
Good point. I would need to use this distance function instead: http://www.movable-type.co.uk/scripts/latlong.html On it!
import math
from collections import namedtuple
def haversine_distance(origin, destination):
""" Haversine formula to calculate the distance between two lat/long points on a sphere """
radius = 6371 # FAA approved globe radius in km
dlat = math.radians(destination.lat-origin.lat)
dlon = math.radians(destination.lng-origin.lng)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(origin.lat)) \
* math.cos(math.radians(destination.lat)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
# Return distance in km
return int(math.floor(d))
LatLng = namedtuple('LatLng', 'lat, lng')
origin = LatLng(51.507222, -0.1275) # London
destination = LatLng(37.966667, 23.716667) # Athens
print "Distance (km): %d" % haversine_distance(origin, destination)