使用Python从坐标中提取区域

huangapple go评论140阅读模式
英文:

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=&quot;http&quot;)
  point = Point(lat, longi)

  retries = 0

  while retries &lt; MAX_RETRIES:
    retries += 1
    try:
      location = geolocator.reverse(point)
      district = location.raw[&#39;address&#39;][&#39;state_district&#39;]
      return district
    except:
      print(&#39;Request failed.&#39;)
      print(&#39;Retrying..&#39;)
      time.sleep(2)

  print(&#39;Max retries exceeded.&#39;)
  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(&#39;lka_admbnda_adm2_slsd_20220816.shp&#39;)

def get_district(lat, longi):
    point = Point(longi, lat)  # swap longi and lat here
    return gdf.loc[gdf.contains(point), &#39;ADM2_EN&#39;].squeeze()

Usage:

&gt;&gt;&gt; get_district(6.927079, 79.861244)
&#39;Colombo&#39;

&gt;&gt;&gt; get_district(9.661498, 80.025547)
&#39;Jaffna&#39;

huangapple
  • 本文由 发表于 2023年6月15日 14:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定