Share with your friends


Open source intelligence techniques & commentary

Geocoding and Automated Measurements

If you recall our earlier work on the Winnipeg serial killer, I taught you how to take manual measurements between locations on a Google Map. Now we are going to spruce this up a bit and learn how to use geocoding to translate street names to latitude/longitude and then measure distances between these points automatically. This can be extremely useful if you have a number of locations (say from our Panoramio or Wikimapia posts) and you want to measure distances between all of them.


Glorious geopy. This is an amazing library that can utilize numerous geocoding services from Google to Open Street Maps. Not only that but it also has utility functions that will measure distances for us. We are only one pip away from having this magical library at our fingertips. Let’s not waste any time:

Done! You can now import and use this glorious piece of code and begin using it. What we will do is take our three location selectors from the National Post story and script out retrieving their exact location, and then measuring the distance between each of them. If you remember the story mentions an 800 meter distance between the two newest murders and the first murder and we estimated around 530 meters (as the crow flies), so let’s see how our automated solution stacks up.

Coding it Up

As a review the three locations from the earlier post were:

  1. 333 Portage Ave.
  2. 329 Hargrave St.
  3. Main St. and Pioneer Ave.

Crack open a new Python script call it and enter the following code:

Alright, now let’s break this code down a bit:

  • Lines 1-3: these are our imports, where we are pulling in the geopy module, and a default Python module called itertools (you’ll see why soon enough). We also have to use a special import to pull in the distance measuring library called vincenty from the geopy module.
  • Lines 6-11: here we create a simple helper variable that we will just tack on to each of our street names (every city has a Main St. for example, so this helper pins Main St. to Winnipeg) and then a simple dictionary to hold each of our street locations.
  • Line 14: we create a new instance of the Google geocoder. There are a pile of geocoding services you can use through geopy, but we will use Google.
  • Lines 17-20: here we are simply walking through our dictionary of locations and using the geocode function to hand us back a Location object that includes the latitude and longitude.
  • Line 24: here we are using the itertools.combination function to give us every possible combination of our addresses. This enables us to measure the distance between all three points without having to write some complicated code to do so. The itertools.combination function returns a tuple with two items, because we set the r parameter to 2.
  • Lines 26-27: here we are simply retrieving the two locations from our Python dictionary so that we can use them for our measurements. We are also interested in pulling out the point variable from the Location object, as this contains the latitude / longitude we need for measurements.
  • Line 30: here is the magic where we pass in the two locations to the vincenty measurement function and then ask it to return the measurement in meters.
  • Line 32: we output the result of all of this geocoding magic, and then bask in our own Python coding glory.

The result of the script when I run it is:

Future Use

So you can use this script anytime that you have numerous locations in an article or dataset that you want to figure out the distances to. For example, you could use a previous Panoramio script, and then write some code to tell you the largest distance between photographs in a particular geographic area. Send me an email if you do this and share your results. Actually, send me an email if you would like me to blog on how to perform this exact task if you get stuck or you’re interested in learning how.