[R-sig-Geo] how to do randomly sampling in raster layer
caspar hallmann
caspar.hallmann at gmail.com
Tue Aug 3 11:15:55 CEST 2010
Dear Mao,
You can use function rpoint from spatstat, after converting your
raster object into a pixel image.
consider the following:
library(raster)
library(spatstat)
library(maptools)
library(sp)
# An arbitrary raster
r <- raster(system.file("external/test.grd", package="raster"))
# plot it
image(r)
# convert to SpatialGridDataFrame
r.spgrd<-as(r,"SpatialGridDataFrame")
r.spgrd$constant<-ifelse(is.na(r.spgrd[[1]]),NA,1)
# ...this to ensure an equal weight to each non-NA cell
# convert to im
r.im<-as.im(r.spgrd["constant"])
# sample points according to constant
r.points<-rpoint(100,r.im)
# plot the random points
points(r.points)
#..to get the coordinates
as.data.frame(r.points)
Good Luck!
Caspar
On Tue, Aug 3, 2010 at 10:43 AM, Mao Jianfeng <jianfeng.mao at gmail.com> wrote:
> Dear r-sig-geoers,
>
> I want to randomly sample n points from regions of a raster layers,
> the cells denoted as "NA" is not
> included in this sampling process. And, I want to got the longitude
> and latitude of the sampled points.
>
> I checked the manual of raster package, I found several functions is
> relative to my purpose. I tried them all, but I failed.
>
> Can it can be done by raster functionalities. Could you please refer
> me to the right direction?
>
> I expect to hearing from you. Your helps are very valuable for a
> Chinese who can not reach helps nearby.
>
> Best,
>
> Sincerely,
> Mao Jian-Feng
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list