python - 如何在没有外部 API 的情况下通过坐标获取国家名称?
问题描述
我在寻找什么?
我正在寻找一个带有方法/模块的python库,该方法/模块无需连接到外部API即可获取坐标并返回国家名称
为什么?
我有一个带有很多行(更多 10000 行)的 pandas df,我不想为每一行发送请求。
这就是我现在正在做的事情:
from geopy.geocoders import ArcGIS
...
...
...
geolocator = ArcGIS(scheme='http')
for index, row in df.iterrows():
if math.isnan(row['latitude']) or math.isnan(row['longitude']):
continue
else:
try:
location = geolocator.reverse((row['latitude'], row['longitude']), timeout=30)
# takes the country
location = str(location)
if len(location.split(",")) == 4:
country = location.split(",")[3][1:]
df.at[index, 'country'] = country
如果可以为所有行发送一个请求,它仍然可以
解决方案
似乎没有现成的库,但在没有外部 API 的情况下,一种可能的解决方案是:
将自然地球边界数据下载为 shapefile - https://www.naturalearthdata.com/downloads/10m-culture-vectors/
使用 geopandas 将边界 shapefile 加载到您的脚本中 - https://geopandas.org/io.html
对每个纬度点进行多边形测试,以获得与该点相关的国家 - https://automating-gis-processes.github.io/CSC18/lessons/L4/point-in-polygon.html
推荐阅读
- java - 如何在java中将字符串转换为日期?
- php - 在 Laravel 6 上使用自定义身份验证
- python - Kivy 中的 3D 图形
- unit-testing - OpenCover 不工作,它总是显示 0% 代码覆盖率
- docker-image - vcredist_x86.exe 无法安装在从 mcr.microsoft.com/windows/servercore:10.0.14393.3506 构建的容器中
- mysql - 需要外键约束的详细信息失败
- python - 在多个 Excel 电子表格中删除列
- laravel - Laravel Migration 上的意外删除让我找不到迁移
- r - 使用 R 抓取 Google 搜索结果
- powerbi - 无法使用 HTTP 端点远程/从 Power BI 服务访问 SSAS 表格实例