[R-sig-Geo] Problem with getGridIndex() ?

MacQueen, Don macqueen1 at llnl.gov
Tue Feb 10 00:44:46 CET 2015


I have defined an example SpatialPointsDataFrame, and then a GridTopology
following the example in Applied Spatial Data Analysis with R (2008, pg
48-49). With them, getGridIndex() fails. Suggestions would be much
appreciated.

Here's the (reproducible) example:

------------------------------------
require(sp)

spd <- data.frame(x=c(0,100), y=c(2,95), z=1:2)
coordinates(spd) <- c('x','y')

cdim <- 5
bb <- bbox(spd)
cs <- c(cdim, cdim)               ## cellsize
cc <- bb[,1] + cs/2               ## the lower left cell center
cd <- ceiling(diff(t(bb))/cs)     ## cells to over all the data

gr <- GridTopology(cellcentre.offset=cc,
                   cellsize=cs,
                   cells.dim=cd)

cnum <- getGridIndex( coordinates(spd), gr)



Which results in:

> cnum <- getGridIndex( coordinates(spd), gr)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
      0       5      10      10      15      20
Error in getGridIndex(coordinates(spd), gr) : this.idx out of range

------------------------------------

This looks like a boundary issue, in that the upper and lower boundaries
of the grid cells in the x dimension are 0, 100, and so are the upper and
lower x coordinates.


I looked at getGridIndex, and found that changing
   outside = this.idx >= grid at cells.dim[i] | this.idx < 0
to
   outside = this.idx > grid at cells.dim[i] | this.idx < 0

"fixes" the error in this example.

However, in my original example, which I will share upon request, it gets
past the "out of range" error, but then fails with the "index outside
boundaries" error.



------------------------------------
R was started using
   R --vanilla

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] sp_1.0-17

loaded via a namespace (and not attached):
[1] grid_3.1.2      lattice_0.20-29



-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062



More information about the R-sig-Geo mailing list