Distances#
Geopy provides a tool for calculating distances on maps.
from geopy import distance
from geopy.geocoders import Nominatim
Compution#
You need to use the geopy.distance.distance
function to calculate the distance between two points. In the following example the distance between Minsk and Molodechno has been calculated - it looks quite close.
geolocator = Nominatim(user_agent = "knowledge")
location1 = geolocator.geocode("Minsk")
location2 = geolocator.geocode("Molodechno")
my_distance = distance.distance(
(location1.latitude, location1.longitude),
(location2.latitude, location2.longitude)
)
print(f"Minsk<->Molodechno distance is {my_distance.kilometers} km")
Minsk<->Molodechno distance is 64.89474666491046 km
Distance object#
geopy.distance.distance
returns an object of type geopy.distance.geodesic
. The main properties of the object are displayed in the following cell.
for field in dir(my_distance):
if field[0] != "_":
print(field)
ELLIPSOID
destination
ellipsoid_key
feet
ft
geod
kilometers
km
m
measure
meters
mi
miles
nautical
nm
set_ellipsoid