简体   繁体   English

多边形相交查找的优化

[英]Optimizations for Polygon Intersection Finding

I have ~1 billion 2D polygons, each with between 5 and 2000 vertices, laid out on a geographical map. 我有约10亿个2D多边形,每个多边形在地理地图上分布着5到2000个顶点。 I want to generate cutout masks of these polygons in the bounded range of a quadrangle which is a square portion of the map. 我想在四边形的有界范围内生成这些多边形的剪切蒙版,该四边形是地图的正方形部分。 I have a naively-coded solution which collects all the polygons that overlap the quadrangle bounds, then creates a mask and draws each of the polygons which it has found to be overlapping with the quadrangle bounds. 我有一个天真编码的解决方案,该解决方案收集与四边形边界重叠的所有多边形,然后创建一个遮罩,并绘制它发现与四边形边界重叠的每个多边形。

relevant code: 相关代码:

def generate_alteration_layers_by_extent(alterations, poly_locators, extents, output_res=1000):
    left, right, upper, lower = extents
    query_rectangle = pysal.cg.shapes.Rectangle(left, lower, right, upper)
    num_layers = len(shapes_by_alterations)
    boxed_alteration_layers = []
    for i, (alteration, poly_locator) in enumerate(zip(alterations, poly_locators)):
        print "computing overlapping polygons"
        overlapping_polys = poly_locator.overlapping(query_rectangle)
        print "drawing polygons onto mask"
        img = Image.new('L', (output_res, output_res), 0)
        polys = [np.array(map(list, poly.vertices)) for poly in overlapping_polys]
        for poly in polys: 
            # normalize to the dimensions of img
            poly[:,0] -= left;  poly[:,0] *= (right-left); poly[:,0] *= output_res
            poly[:,1] -= lower; poly[:,1] *= (upper-lower); poly[:,1] *= output_res
            ImageDraw.Draw(img).polygon(poly, outline=1, fill=1)
        mask = np.array(img)
        boxed_alteration_layers.append(mask)
    return np.dstack(boxed_alteration_layers)

The problem is that this computation takes too long -- I have run the program for three hours for one mask on a large-memory machine and it has still not finished. 问题是此计算需要太长时间-我已经在大型内存计算机上为一个掩码运行了三个小时的程序,但该程序仍未完成。 The most expensive operation is computing overlapping polygons. 最昂贵的操作是计算重叠的多边形。 I think that the code can by looking over a smaller domain of polygons, excluding searching those that will definitely not overlap. 我认为代码可以通过查看较小的多边形域而排除搜索绝对不会重叠的域。 Note that the poly_locator variables are psyal.cg.locators.PolygonLocator s. 请注意,poly_locator变量是psyal.cg.locators.PolygonLocator Any advice would be appreciated. 任何意见,将不胜感激。 I'm not hell-bent on using Python and think that C++ might have the functionality that I need. 我对使用Python并不是一无所知,并认为C ++可能具有我需要的功能。

I strongly recommend the BOOST.polygon library by Lucanus Simonson. 我强烈推荐Lucanus Simonson的BOOST.polygon库。 It handles all sorts of polygon interactions, with a surprising reduction in computational complexity. 它处理各种多边形交互,并且计算复杂度大大降低。 Simonson's innovative calculus to reduce the dimensionality of the 2-D problems is really neat; 西蒙森(Simonson)为减少二维问题的维数而进行的创新演算确实很巧妙; it left my jaw hanging open for the last 20 minutes or so of his initial proposal. 在他最初的提议的最后20分钟左右,它使我的下巴一直张着。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM