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

Robert J. Hijmans r.hijmans at gmail.com
Mon Feb 2 18:31:15 CET 2015


Jean-Luc
I think you can use the disaggregate method (in package sp) for that.
Robert

On Sat, Jan 31, 2015 at 3:17 AM, Jean-Luc DUPOUEY <dupouey at nancy.inra.fr> wrote:
> I faced the same question recently: how to tranform a multipart polygon layer
> into single polygons? Here is a little bit more general function to do that.
> It keeps the initial ID of the polygons, adding a sequence number. The
> initial plot order and projection information are also kept.
>
> But pay attention, holes are automatically converted into islands, because
> holes cannot be single polygons.
>
> I am interested in improving this function. Thanks in advance,
>
> JL Dupouey
>
> #JL Dupouey INRA-Nancy 2015/01/31
>
> #convert a multipart SpatialPolygons object to a single part SpatialPolygons
> object
> #plot order: keeps the initial plot order
> #p4s: keeps the initial p4s, if not NA
> #ID: keeps the initial ID of the multipart polygons, and add a numeric
> increasing index from 1 to n
> # in order of presence of the single polygons wich compose the multipart
> polygon, if there is more then one part
>
> multitoone=function(mpart)
>
> {
> #vector of number of elementary polygons in each multipart polygon (for
> calculation of plot order, and initialization of islands)
> nbpoly=sapply(mpart at polygons,function(x){length(x at Polygons)})
>
> #initialize the list of single Polygons to be built
> islands=vector("list",sum(nbpoly))
> #initialize the vector plot order of single polygons to be built
> plotorder=vector("integer",sum(nbpoly))
>
> #index of current single Polygons
> k=0
>
> #loop on all Polygons of the object
>
> for (i in 1:length(mpart))
> {
>
> #current multiple polygon
> pols=mpart at polygons[[i]]@Polygons
> ID=mpart at polygons[[i]]@ID
>
> #number of polygons to plot before the current multiple one
> prev=sum(nbpoly[mpart at plotOrder<mpart at plotOrder[i]])
>
> #loop on each elementary polygon of the current multiple polygon
>
> for (j in 1:length(pols))
>  { k=k+1
>    IDs=ifelse(length(pols)>1,paste(ID,'-',j,sep=''),ID)
>    islands[[k]] = Polygons(list(pols[[j]]), IDs)
>
> plotorder[k]=rank(mpart at polygons[[i]]@plotOrder,ties.method="first")[j]+prev
> }
> }#end on loop on all Polygons of the object
>
> multitoone = SpatialPolygons(islands,pO=plotorder)
> if (!is.na(proj4string(mpart))) proj4string(multitoone)=proj4string(mpart)
>
> return(multitoone)
> }
>
>
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/transformin-multipart-shapefile-to-singlepart-shapefile-tp5935148p7587716.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list