/ tuesdaytooling

Tooling Tuesday - Python3 GeoPy

So you want to find something on a map, query a location and use the data in Python 3? I got your back!

Search for address and Google Map appears

So what is it?

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."

So how do I install it?

Using pip3 in a terminal / command prompt

sudo pip3 install geopy

Show me how to use it!

So for a quick example project lets use GeoPy with the webbrowser library which can create new web browser instances and create new tabs in any browser (We covered this excellent library back in Feb 2018)
Using GeoPy and webbrowser we shall create a simple tool that will ask for an address, then supply the longitude and latitude of that address, which are then used to open a Google map straight to that position. In eleven nine lines of Python 3 code! (I refactored the code this morning.)

So first we import the geopy and webbrowser libraries.

from geopy.geocoders import Nominatim
import webbrowser

Next we use the Nominatim Open Street Map search tool to search via address / post / zip code.

geolocator = Nominatim()

Next we create a variable called address and use that to store the keyboard input (the address details) that the user types.

address = input("Give me an address :")

Now we create an object called location that stores the location data for our chosen address.

location = geolocator.geocode(address)

Now lets extract the latitude and longitude data which is output as floats (numbers with decimal places), convert the data to strings and store them in variables.

latitude = str(location.latitude)
longitude = str(location.longitude)

The URL for our web browser is made up of the Google maps URL, with the latitude and longitude joined (concatenated) to create a full URL that will resolve correctly. The reason that we converted the latitude and longitude data from floats to strings is that you can only concatenate like for like. So string to string, float to float, integer to integer.

url = "https://www.google.co.uk/maps/@"+latitude+","+longitude+","+"18z"

Try changing the "18z" value and see what happens!

Lastly we tell the code to open the browser with our URL.

webbrowser.open_new_tab(url)

Complete Code Listing

from geopy.geocoders import Nominatim
import webbrowser
geolocator = Nominatim()
address = input("Give me an address :")
location = geolocator.geocode(address)
latitude = str(location.latitude)
longitude = str(location.longitude)
url = "https://www.google.co.uk/maps/@"+latitude+","+longitude+","+"18z"
webbrowser.open_new_tab(url)

Test The Code!

Search for address and Google Map appears
Save and run the code, et voila! You can now see a map for the address you provided.

Bonus Content

I used GeoPy in last week's Friday Fun blog post to create a way to get pollen count data for your location. Head over and have a read.