5

I am attempting to use a memory vector to crop a raster using GDAL warp but I get the following error:

ERROR 1: Cannot open <osgeo.ogr.DataSource; proxy of <Swig Object of type 'OGRDataSourceShadow *' at 0x000001AF5FC238A0> >.

My code is as follows:

import ogr, gdal

drv = ogr.GetDriverByName("Memory")

feature_ds = drv.CreateDataSource("memory_name")

feature_layer = feature_ds.CreateLayer("layer",srs ,geom_type=ogr.wkbPolygon)

featureDefnHeaders = feature_layer.GetLayerDefn()

outFeature = ogr.Feature(featureDefnHeaders)

geometry = feature.GetGeometryRef() #This feature is a polygon and comes from another dataset 


outFeature.SetGeometry(geometry)

feature_layer.CreateFeature(outFeature)     

feature_ds.FlushCache()

gdal.Warp(out.tif, 
          in.tif, 
          outputType=gdal.GDT_Float32,
          cutlineDSName=feature_ds, 
          cropToCutline=True,
          warpMemoryLimit="5000")               

I have tested to make sure the data is all valid, the code runs and produces the results I expect when I make the vector a shapefile. I just need to know if it is actually possible to use memory vectors or not.

1 Answer 1

4

I have been trying the same thing without any luck using the Memory driver.

However you can do it using the virtual file system and for instance the Shapefile driver, which means that you are still working on the file in memory and not on disk.

You can try replacing

drv = ogr.GetDriverByName("Memory")
feature_ds = drv.CreateDataSource("memory_name")

with

drv = ogr.GetDriverByName('ESRI Shapefile')
feature_ds = drv.CreateDataSource("/vsimem/memory_name.shp")

Then you can reference that same path as you cutlineDSName

cutlineDSName="/vsimem/memory_name.shp"

Your Answer

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

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