检查数据框中的倍数纬度和经度是否在多边形内

问题描述 投票:-1回答:1

我正在尝试将设备的区域名称分配给已注册的每个设备,每个设备都有纬度和经度坐标。

enter image description here

我将此信息转换为地理数据框:

enter image description here

我获得了区域多边形的信息(格式:cpg,dbf,prj,geojson,sbn,sbx,shp,shx):

我的地区多边形信息:enter image description here

我的目标是知道设备是否在多边形名称内,然后创建新列并将该区域名称分配给该设备。

预期输出:enter image description here谢谢,待在家里...

python pandas geojson geopandas shapely
1个回答
0
投票

要查找给定点是否在多边形内,可以使用shapely

from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

point = Point(0.5, 0.5)
polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
print(polygon.contains(point))

然后您只需要处理数据框,因此纬度,经度为Points,区域为Polygons

一旦您的数据采用这种形式,您就可以在apply上使用geo_df来获得相应的区域。

geo_df.apply(lambda row: regions_df[regions_df.polygon.contains( row.lat_lon_point)].region.to_list(), axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.