[R-sig-eco] How to extract values from contiguous raster cells that ate not touched by SpatialLines?

Philippi, Tom tom_philippi at nps.gov
Fri Aug 18 21:05:24 CEST 2017


Andre--
I'm not completely clear on what you are trying to do.  My best guess
interpretation of "my raster sometimes goes from one
to several contiguous cells in all directions"  is that you have a raster
that you are aggregating into blobs of contiguous cells based on values in
one layer.  You then want to extract (area) values by blob for each blob a
polyline intersects.
If that's close to what you want, there are several approaches that will
work, with the most efficient approach dependent on the size of your
rasters, whether you can use gpu computing for very large rasters, etc.
I would first create a new thematic (factor) raster layer of blob
membership, then aggregate or compute your desired per blob measures with
that thematic layer and your other layer(s).  Then, extract blob indices
(values from the thematic layer) for those intersected by your lines with
extract:
blob.hits1 <- extract(blobraster, Spline1,df=TRUE,...), # note using the
polyline Spline1 not the rasterized version
Then subset your blob measures by those indices.

Even if I'm completely wrong in my guess of what you are attempting, I
would wager that you should use extract with the unrasterized Spline1
object.

I hope that this helps, or at least helps you revise your question to get a
better response from R-sig_Geo.

Tom 2

On Fri, Aug 18, 2017 at 11:30 AM, Bede-Fazekas Ákos <bfalevlist at gmail.com>
wrote:

> Hi Andre,
> I don't know the answer, but I think you should post your question to
> R-sig-Geo mailing list (R-sig-Geo at r-project.org), where several authors
> of the spatial R-packages and other GIS experts are members and will read
> your post.
> HTH,
> Ákos Bede-Fazekas
> Hungarian Academy of Sciences
>
>
> 2017.08.18. 17:09 keltezéssel, Andre Rovai írta:
>
>> Hi all,
>>
>> I've been trying to extract values from a single attribute raster (area,
>> in
>> m2) that overlaps with lines (that is, a .shp SpatialLines).
>>
>> The problem is that, along these lines, my raster sometimes goes from one
>> to several contiguous cells in all directions.  Using the
>> extract function only values from cells that are touched by the lines are
>> extracted.  Thus, when I add up the extracted values from all lines 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 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)
>>
>> c1 <- rasterize(Spline1, g.1, field=Spline1$type, fun=sum)
>>
>> c2 <- rasterize(Spline2, g.1, field=Spline2$type, fun=sum)
>>
>> c3 <- rasterize(Spline3, g.1, field=Spline3$type, fun=sum)
>>
>>
>>
>> # resampling back to higher resolution
>>
>> c1 <- resample(c1, g.025)
>>
>> c2 <- resample(c2, g.025)
>>
>> c3 <- resample(c3, g.025)
>>
>>
>>
>> # preparing to extract area (m2) values from raster “g.025”
>>
>> c1tab <- as.data.frame(c1, xy=T)
>>
>> c2tab <- as.data.frame(c2, xy=T)
>>
>> c3tab <- as.data.frame(c3, xy=T)
>>
>> c1tab <- c1tab[which(is.na(c1tab$layer)!=T),]
>>
>> c2tab <- c2tab[which(is.na(c2tab$layer)!=T),]
>>
>> c3tab <- c3tab[which(is.na(c3tab$layer)!=T),]
>>
>>
>>
>> # extracting area (m2) values from raster “g.025”
>>
>> c1tab[,4] <- extract(g.025, c1tab[,1:2])
>>
>> c2tab[,4] <- extract(g.025, c2tab[,1:2])
>>
>> c3tab[,4] <- extract(g.025, c3tab[,1:2])
>>
>> names(c1tab)[4] <- "area_m2"
>>
>> names(c2tab)[4] <- "area_m2"
>>
>> names(c3tab)[4] <- "area_m2"
>>
>>
>>
>> # sum total area (m2)
>>
>> c1_area <- sum(c1tab$area_m2)
>>
>> c2_area <- sum(c2tab$area_m2)
>>
>> c3_area <- sum(c3tab$area_m2)
>>
>> tot_area <- sum(c1_area, c2_area, c3_area)
>>
>>
>> Thanks!
>>
>> Andre Rovai
>> Department of Oceanography and Coastal Sciences
>> Louisiana State University
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-ecology mailing list
>> R-sig-ecology at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>>
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>

	[[alternative HTML version deleted]]



More information about the R-sig-ecology mailing list