[R-sig-Geo] R spatial matrix to SpatialGridDataFrame

Roger Bivand Roger.Bivand at nhh.no
Tue Jan 26 10:18:47 CET 2010


On Tue, 26 Jan 2010, Edzer Pebesma wrote:

> Something seems to have changed, since 2006 ... The following should work:

Specifically, when the data.frame class was changed in R in mid-2006 
(earlier, all data.frames had row.names as long and large character 
vectors), you switched the data= slot from our home-made AttributeList 
class to data.frame:

http://r-spatial.cvs.sourceforge.net/viewvc/r-spatial/sp/R/Class-SpatialGridDataFrame.R?r1=1.4&r2=1.5

change checked in 19 September. I'd forgotten about that, and it does 
point up the need when looking for tips in the mailing list archives to 
see whether the class definition now matches the one that held when the 
advice was given. Going through:

file.show(system.file("ChangeLog", package="sp"))

one can see the change, but it is seriously hard to check whether older 
advice is still relevant. Some is, some isn't, I'm afraid. The message 
that warns about that change is from 28 Sept. 2006:

http://n2.nabble.com/Please-read-before-upgrading-to-R-2-4-0-td2762640.html#a2762640

We do try to post such heads-up messages when appropriate, but archive 
searches on other keys will not locate them.

Does anyone want to add a timeline of class defintion changes to the wiki, 
so that archive tips can be checked?

Roger

>
> library(sp)
> d <- list(x  =1:10, y = 22:31, z = matrix(1:100, 10, 10))
> image(d)
> gt <- GridTopology(cellcentre.offset = c(d$x[1], d$y[1]),
> cellsize = c(diff(d$x[1:2]), diff(d$y[1:2])), cells.dim = dim(d$z))
> grd.atts <- SpatialGridDataFrame(gt, data.frame(depth =
>   as.vector(d$z[,ncol(d$z):1])))
> image(grd.atts, axes=TRUE)
>
>
> Ian McCallum wrote:
>> Greetings,
>> 
>> I am trying to export a matrix out to an arcinfo asciigrid.
>> Write tables might work.
>> Better is perhaps write.asciigrid or writeGDAL
>> To do that however, i need to convert my matrix into a Spatial Grid.
>> 
>> I came across a thread (from R. Bivand in 2006) that seems to address what 
>> i need to do.
>> However if i plug this into R, it fails. I run R 2.10.1 on win XP.
>> Any ideas on why this is crashing, and how to export a matrix to arcinfo 
>> asciigrid would be great.
>> 
>> Thanks
>> Ian
>> 
>>
>>  > library(sp)
>>  > d <- list(x  =1:10, y = 22:31, z = matrix(1:100, 10, 10))
>>  > image(d)
>>  > gt <- GridTopology(cellcentre.offset = c(d$x[1], d$y[1]),
>> +     cellsize = c(diff(d$x[1:2]), diff(d$y[1:2])), cells.dim = dim(d$z))
>>  > grd.atts <- SpatialGridDataFrame(gt, list(depth =
>> +     as.vector(d$z[,ncol(d$z):1])))
>> Error in validObject(.Object) :
>>    invalid class "SpatialGridDataFrame" object: invalid object for slot 
>> "data" in class "SpatialGridDataFrame": got class "list", should be or 
>> extend class "data.frame"
>>  > image(grd.atts, axes=TRUE)
>> Error in image(grd.atts, axes = TRUE) : object 'grd.atts' not found
>> 
>> 
>> 
>> 
>> 
>> 
>> ___________________________________________
>> Ian McCallum
>> Forestry Program
>> International Institute for Applied Systems Analysis
>> Schlossplatz 1, A-2361 Laxenburg, Austria
>> Phone: +43 2236 807-328
>> Fax: +43 2236 807-599 or 71313
>> E-mail: mccallum at iiasa.ac.at
>> Web: http://www.iiasa.ac.at/Research/FOR/
>> ___________________________________________
>>
>> 	[[alternative HTML version deleted]]
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> 
>
>

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