geopy是Python關(guān)于地理位置的一個(gè)第三方庫(kù),用這個(gè)庫(kù)來(lái)進(jìn)行地址位置信息的查詢(xún)和轉(zhuǎn)換非常方便,本文介紹關(guān)于geopy的常用的幾種用法
geopy的安裝
pip install geopy
根據(jù)地址查詢(xún)坐標(biāo)及詳細(xì)信息
>>> import json, logging
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("北京天安門(mén)")
>>> print location.address
天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)
>>> print (location.latitude, location.longitude)
(39.90733345, 116.391244079988)
>>> print json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8')
{
"display_name": "天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)",
"importance": 0.00025,
"place_id": "74005413",
"lon": "116.391244079988",
"lat": "39.90733345",
"osm_type": "way",
"licence": "Data ? OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_id": "25097203",
"boundingbox": [
"39.9072273",
"39.9075343",
"116.3906566",
"116.3918428"
],
"type": "yes",
"class": "building"
}
根據(jù)坐標(biāo)信息查詢(xún)地址
>>> import json, logging
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.reverse("39.90733345,116.391244079988")
>>> print location.address
天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)
>>> print json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8')
{
"display_name": "天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)",
"place_id": "74005413",
"lon": "116.391244079988",
"boundingbox": [
"39.9072273",
"39.9075343",
"116.3906566",
"116.3918428"
],
"osm_type": "way",
"licence": "Data ? OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_id": "25097203",
"lat": "39.90733345",
"address": {
"building": "天安門(mén)",
"city": "北京市",
"house_number": "1",
"country": "中國(guó)",
"suburb": "東城區(qū)",
"state": "北京市",
"postcode": "100010",
"country_code": "cn",
"road": "西長(zhǎng)安街"
}
}