工具原料:地理編碼工具geocoder
用Python實現地理編碼方法如下:
壹、看壹下它都支持哪些公司的地理編碼服務:
二、安裝
pip install geocoder
三、地理編碼
import geocoder
g?=?geocoder.google("1403?Washington?Ave,?New?Orleans,?LA?70130")g?=?geocoder.arcgis(u"北京市海澱區上地十街10號")g.latlng輸出為
[29.9287839,?-90.08421849999999]也可以查看完整的geojson
g.geojson
輸出為
{'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'}直接用Google嘗試查詢中文地址時失敗
g = geocoder.google(u"北京市海澱區上地十街10號")g.ok
輸出為
False
用百度應該沒問題,不過我沒有申請相應的key。切換到arcgis,能夠成功編碼g = geocoder.arcgis(u"北京市海澱區上地十街10號")g.latlng
輸出為
[40.050934, 116.30079]
逆地理編碼
g?=?geocoder.google([29.9287839,?-90.08421849999999],?method='reverse')print?g.addressprint?g.city
print?g.state
print?g.country
輸出為
1403 Washington Ave, New Orleans, LA 70115, USANew Orleans
LA
US
換成中國的地址
g = geocoder.google([40.050934, 116.30079], method='reverse')print g.address
print g.city
print g.state
print g.country
輸出為
Bai Du Da Sha, Haidian Qu, Beijing Shi, China, 100193Beijing
Beijing Shi
CN
用arcgis的服務試試
g = geocoder.arcgis([40.050934, 116.30079], method='reverse')print g.address
print g.city
print g.state
print g.country
輸出為
None
北京市
北京市
CHN
Google轉換成的是英文,但地址比較全。arcgis雖然是中文,但是詳細的地址居然輸出為了None,這有個X用。
其他
geocoder 的功能不止於此,它還可以查IP(包括自己的)。
g = geocoder.ip('199.7.157.0')
print g.latlng
print g.city
g = geocoder.ip('me')
print g.latlng
print g.city
輸出為
[43.6934, -79.4857]
Toronto
[51.05, 13.75]
Dresden
查詢壹個城市的空間包圍盒
g = geocoder.arcgis(u"山東")
g.bbox
輸出為
{'northeast': [38.976997, 121.976998], 'southwest': [33.022997, 116.022998]}