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.
You will need to install the QGIS Python GeoCoding plugin by Alessandro Pasotti for this exercise, as follows:
Geocoding
.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:
geoCoding
object using the following code:from GeoCoding.geopy.geocoders import Nominatim
geocoder = Nominatim()
location = geocoder.geocode("The Ugly Pirate, Bay Saint Louis, MS 39520")
print location
(u'The Ugly Pirate, 144, Demontluzin Street, Bay St. Louis, Hancock County, Mississippi, 39520, United States of America', (30.3124059, -89.3281418))
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.
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.
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.