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?
-
3If 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– SlowLearner2013年06月05日 17:37:51 +00:00Commented Jun 5, 2013 at 17:37
2 Answers 2
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)
-
For sure, @Curlew is right. Make sure to specify
IDs
argument correctly when executingUnionSpatialPolygons
. Otherwise you might get trouble specifyingdata
argument when converting back toSpatialPolygonsDataFrame
.fdetsch– fdetsch2013年05月23日 13:18:15 +00:00Commented May 23, 2013 at 13:18 -
1Sure, quite easy. I edited the original answer for thatCurlew– Curlew2014年03月12日 23:07:36 +00:00Commented Mar 12, 2014 at 23:07
-
1I 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 usingunionSpatialPolygons(...)
, I would like to write result as new shapefilelightonphiri– lightonphiri2014年11月08日 02:54:06 +00:00Commented 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..Curlew– Curlew2014年11月08日 11:56:54 +00:00Commented Nov 8, 2014 at 11:56 -
1@Curlew Thank you, just posted new questions here gis.stackexchange.com/q/121405/40108lightonphiri– lightonphiri2014年11月08日 14:05:37 +00:00Commented Nov 8, 2014 at 14:05
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"));
-
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 directlyMox– Mox2018年03月16日 21:15:37 +00:00Commented Mar 16, 2018 at 21:15