How to extract interior polygon coordinates using Shapely?

前端 未结 1 1573
走了就别回头了
走了就别回头了 2021-01-06 07:38

I am new to Shapely (but enthusiastic about it), and recently I\'ve discovered a bit of a road bump.

I have a polygon shapefile that I am reading in via Fiona. This

相关标签:
1条回答
  • 2021-01-06 07:52

    Interior and exterior rings are structured differently. For any polygon, there is always 1 exterior ring with zero or more interior rings.

    So looking at the structure of a geometry, exterior is a LinearRing object, and interiors is a list of zero or more LinearRing objects. Any LinearRing object will have coords, which you can slice to see a list of the coordinates with coords[:].

    The following is a function that returns a dict of lists of exterior and interior coordinates:

    def extract_poly_coords(geom):
        if geom.type == 'Polygon':
            exterior_coords = geom.exterior.coords[:]
            interior_coords = []
            for interior in geom.interiors:
                interior_coords += interior.coords[:]
        elif geom.type == 'MultiPolygon':
            exterior_coords = []
            interior_coords = []
            for part in geom:
                epc = extract_poly_coords(part)  # Recursive call
                exterior_coords += epc['exterior_coords']
                interior_coords += epc['interior_coords']
        else:
            raise ValueError('Unhandled geometry type: ' + repr(geom.type))
        return {'exterior_coords': exterior_coords,
                'interior_coords': interior_coords}
    

    E.g.:

    extract_poly_coords(myShape)
    
    0 讨论(0)
提交回复
热议问题