有没有办法在空间多边形的每个多边形中采样固定数量的点?
Is there a way to sample a fixed number of points in each polygon of a SpatialPolygon?
我有一个大网格的 SpatialPolygon(约 4000 个单元格,在此示例中减少到 2000 个)。
为此,我想为每个单元格随机抽取三个点。
到目前为止,我有两种方法。
第一个有效但计算量很大 - 我什至不得不将过程分成两部分才能使其有效。
set.seed(123)
pts1 <- spsample(gridcut[1,],n=3,"random")
gc()
for (y in 2:1000) {
set.seed(y)
pt <- spsample(gridcut[y,],n=3,"random")
pts1 <- gUnion(pts1,pt)
}
set.seed(123)
pts2 <- spsample(gridcut[1001,],n=3,"random")
gc()
for (y in 1002:2000) {
set.seed(y)
pt <- spsample(gridcut[y,],n=3,"random")
pts2 <- gUnion(pts2,pt)
}
pts <- gUnion(pts1,pts2)
我认为以下代码片段可以使用正确的参数,但我不知道如何操作。
set.seed(123)
pts2 <- spsample(gridcut,n=3*nrow(gridcut),"stratified")
结果是随机分布的点,每个单元格(多边形)至少有一个点(“分层”选项就这么多),但不是我希望的每个单元格三个。
到目前为止,我的具体情况。
添加一个更简单的 reprex - 假设我想在法国的每个地区获得三个点:
fra <- getData('GADM', country='FRA', level=1)
pts <- spsample(fra,n=nrow(fra),"stratified")
set.seed(123)
pts <- spsample(fra[1,],n=3,"random")
for (y in 2:nrow(fra)) {
set.seed(y)
pt <- spsample(fra[y,],n=3,"random")
pts <- gUnion(pts,pt)
}
同样,这可行,但就我而言,它花费的时间太长,并且大大降低了我的机器速度。我正在寻找更优雅的解决方案!
提前致谢!
以下快速而肮脏的解决方案并不完美。它假装你的
生成随机点时坐标是平坦的。真的你应该
在继续之前将 fra
中的多边形投影到平面坐标。
library(maptools)
library(spatstat)
fra <- raster::getData('GADM', country='FRA', level=1)
regions <- lapply(slot(fra, "polygons"), function(x) { SpatialPolygons(list(x)) })
windows <- lapply(regions, as.owin)
p <- lapply(windows, runifpoint, n = 3)
co <- Reduce(rbind, lapply(p, coords))
pts <- SpatialPoints(co, CRS(proj4string(fra)))
#> Warning in proj4string(fra): CRS object has comment, which is lost in output
plot(fra)
plot(pts, add = TRUE, col = "red")
我有一个大网格的 SpatialPolygon(约 4000 个单元格,在此示例中减少到 2000 个)。 为此,我想为每个单元格随机抽取三个点。 到目前为止,我有两种方法。 第一个有效但计算量很大 - 我什至不得不将过程分成两部分才能使其有效。
set.seed(123)
pts1 <- spsample(gridcut[1,],n=3,"random")
gc()
for (y in 2:1000) {
set.seed(y)
pt <- spsample(gridcut[y,],n=3,"random")
pts1 <- gUnion(pts1,pt)
}
set.seed(123)
pts2 <- spsample(gridcut[1001,],n=3,"random")
gc()
for (y in 1002:2000) {
set.seed(y)
pt <- spsample(gridcut[y,],n=3,"random")
pts2 <- gUnion(pts2,pt)
}
pts <- gUnion(pts1,pts2)
我认为以下代码片段可以使用正确的参数,但我不知道如何操作。
set.seed(123)
pts2 <- spsample(gridcut,n=3*nrow(gridcut),"stratified")
结果是随机分布的点,每个单元格(多边形)至少有一个点(“分层”选项就这么多),但不是我希望的每个单元格三个。
到目前为止,我的具体情况。 添加一个更简单的 reprex - 假设我想在法国的每个地区获得三个点:
fra <- getData('GADM', country='FRA', level=1)
pts <- spsample(fra,n=nrow(fra),"stratified")
set.seed(123)
pts <- spsample(fra[1,],n=3,"random")
for (y in 2:nrow(fra)) {
set.seed(y)
pt <- spsample(fra[y,],n=3,"random")
pts <- gUnion(pts,pt)
}
同样,这可行,但就我而言,它花费的时间太长,并且大大降低了我的机器速度。我正在寻找更优雅的解决方案!
提前致谢!
以下快速而肮脏的解决方案并不完美。它假装你的
生成随机点时坐标是平坦的。真的你应该
在继续之前将 fra
中的多边形投影到平面坐标。
library(maptools)
library(spatstat)
fra <- raster::getData('GADM', country='FRA', level=1)
regions <- lapply(slot(fra, "polygons"), function(x) { SpatialPolygons(list(x)) })
windows <- lapply(regions, as.owin)
p <- lapply(windows, runifpoint, n = 3)
co <- Reduce(rbind, lapply(p, coords))
pts <- SpatialPoints(co, CRS(proj4string(fra)))
#> Warning in proj4string(fra): CRS object has comment, which is lost in output
plot(fra)
plot(pts, add = TRUE, col = "red")