[R-sig-Geo] different samples size for each polygon and raster question
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Fri Apr 10 10:38:24 CEST 2009
Arien Lam wrote:
> On Wed, April 8, 2009 21:33, Michael Denslow wrote:
>
>> Dear r-sig-geo,
>>
>> I would like to generate random points within each of several polygons. I
>> would like for the sample size of each point sample to be different for
>> each polygon. I would then like to overlay a grid or raster to visualize
>> these point over the entire study area. In other words have the cell value
>> be based on the number of points that fall into each of the grid cells. I
>> have included a reproducible example below to illustrate my problem.
>>
>> library(sp)
>> Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
>> Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
>> Srs1 = Polygons(list(Sr1), "s1")
>> Srs2 = Polygons(list(Sr2), "s2")
>> SpP = SpatialPolygons(list(Srs1, Srs2), 1:2)
>>
>> attr = data.frame(a = 1:2, b = 2:1, row.names = c("s1",
>> "s2"))
>> SrDf = SpatialPolygonsDataFrame(SpP, attr)
>>
>> plot(SpP)
>> ## try to get same number in each polygon
>> # this seems to generate 10 total not 10 for each
>> # the help file says 'use subsetting to vary sampling intensity'
>> # I am not sure how to do this
>> points(spsample(SrDf,n=10,type="random"), pch=3, cex=.5)
>>
>
> I don't know how to do it directly, but the following works (though I
> would have expected that type='stratify' would also do something like
> this)
>
type = 'stratify' follows the ideas of spatially stratified sampling,
(B.D. Ripley, 1981. Spatial Statistics, Wiley) where the strata are
square blocks layed out over the study area. Yes, it would be nice to
have a more generic stratified sampling scheme here.
Getting the number of points in grid cells should be fairly easy: assign
values 1 to the sampled points, overlay them with the grid and use
function sum to get the grid cell total; this might help:
require(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
x = SpatialPixels(SpatialPoints(makegrid(meuse.grid, 20)))
n = nrow(coordinates(x))
pts = spsample(meuse.grid, 100, "random")
x = SpatialPixelsDataFrame(x, data.frame(a = rep(1,n)))
m = as.matrix(table(overlay(x, pts)))
x$b = rep(0,n)
x$b[as.numeric(rownames(m))] = m[,1]
x$b
image(x["b"])
--
Edzer
> ptsin1 <- spsample(SrDf[1,],n=10,type="random")
> ptsin2 <- spsample(SrDf[2,],n=10,type="random")
>
> plot(SrDf)
> points(ptsin1,col='blue')
> points(ptsin2,col='red')
>
> allpts <- SpatialPoints(rbind(coordinates(ptsin1),coordinates(ptsin2)))
> points(allpts,pch=3)
>
>
>> ## 3 try to get unique sample size for each based on attribute data
>> # this does not work
>> points(spsample(SrDf,n=SrDf[[2]],type="random"), pch=3, cex=.5)
>>
>
> You could work the subsetting to a function, e.g. (not optimized):
>
> stratsample <- function(spPolDf, neach=c(10,100)) {
> k = length(spPolDf at polygons)
> allcoors=c(0,0)
> for(i in 1:k) {
> ni = neach[i]
> ptsi <- spsample(spPolDf[i,],n=ni, type="random")
> allcoors = rbind(allcoors,coordinates(ptsi))
> }
> SpatialPoints(allcoors[-1,])
> }
>
> Cheers, Arien (skipping the grid question)
>
>
>> ## generate a grid file?
>> ## use overlay to combine grid with points?
>>
>>
>> Thanks in advance for any help you can provide,
>> Michael
>>
>> Michael Denslow
>>
>> Graduate Student
>> I.W. Carpenter Jr. Herbarium [BOON]
>> Department of Biology
>> Appalachian State University
>> Boone, North Carolina U.S.A.
>>
>> -- AND --
>>
>> Communications Manager
>> Southeast Regional Network of Expertise and Collections
>> sernec.org
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>
>
>
--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de/
http://www.springer.com/978-0-387-78170-9 e.pebesma at wwu.de
More information about the R-sig-Geo
mailing list