Home >Backend Development >Python Tutorial >How to implement geocoding with Python

How to implement geocoding with Python

高洛峰
高洛峰Original
2016-11-21 17:52:252721browse

Taking the logistics industry as an example, this paper analyzes the application methods of PostgreSQL and Greenplum in the logistics industry in terms of geographical location information processing, optimal path algorithms, and machine learning. It mentioned the problem of converting addresses into coordinates. A more professional term should be "geocoding", that is, knowing an address, such as No. 10, Shangdi 10th Street, Haidian District, Beijing, how to obtain the corresponding longitude and latitude location information ( 40,116), or vice versa.

Geocoding concept

Many map-related vendors provide related APIs, and we can directly use these APIs to obtain this information. For example, Baidu's Geocoding API.

Geocoding API is a type of interface used to provide conversion services from addresses to longitude and latitude coordinates or from longitude and latitude coordinates to addresses. Users can use development languages ​​such as C#, C++, and Java to send requests and receive return data in JSON and XML. Geocoding API includes address parsing and reverse geocoding functions:

How to implement geocoding with Python

Borrowing a more intuitive picture from the ESRI document

How to implement geocoding with Python

Geocoding: that is, address parsing, obtaining Baidu longitude and latitude information from structured addresses as detailed as streets , for example: the result of address resolution of "No. 27, Zhongguancun South Street, Haidian District, Beijing" is "lng:116.31985,lat:39.959836". At the same time, geocoding also supports direct parsing of the names of places of interest and landmark buildings to return Baidu latitude and longitude. For example, the result of address parsing of "Baidu Building" is "lng:116.30815,lat:40.056885".

Reverse geocoding: that is, reverse geocoding, which obtains structured address information from Baidu longitude and latitude information. For example: "lat:31.325152,lng:120.558957" The result of reverse geocoding is "318 Tayuan Road, Huqiu District, Suzhou City, Jiangsu Province Number".

However, one thing that needs to be explained is that if you want to use Baidu’s API, you need to have a Baidu account and apply for the corresponding Key. In fact, in addition to Baidu, Google, ESRI, Microsoft's Bing, etc. all have similar geocoding services. However, most of these services do not have libraries specifically for Python and their Json structures are inconsistent with each other. Therefore, the Python master who specializes in dealing with dissatisfaction made a special geocoding tool geocoder to integrate and unify the services of these different manufacturers.

Geocoder

First take a look at which companies' geocoding services it supports:

How to implement geocoding with Python

Installation

pip install geocoder

Geocoding

import geocoder
g = geocoder.google("1403 Washington Ave, New Orleans, LA 70130")
g = geocoder.arcgis(u"北京市海淀区上地十街10号")
g.latlng

The output is

[29.9287839, -90.08421849999999]

You can also view the complete geojson

g.geojson

output For

{'bbox': [-90.0855674802915,
  29.9274349197085,
  -90.0828695197085,
  29.9301328802915],
 'geometry': {'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'}

, when trying to query the Chinese address directly using Google, it failed

g = geocoder.google(u"北京市海淀区上地十街10号")
g.ok

The output is

False

It should be fine using Baidu, but I did not apply for the corresponding key. Switch to arcgis and be able to successfully encode

g = geocoder.arcgis(u"北京市海淀区上地十街10号")
g.latlng

The output is

[40.050934, 116.30079]

Reverse geocoding

g = geocoder.google([29.9287839, -90.08421849999999], method='reverse')

print g.address
print g.city
print g.state
print g.country

The output is

1403 Washington Ave, New Orleans, LA 70115, USA
New Orleans
LA
US

Change to the Chinese address

g = geocoder.google([40.050934, 116.30079], method='reverse')
print g.address
print g.city
print g.state
print g.country

The output is

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

Try using the arcgis service

g = geocoder.arcgis([40.050934, 116.30079], method='reverse')
print g.address
print g.city
print g.state
print g.country

The output is

None
北京市
北京市
CHN

Google converts it into English, but the address is relatively complete. Although arcgis is in Chinese, the detailed address is actually output as None, which is useful.

Others

geocoder’s functions don’t stop there, it can also check IPs (including your own).

g = geocoder.ip('199.7.157.0')
print g.latlng
print g.city
g = geocoder.ip('me')
print g.latlng
print g.city

The output is

[43.6934, -79.4857]
Toronto
[51.05, 13.75]
Dresden

Querying the spatial bounding box of a city

g = geocoder.arcgis(u"山东")
g.bbox

The output is

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

Summary

Spatial information can be described by text information such as administrative divisions and natural geographical areas, or it can be identified by coordinate systems and numbers (zip codes, etc.). Geocoding technology can be used to associate geolocation elements of spatial information with corresponding text information. This article mainly introduces the geocoder geocoding tool, which can conveniently and quickly use the geocoding services provided by maps and other related manufacturers to convert the location described in text into longitude and latitude on the map, or obtain the corresponding coordinates through a certain location on the map. Text description of the location information.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:python list generatorNext article:python list generator