【问题标题】:how to optimize performances of geometry operations如何优化几何运算的性能
【发布时间】:2015-04-24 04:37:15
【问题描述】:

我正在寻找一种优化几何运算性能的方法。我的目标是计算一系列多边形(21,562)中有多少点(205,779)。最好使用 python 和 R 以及 GIS 软件,如 ArcGIS、QGIS。

这是我搜索并编写的解决方案。

  1. 使用ArcGIS:一个例子是http://support.esri.com/cn/knowledgebase/techarticles/detail/30779 -> 虽然我没有尝试过,但根据我以前的经验,空间连接总是需要大量时间。

  2. 使用 GDAL、OGR:这是一个示例:http://geoexamples.blogspot.tw/2012/06/density-maps-using-gdalogr-python.html -> 每个多边形需要 5 到 9 秒。

  3. 通过循环使用 Shapely 准备的几何操作:这是我的示例,每个多边形需要 2.7 到 3.0 秒。 (注意点是列表中的点对象)

    prep_poly=[]
    for i in polygons:
        mycount=[]
        for j in points:
            if prep(i).contains(j):
                mycount.append(1) #count how many points within polygons
        prep_poly.append(sum(mycount)) #sum once for every polygon
        mycount=[]
    
  4. 使用带有过滤器的 Shapely 准备几何操作:这是我的示例,每个多边形大约需要 3.3 到 3.9 秒。(注意点是 MultiPoint 对象)

    prep_poly=[]
    for i in polygons:
        prep_poly.append(len(filter(prep(i).contains, point1)))
    

虽然准备好的几何操作确实提高了性能,但处理大量多边形仍然很耗时。有什么建议吗?谢谢!

【问题讨论】:

    标签: python computational-geometry shapely


    【解决方案1】:

    您可以执行以下操作(Python 代码),而不是查看屏幕上每个矩形的每个像素:

    first_pixel = any pixel in the polygon
    px_list = [] # array with pixels left to check
    px_list.append(first_pixel) # add pixel to list of pixels to process
    
    count = 0
    
    while len(array) > 0: # pixels left in pixel list
        curr_pixel = array[0]
        for pixel in get_adjacent_pixels(curr_pixel): # find adjacent pixels
                                                      # ie (vertical, horizontal, diagonal)
            if pixel in shape:
                px_list.append(pixel) # add pixel to list
    
        px_list.remove(curr_pixel)
        count += 1
    

    基本上,路径查找的工作方式相同。查看此 wiki 文章以获取上述算法的直观表示: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Algorithm

    如果您没有简单的方法来找到起点,您可以遍历所有点一次,检查每个点是否包含在形状中,然后将该点与形状一起存储在单独的列表中并删除它来自原始的形状,我们还没有点。

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2010-11-29
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多