# Cell calculations; QGIS,GRASS,GDAL,SAGA

I recently asked a question about trying to calculate the slope over distance (manually) using QGIS, but then I came up with another way of solving the problem, but I would have to get some help for that as well (Sorry, new to open source GIS).

Can I calculate the value difference between adjacent cells, and if the condition is met (e.g. Cell A-Cell B => 1) the cell is given a value of 1, and if false, a value of 0? Or just a color map with intervals of values? This calculation would have to be repeated until every cell is compared with its connecting cells. Does this make any sense? I have latest QGIS with GRASS and gdal, and I also use SAGA. Thanks Fredrik

• Bearing in mind that it's almost always simpler and faster to use existing, tested, optimized code, check out r.mfilter and r.neighbors before considering writing cell-by-cell programs. May 8, 2012 at 15:40
• I read about both of those functions you suggested, and I might be wrong here, but both of them seems to just be averaging/smoothing functions. How would I specify them to achieve what I'm after? May 10, 2012 at 9:42
• Given that you are dealing with a raster, every cell apart from the edges has eight neighbours (assuming square cells), you will not have a single value for any individual cell as per your example. So are you wanting to simply calculate the the actual slope of a cell or the eight 'relative slopes' (which is what you describe)? If the latter, what do you want to achieve by this? May 10, 2012 at 10:51
• ps - I'm trying to understand what you want to achieve that can't be done using established tools like 'slope', 'aspect', 'ruggedness index' etc. May 10, 2012 at 10:58
• alright, I might be way of here, and if so I am sorry :) You are absolutely right, of course what I wrote will be hard to do. I am dealing with elevation data, and thought that if the elevation difference between two cells would be high enough, it would give a true statement, and vice versa. What I want to achieve is to display two areas, one where the slope is considered "extreme" (which would be highlighted), and one where it is not. This might be achievable with the normal tools, but I don't know how! May 10, 2012 at 12:02

Using GRASS ("existing, well tested" as whuber suggested) you might try:

Import your DEM into GRASS, and set the GRASS region to match that raster

``````r.in.gdal in=dem.tif out=my_dem
g.region -p rast=dem
``````

Now the r.slope.aspect module to get a slope raster:

``````r.slope.aspect elev=my_dem slope=my_slope asp=my_aspect
``````

and use r.reclass to create a (virtual) new raster with values (and labels) split to your liking (slopes from r.slope.aspect are in degrees):

``````r.reclass in=my_slope out=my_reclass_slope rules=slope_rules
``````

where the text file "slope_rules" might look like:

``````0 thru 2 = 0  Flat
3 thru 6 = 1  Mild
7 thru 12 = 2  Steep
13 thru 90 = 3  Extreme
``````

Good luck, Micha

• (+1) This is the right idea. I believe the OP wants specifically to compare individual cell values. Instead of using r.slope.aspect as an example (which doesn't do this), why not use r.mfilter? Create a matrix file like ` MATRIX 3 \ 0 0 0 \ 0 1 -1 \ 0 0 0 ` to subtract the rightmost value from the current value, for instance. May 17, 2012 at 22:33