1

I'd like to set a metadata value within a shapefile using Python bindings. I'm using the SetMetaData() / GetMetaData() methods within GDAL (gdal.org/python/osgeo.ogr.MajorObject-class.html). I've had no problem with raster files, but when I go to do the same with shapefiles, it will update the datasource while it's open, but won't save them if it's closed.

ds = ogr.GetDriverByName('ESRI Shapefile').Open('C:/Polygons.shp', 1)
print( ds.GetMetadata() )
ds.SetMetadata( {'ThisKey':'ThisValue'} )
print( ds.GetMetadata() )
ds.Destroy()
del ds

ds = ogr.GetDriverByName('ESRI Shapefile').Open('C:/Polygons.shp', 1)
print( ds.GetMetadata() )
ds.Destroy()
del ds

produces

{}
{'ThisKey': 'ThisValue'}
{}

I can't see what I'm doing wrong.

1 Answer 1

2

The shapfile driver doesn't support metadata writing. However, the geopackage driver does: https://gdal.org/drivers/vector/gpkg.html#metadata

Changing your code to use this should work with GDAL 2+:

ds = ogr.GetDriverByName('GPKG').Open('C:/Polygons.gpkg', 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.