[R-sig-Geo] Selecting n cells more then minDist apart

pgalpern pgalpern at gmail.com
Wed Jan 25 21:06:43 CET 2012


Here is some code that might work using the utterly fabulous raster 
package!

It is inefficient for a large number of points, and at best this 
approach could be described as "kludgy."  I have not thoroughly tested 
this, but it appears to function as expected.  No doubt others can 
suggest efficiencies, or alternate approaches.  Perhaps sampleRegular() 
will work for you.

library(raster)

sampleMinDist <- function(r, n, minDist) {
    pts <- r
    pts[] <- NA
    pts[sampleRandom(pts, size=1, cells=TRUE, na.rm=FALSE)[1]] <- 1
    for (i in 1:(n-1)) {
      repeat {
         repeat {
            rPt <- sampleRandom(pts, size=1, cells=TRUE, na.rm=FALSE)[1]
            if (length(rPt) > 0) break
            }
         if ((distance(pts)[rPt]) >= minDist) break
       }
       pts[rPt] <- 1
    }
    return(list(pts=pts, ptsXY=xyFromCell(pts, which(pts[]==1))))
}

## Example usage for longlat
rLongLat <- raster(nrow=50, ncol=50)
## Select 10 points a minimum of 5000 m apart
myPts <- sampleMinDist(rLongLat, n=10, minDist=5000)
plot(myPts$pts)
myPts$ptsXY

## Example usage for UTM
rUTM <- raster(nrow=50, ncol=50, xmn=0, xmx=50, ymn=0, ymx=50, 
crs="+proj=utm")
## Select 10 points a minimum of 10 m apart
myPts <- sampleMinDist(rUTM, n=10, minDist=10)

Best,
Paul

-- 
Paul Galpern, PhD Candidate
Natural Resources Institute
70 Dysart Road
University of Manitoba
Winnipeg, Manitoba, Canada R3T 2M6
http://borealscape.ca


On 25/01/2012 5:10 AM, Rainer M Krug wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi
>
> I need to select n cells on a raster map with no cell closer then
> minDist from any other selected cell. The distances can be larger - no
> problem - but they must not be smaller then minDist.
>
> I looked at r.random.cell in GRASS with using distance= as the min
> distance, but if I select randomly n cells from the set generated, I
> get cells with a multiple of minDist apart - not exactly what I am
> looking for.
>
> I could use r.random and iterate until my requirements are met, but
> that strikes me as extremely inefficient.
>
> As I would be calling GRASS from R, I could also easily use R directly.
>
> Is there any way of doing this easily in R?
>
> Cheers,
>
> Rainer
>
> - -- 
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology, UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Stellenbosch University
> South Africa
>
> Tel :       +33 - (0)9 53 10 27 44
> Cell:       +33 - (0)6 85 62 59 98
> Fax :       +33 - (0)9 58 10 27 44
>
> Fax (D):    +49 - (0)3 21 21 25 22 44
>
> email:      Rainer at krugs.de
>
> Skype:      RMkrug
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk8f4ygACgkQoYgNqgF2ego/JACfb/FL5Cyqjm9/Dju0cyi+XRpj
> 1TgAn2oojKKE5xHGNSluhfdxnal27bad
> =L9s4
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Paul Galpern, PhD Candidate
Natural Resources Institute
70 Dysart Road
University of Manitoba
Winnipeg, Manitoba, Canada R3T 2M6
http://borealscape.ca



More information about the R-sig-Geo mailing list