[R-sig-Geo] class ker (ks) --> conversion --> raster

Roger Bivand Roger.Bivand at nhh.no
Tue Mar 4 11:21:08 CET 2008


On Wed, 27 Feb 2008, Nicolas Bergeron wrote:

> Hi All,
>
> I'm searching for the method to convert a class ker (library(ks)) to the 
> format raster. Can you suggest a possible solution?
>
> This code produces the object of class ker:
>
> # Modules
> library(ks)
>
> # xy
> xy <- as.matrix(loc[loc$ID_animal == 2004021,c("x","y")])
>
> # HPI-SCALE
> HpiScale <- Hpi(x=xy, pilot="samse", pre="scale")
> fhat.HpiScale <- kde(x=xy, H = HpiScale)

So far so good - an example:

library(ks)
library(sp)
data(meuse)
xy <- cbind(meuse$x, meuse$y)
HpiScale <- Hpi(x=xy, pilot="samse", pre="scale")
fhat.HpiScale <- kde(x=xy, H = HpiScale)
plot(fhat.HpiScale)
im <- list(x=fhat.HpiScale$eval.points[[1]],
  y=fhat.HpiScale$eval.points[[2]], z=fhat.HpiScale$estimate)
# make an image-style list
image(im)
contour(im)
SPim <- image2Grid(im)
# convert the image-style list to a SpatialGridDataFrame object,
# thanks to Mike Sumner for code now in sp
image(SPim)

So now we have a SpatialGridDataFrame (note that the values are very 
small, consider re-scaling).

>From there, look at writeAsciiGrid() in maptools to write an Arc ASCII 
grid (r.in.arc in GRASS), writeGDAL() in rgdal and r.in.gdal in GRASS with 
a suitable driver, or writeRAST6() in spgrass6 to write a 
SpatialGridDataFrame to GRASS using temporary files.

Hope this helps,

Roger

>
> # Code for conversion
> # Export {XY=long,lat, Z=fhat} --> GRASS (r.in.xyz)
> XY <- data.frame(x=fhat.HpiScale[[2]][[1]],y=fhat.HpiScale[[2]][[2]])
> Z <- fhat.HpiScale[[3]]
>
> ... suggestions will appreciate or  others methods to write a text file
> of X,Y coordinates and Z value.
>
> after that, in GRASS-GIS r.in.xyz
>
> Thank's
>
>

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