如何使用 GeoPy 在 Python 中解析任何 lat/lon 字符串
处理用户输入的坐标时,你通常有类似 N 48° 06.976 E 11° 44.638、48°06'58.6"N 11°44'38.3"E 或 N48.116267, E11.743967 的字符串。这些 lat/lon 坐标有许多许多不同的格式,这使得在自动化设置中解析相当困难。
Python 的一个简单解决方案是使用 geopy,它提供对 ArcGIS 等在线服务的访问。这些服务使解析几乎任何形式的坐标变得容易。你可以使用以下命令安装 geopy
install_geopy.sh
pip install geopy注意 Nominatim 不适用于纯坐标用例 - 它可以很好地解析坐标,但会返回最近的建筑物/地址。
geopy_parse_example.py
from geopy.geocoders import ArcGIS
geolocator = ArcGIS()
result = geolocator.geocode("N 48° 06.976' E 11° 44.638'")如果坐标无法解析,result 将为 None
之后,你可以通过以下方式使用 result:
print(result) 将只打印结果:
geopy_print_result.txt
>>> print(result)
Location(Y:48.116267 X:11.743967, (48.11626666666667, 11.743966666666667, 0.0))你可以使用 result.latitude 和 result.longitude 提取纬度和经度。
geopy_coords_example.py
>>> print(result.latitude, result.longitude)
(48.11626666666667, 11.743966666666667)有关使用这些坐标的其他方法,请参阅 geopy 文档。
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow