2

I am currently trying to create a polygon shapefile from a list of polygons (study areas for biodiversity research).

Currently these polygons are stored in a list in this format:

$SEW22
     [,1]    [,2]
[1,] 427260.4 5879458
[2,] 427161.4 5879472
[3,] 427175.0 5879571
[4,] 427273.9 5879557
[5,] 427260.4 5879458

$SEW23
     [,1]    [,2]
 [1,] 418011.0 5867216
 [2,] 417912.0 5867230
 [3,] 417925.5 5867329
 [4,] 418024.5 5867315
 [5,] 418011.0 5867216

I tried to simply write them as shpfile with writeOGR but the following error occurs:

> #write polygons to shp
> filenameshp <- paste('Forestplots')
> layername <- paste('Forestplots')
> writeOGR(obj=forest, dsn = filenameshp, 
+          layer=layername, driver="ESRI Shapefile", overwrite_layer =     TRUE)
Error in writeOGR(obj = forest, dsn = filenameshp, layer = layername,  : 
 inherits(obj, "Spatial") is not TRUE

I read this tutorial by Barry Rowlingson to create spatialpolygons and thought I should probably first create a dataframe and did this:

forestm<-do.call(rbind,forest)

but this returned nothing useful as you can imagine, plus it lost the names of the plots.

As I am still new to R I also tried lots of different other approaches which sensefulness I could not fully judge but none returned what I hoped for and so I spare you with these random approaches.....

I am looking forward to your propositions.

Many thanks

P.S. I also tried the following as described in the spatialpolygons{sp} package:

> Polygons(forest, ID)
Error in Polygons(forest, ID) : srl not a list of Polygon objects

1 Answer 1

2

You can follow the approach described in this answer: https://gis.stackexchange.com/questions/18311/instantiating-spatial-polygon-without-using-a-shapefile-in-r.

Here's how to apply the approach to your case. First, I create a list of matrices as in your sample data:

forest <- list(
  "SEW22" = matrix(c(427260.4, 5879458, 427161.4, 5879472, 427175.0, 5879571, 427273.9, 5879557, 427260.4, 5879458),
                   nc = 2, byrow = TRUE),
  "SEW23" = matrix(c(418011.0, 5867216, 417912.0, 5867230, 417925.5, 5867329, 418024.5, 5867315, 418011.0, 5867216),
                   nc = 2, byrow = TRUE)
  )

Now

library(sp)
p <- lapply(forest, Polygon)
ps <- lapply(seq_along(p), function(i) Polygons(list(p[[i]]), ID = names(p)[i]))
sps <- SpatialPolygons(ps)
sps_df <- SpatialPolygonsDataFrame(sps, data.frame(x = rep(NA, length(p)), row.names = names(p)))

In the first step, we iterate through the list of matrices and apply the Polygon function to each matrix to create a list of Polygon objects. In the second step, we iterate through this list to create a Polygons object, setting the ID of each element in this object to the corresponding name in the original list (e.g. "SEW22", "SEW23"). The third step creates a SpatialPolygons object. Finally, we create a SpatialPolygonsDataFrame object. Here I have a dummy dataframe populated with NAs (note that the row names must correspond to the polygon IDs).

Finally, write the data

rgdal::writeOGR(obj = sps_df,
                dsn = "Forestplots",
                layer = "Forestplots",
                driver = "ESRI Shapefile",
                overwrite_layer = TRUE)

This creates a new folder in your working directory:

list.files()
# [1] "Forestplots"
list.files("Forestplots")
# [1] "Forestplots.dbf" "Forestplots.shp" "Forestplots.shx"

Consult the linked answer for more details.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much for this complete answer! Is there a way to create the matrices automatically? I have 150 plots and assuming I would be able to write all coordinates of all vertices of each polygon correctly in code, it would take a lot of time....
Welcome to Stack Overflow! If this answer resolved your question, please mark it as accepted. If you have follow-up questions, please open a new question -- the comments section is not designed for answers.
I just saw your other question. I think you may have misunderstood my answer. The forest <- list(...) part of the answer was just so I could re-create your original list of matrices. Obviously you don't have to do this if you already have the forest object.
Thank you very much Weihuang, actually I was just in the process of learning lapply function for my second question. Works like a treat. Many thanks!
I realised, that using the writeOGR function above, the plot names from the spatialpolygonsdataframe get lost. Is there a way to maintain the plotnames?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.