【发布时间】:2020-08-06 12:02:56
【问题描述】:
我有一系列不重叠的形状多边形 (>1000)。我想引入一个新的形状点,并想快速知道该点在哪个多边形中。我为此有一个 for 循环,但我正在寻找一种更快的方法。
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
test_points = pd.Series([[(0,1), (1,1), (1,0)], [(0,0), (0,1), (1,0)]])
# a Dataframe containing my polygons and an id
polygonized_points = pd.DataFrame({"polygons" : test_points.map(lambda x : Polygon(x)), "id" : range(0, len(test_points), 1)})
# a new point
new_point = Point(0.4, 0.3)
# allocation of point to hexes (which I want to be faster)
for idx, row in polygonized_points.iterrows() :
if row.polygons.contains(new_point) :
new_point_in_id = row.id # imagine this would be a df with an empty column for the id variable
我很确定我错过了一些东西来加速这个 b/c 我认为 for 循环不能很好地扩展。感谢您的帮助!最好的
【问题讨论】: