Geocoding addresses

Geocoding is the process of turning an address into earth coordinates. Geocoding requires a comprehensive dataset that ties zip codes, cities, streets, and street numbers (or street number ranges) to the coordinates. In order to have a geocoder that works for any address in the world with reasonable accuracy, you need to use a cloud service because geocoding datasets are very dense and can be quite large. Creating a geocoding dataset for any area beyond a few square miles requires a significant amount of resources. There are several services available, including Google and MapQuest. In QGIS, the easiest way to access these services is through the QGIS Python GeoCoding plugin. In this recipe, we'll use this plugin to programmatically geocode an address.

Getting ready

You will need to install the QGIS Python GeoCoding plugin by Alessandro Pasotti for this exercise, as follows:

  1. From the QGIS Plugins menu, select Manage and Install Plugins….
  2. In the Plugins dialog search box, search for Geocoding.
  3. Select GeoCoding plugin and click on the Install plugin button.

How to do it...

In this recipe, we will access the GeoCoding plugin methods using Python, feed the plugin an address, and print the resulting coordinates. To do this, we need to perform the following steps:

  1. In the QGIS Python Console, import the OpenStreetMap geoCoding object using the following code:
    from GeoCoding.geopy.geocoders import Nominatim
    
  2. Next, we'll create our geocoder:
    geocoder = Nominatim()
    
  3. Then, using the following code, we'll geocode an address:
    location = geocoder.geocode("The Ugly Pirate, Bay Saint Louis, MS 39520")
    
  4. Finally, we'll print the results to see the coordinates:
    print location
    
  5. Check whether you have received the following output printed to the console:
    (u'The Ugly Pirate, 144, Demontluzin Street, Bay St. Louis, Hancock County, Mississippi, 39520, United States of America', (30.3124059, -89.3281418))
    

How it works...

The GeoCoding plugin is designed to be used with the QGIS GUI interface. However, like most QGIS plugins, it is written in Python and we can access it through the Python console.

Tip

This trick doesn't work with every plugin. Sometimes, the user interface is too intertwined with the plugin's GUI that you can't programmatically use the plugin's methods without triggering the GUI.

However, in most cases, you can use the plugins to not only extend QGIS but also for its powerful Python API. If you write a plugin yourself, consider making it accessible to the QGIS Python console in order to make it even more useful.

There's more...

The GeoCoding plugin also provides the Google geocoding engine as a service. Note that the Google mapping API, including geocoding, comes with some limitations that can be found at https://developers.google.com/maps-engine/documentation/limits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset