Geocoding in QGIS: Address to Latitude and Longitude.
If you were to tell someone where you are located, how would you do it? Maybe you would give them your home address if you are home, or give your relative location from a popular feature. These are just a few ways of describing location among many.
Ways of describing a location on or above the earth’s surface:
- Geographic Coordinates.
- Zip code.
- Geospatial Index
While the use of address involves the use of a human-readable address such as (00200 City Square, Mountain View, NBI), Geographic coordinates give the location in the form of a pair of latitude and longitude(-1.286389, 36.817223) which is more oriented towards the use by machines. On the other hand, the use of a geospatial index system is fairly new. It involves the use of a discrete system where every position in the world has a cell identifier associated with it. To understand more about the use of a geospatial grid system refer to this podcast
Through geocoding and reverse geocoding you can do transformation between geographic coordinates and physical addresses. Geocoding refers to the process of transforming addresses into geographic coordinates. While reverse geocoding refers to the transformation from geographic coordinates to physical addresses.
It should be noted that different ways of describing location are suited for different applications. For instance, where else addresses are easily understood by humans, they are not suitable for spatial analysis and use by computers.
How to do Geocoding and Reverse Geocoding in QGIS.
QGIS is an easy-to-use freely available GIS software that comes with a wide variety of functionalities just like its commercial counterparts and this is not any different when it comes to carrying out Geocoding of addresses or postal codes to spatial coordinates. To understand QGIS more listen to this podcast episode:
There are several Geocoding plugins available out there to be used, but we will focus on the use of the MMQGIS geocoding plugin. It is a set of Python plugins for carrying out Geocoding as well as manipulating vector map data.
Installing MMQGIS geocoding plugin in QGIS.
QGIS allows you to download and install plugins from its plugin repository. From QGIS click on plugins on the toolbar then on the dropdown select manage and install plugins. This will bring up the plugins’ dialogue, search the MMQGIS on the search box and then click install.
Launching MMQGIS geocoding plugin.
Once you have installed the plugin, a dedicated menu for the MMQGIS geocoding plugin will be created at the top taskbar. Click on this menu to open the MMQGIS plugin. On clicking on MMQGIS a dropdown will appear, from the dropdown select Geocode: Which will give you three options:
- Geocode CSV with Web service
- Geocode from street Layer
- Reverse Geocode
The Geocode CSV with Web service option allows for bulk geocoding by taking a CSV containing addresses or names and creating latitude and longitude values based on the records where else, the Reverse Geocoding option returns place address/names from geographic coordinates input.
Sample Geocoding Dataset
For this tutorial, we are geocoding major cities in the United States. As our input file, we will use a CSV file with city names, State the city is located among other fields.
Bulk Geocoding Major Cities.
To carry out bulk geocoding of addresses in a CSV file, click on The Geocode CSV with the Web service option. An MMQGIS dialog box will pop up for you to select your file and fields to match those required by MMQGIS.
After entering all the fields click apply to start up the Geocoding process. The Web Service you selected will then be used to search and geocode the address provided. Depending on the size of your data and your internet speed this might take some time. The Web Service field provides popular geocoders to choose from. We used OpenStreetMap as the others such as Google Geocoding Web Service are commercial and will require you to have an API key.
The final output will be a shapefile of all the points that were successfully geocoded with their respective latitude and longitude provided in a single latlong column.
You will notice that the attributes of the output come with additional six fields. The latlong field contains the latitude-longitude pair values for the location after Geocoding. This is not ideal as we would like to have them in their columns. To split them you can use the field calculator contained at the top of the attribute table menu. On the field, calculator dialog menu, select create new field and name the field latitude. Change the Output field type to Decimal number (real). And the Precision to 5. In the Expression field, type $Y. Hit OK. Repeat for longitude, but call the field longitude and use $X.
We will now have two columns containing the location’s latitude and longitude.
A .CSV file containing a list of records that were not geocoded will be created.
Reverse Geocoding In QGIS
The MMQGIS plugin also provides reverse geocoding functionality. Through reverse geocoding you can use latitude and longitude value pairs to get the physical address names of a location. Since we have latitude and longitude values from the previous geocoding process we will use them in this step.
For this, we will use the Reverse Geocoding option that pops up when you click on Geocode under the MMQGIS menu. On clicking on reverse geocoding a dialog box will open up from which you specify the fields to be used in the reverse geocoding process.
On complete reverse geocoding a shapefile containing extra fields such as city, admin_name, population, category, and type among others will be created.
More Geocoding plugins in QGIS.
We looked at the MMQGIS plugin but there are many options to choose from on the QGIS plugins repository. Some of the Geocoding plugins that provide both geocoding and reverse geocoding functionalities are:
- Google Maps Geocoder
- Geocoding plugin
- OSM Place Search
Go to the QGIS plugin repository download, install and explore them to see which one fits your use case.
Geocoding and Reverse Geocoding APIs.
Depending on your working environment and use case it might be more suitable to write an independent script that will implement the Geocoding process by making an HTTP request to a REST API. This can be done using any of the major scripting programming languages. There are various APIs provided by different spatial service providers. Some are free while others are commercial. Each API comes with its strengths and weaknesses.
Some of the geocoding services to look into include:
- Google Maps
- US Census Bureau
- Open Cage
In conclusion, geocoding has become a useful tool for translating machine-generated data as they collect and process location data in terms of latitude and longitude to human readable data as discussed further in this podcast episode
Additional GeoCoding Resources