[STATSGRASS] spatstat to grass

Roger Bivand Roger.Bivand at nhh.no
Mon Jan 15 14:30:57 EST 2007


On Sun, 14 Jan 2007, Roger Bivand wrote:

> On Fri, 5 Jan 2007, Jarek Jasiewicz wrote:
> 
> > Hi
> > My new problem: how to simply convert obiect of class im (spatstat) to 
> > SpatialGridDataFrame (sp) to put it to grass.
> > I found im maptools packages as.ppp function to convert spatial sp obiects to 
> > spatstat (ppp) and now I'm looking for function to convert it back. 
> > It seems that I know how to do manualy (little works), but If there is 
> > something tool do to it autoimaticaly, I will be welcome
> 
> Thanks for an interesting question. For SpatialGridDataFrame to im, these 
> work, and can be packaged:
> 
> library(maptools)
> library(spatstat)
> x <- readAsciiGrid(system.file("grids/test.ag", package="maptools")[1])
> image(x)
> xi <- sp:::as.image.SpatialGridDataFrame(x)
> im_x <- im(t(xi$z), xcol=xi$x, yrow=xi$y)
> plot(im_x)
> 
> and I'm working on the reverse wrapper.
> 
> Sorry not to have replied sooner - if you have ideas please share them.

I've attached a file for source()ing, with object x as above and example:

source("as_im.R")
xi2 <- as(x, "im") # like the as.image method, it takes the first column;
                   # drop other columns first if need be
plot(xi2)
x2 <- as(xi2, "SpatialGridDataFrame") # based on the method for masks
image(x2, axes=TRUE)

Please let me know if this does what you need. I'll probably add it to 
maptools for the next release.

Best wishes,

Roger

> 
> Best wishes,
> 
> Roger
> 
> > 
> > Jarek
> > 
> > _______________________________________________
> > statsgrass mailing list
> > statsgrass at grass.itc.it
> > http://grass.itc.it/mailman/listinfo/statsgrass
> > 
> 
> 

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

-------------- next part --------------
if (!isClass("im"))
    setClass("im")

as.SpatialGridDataFrame.im = function(from) {
    require(spatstat)
    offset = c(from$xrange[1] + 0.5 * from$xstep, from$yrange[1] + 
        0.5 * from$ystep)
    cellsize = c(diff(from$xrange)/from$dim[2], diff(from$yrange)/from$dim[1])
    dim = c(from$dim[2], from$dim[1])
    gt = GridTopology(offset, cellsize, dim)
    m = t(from$v[nrow(from$v):1,])
    data = data.frame(v = as.vector(m))
    SpatialGridDataFrame(gt, data)
}
setAs("im", "SpatialGridDataFrame", as.SpatialGridDataFrame.im)


as.im.SpatialGridDataFrame = function(from) {
    require(spatstat)
    xi <- sp:::as.image.SpatialGridDataFrame(from)
    im(t(xi$z), xcol=xi$x, yrow=xi$y)
}
setAs("SpatialGridDataFrame", "im", as.im.SpatialGridDataFrame)




More information about the grass-stats mailing list