What is the cause for the degradation of environment?
Capitalism, corruption, consuming society? - OVERPOPULATION!
Please, save the Planet - kill yourself...

Sunday, September 2, 2012

Raster Extent Modification Using QGIS

UPDATE: you can download script for Processing module to perform this task from here.

When you need to perform algebraic operations with two or more rasters you often have to ensure that these rasters have the same extent, otherwise the operation either  won't be allowed (in SAGA for example) or you will get output raster cropped to the overlapping part. With regular (not georeferenced) images you can simply load them in GIMP and increase or decrease canvas for amount of pixels you like. But in case of georeferenced images you will loose spatial reference information. 

In QGIS you can change extent of the rasters. Lets examine one of the worst case scenarios. There are two overlapping (one band) rasters A and B. Say, we need to add A values to B values and get the final image to have extent that will contain both images.

Overlapping rasters
We will use QGIS's Raster calculator. Firstly we need to create an empty georeferenced image with needed extent. Go to Raster -> Miscellaneous -> Build Virtual Raster. Here choose rasters A and B to become a virtual raster (VR) and do not define noData values. Add VR to the TOC.

Now we will create a templates for expanded A and B. In Raster calculator (Raster -> Raster calculator) insert expression: VR@1 * 0, highlight VR raster (by clicking on it) in Raster bands and click on Current layer extent button. Execute expression and save its result - it will be a template for raster A. Copy it to create template for raster B.

Now lets change extent for A. In Raster calculator enter expression:  A@1 this will simply leave A values unchanged. Then click on VR raster in Raster bands to highlight it and click a on Current layer extent button - this will set desired extent of the output raster. Save result as a template for A (overwrite existing file). Do the same for B.

Now you have both A and B with the same extent and can freely perform calculations over them.

14 comments :

  1. This doesnt function to me...

    ReplyDelete
    Replies
    1. Any details? Or you just wanted me to feel sorry for you?

      Delete
    2. This doesn't seem to work for me either. I've checked the metadata for the rasters and they all have the same layer extents, yet when running a simple calculation layerA + layerB + layerC, only the area where all three layers overlap is returned.

      Delete
    3. I suppose that you have NULL values assigned to the new pixels that were created during raster modification. Use RasterCalc plugin and perform this: eq([your_raster]@1, -9999, 0) where you have to put actual NULL values of your raster instead of "-9999" and the desired valid value you want these pixels to have instead of "0" (I suppose that in most cases you would want them to be "0").

      Delete
    4. Everything apart from the area which overlaps all three layers already has actual NULL values. Each layer has a value field of 1, so my aim is to show areas with values of 1,2,3 after the calculation, but I just can't get anything other then the overlap area (3) to return.

      Delete
    5. NULL + n = NULL. Performing any calculations with NULL values will lead only to getting NULL as a result. The suggestion above will allow NULL values to became usable. Please, consider post detailed question on http://gis.stackexchange.com/ and provide me with a link - I will look into it.

      Delete
  2. Hi, thanks for the post it helped me a lot until I got a problem. I have 19 layers and when I try to calculate to new extent and click on the layers such as A@10 and so on till A@19, raster calculator says it is invalid expression. do you know what can I do to change this? Many thanks

    ReplyDelete
    Replies
    1. Seems that it is a bug of raster calculator. I've encountered similar issue when tried to perform calculation using over 10 bands. You may calculate needed extent step-by-step. Calculate extent for the first 10 rasters then for the next 10 and so on. Then calculate final extent for the intermediate rasters.

      Delete
  3. Thanks for your prompt reply! yes, I had to do one by one from the original 19 files them merge again. zzzzzz.....

    ReplyDelete
  4. Hi! I have a question :) Somehow when it comes to chosing 2 raster files (my A and B) I only have a bare window, even though everything is already in the project. I just cannot select any raster. Do you have any idea why? Thank you!

    ReplyDelete
  5. Sorry, I'm talking about the script for processing module

    ReplyDelete
    Replies
    1. May be you just missed the "..." button that opens the dialogue for the rasters selection (there is no drop-down menu if you are looking for one)?

      Delete
  6. Sorry, i've figured it out^^ there was a stupid reason for that - I was trying to open your plugin through graphical modeler... as I usually do when i create my own ones. Now it works perfectly! Thank you very much!

    ReplyDelete