Date Thu 04 November 2010 Tags QGIS

A common activity in GIS is to mask off parts of an image such that it has transparent pixels in the masked area. We can do this in QGIS too with a bit of help from gdal. Lets look at this niche model I ran for Anopheles gambiae:

Ecological Niche model for Anopheles gambiae

What I really want is all the parts of the model that fall outside of the borders of Tanzania to appear transparent. We will use a vector layer to delineate the masked area. I am going to make a mask for Tanzania. To make the mask from the vector layer we can use the gdal rasterize tool. But before we can do that we need to deal with the fact that rasterize is a bit awkward (in current version of GDAL) in that the image you are rasterizing must pre-exist. The simple work around is to just make a copy of my raster that I am trying to make a mask for using gdalwarp. I am using warp with the cutline option, in so doing assigning all cells outside of my country of interest to have a value of nodata. Unfortunately no gui tool exists for this yet so you need to run the command from the command line:

  gdalwarp -of GTiff -dstnodata 0 \
  -cutline Boundaries/Tanzania.shp Anopheles_gambiae__projection.tif maskbase.tif

|The first step to making our mask - all the non TZ areas are assigned a null value.|

Next I will load my vector layer containing the Tanzanian political boundary into QGIS:

Our base mask and the Tanzania boundaries loaded into QGIS.

Now I will use the Raster -> Rasterize tool to create the next phase mask - to fill the inside of the mask with the same value. Note that it will overwrite the contents of maskbase.tif - thats ok since we created maskbase specifically for that purpose. You can choose any attribute field for the mask, it doesn't really matter since we will be replacing all no data values in a few steps time:

Using the rasterize tool to assign areas inside the mask with the same value.
gdal_rasterize -a POP_CNTRY \
-l Tanzania \
/home/timlinux/gisdata/Africa/Tanzania/MasterDataSet/Boundaries/Tanzania.shp \

After it has run we should have something like the image below. Note how the areas outside the country borders are transparent, while the areas inside all have the same value (in my case 222).

After rasterization, all non-masked areas are assigned a value from the vector layer.

Thats great, but I want a value of 1 inside all of the country so I use the shiny new raster calculator tool that will be in QGIS 1.6:

Layer -> Raster Calculator

And then create a simple boolean expression to assign all cells of value greate than 1 a value of 1.

Maskbase@1 > 1

I've called the resulting file mask.tif. I'll be throwing maskbase.tif away when I am done since is is a temporary working file. Note that the boolean (< and >) operators in the raster calculator are undocumented, and don't appear as buttons on the user interface - you will need to type the expression manually. Buttons for these will be added in QGIS 1.7. In the screen shot below if you look at the Value Tool dock window (ValueTool is a great plugin!), you will see that the final mask has a value of 1 where the cursor is placed while the maskbase layer has a value of 222.

The masked areas are now all assigned a value of 1.

Ok so now that we have a mask, we can apply it to arbitrary rasters to make areas of the raster transparent e.g. (Using Layer -> Raster Calculator again):

Mask@1 * Anopheles_gambiae__projection@1

Which produces this:

Our final masked model (shown in greyscale) over the original, non-masked model (shown in colour).

Some afterthoughts:

There are always many ways to do things with computers in general and with FOSSGIS in particular - and after writing this I thought of some ways to reduce the number of steps ... I'll leave it to my readers to post in the comments if they have other more efficient ways of going about this activity.

Also just a note that I am using the QGIS raster calculator from SVN with a patch applied from Marco for transparency support. The patch will make its way into SVN in the next day or two and the raster calculator in general will arrive in 1.6 release of QGIS (or grab a nightly test build).

Update:  The patch mentioned above is now in SVN trunk.


comments powered by Disqus