How to do Geocoding and Reverse Geocoding for Free

Finding an address will become more important in the future due to increasing online shopping activities and advanced technology achievement like autonomous car/taxi, drone delivery service, robotic mailman, etc. All those activities required location of an address, so the autonomous taxi will pick you up at your house not your neighbor's house or your important mail will be arrived exactly in your mailbox. How could it be possible telling an autonomous taxi, drone or a robot to go to the right location based on an address? The answer is geocoding.

What is Geocoding?

Geocoding is a process to translate or convert an address to a coordinate in latitude and longitude. By knowing the coordinate of an address the position or location of the address can be known on the earth surface. Then how Geocoding works? There are some approaches that a geocoder could do in converting an address into coordinate such as absolute position, relative position and address interpolation.

Absolute position is finding a coordinate of an address based on an object location, for example address The Art Institute of Chicago, 111, South Michigan Avenue. The key word of the address is The Art Institute of Chicago, the location database of a geocoder stores the object position, therefore based on object's name the geocoder will take the coordinate of the address. This approach will return a good accuracy as long as the object could be found in the database.  Relative position geocoding is finding a position of address based on spatial relation. More precise an object is defined spatially with another object, will return more correct probability. It is like asking someone, where is building X? The answer could be it is 100 ft to the north from Y building, or it is in front of Y building. Here is Y is a reference location which is famous or a landmark that everybody is supposed to be known. Address interpolation is the next approach in defining a location. This approach will work in an area where building distribution is in order along a road. For example a building number 214 at Olympus street as shown in figure 1. The building is between building number 212 and 218, therefore the building could be located with a good precision. Unfortunately this method won't work at any place around the world, because not every country has a good building distribution.       

Interpolation address geocoding
Figure 1. A good building distribution for geocoding interpolation

Nominatim Geocoding API Service


There are many geocoding API services available such as Google Geocoding service, ESRI geocoding, HERE, Nominatim, US Census Geocoder, Bing, and so forth. Some geocoding services are paid or limited usage, such as Google, ESRI, HERE and Bing. In this post, we will discuss only Nominatim geocoder service.

Nominatim is a geocoder based on Open Street Map(OSM) data. To use this geocoding API service we have to define a query(q) in the nominatim address. For example, I would like to search for MIT, the query will be: https://nominatim.openstreetmap.org/search?q=mit. The result of this query will return Open Street Map with a list of geocoding results as in figure 2.

Nominatim geocoding output
Figure 2. Nominatim geocoding output
To get a closer result as wishes, we have to define the query more detail like adding country name, county, street address, postal code, amenity, etc. For example mit, massachusetts, university. This query will return just one result only.

Nominatim supports some outputs format like XML, JSON, GEOJSON or GEOCODEJSON. To get an output in a specific format, we have add the output format after query, for example: https://nominatim.openstreetmap.org/?q=mit,massachusetts,university&format=xml. The result can be seen as in figure 3.

Geocoding XML output
Figure 3. Geocoding XML output

Reverse Geocoding

What about reverse geocoding? how to do it in Nominatim? To do reverse geocoding using free nominatim geocoder service is quite simple. We just need to specify the coordinate and output format as follow: https://nominatim.openstreetmap.org/reverse?lat=42.3583961&lon=-71.0956778766393&format=json. The result of this syntax as in figure 4.    

JSON reverse geocoding output
Figure 4. JSON reverse geocoding output
There are more options available in Nominatiom geocoding API service, such as address lookup, limit, polygon output, and also deploying Nominatim in local hard drive. The complete reference can be found at Nominatim documentation.

Batch Geocoding

Batch geocoding is doing multiple geocoding for multiple addresses. Usually the addresses are stored in a file like txt or csv. To do batch geocoding we can do it in a programming language like Python. For this, of course Python programming knowledge is required. But what about if we don't have any knowledge of Python. Don't worry, the free open source software QGIS can be used to do geocoding. So, the rest of this post will show you how to do geocoding using QGIS.

Geocoding in QGIS

To do geocoding in QGIS we need a plugin called MMQGIS. To install the plugin, please refer to a post about how to create map animation which uses the same plugin.

From MMQGIS menu, select Geocode > Geocode CSV with Web Service like figure 5.

MMQGIS geocode menu
Figure 5. MMQGIS geocode menu
The web service geocoding window will appear as in figure 6. In the Input CSV File give a csv file which has several columns such as Address, City, State and County (Download sample address file). In the Web Service option there are some geocoding services available namely Google, OSM/Nominatim, US Census Bureau and ESRI Server.  Google and ESRI Server are paid services, so it requires API Key. US Census Bureau has US coverage only. For free global coverage geocoding we use OSM/Nominatim service. The Duplicate Handling parameter determines how to handle multiple geocoding results. The Only First Result option will take only the first result, and on the other hand the option Multiple Features for Multiple Results will take all results and stored in multiple features. The last two parameters are Output File Name and Not Found Output List. The Output File Name is the location of the geocoding output will be saved in shape file format. Lastly the address that not found will be saved in the path that specified in Not Found Output List

QGIS batch geocoding
Figure 6. QGIS batch geocoding
The output geocoding will be added to the QGIS map as in figure 7. The attribute table of the output will consist of several columns such as osm id, display name, category, type and latlong coordinate. The output table can be seen as in figure 8. 

Geocoding output
Figure 7. Geocoding output
 
Geocoding output table
Figure 8. Geocoding output table

QGIS Reverse Geocoding


To do reverse geocoding in QGIS the Reverse Geocode tool from MMQGIS is used. The reverse geocode window can be seen as in figure 9. To do a reverse geocoding an input layer is required. The input layer must be in unprojected coordinate system (Geographic Coordinate System). In the Web Service parameter there are only two services availble, there are Google and OSM/Nominatim. The Duplicate Result Handling is the same with the geocoding option. The result of reverse geocoding will be stored in a shape file that can be specified with Output File Name parameter. 

QGIS reverse geocoding
Figure 9. QGIS reverse geocoding
That's all the tutorial how to do geocoding and reverse geocoding using free OSM/Nominatim web API service and also doing batch geocoding with MMQGIS plugin in QGIS open source software. Hopefully this tutorial is useful for you and please consider to share it to other people if you think it will give benefit to them. Thanks! Anyway in another post I wrote how to do geocoding using ArcGIS and Python. Check it out!

Related Posts

Disqus Comments