2

I have a SpatialGridDataFrame in R and want to plot just one of its attributes as as polygons in a GIS. There seems to be no simple way to do this.

The situation is complicated by the fact that the variable I want to export is not numeric so I can't just convert it to a raster and export it. It seems like I have to have the grid file converted to polygons in R and then export the polygons into a GIS format.

I've tried Grid2Polygons package but I don't seem to be able to carry across the attributes.

0

2 Answers 2

6

This is not as complicated as it seems. Factors in R are ordered in the object. If you use levels() to look at the contents of the factor the order corresponds to the factor index (i.e., first class is 1, second 2, ect...). Because of this you can deal with the character component of a factor indirectly and never have to muck with the actual attribute value.

Here is a worked example of what you are wanting.

Add the required packages and an example SpatialPixelsDataFrame

require(sp)
require(raster)
data(meuse.grid)
  sgdf <- SpatialPixelsDataFrame(points=meuse.grid[c("x", "y")], data=meuse.grid)

Here we make the soil attribute into a character factor. We can examine the resulting factor using levels(), nlevels() or str(). The order of the factor dictates the resulting position index.

levels(sgdf@data$soil) <- c("type1","type2","type3")
  levels(sgdf@data$soil)
    str(sgdf@data)

Now we can coerce the SpatialPixelsDataFrame into a raster object and by passing a function to rasterToPolygons convert a single class into a polygon. Since we know that the first factor level corresponds with "type 1" we can index the value in our function.

If you print the resulting object "r" you will see that the attributes are maintained along with the factor index value, which in turn, corresponds to the values in the raster.

Note; the argument "layer=6" in the raster function corresponds to the column in the SpatialPixelsDataFrame's @data slot.

( r <- raster(sgdf, layer=6) )
  r.poly <- rasterToPolygons(r, fun=function(x) {x == 1}, dissolve=TRUE ) 
    class(r.poly) 

Now, let's plot the results (raster with the "type1" polygon overlay).

plot(r)
  plot(r.poly, col="red", add=TRUE)
5

Note that package sp now allows conversion of SpatialGridDataFrame into SpatialPolygonsDataFrame, you just need to use:

as(., "SpatialPolygonsDataFrame") 

and this will contain all the attributes of the initial dataframe, unlike Grid2Polygons.

Full example:

require(sp)
data(meuse.grid)
sgdf <- SpatialPixelsDataFrame(points=meuse.grid[c("x", "y")],     data=meuse.grid)

sgdf_poly <-  as(sgdf, "SpatialPolygonsDataFrame") 
head(as.data.frame(sgdf_poly))

Check that attributes are there :

all.equal(as.data.frame(sgdf)[,-c(8,9)], as.data.frame(sgdf_poly), 
      check.attributes=FALSE)

Results in TRUE!

1
  • what to do is my grid is equidistant: so that the coordinates are not spaced equally?
    – yuliaUU
    Aug 12, 2022 at 21:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.