The introduction

Today, I saw an article by Alicloud called “The world’s martial arts, only fast can’t break”. Taking the logistics industry as an example, I analyzed PostgreSQL and Greenplum’s application methods in geographical location information processing, optimal path algorithm, machine learning and other aspects of the logistics industry. It mentioned the problem of address conversion into coordinates. The more technical term should be “geocoding”, that is to know how an address, such as no. 10 shangdi tenth street, haidian district, Beijing, can obtain the corresponding latitude and longitude location information (40,116), or vice versa.

Geocoding concept

Many map-related vendors provide apis that can be used directly to obtain this information. Take Baidu’s Geocoding API.

Geocoding API is a kind of interface for providing translation service from address to latitude and longitude coordinates or from latitude and longitude coordinates to address. Users can use C#, C++, Java and other development languages to send requests and receive returned data from JSON and XML. The Geocoding API includes address resolution and reverse address resolution capabilities:

Borrow a more intuitive figure from the ESRI documentation


Address resolution, by structured address in detail to the street to get baidu longitude and latitude information, for example: “27” haidian district of Beijing zhongguancun south street address resolution is the result of the LNG: 116.31985, lat: 39.959836

At the same time, geocoding support scenic spots and historical sites, landmarks name directly resolve return baidu latitude and longitude, for example: “baidu mansion” address resolution is the result of the LNG: 116.30815, lat: 40.056885

Inverse geocoding:

Reverse address resolution, namely by baidu latitude and longitude information is structured address information, for example: “lat: 31.325152, LNG: 120.558957” reverse address resolution is the result of the “jiangsu province suzhou huqiu district garden road no. 318”.

However, it should be noted that if you want to use baidu’s API, you need to have a Baidu account and apply for the corresponding Key. In addition to Baidu, Google, ESRI, Microsoft’s Bing and others have similar geocoding services. However, most of these services do not have python-specific libraries and do not have consistent Json structures with each other. As a result, the Python gods created a special geocoding tool, GeoCoder, to integrate the services of these different vendors.

Geocoding toolgeocoder

Let’s take a look at which companies’ geocoding services are supported:

Provider Optimal Usage Policy
ArcGIS World
Baidu China API key
Bing World API key
CanadaPost Canada API key
FreeGeoIP World CA & US Rate Limit
GeocodeFarm World Policy
GeoNames World Username
GeoOttawa Ottawa
Google World Rate Limit, Policy
HERE World API key
IPInfo World
Mapbox World API key
MapQuest World API key
Mapzen World API key
MaxMind World
OpenCage World API key
OpenStreetMap World Policy
Tamu US API key
TomTom World API key
What3Words World API key
Yahoo World
Yandex Russia
TGOS Taiwan
The installation
pip install geocoderCopy the code
import geocoder g ="1403 Washington Ave, New Orleans, LA 70130") g = geocoder. Arcgis (u" 10 Shangdi 10th Street, Haidian District, Beijing, China ") g = geocoderCopy the code

The output is

[29.9287839, 90.08421849999999]Copy the code

You can also view the full GeoJSON

g.geojsonCopy the code

The output is

{'coordinates': [-90.0855674802915, 29.9274349197085, -90.0828695197085, 29.9301328802915], 'coordinates': {'coordinates': [-90.08421849999999, 29.9287839], 'type': 'Point'}, 'properties': {'accuracy': u'ROOFTOP', 'address': u'1403 Washington Ave, New Orleans, LA 70130, USA', 'bbox': [-90.0855674802915, 29.9274349197085, -90.0828695197085, 29.9301328802915], 'City ': U 'New Orleans', 'confidence': 9, 'country': u'US', 'county': u'Orleans Parish', 'encoding': 'utf-8', 'housenumber': u'1403', 'lat': 29.9287839, 'LNG ': -90.08421849999999,' Location ': '1403 Washington Ave, New Orleans, LA 70130', 'Neighborhood ': u'Garden District', 'ok': True, 'place': u'ChIJGyFHWc2lIIYRYSoneaXAUiw', 'postal': u'70130', 'provider': 'google', 'quality': u'street_address', 'state': u'LA', 'status': 'OK', 'status_code': 200, 'street': u'Washington Ave'}, 'type': 'Feature'}Copy the code

Failed to query the Chinese address directly using Google

G = geocoder. Google (u" 10, Shangdi 10th Street, Haidian District, Beijing ")Copy the code

The output is

FalseCopy the code

It should be ok to use Baidu, but I have not applied for the corresponding key. Switch to ArcGIS, coding can be successful

G = geocoder. Arcgis (u" 10, Shangdi 10th Street, Haidian District, Beijing ")Copy the code

The output is

[40.050934, 116.30079]Copy the code

Inverse geocoding
G = geocoder. Google ([29.9287839, 90.08421849999999]. method='reverse') print g.address print print g.state print g.countryCopy the code

The output is

1403 Washington Ave, New Orleans, LA 70115, USA

New Orleans


USCopy the code

Change it to a Chinese address

G = geocoder. Google ([40.050934, 116.30079], method='reverse') print g.dress print print g.state print g.tryCopy the code

The output is

Bai Du Da Sha, Haidian Qu, Beijing Shi, China, 100193


Beijing Shi

CNCopy the code

Try using arcGIS’s service

G = geocoder. Arcgis ([40.050934, 116.30079], method='reverse') print g.state print g.ountryCopy the code

The output is

None Beijing Beijing CHNCopy the code

Google is translated into English, but the address is more complete. Although Arcgis is In Chinese, the detailed address is output as None, which has an X.


Geocoder does more than that. It can also look up IP (including its own).

G = geocoder. IP ('') print g = geocoder. IP ('me') print g = geocoderCopy the code

The output is

[43.6934, -79.4857] Toronto [51.05, 13.75] TorontoCopy the code

Query the spatial bounding box of a city

G = geocoder. Arcgis (u" shandong ") g.boboxCopy the code

The output is

{'northeast': [38.976997, 121.976998], 'southwest': [33.022997, 116.022998]}Copy the code


Spatial information can be described by text information, such as administrative divisions and physical geographical regions, as well as identified by coordinate system and numbers (postcodes, etc.). Geocoding technology can be used to associate geo-location elements of spatial information with corresponding text information. This paper mainly introduces the small tool Geocoder, which can easily and quickly use the geocoding services provided by map and other relevant manufacturers to convert the location of text description into longitude and latitude on the map, or obtain the corresponding text description of location information through the coordinates of a location on the map.

The original post was published on November 18, 2016

Author: Space-time Drei

This article is from the Python Chinese Community, a cloud community partner. For more information, please follow the wechat official account of Python Chinese Community