[gdal-dev] Fwd: Problem with gdal.Rasterize utility : argument GDALDatasetShadow in wrapper_GDALRasterizeDestName
François ALLAIN
frallain at gmail.com
Mon Jul 18 09:19:19 PDT 2016
Here is how I did finally :
INPUTS:
mask_geojson = [feature['geometry'] for feature in geojson['features']]
filelike_in # my Geotiff file in-Memory (cStringIO.StringIO)
# https://lists.osgeo.org/pipermail/gdal-dev/2010-May/024723.html
# Create in-memory file and initialize it with the content
gdal.FileFromMemBuffer('/vsimem/tiffinmem', filelike_in.read())
# Open the in-memory file
ds = gdal.Open('/vsimem/tiffinmem', gdal.GA_Update)
#
https://trac.osgeo.org/gdal/browser/trunk/autotest/utilities/test_gdal_rasterize_lib.py?rev=30939
# https://trac.osgeo.org/gdal/wiki/rfc59.1_utilities_as_a_library
#
https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#spatial-filter
vector_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0)
layer = vector_ds.CreateLayer('layer')
layer.GetLayerDefn()
feature = ogr.Feature(layer.GetLayerDefn())
geojson_str = json.dumps(mask_geojson[0])
feature.SetGeometryDirectly(ogr.CreateGeometryFromJson(geojson_str))
layer.CreateFeature(feature)
ret = gdal.Rasterize(ds,
vector_ds,
bands = [1,2,3,4],
inverse = True,
burnValues = [0])
# rasterio.mask.mask burns the RGB channels on top of the Alpha
# -> this is not needed but results in a smaller png :
# 56.7kB instead of 78.5 kB. Any change in the time required ?
# transforming a GDAL-style shape to a standard-style shape
np_array = np.rollaxis(ds.ReadAsArray(), 0, 3)
filelike_out = cStringIO.StringIO()
misc.imsave(filelike_out, np_array, 'png')
# Close the dataset
ds = None
# Release memory associated to the in-memory file
gdal.Unlink('/vsimem/tiffinmem')
Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20160718/de786812/attachment.html>
More information about the gdal-dev
mailing list