Search code examples
rterrain

How to calculate slope and aspect ratio values by giving latitude, longitude and elevation data as input?


As I have no much idea regarding the calculation of slope, Can anyone help me with the code that can calculate slope and aspect ration values?

I have latitude, longitude and elevation values in a file. Is there any method to get these values?

I searched a lot and got to know there is a function called "terrain" in R, which gives slope and aspect ratio. But I got nan values everywhere.


Solution

  • using some dummy data:

    library(raster)
    
    long <- rep(rep(seq(12,36,0.5),41))
    lat <-rep(seq(32,52,0.5), each=49)
    dat <- rnorm(2009, 26.5, 44.0)
    
    data <- data.frame(long, lat, dat)
    
    rast <- rasterFromXYZ(data)
    crs(rast) <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs "
    
    slope <- terrain(rast,opt = 'slope', unit = 'degrees')
    aspect <- terrain(rast, opt = 'aspect', unit = 'degrees')
    flowdir <- terrain(rast, opt = 'flowdir')
    
    spplot(stack(rast, slope, aspect, flowdir), scales = list(draw = T)) # the legend is not correct - only for demonstration purposes