I've been trying to extract values from a single attribute raster (area, in
m2) that overlaps with different lines (that is, a .shp SpatialLines).
I am using the extract() function but it only extracts values from
cells that are touched (that is, crossed) by the lines. The problem is
that my raster has adjacent cells both right and left of the cell that
is actually touched by the lines. Thus, when I add up the extracted values
a significant amount of area (m2) is lost due to cells that were not
touched by the line and therefore values were not extracted.
I tried to work it around by:
Step 1 - first aggregating my raster to a lower resolution (i.e. increasing
the fact argument) and then
Step 2 - rasterizing the lines using this aggregated raster (created in
step 1) as a mold to make sure the rasterized lines would get thick enough
to cover the horizontal spread of cells in my original resolution raster.
Step 3 - Then I resample the rasterized lines (created in step 2) back to
the original resolution I started with.
Step 4 - Finally, extracted the values from the resampled rasterized lines
(created in step 3).
However, it didn't quite work as now the total area (m2) varies according
to the fact="" value I use when first aggregating the raster (in step 1).
I also tried using crop() and mask() but had no success as well.
I really appreciate if anyone has already dealt with a similar problem and
can help me out here. Here are the codes I've been running to try to get
it to work:
# input raster file
g.025 <- raster("ras.asc")
g.1 <- aggregate(g.025, fact=2, fun=sum)
# input SpatialLines
Spline1 <- readOGR("/Users/xxxxx.shp")
Spline2 <- readOGR("/Users/xxxxx.shp")
Spline3 <- readOGR("/Users/xxxxx.shp")
# rasterizing using low resolution raster (aggregated)
Re: How to extract values from adjacent raster cells that are not touched by SpatialLines?
I'm not completely sure if this is what you are looking for, but here is a
worked example on how to get cell indices (thus, data) from neighbouring
cells. Essentially, I'm using the adjancency() function from raster,
reducing to unrepeated cells, and excluding the ones that fall under a line
r <- raster(ncol=10,nrow=10) # generate a raster object
r <- 0 # populate values with 0's
p <- rasterToPolygons(r) # get a polygon for each cell (for visual purposes)
# make a line object (SpatialLines)
x <- c(-124.66110, -93.04031, -52.37858, 24.44486, 15.98469, 52.12075,
y <- c(-46.021148, -27.197684, -6.804443, 10.113111, 28.375197, 8.851744,
xy <- data.frame(cbind(x,y))
spl <- SpatialLines(list(Lines(list(Line(xy)), ID=1)))
# get adjacent cells
lcells <- cellFromLine(r, spl)[] # cells from line
r[lcells] <- 1 # mark line cells with 1
ad <- adjacent(r, lcells, 4) # this gives you a matrix with adjacent cells,
# you can specify 8 or 16
# here I'm using 4
# sorting and removing duplicates
ad <- sort(as.vector(ad))
ad <- ad[!duplicated(ad)]
ad2 <- ad[ ! ad %in% lcells ]
r[ad2] <- 2
# to visualize the example:
plot(spl, add=T, col="red")
# Obviously, you con extract the values of adjacent cells simply with:
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Hope this helps,
Santiago Sanchez-Ramirez, PhD
Ecology and Evolutionary Biology
University of Toronto