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) 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(sppo...@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@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- drs. H.A. (Arien) Lam (Ph.D. student) Department of Physical Geography Faculty of Geosciences Utrecht University, The Netherlands _______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo