18

My goal is to modify an existing shapefile by merging certain polygons.

After importing the shapefile and using the UnionSpatialPolygons command, I get the polygon outline that I want.

However, this is now a SpatialPolygons object and not a SpatialPolygonsDataFrame, so I'm unable to export it to a shapefile using writeOGR.

How can I get around this problem?

Midavalo
  • 29,696
  • 10
  • 48
  • 104
John
  • 181
  • 1
  • 1
  • 3
  • 3
    If the answer below was useful, you should select it as the correct one by clicking on the tick mark to the left of the answer text. – SlowLearner Jun 05 '13 at 17:37

2 Answers2

21

As the name says, a SpatialPolygonsDataFrame is basically just a SpatialPolygons object with data attached (the attribute table). The data must have at least as many rows as there are features

library(rgdal)
ob <- SpatialPolygons(..)# Your SpatialPolygons Object
spp <-     SpatialPolygonsDataFrame(ob,data=as.data.frame("yourData"),proj4string=CRS("+proj=    aea > +ellps=GRS80 +datum=WGS84"))
writeOGR(spp,"shapes","testShape",driver="ESRI Shapefile",)

----EDIT----

If you want to convert your SpatialPolygonsDataFrame back to a SpatialPolygons object you just need to address the object structure within R

ob <- SpatialPolygons(spp@polygons,proj4string=spp@proj4string)
Curlew
  • 8,152
  • 5
  • 36
  • 72
  • For sure, @Curlew is right. Make sure to specify IDs argument correctly when executing UnionSpatialPolygons. Otherwise you might get trouble specifying data argument when converting back to SpatialPolygonsDataFrame. – fdetsch May 23 '13 at 13:18
  • 1
    Sure, quite easy. I edited the original answer for that – Curlew Mar 12 '14 at 23:07
  • 1
    I only recently started working with shapefiles and still trying to familiarise myself with this. What exact is should be substituted for youData in data=as.data.frame("yourData")? After dissolving inner polygons using unionSpatialPolygons(...), I would like to write result as new shapefile – lightonphiri Nov 08 '14 at 02:54
  • You need to have a data.frame with the same number of rows as you have features in your SpatialPolygons object. Better ask a new question regarding you union issue.. – Curlew Nov 08 '14 at 11:56
  • 1
    @Curlew Thank you, just posted new questions here http://gis.stackexchange.com/q/121405/40108 – lightonphiri Nov 08 '14 at 14:05
10

Problems:

1: the outcome of UnionSpatialPolygons is a spatial polygon

2: converting the result back into a spatial polygon data frame is a real pain

-a. you need a very exact data frame to attach to a spatial polygon

-b. data you used for UnionSpatialPolygons has more rows than the output and is not formatted in the way that is needed.

My (ugly) solution:

### Coerce into spatial polygon data frame with id and row name of spatial polygon

# Make a data frame that meets the requirements above:

df<- data.frame(id = getSpPPolygonsIDSlots(your.spatialpolygon))
row.names(df) <- getSpPPolygonsIDSlots(your.spatialpolygon)

# Make spatial polygon data frame
spdf <- SpatialPolygonsDataFrame(your.spatialpolygon, data =df)

# Then don't forget to make sure the projection is correct
# XXXX is your SRID

proj4string(spdf) <- CRS("+init=epsg:XXXX");
spdf <- spTransform(spdf , CRS("+init=epsg:XXXX"));
K .
  • 303
  • 2
  • 7
  • Error in if (length(Sr@polygons) != nrow(data)) stop(paste("Object length mismatch:\n ", : argument is of length zero In addition: Warning messages: 1: use *apply and slot directly – Mox Mar 16 '18 at 21:15