[GRASS-stats] Extracting x,y-coordinates from a SGDF

Roger Bivand Roger.Bivand at nhh.no
Tue Jul 14 03:41:56 EDT 2009


On Tue, 14 Jul 2009, Johannes Signer wrote:

> Dear List-Members,
>
> I encountered a problem with with importing data from GRASS to R. I found a
> work around to my problem, but maybe someone is aware of a more elegant
> solution.
>
> I imported a raster from GRASS to R with the readRAST6() method. My
> ultimate goal is to get a a new data.frame which looks like this:
>
> x-coordinate | y-coordinate | z-value
>

For example:

library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
fullgrid(meuse.grid) <- TRUE
class(meuse.grid)
df <- as(as(meuse.grid, "SpatialPointsDataFrame"), "data.frame")
str(df)

Because this example coerces to SpatialGridDataFrame, on the way back 
raster cells with no attribute values are dropped.

str(coordinates(meuse.grid))

retrieves all the cells, whether missing attribute values or not. So 
assuming you want "soil":

df1 <- data.frame(coordinates(meuse.grid), soil=meuse.grid$soil)
str(df1)

will do that.

Hope this helps,

Roger

>
>
> I managed to extract the z-value from the data slot of the SGDF, but I did
> not manage to extract the x and y values.
>
> My current work around is to write a function recalculating the grid points
> from the information provided within the SGDF. However if anyone is aware of
> a easier method of how to extract x and y values from a SGDF I would be very
> interested.
>
> Many thanks in advance
>
> Johannes
>

-- 
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 grass-stats mailing list