4 Ways to Geocode in a Hurry

Let’s talk about giving an address X Y coordinates. If you need to geocode something it’s probably going to be in a .csv .xlxs or .xls format. This post will talk you through adding a location and putting it on a map.

Geocoding is the process of converting addresses to like “32 The Square, Palmerston North, Manawatu” into geographic coordinates that are plottable (-40.3603997,175.5975249). Reverse geocoding, is the exact opposite; converting coordinates to a human readable addresses.

You can build a geocoder from scratch or leverage one of the Geocoding services around the web. Most of the services offer some free geocoding with premium services for paying clients. Here are just few:

There are also geocoders provided by national agencies for specific regions, such as the Canadian Posts geolocator service. The Texas A&M site has a pretty comprehensive list. Fundamentally you interact with these services using their API looking something like this:

GET https:///search//geocode/.?key=[&storeResult=][&typeahead=][&limit=][&ofs=][&lat=][&lon=][&countrySet=][&radius=][&topLeft=][&btmRight=][&language=][&extendedPostalCodesFor=]

The idea being that you can send your data, perform the processing remotely and get a return including lat long results which are plottable.

There are two primary ways to interact with a geocoding service, through the GUI or programmatically via the services API. There is no right or wrong answer here. Just two different tools to get the same job done. so…

Using a GUI is quick and easy.

This is definitely what I would default to for a start. There are two way I’ve used for this. Firstly the the ArcGIS Online world geocoding service which is great and secondly the MMQGIS plugin for QGIS.

Method 1: ArcGIS Online Geocoder GUI

Pro’s

  • It supports drag and drop
  • It can handle some typos
  • It’s probably the quickest and easiest option for a small number of addresses

Con’s

  • It costs esri credits
  • It’s a little US centric when it comes to finding some address so it struggles a little when it comes to New Zealand addresses such as 101B CHURCH ST, or 2/101 HERETAUNGA ST

In the demo below I’m writing out an address to a .csv file and dropping it into ArcGIS online.

ESRIgeocoder

Method 2: QGIS MMGIS plugin GUI

There is a great plugin for QGIS which lets you access the google and open street map services easily. There is a nice concise tutorial on doing this here at the GIS Lounge.

The plugin lets you upload an excel doc, specify the fields to be used for the geocode and it will return a shapefile. It also writes out any failed geocoding results.

Geocoding with the Google service

One of the great advantages of using the google geocoder is its remarkable ability to deal with typos. During the Kaikoura recovery effort I was getting 98% matches on thousands of addresses. However there is a 2000 address limit per day. You can get around this by changing your IP address but it does restrict you.

Accessing geocoders with their API is preferable if you want to run scheduled jobs or perform more error checking.

Method 3: The Service API

You can access the individual services like this:

import requests
url = 'https://maps.googleapis.com/maps/api/geocode/json'
params = {'sensor': 'false', 'address': '35 The Square, Palmerston North, NZ'}
r = requests.get(url, params=params)
results = r.json()['results']
location = results[0]['geometry']['location']
location['lat'], location['lng']
(-40.3603997,175.5975249)

Method 4: The Geocoder Python Module

But there is a great python module that succently brings all the geocoders togther like this:

import geocoder
g = geocoder.google('35 The Square, Palmerston North, NZ')
g.latlng
(-40.3603997,175.5975249)

Installation is easy with pip, simply run if your working on a linux terminal

sudo pip install geocoder

The Geocoder module written by Denis Carriere brings all of these services together. In the .gif below I demo its use by

  • Reading each row in a file using the csv module
  • Sending the request to Open Street Maps using the Geocoder module
  • Write the results into a .csv file with lat long coordinates with the csv module again

geocoderpython

If you would like to use this code to geocode your own addresses here it is.

import geocoder
import csv

def geocode_from_file():
    print("########OPENING WORKBOOK############")
    f = open('geocode.csv')
    print("########CONSTRUCTING ADDRESS TEXT###########")
    # working on 0 - 1000
    reader = csv.reader(f)
    for row in reader:
        print "geocoding: {}".format(row)
        g = geocoder.osm(str(row)) #use any geocoder you like here

        print("# #######Output in a csv###############")
        outputFile = open('output1.csv', 'a')
        outputWriter = csv.writer(outputFile, lineterminator='\n')
        outputWriter.writerow([row[0], g.latlng[0], g.latlng[1]])
        outputFile.close()

geocode_from_file()

Once the program has run through it will output a file called, creatively “output.csv”. This you can drag and drop into your favorite mapping platform to visualize the addresses.

addingToAMap

In summary the four ways to geocode in a hurry are:

  1. ArcGIS online
  2. The MMQGIS plugin for QGIS
  3. Directly using the API
  4. The Python Geocoder Module

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. If you would like an email notice when I publish the next post feel free to signup here. Thank you!

4 Replies to “4 Ways to Geocode in a Hurry”

    1. Apologies but I’ve never had to REVERSE geocode before. I do not think that the MMQGIS extension is capable of doing it. ArcGIS online can do it. sorry that I cant be of more help

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: