Faster way of polygon intersection with shapely

前端 未结 2 1920
时光取名叫无心
时光取名叫无心 2020-11-28 05:20

I have a large number of polygons (~100000) and try to find a smart way of calculating their intersecting area with a regular grid cells.

Currently, I am creating th

2条回答
  •  旧巷少年郎
    2020-11-28 05:32

    Since 2013/2014 Shapely has STRtree. I have used it and it seems to work well.

    Here is a snippet from the docstring:

    STRtree is an R-tree that is created using the Sort-Tile-Recursive algorithm. STRtree takes a sequence of geometry objects as initialization parameter. After initialization the query method can be used to make a spatial query over those objects.

    >>> from shapely.geometry import Polygon
    >>> from shapely.strtree import STRtree
    >>> polys = [Polygon(((0, 0), (1, 0), (1, 1))), Polygon(((0, 1), (0, 0), (1, 0))), Polygon(((100, 100), (101, 100), (101, 101)))]
    >>> s = STRtree(polys)
    >>> query_geom = Polygon([(-1, -1), (2, 0), (2, 2), (-1, 2)])
    >>> result = s.query(query_geom)
    >>> polys[0] in result
    True
    

提交回复
热议问题