[R-sig-Geo] transformin multipart shapefile to singlepart shapefile

Robert Hijmans r.hijmans at gmail.com
Wed Jan 19 01:54:12 CET 2011


> is there a package in R that allows me to split multipart shapefiles in 
> singlepart ones? In my shape file there is a region that consists of 
> more than one parts

Perhaps the below is useful. I extract the UK from the world's countries,
and create a multi-part polygon.
In the second step I select the largest part, and re-insert that into the
countries object, replacing the original. Just a hack, no guarantees,
perhaps there are more formal methods to accomplish some of this  (e.g.
spRbind in maptools)


library(maptools)
data(wrld_simpl)
plot(wrld_simpl)
x <- wrld_simpl at data[,1] == 'UK'
uk <- wrld_simpl[x,]
pols <- uk at polygons[[1]]@Polygons
islands <- list()
for (i in 1:length(pols)) { islands[i] <- Polygons(list(pols[[i]]), i)    }
islands <- SpatialPolygons(islands)
plot(islands, col=terrain.colors(length(pols)))


# step2
mx <- which.max(sapply(pols, function(x)x at area))
newuk <- Polygons(list(pols[[mx]]), uk at polygons[[1]]@ID)
wrld2 <- wrld_simpl
wrld2 at polygons[x] <- newuk
plot(wrld_simpl, xlim=c(-10, 10), ylim=c(50,60), lwd=3)
plot(wrld2, xlim=c(-10, 10), ylim=c(50,60), border='red', add=TRUE)

-- 
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/transformin-multipart-shapefile-to-singlepart-shapefile-tp5935148p5937859.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list