1

Let say i have a SpatialPolygons object with 3 polygons data name groupexc:

library(raster) 
p1 <- matrix(c(2, 3, 4, 5, 6, 5, 4, 3, 2, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p2 <- matrix(c(8, 9, 10, 11, 12, 11, 10, 9, 8, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p3 <- matrix(c(5, 6, 7, 8, 9, 8, 7, 6, 5, 9, 10, 11, 10, 9, 8, 7, 8, 9), ncol=2)
groupexc <- spPolygons(p1, p2, p3)

And a SpatialPolygons object zoneexc that represents a single zone:

zoneexc = spPolygons(matrix(c(2,1,3,4,6,8,10,13,14,14,12,10,8,6,4,2,1,3,7,10,12,14,12,6,4,3,1,1,1,1,1,1), ncol=2))

Is there a way for me to expand the output from groupexc until it reach points in zoneexc?

before

plot(zoneexc, border='red', lwd=3)
plot(groupexc, add=TRUE, border='blue', lwd=2)
text(groupexc, letters[1:3]) 

enter image description here

after:

enter image description here

Any help would be appreciated.

5
  • please provide the data as code like groupexc <- cbind(c(1,2,3), c(1,2,3)) (and see raster::spPolygons to make polygons.) Jun 10, 2016 at 1:06
  • im sorry i have tried but i dont know how to Jun 10, 2016 at 2:10
  • i didnt need any specific condition.myb they can expand fairly until they reach point to zoneexc. i just need it to be expand so that there is no hole in my polygon Jun 10, 2016 at 4:07
  • no sorry was wrong.i just want to expand it until its exterior part Jun 10, 2016 at 4:15
  • i have edit my post Jun 10, 2016 at 6:14

1 Answer 1

3

Here is an approximate solution. This approach might break for large problems, and it depends on having sufficient number of nodes in each polygon. But it may be good enough for your purpose.

# example data 
library(raster) 
p1 <- matrix(c(2, 3, 4, 5, 6, 5, 4, 3, 2, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p2 <- matrix(c(8, 9, 10, 11, 12, 11, 10, 9, 8, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p3 <- matrix(c(5, 6, 7, 8, 9, 8, 7, 6, 5, 9, 10, 11, 10, 9, 8, 7, 8, 9), ncol=2)
groups <- spPolygons(p1, p2, p3, attr=data.frame(name=c('a', 'b', 'c')))
zone <- spPolygons(matrix(c(2,1,3,4,6,8,10,13,14,14,12,10,8,6,4,2,1,3,7,10,12,14,12,6,4,3,1,1,1,1,1,1), ncol=2))

Now create nearest neighbor polygons. For this to work as below, you need dismo version 1.1-1 (or higher)

library(dismo)
# get the coordinates of the polygons    
g <- unique(geom(groups))
v <- voronoi(g[, c('x', 'y')], ext=extent(zone))
# plot(v)
# assign group id to the new polygons
v$group <- g[v$id, 1]

# aggregate (dissolve) polygons by group id
a <- aggregate(v, 'group')
# remove areas outside of the zone
i <- crop(a, zone)

# add another identifier 
i$name <- groups$name[i$group]

plot(i, col=rainbow(3))
text(i, "name", cex=2)
plot(groups, add=TRUE, lwd=2, border='white', lty=2)

white lines show original areas, now expanded to fill up the zone

To see how it works:

points(g[, c('x', 'y')], pch=20, cex=2)
plot(v, add=TRUE)
8
  • whats the -999 for? Jun 13, 2016 at 0:51
  • this is work with the data. but when i am using my data, it says, Error in [[<-.data.frame(*tmp*, name, value = c(1, 1, 1, 1, 1, 1, : replacement has 8400 rows, data has 8395). do u have any idea why its become like this? Jun 13, 2016 at 6:18
  • it happen at here, v$group <- g[,1]. i will try to learn to make my data available via fileshare. i actually didnt know how to share my data Jun 14, 2016 at 0:31
  • and after i do geom, the variable name for a,b,c is gone. how can i keep it? Jun 14, 2016 at 6:48
  • i think it is because like u said it will have problems if data is big. i can run it smoothly by using smaller sample. Jun 15, 2016 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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