[R-sig-Geo] subsetting a SpatialGridDataFrame image.SpatialGridDataFrame

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat Aug 14 12:05:49 CEST 2010



On 08/13/2010 03:33 PM, Patrick Giraudoux wrote:
>  Thank you both, that is very clear now. SpatialPixelsDataFrame objects
> can be "indexed" as a data frame, but not SpatialGridDataFrame where
> row, col, band must be specified (thanks also not to have written,
> please read the doc, which was all what I deserved..)
> 
> Actually, my question was simplified  with regard to what I intend to
> do: to display pixels selected on their value. With the meuse example
> and after your explanations, this gives:
> 
> data(meuse.grid)
> coordinates(meuse.grid)<-~x+y
> gridded(meuse.grid)<-TRUE
> names(meuse.grid at data)
> 
> idx<-meuse.grid at data$dist<0.1
> image(meuse.grid[idx,],useRasterImage=F)
> 
> ... and it works well.
> 
> Well, looks like if a similar selection was quite hard to achieve
> straightfully  with a SpatialGridDataFrame, as long as each row and col
> number should be identified according to the criterion (eg
> meuse.grid at data$dist<0.1). Then I suppose that the simplest way is to
> coerce SpatialGridDataFrame to SpatialPixelsDataFrame. Agreed ?

Yes.  You can obtain a similar picture from a SpatialGridDataFrame by
the following construct:

fullgrid(meuse.grid) = TRUE
class(meuse.grid) # now SpatialGridDataFrame
meuse.grid$part.a.sel = meuse.grid$part.a
meuse.grid$part.a.sel[meuse.grid$dist >= 0.1] = NA
image(meuse.grid[,,"part.a.sel"],useRasterImage=F)

As it shows, no selection takes place, but masking by setting the
deselected grid cells to NA. SpatialGridDataFrame's always store the
full grid; only after selecting a subset of rows/cols a smaller object
is obtained, but as a new (full) grid, not a set of pixels like
SpatialPixelsDataFrame.

> 
> Patrick
> 
> 
> 
> Le 13/08/2010 14:31, Roger Bivand a écrit :
>> On Fri, 13 Aug 2010, Patrick Giraudoux wrote:
>>
>>>
>>> Hi,
>>>
>>> I am trying to subset a SpatialGridDataFrame object as following:
>>>
>>> idx<-ChinaTempUTM47 at data[,1]>10 # select elements of column1  whose
>>> values are > 10
>>> idx[is.na(idx)]<-FALSE # make a vector of class "logical" (replacing
>>> NA by FALSE)
>>>
>>> and this gives:
>>>
>>> ChinaTempUTM47[idx,]
>>> Error in ChinaTempUTM47[idx, ] : (subscript) logical subscript too long
>>>
>>> However,
>>>
>>>> length(idx)
>>> [1] 13536
>>>> nrow(ChinaTempUTM47 at data)
>>> [1] 13536
>>>
>>
>> This was discussed in:
>>
>> https://stat.ethz.ch/pipermail/r-sig-geo/2010-August/008973.html
>>
>> Use one of the solutions suggested there, and note that the "[" method
>> for SpatialGridDataFrame takes [ row, col, band, ...], like an array
>> but not like a data.frame; see ?"SpatialGridDataFrame-class". You
>> could also coerce to SpatialPixelsDataFrame, which then behaves like a
>> data.frame, not an array.
>>
>> Hope this helps,
>>
>> Roger
>>
>>>
>>> So, I canot sort out what happens.
>>>
>>> Any hint welcome
>>>
>>> Patrick
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
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