Skip to main content
make it work with other formats and not overwrite python's input keyword
Source Link
bugmenot123
  • 11.3k
  • 3
  • 38
  • 75

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one polygon = list of polygons, look at GDAL: ESRI Shapefile)

It is easier with Fiona and Shapely:

import fiona
from shapely.geometry import shape, mapping 

# open the original MultiPolygon file
with fiona.open('multipolygons.shp') as inputsource:
    # create the new file: the driver, crs and schemacrs are the same
    # for the schema the geometry type is "Polygon" instead
    output_schema = dict(source.schema)  # make an independant copy
    output_schema['geometry'] = "Polygon"

    with fiona.open('output.shp', 'w',driver=input 
                    driver=source.driver, 
 crs=input                   crs=source.crs, 
 schema=input.schema                   schema=output_schema) as output: 

        # read the input file
        for multi in inputsource: 

           # extract each Polygon feature
           for poly in shape(multi['geometry']): 

              # write the Polygon feature
              output.write({
                  'properties': multi['properties'],
                  'geometry': mapping(poly)
              })

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one polygon = list of polygons, look at GDAL: ESRI Shapefile)

It is easier with Fiona and Shapely:

import fiona
from shapely.geometry import shape, mapping
# open the original MultiPolygon file
with fiona.open('multipolygons.shp') as input:
    # create the new file: the driver, crs and schema are the same
    with fiona.open('output.shp','w',driver=input.driver, crs=input.crs, schema=input.schema) as output:
        # read the input file
        for multi in input:
           # extract each Polygon feature
           for poly in shape(multi['geometry']):
              # write the Polygon feature
              output.write({'properties': multi['properties'],'geometry': mapping(poly)})

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one polygon = list of polygons, look at GDAL: ESRI Shapefile)

It is easier with Fiona and Shapely:

import fiona
from shapely.geometry import shape, mapping 

# open the original MultiPolygon file
with fiona.open('multipolygons.shp') as source:
    # create the new file: the driver and crs are the same
    # for the schema the geometry type is "Polygon" instead
    output_schema = dict(source.schema)  # make an independant copy
    output_schema['geometry'] = "Polygon"

    with fiona.open('output.shp', 'w', 
                    driver=source.driver, 
                    crs=source.crs, 
                    schema=output_schema) as output: 

        # read the input file
        for multi in source: 

           # extract each Polygon feature
           for poly in shape(multi['geometry']): 

              # write the Polygon feature
              output.write({
                  'properties': multi['properties'],
                  'geometry': mapping(poly)
              })
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one polygon = list of polygons, look at GDAL: ESRI Shapefile)

It is easier with Fiona and Shapely:

import fiona
from shapely.geometry import shape, mapping
# open the original MultiPolygon file
with fiona.open('multipolygons.shp') as input:
    # create the new file: the driver, crs and schema are the same
    with fiona.open('output.shp','w',driver=input.driver, crs=input.crs, schema=input.schema) as output:
        # read the input file
        for multi in input:
           # extract each Polygon feature
           for poly in shape(multi['geometry']):
              # write the Polygon feature
              output.write({'properties': multi['properties'],'geometry': mapping(poly)})