[gdal-dev] problem with datatype in python

Even Rouault even.rouault at spatialys.com
Thu Sep 3 08:42:01 PDT 2015


Le jeudi 03 septembre 2015 17:34:27, Luca Delucchi a écrit :
> Hi devs,
> 
> I'm trying to rasterize a vector layer and convert it to numpy array.
> I'm using the gdal.RasterizeLayer function but I have problem to set
> the right datatype. I would like to have UInt32 but I got every time
> Byte, this is my code
> 
> target_ds = gdal.GetDriverByName('MEM').Create('', xcount, ycount,
> gdal.GDT_UInt32)

You should add ,1, between ycount and gdal.GDT_UInt32. This is the band count.

> target_ds.SetGeoTransform((fi.ulx, fi.geotransform[1], 0, fi.uly, 0,
> fi.geotransform[5]))
> shape_datasource = ogr.Open(invect)
> shape_layer = shape_datasource.GetLayer()
> # create for target raster the same projection as for the value raster
> raster_srs = osr.SpatialReference()
> raster_srs.ImportFromWkt(fi.s_fh.GetProjectionRef())
> target_ds.SetProjection(raster_srs.ExportToWkt())
> # rasterize zone polygon to raster
> gdal.RasterizeLayer(target_ds, [1], shape_layer, None, None, [1],
> ['ATTRIBUTE=id', 'ALL_TOUCHED=TRUE'])
> trees_indices = gdal_array.DatasetReadAsArray(target_ds)
> 
> This is the debug
> 
> (Pdb) trees_indices.max()
> 255
> (Pdb) ba=target_ds.GetRasterBand(1)
> (Pdb) ba.DataType
> 1
> 
> Have you any idea to solve this problem?

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list