Skip to content
Prev 6084 / 29559 Next

Slope and Aspect calculations in R

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: