From Open Foris Wiki
A script for combining several masks
Usage: oft-combine-masks.bash <input1> <input2> .... <nodata> [EPSG code]
Example: oft-combine-masks.bash mask1.img mask2.img clouds.shp badregions.shp -9999 EPSG:32636
- Idea is to allow user to give mask images and shapefiles and this script combines them into 1 mask
- The first one is the base (and at this point it must be an image, not shapefile)
- The next ones will be written on only if there is nodata (user-defined value)
- Extent comes from the first image
- Projection is given by the user (optinally)
- If not, all files are assumed to be in same projection
- In the shapefiles, the last field is assumed to be the one containing the mask values
- At least 2 files and nodata value are needed
- Get Example data set
- For this exercise following tools are used: combine_masks.bash, oft-calc, gdal_rasterize
- Open your working directory using
1. Create masks
- To run oft-combine-masks.bash we need to create some mask files. To do so, we burn the attribute values of the column mask from the shapefile landuse.shp into the raster forest.tif:
gdal_rasterize -b 1 -a mask -l landuse landuse.shp forestc.tif
- Verify in QGIS if your pixel values of forestc.tif match the polygon values of landuse.shp.
- Note: if the raster output is black, click on it's Properties -> Style -> Colour Map and chose Pseudo Colour
|Attribute table of landuse.shp.||Zoom of output raster forestc.tif in QGIS using the colourmap Pseudocolour.|
- Forestc.tif is the base raster to create some masks files by extracting those pixels that contain values which were previously in the shapefile and then burned into the raster:
oft-calc forestc.tif mask1.tif 1 #1 55 = 0 1 ? //If the pixel values is 55 in ''forestc.tif'', then give it in ''mask1.tif'' the value 1, otherwise 0
oft-calc forestc.tif mask2.tif 1 #1 11 = 0 2 ? //If the pixel values is 11 in ''forestc.tif'', then give it in ''mask2.tif'' the value 2, otherwise 0
oft-calc forestc.tif mask3.tif 1 #1 33 = 0 3 ? //If the pixel values is 33 in ''forestc.tif'', then give it in ''mask3.tif'' the value 3, otherwise 0
oft-calc forestc.tif mask4.tif 1 #1 44 = 0 4 ? //If the pixel values is 44 in ''forestc.tif'', then give it in ''mask4.tif'' the value 4, otherwise 0
oft-calc forestc.tif mask5.tif 1 #1 22 = 0 5 ? //If the pixel values is 22 in ''forestc.tif'', then give it in ''mask5.tif'' the value 5, otherwise 0
- Again, check in QGIS if the masks contain the extracted value for the same location of the corresponding polygon in landuse.shp.
- In the final step we run the command oft-combine-masks.bash. Note that output file is automatically processed called combined_mask.img
oft-combine-masks.bash mask1.tif mask2.tif mask3.tif mask4.tif mask5.tif 0
2. Combine masks using mask images and shapefile
- Run oft-combine-masks.bash: Input: mask1.tif, mask2.tif, mask3.tif, mask4.tif, mask5.tif and the additional shapefile clouds.shp In the shapefile the values of the last column are picked up for processing; output is automatically processed: combined_masks.img.
NOTE: copy your combined-mask.img output from the first exercise as it will be overwritten running oft-combine-masks.bash again.
oft-combine-masks.bash mask1.tif mask2.tif mask3.tif mask4.tif mask5.tif cloud.shp 0 //the 0 defines nodata values to be 0
- Verify in QGIS if combined_masks.img contains all mask values, and if the additional polygon of cloud.shp has the values 99 (look into attribute table of cloud.shp under the last column).
|Combined masks including the larger polygon from cloud.shp.|