英文:
Extract District from Coordinates Using Python
问题
我有一组经度和纬度坐标,并希望能够使用Python提取每个坐标的区域。
到目前为止,我已经使用geopy
库开发了以下函数:
from geopy.geocoders import Nominatim
from geopy.point import Point
MAX_RETRIES = 5
def get_district(lat, longi):
geolocator = Nominatim(user_agent="http")
point = Point(lat, longi)
retries = 0
while retries < MAX_RETRIES:
retries += 1
try:
location = geolocator.reverse(point)
district = location.raw['address']['state_district']
return district
except:
print('Request failed.')
print('Retrying..')
time.sleep(2)
print('Max retries exceeded.')
return None
这对于单个点来说是有效的,但我有很多这样的点(大约10,000个),而且这只能适用于一个坐标。没有批量请求多个点的选项。
此外,当进行多个此类请求时,此API变得不太可靠。
有没有更好的方法来使用Python实现这一点?我愿意尝试任何方法。即使有一个包含坐标与区域映射的文件,我也可以接受。
注意:目前,我正在查看斯里兰卡的坐标。
英文:
I have a collection of longitudes and latitudes, and I want to be able to extract the district of each of these coordinates using Python.
As of right now, I have developed the following function using the geopy
library,
from geopy.geocoders import Nominatim
from geopy.point import Point
MAX_RETRIES = 5
def get_district(lat, longi):
geolocator = Nominatim(user_agent="http")
point = Point(lat, longi)
retries = 0
while retries < MAX_RETRIES:
retries += 1
try:
location = geolocator.reverse(point)
district = location.raw['address']['state_district']
return district
except:
print('Request failed.')
print('Retrying..')
time.sleep(2)
print('Max retries exceeded.')
return None
This works fine for a single point, but I have a number of them (approximately 10,000) and this only works for one coordinate at a time. There is no option to make bulk requests for several points.
Furthermore, this API becomes quite unreliable when making multiple such requests.
Is there a better way to achieve this using Python? I am open to any approach. Even if there is a file of sorts that I can find with a mapping of the coordinates against the districts, it works for me.
Note: At the moment, I am looking at coordinates in Sri Lanka.
答案1
得分: 1
你可以使用Geopandas。首先,你需要下载斯里兰卡的shapefile文件(DDL),然后提取第二级的文件(行政区划,ADM2)。最后,
# pip install geopandas
import geopandas as gpd
from shapely.geometry import Point
# 你还需要.shx文件
gdf = gpd.read_file('lka_admbnda_adm2_slsd_20220816.shp')
def get_district(lat, longi):
point = Point(longi, lat) # 在这里交换了longi和lat
return gdf.loc[gdf.contains(point), 'ADM2_EN'].squeeze()
用法:
>>> get_district(6.927079, 79.861244)
'Colombo'
>>> get_district(9.661498, 80.025547)
'Jaffna'
英文:
You can use Geopandas. First you have to download the shapefiles of Sri Lanka (DDL) then extract the files of the second level (district, adm2). Finally,
# pip install geopandas
import geopandas as gpd
from shapely.geometry import Point
# you also need .shx file
gdf = gpd.read_file('lka_admbnda_adm2_slsd_20220816.shp')
def get_district(lat, longi):
point = Point(longi, lat) # swap longi and lat here
return gdf.loc[gdf.contains(point), 'ADM2_EN'].squeeze()
Usage:
>>> get_district(6.927079, 79.861244)
'Colombo'
>>> get_district(9.661498, 80.025547)
'Jaffna'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论