📜  Python|反向地理编码以使用地理坐标在地图上获取位置

📅  最后修改于: 2022-05-13 01:54:55.602000             🧑  作者: Mango

Python|反向地理编码以使用地理坐标在地图上获取位置

反向地理编码是从给定的一对地理坐标(纬度和经度)中查找地点或位置地址的过程。
需要的模块:

reverse_geocoder: A Python library for offline reverse geocoding.
pprint: A module which helps to "pretty-print" any arbitrary python data structure.

安装:
这些模块可以使用pip轻松安装。

pip install reverse_geocoder
pip install pprint

例子:

Input : (36.778259, -119.417931)
Output : 
Loading formatted geocoded file...
[{'admin1': 'California',
  'admin2': 'Fresno County',
  'cc': 'US',
  'lat': '36.72384',
  'lon': '-119.45818',
  'name': 'Minkler'}]
Input : (28.644800, 77.216721)
Output : 
Loading formatted geocoded file...
[{'admin1': 'NCT',
  'admin2': 'New Delhi',
  'cc': 'IN',
  'lat': '28.63576',
  'lon': '77.22445',
  'name': 'New Delhi'}]

下面是实现:

Python3
# Python3 program for reverse geocoding.
 
# importing necessary libraries
import reverse_geocoder as rg
import pprint
 
def reverseGeocode(coordinates):
    result = rg.search(coordinates)
     
    # result is a list containing ordered dictionary.
    pprint.pprint(result)
 
# Driver function
if __name__=="__main__":
     
    # Coordinates tuple.Can contain more than one pair.
    coordinates =(28.613939, 77.209023)
     
    reverseGeocode(coordinates)


输出:

Loading formatted geocoded file...
[{'admin1': 'NCT',
  'admin2': 'New Delhi',
  'cc': 'IN',
  'lat': '28.63576',
  'lon': '77.22445',
  'name': 'New Delhi'}]

参考:
https://pypi.org/project/reverse_geocoder/
https://www.latlong.net/