[R-sig-Geo] SP Spatial List operation - error in R 2.9 not in R 2.8 (download example)
Roger Bivand
Roger.Bivand at nhh.no
Tue May 26 20:41:23 CEST 2009
On Tue, 26 May 2009, rick reeves wrote:
> Hello List:
>
> While writing an R script to display and subsample a raster image,
> I encountered an error in the Spatial_.xxxx classes that occurs in
> R ver 2.9, but not in R ver 2.8.
Thanks for a fully documented case. The key NEWS item in R 2.9.0 here is
probably:
o Ambiguities in class inheritance and method selection resulting
from duplicates in superclasses are now resolved by requiring
(if possible) consistency with all the superclass inheritance. The
rules for method selection have been revised to take advantage of
the improved ordering.
See ?Methods and the reference there related to inheritance.
It is possible that your coding is sensitive to this, in that coercion
(as()) is a method, and before 2.9.0 as(SamplingGrid, "SpatialPoints") did
a silent coercion to SpatialPixels first, which was strictly not needed -
a SpatialGrid does only have two points. I suggest that you remove:
OutSpatialGridSGDF <- as(SamplingGrid, "SpatialGridDataFrame")
which creates an object that is defective (in 2.9.0 anyway), change
SamplingPoints <- as(SamplingGrid,"SpatialPoints")
to
SamplingPoints <- as(as(SamplingGrid, "SpatialPixels"),
"SpatialPoints")
then just go for:
SamplingResultsSPDF <- overlay(InputImage,SamplingPoints)
gridded(SamplingResultsSPDF) <- TRUE
and just use that. Personally, I would avoid the @ operator for accessing
slots, I never use it except deep in functions and very seldom then, it is
much tidier to use accessor functions, which will then call validators to
make sure the object didn't get perverted in some way.
So my diagnosis would be that you were relying on an ambiguity in the
selection of methods by classes, and when the ambiguity was removed by
changes to the methods package, it showed up the vulnerability in your
code.
Does that seem reasonable?
Roger
>
> Note: I installed both versions within the last 5 days, and ran
> the update.packages() command after each installation.
>
> Please download the script (and a small sample image .tif file) at:
>
> http://www.nceas.ucsb.edu/scicomp/SpSamplingGridQuest.zip
>
> (complete script replicated at bottom of this message)
>
> Here is the problem:
>
> I developed this script using R 2.8, and the results were as expected:
> A subsampled image. But when I ran the script under R 2.9.0, the
> subsampled image is 'empty'. Checking the data objects, the first
> statement to exhibit a problem is:
>
> SamplingPoints <- as(SamplingGrid,"SpatialPoints")
>
> For statement: < str(SamplingPoints) >
>
> R version 2.8.1 produces this result:
>
> Formal class 'SpatialPoints' [package "sp"] with 3 slots
> ..@ coords : num [1:24021, 1:2] -1759592 -1758712 -1757832 -1756952
> -1756072 ...
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : NULL
> .. .. ..$ : chr [1:2] "x" "y"
> ..@ bbox : num [1:2, 1:2] -1760032 5929 -1621872 140569
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : chr [1:2] "x" "y"
> .. .. ..$ : chr [1:2] "min" "max"
> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
> .. .. ..@ projargs: chr NA
>
> But R version 2.9 produces this (incorrect) result. Compare the @coords slot
> produced by each version:
>
> Formal class 'SpatialPoints' [package "sp"] with 3 slots
> ..@ coords : num [1:2, 1:2] -1759592 -1622312 6369 140129
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : NULL
> .. .. ..$ : chr [1:2] "x" "y"
> ..@ bbox : num [1:2, 1:2] -1760032 5929 -1621872 140569
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : chr [1:2] "x" "y"
> .. .. ..$ : chr [1:2] "min" "max"
> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
> .. .. ..@ projargs: chr NA
> Warning message:
> In data.row.names(row.names, rowsi, i) :
> some row.names duplicated:
> 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,
> 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,.........
>
> Have I made a coding mistake? Has anyone encountered a similar problem? Is
> there a fix underway?
>
> Thanks for any information -
>
> Rick R
>
>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list