Skip to content
Back to formatted view

Raw Message

Message-ID: <44168F24.7070803@geo.uu.nl>
Date: 2006-03-14T09:38:44Z
From: Edzer Pebesma
Subject: SpatialGrid from matrix
In-Reply-To: <44160B40.4050304@utas.edu.au>

Michael Sumner wrote:

>>require(splancs)
>>data(bodmin)
>>x= kernel2d(as.points(bodmin), bodmin$poly, h0=2, nx=100, ny=100)
>>require(sp)
>>g=expand.grid(x$x,x$y,x$z)
>>
>>The last line gives the following error:
>>Error: cannot allocate vector of size 390625 Kb
>>  
>>    
>>
>
>You are expanding your grid for every cell in the matrix, per every grid 
>location - which *really is* a lot of memory.  You really want something 
>like this:
>
>g <- cbind(expand.grid(x = x$x, y = x$y), as.vector(x$z))
>coordinates(g) <- c("x", "y")
>gridded(g) <- TRUE
>image(g)
>contour(x, add = T)  ## just to be sure
>
>I'm pretty sure the ordering is right for this case, but be sure to check.
>
>Cheers, Mike.
>
>_______________________________________________
>R-sig-Geo mailing list
>R-sig-Geo at stat.math.ethz.ch
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>  
>
Thanks, Mike, that's an excellent solution.

In this way g is of

 > class(g)
[1] "SpatialPixelsDataFrame"
attr(,"package")
[1] "sp"

meaning that it stores x and y coordinates. To save this
space, you could store it as SpatialGridDataFrame:
 > object.size(g)
[1] 283528
 > fullgrid(g)=T
 > object.size(g)
[1] 83476
 > class(g)
[1] "SpatialGridDataFrame"
attr(,"package")
[1] "sp"

A direct way to create a SpatialGridDataFrame without
using expand.grid() would use GridTopology() and
SpatialGridDataFrame(). You'd have to reorder x$z
for that, too.
--
Edzer