R: overlay a line on a raster stack/brick, and get profile of cell values along the line

The extract should do the trick, but you may need to update to the version of raster on CRAN first. To use it, pull in the geometries you're interested in into SpatialLines objects like so:

require raster
require rgdal

r <- raster('dem.tif')
lines <- readOGR(dsn='lines.shp', layer='lines')

elevations <- extract(r, lines)

This works well for most analysis, but isn't fast enough if you're performing very large sets of data (I have an OGR/GDAL implementation I can post somewhere if it'd be useful).


If speed is an issue, consider using RSAGA with the profiles from lines module. http://www.saga-gis.org/saga_tool_doc/7.2.0/ta_profiles_4.html

Tags:

R

Raster