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
30k11 gold badges53 silver badges108 bronze badges
asked May 23, 2013 at 9:31
1
  • 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. Commented Jun 5, 2013 at 17:37

2 Answers 2

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)
answered May 23, 2013 at 9:54
5
  • 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. Commented May 23, 2013 at 13:18
  • 1
    Sure, quite easy. I edited the original answer for that Commented Mar 12, 2014 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 Commented Nov 8, 2014 at 2: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.. Commented Nov 8, 2014 at 11:56
  • 1
    @Curlew Thank you, just posted new questions here gis.stackexchange.com/q/121405/40108 Commented Nov 8, 2014 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"));
answered Sep 1, 2015 at 3:36
1
  • 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 Commented Mar 16, 2018 at 21:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.