[R-sig-Geo] Union sp polygons ID (rgoes and sp)

Roger Bivand Roger.Bivand at nhh.no
Sat Mar 16 16:34:02 CET 2013


On Sat, 16 Mar 2013, Omphalodes Verna wrote:

> Dear list.
>
> I would like to do union of many polygons in R (rgeos) with ''gUnion'' 
> function (in attached code only 5). Final result should be polygons with 
> unique ID (using attached code it is), but when I plot them (colors by 
> ID value), there are only 5 polygons. Please, give me some idea. Thanks 
> to all, OV

Are you mistaking gUnion() for gUnaryUnion()?

gU = gUnaryUnion(SPLpoly, id=rep("1", length(SPLpoly)))

Roger

>
> RCode:
> library(rgeos)
> library(sp)
> n <- 5
> xy <- cbind(runif(n), runif(n), runif(n), runif(n))
> LLine <- vector("list", n)
> LLines <- vector("list", n)
> for(i in 1:n) LLine[[i]] <- Line(rbind(xy[i, 1:2], xy[i, 3:4]))
> for(i in 1:n) LLines[[i]] <- Lines(LLine[[i]], ID = i)
> SPLines <- SpatialLines(LLines)
> SPLpoly <- gBuffer(SPLines, width = 0.05, byid = TRUE)
> plot(SPLines, col = getSLLinesIDSlots(SPLines))
> plot(SPLpoly, add = TRUE, col = getSLLinesIDSlots(SPLines))
> SPLpolyA <- SPLpoly
> SPLpolyB <- SPLpoly
> gU = gUnion(SPLpolyA, SPLpolyB,  byid = TRUE)
> plot(gU, col = rainbow(length(gU)))
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list