geocoding.py 758 B

12345678910111213141516171819202122232425262728
  1. from geopy.geocoders import Nominatim
  2. from geopy.distance import vincenty
  3. geolocator = Nominatim()
  4. def position_of_location(location):
  5. """returns a pair (latitude, longitude) from an adress
  6. or None if not possible"""
  7. try:
  8. pos = geolocator.geocode(location)
  9. return (pos.latitude, pos.longitude)
  10. except:
  11. return None
  12. def dist(posa, posb):
  13. """compute the distance between two position (pair latitude, longitude)
  14. in kilometers
  15. or None if one of the argument is None"""
  16. if not posa or not posb:
  17. return None
  18. else:
  19. return vincenty(posa, posb).km
  20. p1 = position_of_location("22 rue Henri Barbusse VILLEJUIF")
  21. p2 = position_of_location("Université paris diderot")
  22. print(p1, p2, dist(p1, p2))