Wednesday, 13 January 2016

Reclassify and combine rasters in QGIS?


I have a vector layer containing watercourse location which I have rasterized. I also have a DEM containing elevation data for the region in which the watercourses are located. I would like to reclassify my rasterized watercourse layer such that pixels which contain a watercourse are equal to 1, and pixels which do not contain watercourses are equal to 0. I would then like to combine this new watercourse layer with my existing DEM to generate a layer containing watercourses showing the elevation at each pixel along their length.



I have attached a picture describing what I would like to achieve.


enter image description here


Would anyone please be able to provide a step-by-step guide for reclassifying my rasterized watercourse layer and for generating my desired end product? I am using QGIS 3.4 Madeira.



Answer



The tool is called Raster Calculator, but in QGIS there are two, both can be found in the Processing Toolbox. One in Raster analysis > Raster calculator, the other in GDAL > Raster miscellaneus > Raster calculator.




If you are using QGIS 3.4.3 for Windows, one way or another, you will need to upgrade the numpy module version of the OSGeo4W builds, to have GDAL working properly. It's an already reported bug.


You will need to open your OSGeo4W Shell (OSGeo4w.bat) with administrator privileges (Run as Administrator). By default it's located in C:\OSGeo4W\ or C:\OSGeo4W64\ for Network Installers, or C:\Program Files\QGIS 3.4 for Stand-alone Installers.


At the command prompt, run: python -m pip install -U numpy. Wait for the Successfully installed numpy-1.15.4 return and close the Shell.





For the first option (Raster analysis > Raster calculator), you will need one more preprocess, because the calculator isn't working if raster files don't have Float32 type data. That requiriment isn`t necessary for the second option (GDAL > Raster miscellaneus > Raster calculator).


You can run the GDAL > Raster conversion > Translate (convert format) tool, whit the Output data type option set to Float32, to convert both raster files before the calculation.




Assuming that you have a "DEM" raster layer, wich have elevations in Band 1.
Assuming also that you have a "river" raster layer, wich have only zeros and ones values, in Band 1.
Both with Float32 data type.


Then, the expression for the Raster Calculator of QGIS can be: "DEM@1" * "river@1". Make sure that you have chosen a Reference layer ("DEM" is a good option) to define cellsize, extents and CRS. If not, assign those values.
The operation carried out is the simple multiplication between the values of both layers. The result is a new "Output" raster layer with DEM elevations where there were values ones in the river layer, and zero values in all other cells.
As was well said in the comments, you can get the same result through a conditional expression: ("river@1" = 1) * "DEM@1".


If you want to use the GDAL Raster Calculator, select Input layers as A and B, Input bands for A and B, and use the expression: A * B. The result will be the same.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...