我在寻求一些关于基本地理空间统计的建议。我正在使用来自世界流行的光栅文件,每100平方米就显示巴西的人口。我有另一个有巴西医院坐标的latlong数据集。
我想做以下几点:
我想提供一些可重复的例子,但光栅文件非常大。有一些关于如何做到这一点的说明有两个单独的lat/lon点数列表,但我不知道如何用光栅文件来实现这一点。
有什么想法吗?
发布于 2018-02-09 06:58:53
示例数据
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)
https://stackoverflow.com/questions/48699435
复制相似问题