Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

How to Use the Geocoder

Thomas Norling edited this page Jun 14, 2016 · 19 revisions

A geocoder is a tool that allows us to obtain latitude and longitude coordinates from an address. There are many different geocoders available to us through the geopy python library. As of this writing the geopy library is not installed on our development machine so you will need to run scripts that use this on your own personal machine. You can use the Google geocoding API directly but geopy is much simpler to use and is the recommended method of obtaining coordinates.

A module has already been written to perform most geocoding tasks you will need to perform for this project. It is located in NetworkCameras/Discovery/Tools/Geocoding.py Instructions for using that module are located in the header. If that module doesn't work for what you need, instructions for using geopy are outlined below.

Install Library

The first thing you will need to do is install geopy:

pip install geopy

Obtaining an API Key

Many geocoders require the use of a key to ensure you are not making too many requests in a day. Google allows you 2500 requests per 24 hours without a key and will give you an additional 2500 requests with a key. It is a good idea to get an API key to use with your scripts. To obtain a key for the Google Geocoding API go to developers.google.com and click on "Get a Key" and follow the instructions to create a project. Then go to your Google Developer Console, head to "Credentials", click "Create credentials", choose "API key" and then select "Browser Key". The key that is created is the key you should use when you are trying to use the Google Geocoding API.

Using the Library

Geopy allows you to use many different geocoding APIs. You can experiment with different geocoders until you find one that gives you the most accurate results. I have found GoogleV3 and Nominatim to be the most accurate but you may find different geocoders work better than others depending on how you search for a location. Each geocoder works slightly differently so you will have to look at the geopy documentation to see how they are used. For the purposes of this wiki I will go through how to use the GoogleV3 geocoder.

First you will need to import geopy into any script you want to use it in:

from geopy.geocoders import GoogleV3

If you want to use a different geocoder you would replace "GoogleV3" with the name of another geocoder such as "Nominatim"

Now when you want to use the geocoder you will need to create an instance of the geopy class by using the following line of code:

geolocator = GoogleV3(api_key = API_KEY)

Where 'geolocator' can be any variable name you choose, 'GoogleV3' can be replaced by your chosen geocoder, and 'API_KEY' is your API key string. If you don't have an API key you can either use 'None' or you can enter this line instead:

geolocator = GoogleV3()

Now in order to make a location request you will need to use the following line of code:

location = geolocator.geocode(searchQuery)

Where 'location' can be any variable name you choose, 'geolocator' is the variable name you chose for the previous step, and 'searchQuery' is the string of the location you are searching for.

If you need to do the reverse, finding an address from the latitude and longitude coordinates, you would use this line instead:

location = geolocator.reverse("latitude, longitude")

Where 'location' can be any variable name you choose, 'geolocator' is the variable name you chose for the previous step, and 'latitude' and 'longitude' correspond to the latitude/longitude coordinates you are searching for.

Obtaining Results

As long as a location was found and no errors were encountered you can access your results with:

lat = location.latitude
long = location.longitude
adrs = location.address

Where 'location' is the variable name you chose for the previous step and 'lat'/'long'/'adrs' can be any variable name you choose. Additionally you can use:

info = location.raw

This will give you the raw output of the search results. Each geocoder may have a different format for how they output the information. In the case of the Google API the results will look something like this: GoogleAPIOutput.png

This is useful when you need to find information other than the latitude and longitude coordinates. For example, if you need to find the name of the city, using the Google API, you could write a piece of code something like this:

extractCity = location.raw['address_components']
for item in extractCity:
    types = item['types']
    if types[0] == "locality":
        city = item['long_name']

Relevant Resources

Clone this wiki locally