计算与R交叉栅格单元格的行

时间:2017-12-01 12:13:51

标签: r raster

我有以下栅格(使用readORG的shp文件):

dput(summary_grid)
structure(list(class = structure("SpatialPolygonsDataFrame", package = "sp"), 
    bbox = structure(c(4346000, 3819000, 4445000, 3867000), .Dim = c(2L, 
    2L), .Dimnames = list(c("x", "y"), c("min", "max"))), is.projected = TRUE, 
    proj4string = "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs", 
    data = structure(c("Min.   :0  ", "1st Qu.:0  ", "Median :0  ", 
    "Mean   :0  ", "3rd Qu.:0  ", "Max.   :0  "), .Dim = c(6L, 
    1L), .Dimnames = list(c("", "", "", "", "", ""), "      Id"), class = "table")), .Names = c("class", 
"bbox", "is.projected", "proj4string", "data"), class = "summary.Spatial")

以及包含行的shp文件:

dput(summary_lines)
structure(list(class = structure("SpatialLinesDataFrame", package = "sp"), 
    bbox = structure(c(4329488.96922647, 3429159.10800761, 4998503.48859431, 
    4055688.10547651), .Dim = c(2L, 2L), .Dimnames = list(c("x", 
    "y"), c("min", "max"))), is.projected = TRUE, proj4string = "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs", 
    data = structure(c("Min.   :0  ", "1st Qu.:0  ", "Median :0  ", 
    "Mean   :0  ", "3rd Qu.:0  ", "Max.   :0  ", "Min.   :  448  ", 
    "1st Qu.:13229  ", "Median :28235  ", "Mean   :27205  ", 
    "3rd Qu.:40724  ", "Max.   :50608  ", "Min.   :  485  ", 
    "1st Qu.:13731  ", "Median :29399  ", "Mean   :28635  ", 
    "3rd Qu.:43159  ", "Max.   :53607  ", "Min.   :6519754  ", 
    "1st Qu.:8741416  ", "Median :9167928  ", "Mean   :8894830  ", 
    "3rd Qu.:9414708  ", "Max.   :9762259  ", "Fishing:121  ", 
    NA, NA, NA, NA, NA), .Dim = c(6L, 5L), .Dimnames = list(c("", 
    "", "", "", "", ""), c("      Id", "   trip_id", "  new_tr_id", 
    "     species", "  HELCOM_Gro")), class = "table")), .Names = c("class", 
"bbox", "is.projected", "proj4string", "data"), class = "summary.Spatial")

在带有这些行的shp文件中,每行都有一个名为new_tr_id的唯一标识号。

我的目标是创建一个带有数字的光栅文件,如果行穿过每个单元格。一行(所以相同的new_tr_id)可以跨越同一个单元X次,它将被计数X次。我在下面添加了下图:

In red are the values for each cell. I added the new_tr_id for each line to understand the counting method. For example in the cell surrounded with the blue color, the value is 2 because the lines with the new_tr_id 3 is crossing 2 times

我使用包raster进行了一些有趣的讨论,但我仍然无法找到这个问题的正确答案。

非常欢迎帮助:)

1 个答案:

答案 0 :(得分:0)

这很棘手。计数是标准的,计算总长度不是that hard。但我觉得我来的东西。

示例数据

library(raster)
cds1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60))
cds2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55))
cds3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45))
lns <- spLines(cds1, cds2, cds3, crs="+proj=utm +zone=1 +datum=WGS84")   
r <- raster(ncols=9, nrows=5, vals=1:45, crs="+proj=utm +zone=1 +datum=WGS84")

标准计数:

x <- rasterize(lns, r, fun='count')

我们可以通过栅格单元边框打破线段(这对于非常大的栅格不起作用)。

rsp <- rasterToPolygons(r)
lns2 <- intersect(lns, rsp)
r <- rasterize(lns2, r, fun='count')

但结果不正确,因为如果一个片段接触一个细胞,它将被计为。

另一种方法是切割它们并在边缘处移除一点。您必须为数据使用两个参数

wdth = .25
smallp = 100
b <- buffer(rsp, dissolve=FALSE, width=wdth)
j <- intersect(b, b)
j$area <- area(j)
small <- j[j$area < smallp, ]
small <- aggregate(small)
lns3 <- erase(lns, small)

r <- rasterize(lns3, r, fun='count')
plot(r)
lines(rsp)
plot(lns, col=rainbow(3), lwd=2, add=T)

很好的解决方法,但结果看起来不错。

R plot