1

Is there a way to rasterize each polygon instance with its own burn value? So what I am trying to do is create a mask of the polygons in mask.shp using the projection and geotransform information from raster.tif. Each polygon mask needs to have a unique value. My current approach burns all of them as 255. Here is my code:

vector_layer = "mask.shp"
raster_layer = "raster.tif"
target_layer = "mask.tif"

# open the raster layer and get its relevant properties
raster_ds = gdal.Open(raster_layer, gdal.GA_ReadOnly)
xSize = raster_ds.RasterXSize
ySize = raster_ds.RasterYSize
geotransform = raster_ds.GetGeoTransform()
projection = raster_ds.GetProjection()

# create the target layer (1 band)
driver = gdal.GetDriverByName('GTiff')
target_ds = driver.Create(target_layer, xSize, ySize, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform(geotransform)
target_ds.SetProjection(projection)
source_ds = ogr.Open(vector_layer)
source_layer = source_ds.GetLayer()

ds = gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[255])

target_ds = 0

1 Answer 1

2

If your vector layer contains a field with an unique value, e.g. ID, to burn these values simply use:

ds = gdal.RasterizeLayer(target_ds, [1], source_layer, options = ["ATTRIBUTE=ID"])
1
  • Hey, some what of a related question... Do you know how to create a field with a unique value in my .kml file? I was able to get it working with .json data but could not with kml data. If you know how to get this working could you please comment on a new question I created for this? gis.stackexchange.com/questions/261859/… Thanks!
    – jlcv
    Nov 15, 2017 at 2:58

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.