[R-sig-Geo] convert nominal grid values into polygons
Häring, Tim (LWF)
Tim.Haering at lwf.bayern.de
Tue Feb 23 11:42:39 CET 2010
Hello !
I`m looking for a way to convert a grid with nominal values into polygons. Up to now I didn`t find a way to do this in R. Maybe someone can give me a hint.
Here is what I did:
# I start with a xyz table with the coordinates and a factor variable as z-value.
# The points were regularly spaced because they were converted from a grid to apply a statistical model in R
str(dat)
'data.frame': 337842 obs. of 3 variables:
$ soil_unit: Factor w/ 37 levels "12b","16b","3c",..: 1 1 1 2 2 2 2 2 2 2 ...
$ X : num 4400098 4395378 4400358 4399438 4399638 ...
$ Y : num 5578198 5563698 5578238 5564918 5565358 ...
gridded(dat) = ~X+Y # to get a SpatialPixelsDataFrame
# the following I found on this list to convert
# grid objects to spatial polygon objects
# see: http://www.mail-archive.com/r-sig-geo@stat.math.ethz.ch/msg06549.html
dat <- as(dat, "SpatialPixels")
dat <- as(dat, "SpatialPolygons")
The problem is, that every SpatialPixel is converted to one polygon - in my case 337842 polygons.
Isn`t there a way to convert a SpatialGridDataFrame or a SpatialPixelsDataFrame into a SpatialPolygonsDataFrame. Because I have nominal values the conversion has to consider the DateFrame, I think.
But something like
poly <- SpatialPolygonsDataFrame(dat, ...)
gives an error message.
Any help is very welcome.
Thanks
TIM
-----------------------------------------------------------------------------------
Tim Häring
Bavarian State Institute of Forest Research
Department of Forest Ecology
Hans-Carl-von-Carlowitz-Platz 1
D-85354 Freising
E-Mail: tim.haering at lwf.bayern.de
http://www.lwf.bayern.de
More information about the R-sig-Geo
mailing list