0

I'm triyng to use the python bindings of gdal to rasterize a raster using a shapefile. The code I'm using is rather simple:

gdal.RasterizeLayer(target_raster, [1], input_layer ,burn_values=[-9999],options=['INVERSE=TRUE'])

The code runs and doesn't produce any errors, but it is completely ignoring the "inverse" option meaning that it is removing the part of the raster I want to keep.

  • So if I understand you correctly you want to create a raster where, lets say 1 represents the areas that IS NOT covered by the vector file (I assume polygon) and 0 - or NoData - represents the areas that IS covered by the vector file? – Jascha Muller Mar 12 at 8:54
  • Currently the RasterizeLayer burns the nodata value into everything below the polygon and ignores everything outside. What I want it to do is to leave everything below the polygon alone and instead burn the nodata value outside of the polygon. (This is what inverse=true should do) – Yjelza Mar 12 at 16:34
  • Have proposed a solution. Hope it helps – Jascha Muller Mar 12 at 20:45
1

I think I understand what you want. I have not had much success in passing option arguments for RasterizeLayer (e.g. options=['INVERSE=TRUE']). So I will propose an alternative. I have written the full code to give you perspective. I hope it helps.

from osgeo import gdal, osr, ogr

StructRas = 'E:/Path/to/your/Raster.tif' # Raster on which I will base the structure of the rasterized output (cell size, projection and extent)
OutRas = 'E:/Path/to/your/OutRaster.tif' # Output Raster (will contain the rasterized vectors)
Shapefile = 'E:/Path/to/your/shapefile/ToRasterize.shp' # vector file to be rasterized
UniqueNumerical = 'RstrField'   # OPTIONAL if you have a field in the vector layer you want to base the rasterazation on (instead of burning values)
                                # This needs to be the name of field column in the vector layer and it needs to be numerical

# Getting Vital signs from your structure raster
StructRas = gdal.Open(StructRas)
rasXsize = StructRas.RasterXSize
rasYsize = StructRas.RasterYSize
gt = StructRas.GetGeoTransform()
prjWKT = StructRas.GetProjection()
nowBand = StructRas.GetRasterBand(1)
NDvaule = StructRas.GetNoDataValue() # In your case the NoData value is -9999

# Opening the vector file
inVect = ogr.Open(Shapefile)
inLyr = inVect.GetLayer()

#Creating empty output raster in memory
RasterizedRas = gdal.GetDriverByName('MEM').Create('memory_raster', rasXsize, rasYsize,1 ,gdal.GDT_Float32)
RasterizedRas.SetGeoTransform(gt)
RasterizedRas.SetProjection(prjWKT)
inBand = RasterizedRas.GetRasterBand(1)
inBand.SetNoDataValue(NDvaule) # In your case -9999
inBand.Fill(NDvaule) # In your case -9999

# Use this is you want to rasterize with the values from your specified column "UniqueNumerical" - DO NOT USE BOTH RasterizeLayer's, choose 1
gdal.RasterizeLayer(RasterizedRas, [1], inLyr, options = ['ATTRIBUTE=%s' % UniqueNumerical]) 
# Use this if you want to burn in values (I have made the burn value 1) - - DO NOT USE BOTH RasterizeLayer's, choose 1
gdal.RasterizeLayer(RasterizedRas, [1], inLyr ,burn_values=[1])

# Writing out the final output raster
gdal.GetDriverByName('GTiff').CreateCopy(OutRas, RasterizedRas)

print('Done')
  • Hello and sorry for the late reply. I have decided to accept this as the solution, while it will not work for me exactly as the rasters are too large to recreate in memory it is still a workable option. – Yjelza Mar 25 at 8:59
  • Hi, thanks. You don't have to create the raster in memory, sorry, that's just the way I do it. You can create it on your drive as well, just replace RasterizedRas=gdal.GetDriverByName('MEM').Create('memory_raster',rasXsize,rasYsize,1,gdal.GDT_Float32) with RasterizedRas=gdal.GetDriverByName('GTiff').Create('C:\Path\to\raster\Outras.tif', rasXsize, rasYsize,1 ,gdal.GDT_Float32) and then replace the gdal.GetDriverByName('GTiff').CreateCopy(OutRas, RasterizedRas) with RasterizedRas = None . I hope that would do it. – Jascha Muller Mar 25 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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