[R-sig-Geo] how to do randomly sampling in raster layer
p.hiemstra at geo.uu.nl
Tue Aug 3 11:51:55 CEST 2010
On 08/03/2010 11:15 AM, caspar hallmann wrote:
> Dear Mao,
> You can use function rpoint from spatstat, after converting your
> raster object into a pixel image.
> consider the following:
> # An arbitrary raster
> r<- raster(system.file("external/test.grd", package="raster"))
> # plot it
> # convert to SpatialGridDataFrame
I would consider converting it to SpatialPointsDF instead.
Now you can eliminate the NA value:
r.spgrd = r.spgrd[!is.na(r.spgrd[]),]
In stead of rpoint you can also use sample:
selectedPoints = sample(1:length(r.spgrd[]), 1000)
r.sampled = r.spgrd[selectedPoints,]
> # ...this to ensure an equal weight to each non-NA cell
> # convert to im
> # sample points according to constant
> # plot the random points
> #..to get the coordinates
> Good Luck!
> 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.
>> Mao Jian-Feng
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
P.O. Box 80.115
3508 TC Utrecht
Phone: +3130 253 5773
More information about the R-sig-Geo