Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shapefile containing many polygons that I want to use to mask a raster. I have tried gdalwarp -cutline <shapefile> -crop_to_cutline -srcnodata -9999 -dstnodata -9999 <source_raster> <target_raster>, but the results are visually unappealing because of the low resolution of the raster (i.e. jagged edges). It would look better if I could simply place the vector layer over the raster as a masking layer in the map. I would need to see the underlying raster inside the polygons, but not outside. Two options have occurred to me for accomplishing this.

  1. Invert the polygons and save as new shapefile then fill with opaque background color.
  2. Style the vector layer such that inside the polygons is transparent and outside is opaque.

Being new to GIS, I haven't figured out how to accomplish either of these approaches. I have at my disposal the gdal framework, QGIS, and GRASS. I think that I would prefer a GRASS command-line solution, as I may need to repeat this process more than a few thousand times in the next few days. Also, please feel free to direct me to the appropriate documentation that I have obviously missed.

I hope that this question isn't a duplicate of one of the many I've read about masking rasters with vectors. It appears to me that this case is different because I specifically want the final map to display partial pixels from the raster via a smooth cut by a vector.

share|improve this question
It occurs to me that if I knew how to create a higher resolution version of the rasters (they'd look the same but would have many identical pixels in the new version corresponding to each of the pixels in the current version) then I could apply the gdalwarp masking technique I referenced. – Gregory Mar 21 '13 at 14:45
up vote 1 down vote accepted

You could achieve this using GRASS GIS and the r.mask module by specifying the -i flag to create the inverted MASK. If you need to repeat this for several Shapefiles, you could probably automate this process with a shell script something along the lines of:

for file in `ls *.shp`
do dsn=$file output=${file%.shp} input=${file%.shp} output=${file%.shp}_r use=cat
   r.mask -i ${file%.shp}_r 
   g.copy rast=MASK,${file%.shp}_mask

You may want to change the resolution of the location before converting the vector to a raster, e.g. g.region res=...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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