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 [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
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 <[hidden email]> 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 > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
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: > > 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<-as(r,"SpatialPointsDataFrame") Now you can eliminate the NA value: r.spgrd = r.spgrd[!is.na(r.spgrd[[1]]),] In stead of rpoint you can also use sample: selectedPoints = sample(1:length(r.spgrd[[1]]), 1000) r.sampled = r.spgrd[selectedPoints,] cheers, Paul > 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<[hidden email]> 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 >> [hidden email] >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >> >> > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 253 5773 http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770 _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
In reply to this post by caspar-9
Dear Mao,
This is an alternative approach, using only package raster and working with a lonlat grid. In a lonlat grid, cells represent different areas. Therefore, I randomly draw cells with probabilities relative to their area. I simply use the function sample() from base to do the sampling. library(raster) #Create a raster and set its values to 1 r <- raster(ncol=36, nrow=18) r[] <- rep(1,times=ncell(r)) #Draw 10 cell numbers with a probability relative to their area probs <- values(area(r)) probs <- probs/sum(probs) randomCells <- sample(1:ncell(r), 10, prob=probs) #Set the selected cells to NA and plot the result r[randomCells] <- NA plot(r) #Get the coordinates of the cell centres of the selected cells xyFromCell(r, randomCells) Jacob. --- On Tue, 3/8/10, caspar hallmann <[hidden email]> wrote: From: caspar hallmann <[hidden email]> Subject: Re: [R-sig-Geo] how to do randomly sampling in raster layer To: "Mao Jianfeng" <[hidden email]> Cc: [hidden email] Date: Tuesday, 3 August, 2010, 11:15 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 <[hidden email]> 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 > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
In reply to this post by Paul Hiemstra
Dear Mao,
If r is a RasterLayer, you can also do library(dismo) xy <- randomPoints(r, n=100) plot(r) points(xy) Best, Robert On Tue, Aug 3, 2010 at 2:51 AM, Paul Hiemstra <[hidden email]> wrote: > 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: >> >> 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") >> > > I would consider converting it to SpatialPointsDF instead. > > r.spgrd<-as(r,"SpatialPointsDataFrame") > > Now you can eliminate the NA value: > > r.spgrd = r.spgrd[!is.na(r.spgrd[[1]]),] > > In stead of rpoint you can also use sample: > > selectedPoints = sample(1:length(r.spgrd[[1]]), 1000) > r.sampled = r.spgrd[selectedPoints,] > > cheers, > Paul >> >> 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<[hidden email]> >> 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 >>> [hidden email] >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >>> >>> >> >> _______________________________________________ >> R-sig-Geo mailing list >> [hidden email] >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >> > > > -- > Drs. Paul Hiemstra > Department of Physical Geography > Faculty of Geosciences > University of Utrecht > Heidelberglaan 2 > P.O. Box 80.115 > 3508 TC Utrecht > Phone: +3130 253 5773 > http://intamap.geo.uu.nl/~paul > http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770 > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
Free forum by Nabble | Edit this page |