Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Thanks in advance,


share|improve this question
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 5 '13 at 17:37

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

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


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)
share|improve this answer
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
Sure, quite easy. I edited the original answer for that – Curlew Mar 12 '14 at 23:07
I only recently started working with shapefiles and still trying to familiarise myself with this. What exact is should be substituted for youData in"yourData")? After dissolving inner polygons using unionSpatialPolygons(...), I would like to write result as new shapefile – lightonphiri Nov 8 '14 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.. – Curlew Nov 8 '14 at 11:56
@Curlew Thank you, just posted new questions here – lightonphiri Nov 8 '14 at 14:05


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"));
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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