如何叠加栅格和点图层并计算摘要统计信息

时间:2018-02-09 05:44:21

标签: r geospatial raster

我正在寻找有关基本地理空间统计数据的一些建议。 我正在使用Worldpop的光栅文件,表示每100平方米的巴西人口。我有另一个latlong数据集,其中包含巴西医院的坐标。

我想做以下事情:

  1. 确定距医院1公里范围内,2-10公里至10公里以上的区域(并可能创建多边形)
  2. 计算上述每个区域的人数
  3. 我想提供一些可重现的示例,但栅格文件非常大。有一些instructions on how to do this有两个单独的lat / lon点列表,但我无法弄清楚如何使用光栅文件执行此操作。

    有什么想法吗?

1 个答案:

答案 0 :(得分:3)

示例数据

library(raster)
bra <- getData('GADM', country="BRA", level=1)
r <- raster(bra, res=1)
values(r) <- 1:ncell(r)
r <- mask(r, bra)
pts <- coordinates(bra)
# plot(r)
# points(pts)

解决方案

b1 <- extract(r, pts, buffer=100000)  # 100 km
b2 <- extract(r, pts, buffer=200000)  # 200 km
pop1 <- sapply(b1, sum)
pop2 <- sapply(b2, function(i)sum(i, na.rm=TRUE)) - pop1

查看区域

spts <- SpatialPoints(pts, proj4string=crs(bra))
buf1 <- buffer(spts, width=100000, dissolve=FALSE)
buf2 <- buffer(spts, width=200000, dissolve=FALSE)

# adding IDs so that they can also be used in "extract"
buf1 <- SpatialPolygonsDataFrame(buf1, data.frame(id1=1:length(buf1)))
buf2 <- SpatialPolygonsDataFrame(buf2, data.frame(id2=1:length(buf2)))

# To combine buf1 and buf2 you could do
# buf <- (buf2-buf1) + buf1
# but in this example there are overlapping buffers, so I do
bb <- list()
for (i in 1:length(buf1)) {
    bb[[i]] <- (buf2[i,]-buf1[i,]) + buf1[i,]
}
buf <- do.call(bind, bb)

plot(r)
plot(buf,  col=c("red", "blue"), add=TRUE)

现在你可以做到

z <- extract(r, buf, fun=sum, na.rm=TRUE)
z <- cbind(data.frame(buf), z)
head(z)

为pop1和pop2

获得与上面相同的结果
head(pop1)
head(pop2)