In a previous post, I discussed calculating distances based on GPS coordinates, subsequently I came across a good python library that offers this functionality as well as a lot more. I decided that anybody that is interested in the distance calculation via GPS coordinates will probably appreciate everything else that the GeoPy library has to offer.
In addition to the distance calculation, GeoPy offers a client functionality to access several popular geocoding web servers.
From the Readme file of the package’s GitHub site:
geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources.
geopy includes geocoder classes for the OpenStreetMap Nominatim, Google Geocoding API (V3), and many other geocoding services.
Here is my previous example coded with the GeoPY library
from geopy.distance import great_circle
Dublin = (53.35, -6.27)
SanFrancisco=(37.78, -122.42)
great_cicle_dis = great_circle(Dublin, San Francisco).kilometers
geodesic_dis = geodesic(Dublin, San Francisco).kilometers
Cities = { 'Vancouver':(49.25,-123.1),
'Portland':(45.52,-122.68),
'San Francisco':(37.78, -122.42),
'Seattle':(47.62, -122.33),
'San Antonio':(29.42, -98.5),
'Dallas':(32.78,-96.8),
'Austin':(30.25,-97.75),
'Dublin':(53.35, -6.27),
'Sevilla':(37.38, -5.98),
'Belfast':(54.6,-5.93),
'Sydney':(-33.87, 151.22),
'Canberra':(-35.3, 149.12),
'Tokyo':(35.68, 139.7)}
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
Cnames = ['Vancouver', 'Portland', 'San Francisco', 'Seattle',
'San Antonio', 'Dallas', 'Austin', 'Sevilla',
'Belfast', 'Sydney', 'Canberra', 'Tokyo']
nodes = []
for name in Cnames:
nodes.append(name)
G.add_node(name)
G.add_edge(name,'Dublin')
distance = great_circle(Cities['Dublin'],Cities[name]).kilometers
G.edges['Dublin',name]['distance'] = round(distance,1)
plt.figure(figsize=(8, 8))
pos = nx.spring_layout(G)
nx.draw(G,nodelist=nodes,with_labels=True,pos=pos)
_ = nx.draw_networkx_edge_labels(G,pos=pos)
A complete example is provided in the following GitHub page
The GeoPy library is well worth reviewing as it also have a litany of functions which I did not cover here including:
Geocoding is provided by a number of different services, which are not affiliated with geopy. These services provide APIs, which anyone could implement, and geopy is a library which provides these implementations for many different services in a single package.