How to use Python to implement geocoding take the logistics industry as an example. This paper analyzes the logistics industry application methods of PostgreSQL and Greenplum in terms of geographical location information processing, optimal path algorithm, and machine learning. I mentioned the problem of converting addresses into Coordinates. the more professional term should be "geographic code", that is, knowing an address, such as No. 10, Shangdi 10 Street, Haidian district, Beijing, how can I obtain the corresponding latitude and longitude information (40,116), or vice versa.
Concept of geocoding
Many map-related vendors provide related APIs, which can be used directly to obtain the information. For example, Baidu's Geocoding API.
Geocoding API is a type of interface used to provide the conversion service from address to latitude and longitude coordinates or from latitude and longitude coordinates to address, you can use C #, C ++, Java, and other development languages to send requests and receive JSON and XML returned data. Geocoding APIs include Address Resolution and Reverse Address Resolution:
Install
pip install geocoder
Geocode
Import geocoderg = geocoder. google ("1403 Washington Ave, New Orleans, LA 70130") g = geocoder. arcgis (u "10 shangdi10 Street, Haidian district, Beijing") g. latlng
The output is
[29.9287839, -90.08421849999999]
You can also view the complete geojson
g.geojson
The output is
{'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'}
An error occurred while trying to query the Chinese address using Google.
G = geocoder. google (u "10 shangdi10 Street, Haidian district, Beijing") g. OK
The output is
False
It should be okay to use Baidu, but I did not apply for the corresponding key. Switch to arcgis to encode
G = geocoder. arcgis (u "10 shangdi10 Street, Haidian district, Beijing") g. latlng
The output is
[40.050934, 116.30079]
Inverse Geographic Encoding
g = geocoder.google([29.9287839, -90.08421849999999], method='reverse')print g.addressprint g.cityprint g.stateprint g.country
The output is
1403 Washington Ave, New Orleans, LA 70115, USANew OrleansLAUS
Change to Chinese address
g = geocoder.google([40.050934, 116.30079], method='reverse')print g.addressprint g.cityprint g.stateprint g.country
The output is
Bai Du Da Sha, Haidian Qu, Beijing Shi, China, 100193BeijingBeijing ShiCN
Try arcgis service
g = geocoder.arcgis([40.050934, 116.30079], method='reverse')print g.addressprint g.cityprint g.stateprint g.country
The output is
None Beijing CHN
Google is converted to English, but the address is full. Although arcgis is Chinese, the detailed address is output to None, which is X.
Others
Geocoder supports more than this function. It can also query IP addresses (including its own ).
g = geocoder.ip('199.7.157.0')print g.latlngprint g.cityg = geocoder.ip('me')print g.latlngprint g.city
The output is
[43.6934, -79.4857]Toronto[51.05, 13.75]Dresden
Query the space boxes of a city.
G = geocoder. arcgis (u "Shandong") 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 geographical regions, or identified by coordinate systems and numbers (such as zip codes. Geographic encoding technology can be used to associate the geographical positioning elements of spatial information with the corresponding text information. This article mainly introduces geocoder, a small tool used to conveniently and quickly convert the location of text descriptions to the longitude and latitude of the map by using geo-coding services provided by related manufacturers such as maps, alternatively, you can use the coordinates of a location on the map to obtain the corresponding location information text description.