[GIS] How to modify raster pixel inside a polygon

gdalmaskingraster

I would like to set to novalue all the pixels of my raster inside a polygon.
enter image description here

I'm trying the gdalwarp command line with -cutline option.
But this command extract the values of my raster inside the polygone (shapefile).

Raster and vector dataset's are both in the same projection.
What coud be the next steps ?
If this is the right track, is it possible to script the "raster calculator" plugins ?
Thank you in advance

Best Answer

Cutline does the exact opposite of what you are trying to do. If you use cutline with gdalwarp, it should crop your raster to the area INSIDE the polygon and set everything outside to no data (see below):

before after

I don't know why nothing happened in your case (perhaps you forgot to specify the cutline layer option -cl - who knows).

To do what you want there are several ways of going about it. One would be to create a polygon with the same extent as your raster and then calculate the difference of that polygon with your desired polygons. This will effectively give you a new polygon which is the area you want to keep. You can then use that as your cutline.

An alternative is to rasterize your polygons with the same resolution as your existing raster using gdal_rasterize, but set the -burn value to be the same as your NoData value (which you can also specify) and your -init value to be 1. You can then simply multiply your new raster with the existing one.

Related Question