Skip to main content
2 of 4
code edit
gisnside
  • 7.9k
  • 2
  • 32
  • 74

Thank you very much Jeffrey Evans.

Do you know if it is possible to get the length of the line that crosses over a cell? Sometimes the line crosses in the middle of a cell and sometimes in a corner.

length of the line segment crossing a cell varies

#Example with the code provided before by Jeffrey Evans 
library(raster)
library(sp)

# create elevation raster
e <- as(extent(-1942755,-1847241,2524840,2624901), "SpatialPolygons") 
proj4string(e) <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"

r <- raster(e, res=1000)
r[] <- runif(ncell(r),500,2500)

# plot raster
plot(r)

# creat coordinates
xy <- data.frame(x= c(-1859255, -1876255), y=c(2586401, 2552401))

#get elevation of coordinates
elev <- raster::extract(r, SpatialPoints(xy))
#get distance between the two coordinates
d <- spDists( SpatialPoints(xy) )[2] 

# add coordinates to plot
points(-1859255, 2586401, col="red", cex=3, pch= 19)
points(-1876255, 2552401, col="red", cex=3, pch= 19)

#creat spatial line
l <- SpatialLines(list(Lines(list(Line(xy)),ID="1"))) 

#add spatial line to plot
plot(l, add=TRUE, lwd=3, col="Blue")

# extract elevation along the line
elev <- unlist(extract(r, l))

#plot elevation profile
plot(elev, type="o") # !!!!!! Here, I would like to know the length of each segements on the horizontal axis.

Elevation raster with 2 coordinates linked by a line Elevation profile (but we do not know the length of the segment on the horizontal axis

Luc
  • 91
  • 1
  • 6