0

I am having trouble creating a layer with geom type as geometry collection. Thus I am unable to output to a shapefile. I have attached code below. dstLayer is null. This does not happen if geom type is polygon or multilinestring or any other.

spatialReference = osr.SpatialReference()
spatialReference.SetWellKnownGeogCS('WGS84')
driver = ogr.GetDriverByName("ESRI Shapefile")
dstPath = os.path.join("common-border", "border.shp")
dstFile = driver.CreateDataSource(dstPath)
dstLayer = dstFile.CreateLayer("layer", spatialReference, ogr.wkbGeometryCollection)

Any help will be appreciated.

thanks

sushmit sarmah
  • 7,508
  • 6
  • 21
  • 24

2 Answers2

0

The ESRI Shapefile driver supports one geometry type per layer, and this cannot be a geometry collection type.

So it would look something like this:

# this will create a directory with 1 or more shapefiles
dst = driver.CreateDataSource("common-border")
# create common-border/points.shp
pointLayer = dst.CreateLayer("points", spatialReference, ogr.wkbPoint)
# create common-border/polygons.shp
polygonLayer = dst.CreateLayer("polygons", spatialReference, ogr.wkbPolygon)
# ... as needed
dst.GetLayerCount()  # 2 layers / shapefiles

And you would need to add each geometry type to the appropriate layer. There are probably some really smart things you can do with dictionaries to do the mapping, and if a key does not exist for the geometry type, it could create the required layer, etc.

Mike T
  • 41,085
  • 18
  • 152
  • 203
0

I fixed the problem by converting the collection into its individual components and then plotting it into the layer. It wasn't possible any other way as shapefiles dont support collections. As my objective was to plot it onto the shapefile this worked for me.

sushmit sarmah
  • 7,508
  • 6
  • 21
  • 24