Data binning: irregular polygons to regular mesh

后端 未结 2 1599
暗喜
暗喜 2020-12-31 08:43

I have thousands of polygons stored in a table format (given their 4 corner coordinates) which represent small regions of the earth. In addition, each polygon has a data val

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 09:20

    There are plenty of ways to do it, but yes, Shapely can help. It appears that your polygons are quadrilateral, but the approach I'll sketch doesn't count on that. You won't need anything other than box() and Polygon() from shapely.geometry.

    For each pixel, find the polygons that approximately overlap with it by comparing the pixels bounds to the minimum bounding box of each polygon.

    from shapely.geometry import box, Polygon
    
    for pixel in pixels:
        # say the pixel has llx, lly, urx, ury values.
        pixel_shape = box(llx, lly, urx, ury)
    
        for polygon in approximately_overlapping:
            # say the polygon has a ``value`` and a 2-D array of coordinates 
            # [[x0,y0],...] named ``xy``.
            polygon_shape = Polygon(xy)
            pixel_value += polygon_shape.intersection(pixel_shape).area * value
    

    If the pixel and polygon don't intersect, the area of their intersection will be 0 and the contribution of that polygon to that pixel vanishes.

提交回复
热议问题