2

I have a large raster image depicting land use. It's a singleband pseudocolor image whose values are contained in a text file. There are 5 classes.

Following the example of this question I thought I could just use the Raster Calculator tool like this (where 1 is the desired class):

filename@1 = 1

Unfortunately, whenever I do this, QGIS just returns a raster with only missing values. Any idea what I'm doing wrong?

EDIT:

Here is what my color ramp looks like, as well as the raster once the colors are applied: enter image description here

Let's say I want to extract all the dark blue pixels containing water values. These have a value of 4, but they are within Band 1. I wonder if this is where I'm having trouble. enter image description here

Here's the Raster Calculator operation I'm performing. enter image description here

But instead of receiving a raster with the dark blue areas, all I get is a raster covering the entire area with missing values. enter image description here

  • Hey John, can you post an image of how your raster data is? Those 5 classes are stored in that band in what way? Integer numbers range [0,4] or are they float values? – DarkCygnus Oct 16 '18 at 22:22
0

Any idea what I'm doing wrong?

It would help to see what type of values your raster contains (integers, float32, etc.).

However, something that could be happening is that you are not selecting the "current layer extent" option in the Calculator. If not selected, this causes mismatch and issues interpreting the coordinates to which you want to read from (in my experience, when I forget to select the option I end up with nodata's or nonsense on my output as well).

  • Thanks, DarkCygnus. I believe the raster contains integer values, but I've updated the post to demonstrate the problem more fully. I also tried re-running the process while explicitly choosing the current layer extent, but that wasn't successful either. – John J. Oct 17 '18 at 14:50
  • I see your update and will read it in detail when I get some time. Meanwhile, what is your expected outcome on the raster you are creating? You get all nodata but what do you want it to output exactly? – DarkCygnus Oct 17 '18 at 16:23
  • Thanks! My expected outcome was a raster consisting just of the dark blue pixels in the original image. After extracting that, I planned to convert it to a vector polygon shapefile. – John J. Oct 17 '18 at 16:26
  • Oh... in that case I don't think such raster calc would do the job... will get back to you when I get out of work. If you can confirm if your data contains integers or floats would greatly help (perhaps using the value tool, or using numpy to load the raster and peek on its values) – DarkCygnus Oct 17 '18 at 20:37
  • They are integer values. Many thanks. I found an example of exactly what I'm trying to do in ArcGIS here. I'm hoping there is a way to do this in QGIS. – John J. Oct 17 '18 at 21:07
0

your expression is valid and should indeed return a binary raster with 0 and 1. I've checked in QGIS 3.2.3 and it worked fine with a 4 band integer raster as input.

You probably did, but the first thing to check is that your raster calculator really failed. It is quite large so maybe the statistics are simply not up to date.

Also, because of the size, there could be a memory issue. So you should test on a small subset to make sure that this is not the problem.

If you installed QGIS with the OTB application, a workaround could be to use OTB's BandMath. the expression would then be

(im1b1==4)?1:0

and you can use an extended filename for the processing of large images

"output.tif?&gdal:co:TILED=YES&gdal:co:BIGTIFF=YES&gdal:co:COMPRESS=LZW"

Another alternative is to use the gdal raster calculator (processing toolbox > raster miscellaneous > Raster calculator). Then you select the raster and its first band as A, and you can use an expression like below:

gdal_calc --calc "A==4" --format GTiff --type Byte -A input.tif --A_band 1 --outfile OUTPUT.tif -co COMPRESS=LZW -co TILED=YES -co BIGTIFF=YES

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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