3

I have two binary rasters, raster1 and raster2, which represent the distribution of grassland habitats inside an area in two different moments in time (raster1 = older, raster2 = recent). In both rasters, grassland areas have the value 255, while everything else has the No data value of 0. The two rasters have the same extent and the cell centers line up.

I would like to do a simple change detection operation and create a new raster whose values are something like:

if raster1 and raster2 = 255 --> assign 255 to new raster (or any other value, i simply want it to represent "no change and remained grassland"

if raster1 and raster2 = 0 --> 0 (no change and remained not grassland)

if raster1 = 0 and raster2 = 255 --> 100 (changed and became grassland from no grassland)

if raster1 = 255 and raster2 = 0 --> 200 (changed and became not grassland from grassland)

Anyone can suggest a way to do this in QGis? (that doesn't require much knowledge in writing codes, if possible)

2 Answers 2

3

Sure you can achieve this with raster calculator (In the main top menu as Raster / Raster Calculator...)

I simplified the names as raster1 - A, raster2 - B. The expression should look like this:

(A=255 AND B=255)*255+(A=0 AND B=0)*0+(A=0 AND B=255)*100+(A=255 AND B=0)*200


Update: For this operation the rasters in appropriate data type are expected, for example Int16. What you can do with your rasters is simply convert them into lower data type from Float64 using Raster / Conversion / Translate (Convert Format).... After you choose input and output file, you need to make expression in window editable and add -ot Int16 or some other data type to your liking as described here: http://www.gdal.org/gdal_translate.html

Translate raster data type

Or as suggested by wildintellect you can try use GRASS r.mapcalculator for it:

Processing / Toolbox should open toolbox docker, you might need to switch to Advanced interface at the bottom of the docker to see all advanced tools and then find Grass commands - Raster - r.mapcalculator which might work without need of translating the files.

https://grass.osgeo.org/grass64/manuals/r.mapcalculator.html

4
  • Thank you for your reply. Unfortunately when I try to do this I get a minidump error and qgis crashes... both my files have these metadata: Driver GeoTIFF Dimensions X: 18821 Y: 15837 Origin 1.71397e+06,5.13445e+06 Pixel Size 2.00004,-2.00001 No Data Value -99999 Data Type Float64 - Sixty four bit floating point Layer Extent (layer original source projection) 1713966.4223982943221927,5102780.3500192686915398 : 1751609.1579853519797325,5134454.5267527680844069 Any idea if this is my problem or QGis problem?
    – Simona
    Commented Sep 9, 2015 at 13:26
  • It's a little strange to have Float64 rasters considering how simple your data is. You could try converting them to Int16 with GDAL. Or you can try to use the GRASS mapcalulator in the Processing toolbox to see if that gets around the issue. PS: what version of QGIS? Commented Sep 9, 2015 at 16:58
  • @Simona as mentioned above that is very unexpected to have Float64 grids for this kind of data. QGIS raster calculator might struggle with that. Going to edit my answer to help with it.
    – Miro
    Commented Sep 10, 2015 at 0:10
  • Thank you for your help, I followed your instructions and it worked perfectly. I originally had Float64 because it's the default created by the rasterize function, and since I don't have experience with dealing with different data types I left it as it was.
    – Simona
    Commented Sep 10, 2015 at 14:16
3

Everything would be much easier with proper value assignment.

To handle combinations it is good to have values in range 0,1,2,4...2^n.

Reclassify your 1st raster to 0 and 1, second to 0 and 2. Calculate total.

It will have values in range (0,1,2,3) which are easily translated into your categories, e.g 3 corresponds to "no change and remained grassland"

4
  • Thank you, I tried this option too and it is a valid alternative (and simpler too) to solve the problem. I am not sure if I can accept two answers for the same question, though?
    – Simona
    Commented Sep 10, 2015 at 14:23
  • FYI you can always uncheck another one. Don't please. There are always multiple solutions, e.g. COMBINE rasters in ArcGIS does the same thing, perhaps similar tool exist in QGIS
    – FelixIP
    Commented Sep 10, 2015 at 20:35
  • I like this answer because yes, reclassify is the way especially if you have more rasters and more classes. Though for this case it can be done in one step with raster calculator which seems more simple to me. No problem to change final values to 0,1,2,3 with slight adjustment of the expression.
    – Miro
    Commented Sep 10, 2015 at 23:31
  • Now when we all agree on binary approach, In this particular case it has to be CON (A <100, 0,1) + CON (B <100, 0, 2).
    – FelixIP
    Commented Sep 11, 2015 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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