Skip to content

Slope and Aspect calculations in R

13 messages · Roger Bivand, milton ruser, Tomislav Hengl +4 more

#
Hello

We are looking to mimic, in R, the slope (max magnitude difference 
between a cell and its neighbors) and aspect (direction of maximum 
magnitude difference) functions found in ESRI's Spatial Analyst package. 
We were just about to code up the slope function but thought I would 
make sure we weren't re-inventing any wheels already out there.

Thanks
Josh
#
Josh,

I can not answer your specific question WRT R, but before you embark on 
this, you may consider R with GRASS GIS ? they play together pretty 
nicely and the slope and aspect capabilities are part of GRASS.

Regards,
Tom
Josh London wrote:

  
    
#
Thanks for the suggestions so far regarding the use of GRASS or SAGA.

We have considered those options but SAGA doesn't appear available for 
MacOS. My understanding is that spgrass6 is more for accessing R 
code/functions from within GRASS vs accessing GRASS functions from 
within R. We have all our other analysis coded up in R, and just need 
these functions to do one part of a bigger process. Maybe I'm 
misunderstanding how to use R and GRASS together. I'll do some further 
research.

Thanks again
Josh
Josh London wrote:

  
    
#
On Thu, 9 Jul 2009, Josh London wrote:

            
with initGRASS() and run GRASS commands like r.slope.aspect with 
execGRASS() from the R side. See http://www.geomorphometry.org/R.asp for 
some examples - foot of page. You may also want to look at the underlying 
literature, and some of the functionality being provided in the raster 
package on R-Forge.

Roger

  
    
#
Josh,

I use R & GRASS together on both MacOS X and Linux without problems. 
It's very direct to write a shell script that calls both R & GRASS 
within a single script. Writing a R script that calls GRASS may be more 
tedious to do what you want. Writing a shell script that calls both may 
be the wat to go.

Tom
Josh London wrote:

  
    
#
Dear Josh, Thomas,

I typically use R+SAGA (R on top!) for such type of analysis (and I do have access to all operating
system/software). There must be some good reasons that I am using SAGA (speed, stability, rich
geomorphometry module etc.)! So do not give up so easy on SAGA.

I especially like that SAGA is fairly efficient in processing big grids, and the commands are rather
compact. For example to reproject grids from one to another proj4string, you only need to set e.g.:
sep=""), TARGET_PROJ="\"+proj=longlat +datum=WGS84\"", SOURCE="TWI.sgrd", TARGET="TWI_ll.sgrd",
TARGET_TYPE=0, INTERPOLATION=0))

*see http://www.geomorphometry.org/R.asp 

I remember that somebody did manage to set-up SAGA also under Mac OS
(http://www.saga-gis.uni-goettingen.de/html/index.php?module=pnForum&func=viewtopic&topic=458), but
I also remember that most of students on our GEOSTAT course used Windows under Mac OS to run SAGA. I
am sure that it should be possible to compile SAGA under Mac OS, but somebody needs to put some
effort to bridge this gap (check also with Olaf Conrad).

Here is more discussion about the same topic from few months ago:
https://stat.ethz.ch/pipermail/r-sig-geo/2009-April/005418.html 

cheers,

Tom Hengl
#
Tom,

Thanks! I have seen your posts and results of some of your work with 
SAGA and I am very interested. But, I have been to lazy to this point to 
try compiling SAGA for either MacOS X or Linux (is a Linux distribution 
available?).

Regards,
Tom
Tomislav Hengl wrote:

  
    
#
Yes: SAGA runs normally on Linux (I use it also), but you can not use the command line from R (i.e.
RSAGA) to pass command to SAGA  :(

But you can use R to pass command lines to SAGA without RSAGA (i.e. the batch files):

http://saga-gis.wiki.sourceforge.net/Executing+Modules+with+SAGA+CMD 

Tom Hengl
http://spatial-analyst.net
#
Hi,

I have recently made some adjustments to RSAGA that will allow it to 
work under Linux too. I haven't uploaded it to CRAN yet, please try out 
this temporary build if you wish:

http://www.environment.uwaterloo.ca/u/brenning/RSAGA_0.9-6.tar.gz

I appreciate your feedback - unfortunately I am not a Unix/Linux user so 
I depend on your error reports.

Thanks to Carlos Grohmann for his support with the above adaptation to 
Linux.

Cheers
  Alex
Tomislav Hengl wrote:

  
    
#
Thanks to everyone for the feedback. I may dabble around with SAGA/RSAGA 
on a linux box we have and also continue to watch for any MacOS builds.

Roger, I am especially excited that spgrass6 has the initGRASS() 
function. by providing access to r.slope.aspect, I think that will be 
the simplest/quickest route to a solution. I look forward to tapping 
into other GRASS commands in the future. You may want to consider 
changing the description of spgrass6 on CRAN. I have interpreted the 
phrase 'based on starting R from within the GRASS environment' to mean 
the package is for those who use GRASS and want to access functions 
within R.

cheers, and thanks again for the feedback and help

Josh
Tomislav Hengl wrote:

  
    
2 days later
#
Hello Josh, and list,

Figuring out how to link R with GIS may be extremely useful to you, not just 
for calculating slope and aspect, but also for other analyses in the future. 
There are also reasons you might not want to; I am one of those people who 
likes to write his own code. Consequently I implemented the Horne algorithm a 
while back - this is used in ESRI software for calculating slope. The code, a 
pretty simple function, is here:
https://www.msu.edu/~ashton/classes/866/notes/lab12/slope.R

Three caveats:
1. ESRI's version changes the neighborhood shape on the raster edges. The 
result is a slope grid with slopes calculated for every cell in the input 
elevation grid. The linked function only calculates slope on the interior 
cells. It returns a raster of the same dimension as the input, but there are 
no valid slope values around the edges.

2. Many developers on this list might agree with me that often the hardest 
part of coding spatial algorithms is dealing with the data structures. Over 
the years we've seen the development of increasingly sophisticated spatial 
data structures in R. My implementation dates from the Pleistocene of R, and 
is simply implemented on a matrix. Your data may have to be restructured to 
work seamlessly with this script, or you may wish to change the script to fit 
your structure.

3. While this function works perfectly adequately on smaller grids, it has not 
been tested on large matrices. It was not developed with speed in mind - the 
script was for a classroom exercise, and I wanted my students to be able to 
read the code and relate it to the published algorithm before executing it on 
fairly small datasets. If I wanted to calculate slope on huge grids, I'd 
personally do it in GRASS or Arc/Info, or in home-rolled C code. 

Hope this may be useful!

Ashton
On Friday 10 July 2009 14:42:29 Josh London wrote: