1

I am looking to perform raster math on two large rasters (slope ~1.3GB and aspect ~2.4GB). When I use the GIS raster calculator code below, QGIS errors out (out of memory), which is odd considering the computer has 20 cores and 32GB of RAM. I am hoping I could do this using gdal_calc, but am unsure if gdal_calc could perform such an equation, as well as the syntax difference.

("slope@1" <=5)*16 + (("slope@1" > 5) AND ("slope@1" <=38) AND ("aspect@1" >=168) AND ("aspect@1" <=192))*4 + (("slope@1" > 5) AND ("slope@1" <=38) AND ("aspect@1" >=90) AND ("aspect@1" <168))*1 + (("slope@1" > 5) AND ("slope@1" <=38) AND ("aspect@1" >192) AND ("aspect@1" <=270))*1 + ("slope@1" >38)*0 + ("aspect@1" <90)*0 + ("aspect@1" >270)*0
5

gdal_calc.py can certainly do this sort of calculation, but whether you encounter the memory issue or not will be up to actually running it... If you run out of memory again consider splitting the task into multiple smaller units and then adding all those units together.

Original:

("slope@1" <=5)*16 + 
(("slope@1" > 5) AND ("slope@1" <=38) AND ("aspect@1" >=168) AND ("aspect@1" <=192))*4 + 
(("slope@1" > 5) AND ("slope@1" <=38) AND ("aspect@1" >=90) AND ("aspect@1" <168))*1 + 
(("slope@1" > 5) AND ("slope@1" <=38) AND ("aspect@1" >192) AND ("aspect@1" <=270))*1 + 
("slope@1" >38)*0 + 
("aspect@1" <90)*0 + 
("aspect@1" >270)*0

gdal_calc.py (untested):

gdal_calc.py -A aspect.tif -S slope.tif --outfile=output.tif \
--calc="(S <= 5)*16 + 
        logical_and.reduce(S > 5, S <= 38, A >= 168, A <= 192)*4 +
        logical_and.reduce(S > 5, S <= 38, A >= 90, A <= 168)*1 +
        logical_and.reduce(S > 5, S <= 38, A >= 192, A <= 270)*1
       "

I left off the last 3 lines of your original calculation because they seem to always return 0, which is wasting RAM.

  • Seems to be an error with python...cannot import name MAXREPEAT...thoughts? – Ryan Garnett Sep 1 '17 at 19:51
  • @RyanGarnett Don't have an answer for the MAXREPEAT error. Try the calc argument on a single line with no linebreaks? – Logan Byers Sep 1 '17 at 20:43

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.