How to GeoCode with Python

Source: Internet
Author: User
Taking the logistics industry as an example, this paper analyzes the application methods of PostgreSQL and greenplum in logistics industry such as location information processing, optimal path algorithm, machine learning and so on. It refers to the address conversion to coordinate problems, more professional nouns should be "geocoding", that is, to know an address, such as Beijing Haidian District, 10 Street, 10th, how to obtain the corresponding latitude and longitude location information (40,116), or vice versa.

Geo-coding concepts

Many map-related vendors provide the relevant APIs that we can use to get this information directly from these APIs. such as Baidu's geocoding API.

The Geocoding API is a class of interfaces that provide translation services from address to latitude-longitude coordinates or from latitude-longitude coordinates to addresses, and users can send requests using development languages such as C #, C + +, Java, and receive JSON, XML return data. The Geocoding API includes address resolution and inverse address resolution capabilities:

Borrow a more intuitive diagram from an ESRI document

Geocoding: That is, address resolution, from the detailed to the street of the structured address to get Baidu latitude and longitude information, such as: "Haidian District, Beijing Zhongguancun South Street, No. 27th" The result of address resolution is "lng:116.31985,lat:39.959836". At the same time, geo-coding also support the historical sites, landmark building name directly analytic return Baidu latitude and longitude, for example: "Baidu Mansion" address resolution results are "lng:116.30815,lat:40.056885".

Inverse geocoding: That is, inverse address resolution, by Baidu latitude and longitude information to get structured address information, for example: "lat:31.325152,lng:120.558957" inverse address analysis results are "Jiangsu province Suzhou Huqiu District Tower Garden Road No. 318."

However, it is necessary to note that if you want to use this set of Baidu API, the premise is that there is a Baidu account and apply for the corresponding key. In fact, in addition to Baidu, Google, ESRI, Microsoft Bing, etc. have similar geocoding services. However, most of these services do not have a Python-specific library and are inconsistent with each other's JSON structure. As a geocoder, the disobedient python God made a special geocoding tool to unify the services of these different vendors.

Geocoding Tools Geocoder

First look at which company's GeoCode services it supports:

Installation

Pip Install Geocoder

Geo-coding

Import Geocoderg = Geocoder.google ("1403 Washington Ave, New Orleans, LA 70130") g = Geocoder.arcgis (U "No. 10th, Shangdi 10 Street, Haidian District, Beijing") g.la Tlng

Output to

[29.9287839,-90.08421849999999]

You can also view the full Geojson

G.geojson

Output to

{' 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 ': $,  ' street ': U ' Washington Ave '}, ' type ': ' Feature '}

Failed to try to query the Chinese address directly with Google

g = Geocoder.google (U "No. 10th, 10 Street, Haidian District, Beijing") G.ok

Output to

False

Baidu should be no problem, but I did not apply for the corresponding key. Switch to ArcGIS to encode successfully

g = Geocoder.arcgis (U "No. 10th, 10 Street, Haidian District, Beijing") g.latlng

Output to

[40.050934, 116.30079]

Inverse geo-coding

g = Geocoder.google ([29.9287839, -90.08421849999999], method= ' reverse ') print G.addressprint g.cityprint g.stateprint G.country

Output to

1403 Washington Ave, New Orleans, LA 70115, Usanew Orleanslaus

Change to China's address

g = Geocoder.google ([40.050934, 116.30079], method= ' reverse ') print G.addressprint g.cityprint g.stateprint g.country

Output to

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

Try the ArcGIS Service

g = Geocoder.arcgis ([40.050934, 116.30079], method= ' reverse ') print G.addressprint g.cityprint g.stateprint g.country

Output to

None Beijing, Beijing, CHN

Google is converted to English, but the address is quite full. Although ArcGIS is Chinese, but the detailed address actually output for none, this has an X to use.

Other

Geocoder features more than this, it can also check the IP (including its own).

g = Geocoder.ip (' 199.7.157.0 ') print g.latlngprint G.cityg = Geocoder.ip (' Me ') print G.latlngprint g.city

Output to

[43.6934,-79.4857] toronto[51.05, 13.75]dresden

Query a city's space bounding box

g = Geocoder.arcgis (U "Shandong") G.bbox

Output to

{' Northeast ': [38.976997, 121.976998], ' Southwest ': [33.022997, 116.022998]}

Summary

Spatial information can be used to describe both administrative and geographic areas, as well as coordinate systems, numbers (postal code, etc.). The geo-coding technique can be used to correlate the geo-location elements of spatial information with corresponding textual information. This paper mainly introduces the small tool of Geocoder geocoding, which can make use of the geocoding service provided by relevant vendors such as map, convert the position of text description into the latitude and longitude on the map, or obtain the corresponding position information text description through a location coordinate on the map.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.