[R-sig-Geo] trouble coercing SpatialPixelsDataFrame into SpatialGridDataFrame?

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Oct 20 14:14:20 CEST 2010


When trying the example below, I can't reproduce your problem. Could you
provide us with the output of sessionInfo(), and a way to reproduce your
problem?

library(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
summary(meuse.grid)
meuse.grid$B = meuse.grid$dist > .2
summary(meuse.grid)
summary(as(meuse.grid, "SpatialGridDataFrame"))
# try a single band grid:
m = SpatialPixelsDataFrame(as(meuse.grid, "SpatialPixels"),
  data.frame(B = meuse.grid[["B"]]))
summary(m)
summary(as(m, "SpatialGridDataFrame"))


On 10/20/2010 12:56 PM, ElenaC wrote:
> 
> Dear all,
> 
> I'm having trouble doing what I thought would be a very simple conversion. I
> have a SpatialPixelsDataFrame variable which has a single data attribute, a
> logical variable:
> 
>> summary(data_pixels)
> Object of class SpatialPixelsDataFrame
> Coordinates:
>            min max
> coords.x1 -180 180
> coords.x2  -60  60
> Is projected: NA
> proj4string : [NA]
> Number of points: 3972
> Grid attributes:
>           cellcentre.offset cellsize cells.dim
> coords.x1            -179.5        1       360
> coords.x2             -59.5        1       120
> Data attributes:
>    Mode   FALSE    TRUE    NA's
> logical    3733     239       0 
> 
> 
> When I try to convert it to SpatialGridDataFrame, for some reason I lose all
> the grid cells which were TRUE:
> 
>> data_grid <- as(data_pixels,'SpatialGridDataFrame')
>> summary(data_grid)
> Object of class SpatialGridDataFrame
> Coordinates:
>            min max
> coords.x1 -180 180
> coords.x2  -60  60
> Is projected: NA
> proj4string : [NA]
> Number of points: 2
> Grid attributes:
>           cellcentre.offset cellsize cells.dim
> coords.x1            -179.5        1       360
> coords.x2             -59.5        1       120
> Data attributes:
>    Mode   FALSE    NA's
> logical    3733   39467 
> 
> 
> Sorry if this is really obvious, but I don't understand why this is
> happening. What I wanted to do was keep both the FALSE and TRUE data and
> fill in all the rest of the grid cells with NAs. Any hint would be greatly
> appreciated!
> 
> Regards,
> Elena

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list