Hi,

Alternatively you could have a look at the function "terrain" in the raster package, it can calculate the slope for you using different algorithms, not sure if the one below is included though.

For typical spatial requests like this, you could also use the mailing-list r-sig-geo.

Cheers,
Jon

On 01-Dec-11 14:27, Jean V Adams wrote:
jjcabrera20 wrote on 12/01/2011 04:02:47 AM:

Hello everyone in the forum
For introducing myself I would say I have a basic knowledge of R.
Now I am intended to implement a flood algorithm using R. I have some
MatLab
experience doing these, and as an example to explain more or less what I
want, I have a m code to calculate the slope from a Digital elevation
model:
slope=zeros(row,col);
for i=2:row-1
     for j=2:col-1
        dzdx=(raster(i,j+1)-raster(i,j-1))/(2*res);
        dzdy=(raster(i+1,j)-raster(i-1,j))/(2*res);
        slope(i,j)=sqrt(dzdx^2+dzdy^2);
     end
end;

The question is to know how to do the similar procedure on R. All
suggestions are welcome
Thanks

All best,

Jorge
PD:I am using R on windows system 64 bits

If I am interpreting your code correctly (I don't use MatLab myself),
something like this should give you the same result in R:

# example matrix of heights
raster<- matrix(runif(20, 10, 30), nrow=4, ncol=5)

# example resolution
res<- 8.5

# dimensions of matrix
drast<- dim(raster)

# for every non-boundary point in the matrix,
# calculate the distances between its adjacent columns (dzdx) and rows
(dzdy)
dzdx<- raster[2:(drast[1]-1), 3:drast[2]] - raster[2:(drast[1]-1),
1:(drast[2]-2)]
dzdy<- raster[3:drast[1], 2:(drast[2]-1)] - raster[1:(drast[1]-2),
2:(drast[2]-1)]

# calculate the slope from these distances and the resolution
slope<- sqrt(dzdx^2 + dzdy^2) / (2*res)


Jean
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


--
Jon Olav Skøien
Joint Research Centre - European Commission
Institute for Environment and Sustainability (IES)
Global Environment Monitoring Unit

Via Fermi 2749, TP 440,  I-21027 Ispra (VA), ITALY

jon.sko...@jrc.ec.europa.eu
Tel:  +39 0332 789206

Disclaimer: Views expressed in this email are those of the individual and do 
not necessarily represent official views of the European Commission.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to