【发布时间】:2014-01-16 19:57:51
【问题描述】:
我正在尝试编写一个随机分配例程的脚本。抽样设计将一个区域划分为多个多边形或层。一组但不同数量的样本将随机分配给每个层(最少 2 个样本,但在某些层中多达 7 个)。因此,我有一个地层的 shapefile 及其属性表,其中包含地层名称和每个所需的样本数。
- 地层;样品
- 440; 4
- 441; 2
- 5Z3; 4
- 5Z1; 7
- 560; 2
我发现了一些关于这些类型的抽样设计的很好的文档 (http://casoilresource.lawr.ucdavis.edu/drupal/book/export/html/519),尽管我在根据自己的需要实施例程时遇到了一些问题。在这个链接之后,我一直在使用 rgdal 和 maptools 包。我的工作脚本如下:
# read in strata boundary shapefile
strataboundaries <- readOGR('strataboundaries.shp', layer='strataboundaries')
#sample allocation to strata
allocation <- sapply(slot(strataboundaries, 'polygons'), function(i) spsample(i, n=4, type='random'))
allocation.merge <- do.call('rbind', allocation)
stratumID <- sapply(slot(strataboundaries, 'polygons'), function(i) slot(i, 'ID'))
sample <- sapply(allocation, function(i) nrow(i@coords))
sampleID <- rep(stratumID, sample)
allocation.final <- SpatialPointsDataFrame(allocation.merge,
data=data.frame(poly_id=sampleID))
plot(strataboundaries, col="lightcyan", bborder="black", axes=TRUE, bg="lightsteelblue1")
points(allocation.final, col="red", pch=3, cex=0.8)
#write out shapefile containing sampling locations
allocation.final@proj4string <- strataboundaries@proj4string
writeOGR(allocation.final, ".", "allocation", driver='ESRI Shapefile')
但是,每层的采样强度不是静态的(我有 n=4)。我需要它来反映属性表中的列,该列指示给定层所需的相应样本数。我还想将分层名称分配回已分配的采样位置。
理想情况下,该例程将遍历每个多边形并在其中随机分配 n 个样本(如属性表中所示)并写入 shapefile。任何方向将不胜感激。我对这个程序比较陌生,所以如果我遗漏了一些明显的东西,我深表歉意。
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 66 obs. of 3 variables:
.. ..$ OBJECTID_1: int [1:66] 1 2 3 4 5 6 7 8 9 10 ...
.. ..$ Stratum1 : Factor w/ 66 levels "440","441","442",..: 65 64 63 62 61 60 12 11 7 49 ...
.. ..$ Primary : int [1:66] 2 2 4 5 2 7 2 2 2 2 ...
..@ polygons :List of 66
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..@ Polygons :List of 1
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] -68.3 40.4
.. .. .. .. .. .. ..@ area : num 0.769
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:654, 1:2] -66.3 -66.3 -66.4 -66.4 -66.4 ...
.. .. .. ..@ plotOrder: int 1
.. .. .. ..@ labpt : num [1:2] -68.3 40.4
.. .. .. ..@ ID : chr "0"
.. .. .. ..@ area : num 0.769
【问题讨论】:
标签: r spatial shapefile random-sample