Python 求多边形的最大内接矩形
我试图在半打多边形中找到数百万个点。这是我的密码:Python 求多边形的最大内接矩形,python,gis,point-in-polygon,shapely,Python,Gis,Point In Polygon,Shapely,我试图在半打多边形中找到数百万个点。这是我的密码: def find_shape(longitude,latitude): if longitude != 0 and latitude != 0: point = shapely.geometry.Point(longitude,latitude) else: return "Unknown" for current_shape in all_shapes: if curre
def find_shape(longitude,latitude):
if longitude != 0 and latitude != 0:
point = shapely.geometry.Point(longitude,latitude)
else:
return "Unknown"
for current_shape in all_shapes:
if current_shape['bounding_box'].contains(point):
if current_shape['shape'].contains(point):
return current_shape['properties']['ShapeName']
break
return "Unknown"
我已经阅读了关于使用shapely提高多边形中点查询性能的其他问题。他们建议吃火鸡。但是,对于有许多多边形(,)且不希望在所有多边形上循环的情况,这似乎很有用
如您所见,我已经在设置边界框。以下是我的形状设置代码:
with fiona.open(SHAPEFILE) as f_col:
all_shapes = []
for shapefile_record in f_col:
current_shape = {}
current_shape['shape'] = shapely.geometry.asShape(shapefile_record['geometry'])
minx, miny, maxx, maxy = current_shape['shape'].bounds
current_shape['bounding_box'] = shapely.geometry.box(minx, miny, maxx, maxy)
current_shape['properties'] = shapefile_record['properties']
all_shapes.append(current_shape)
检查形状的另一个非常简化的版本是否有用,即由最大内接矩形(或三角形)制成的形状
检查shapely文档,似乎没有这个功能。也许是一些背景?当然,我始终希望确保新的简化形状不会超出原始形状的边界,因此我不必对实际形状调用contains()
。我还想让新的简化形状尽可能简单,以提高速度
任何其他建议,以及感谢。谢谢
编辑:在等待回复的同时,我想到了一个想法,可以创建一个满足我要求的简化形状:
current_shape['simple_shape'] = current_shape['shape'].simplify(.5)
current_shape['simple_shape'] = current_shape['simple_shape'].intersection(current_shape['shape'])
以下是我在测试每个点时如何使用它:
if current_shape['simple_shape'].contains(point):
return current_shape['properties']['ShapeName']
elif current_shape['shape'].contains(point):
return current_shape['properties']['ShapeName']
这并不完美,因为在进行必要的intersection()
之后,形状并不像可能的那样简单。然而,这种方法使处理时间减少了60%。在我的测试中,85%的点查询使用简单多边形
编辑2:关于GIS StackExchange的另一个相关问题:
. 这涉及大约3000个多边形中的150万个点。我将使用R树。
但我会将所有点(而不是多边形的边界框)插入到R树中
使用r树索引,例如:
//插入一个点,即left==right&&top==bottom,本质上会在索引中插入一个单点条目
for current_point in all_points:
idx.insert(current_point.id, (current_point.x, current_point.y, current_point.x, current_point.y))
//现在在多边形中循环
for current_shape in all_shapes:
for n in idx.intersect( current_shape['bounding_box'] )
if current_shape['shape'].contains(n):
# now we know that n is inside the current shape
因此,在较大的R-树上查询六次,而不是在较小的R-树上查询数百万次。啊,我认为这可以减少比较的次数,但我有3亿多个点,所以我不能一次将它们全部加载到索引中。现在,我正在从一个文件中按顺序读取点。我将它们传递到一个多处理池中,该池为每个对象运行
find\u shape()
。而且,大多数情况下,第一个比较的多边形包含该点。(我已经优化了所有形状数组的顺序。)我不认为3亿个条目对于索引来说太多。但是,如果您的数据有些脱节或分区,请将其加载到30个索引中,每个索引有1000万个,并执行30*6查询。这里有人把1000万点载入了一棵R树:这对我帮助很大。感谢@GaryLiao,Joshua Arnott的这个“katana”函数(递归地将每个多边形在其最短维度上一分为二)是一个非常令人愉快的想法。如果你有一个演示如何在交叉点查询中使用该方法的实现(特别是多边形中的点),请分享它,因为博客并没有明确说明如何使用生成的分割多边形。katana生成了两种矩形:(A)多边形内的矩形,以及(B)与多边形边缘相交的矩形。通过索引纬度和经度,很容易找到矩形内的点。A和B中的点都可以在几秒钟内找到。我们确信A内的点也在多边形内,所以我们要做的最后一件事就是依次检查B内的点。
for current_shape in all_shapes:
for n in idx.intersect( current_shape['bounding_box'] )
if current_shape['shape'].contains(n):
# now we know that n is inside the current shape