r - Create a convex hull for shapefile -


i need create convex hull 4 polygons shapefile file. don't create using spatialpolygonsdataframe object, want use shapefile object, possible?

my code:

#packages  require(rgdal) require(maptools)  #------------------------------------------------------------------------------- #create 4 polygons  sr <- spatialpolygons(list( polygons(list(polygon(cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409,   180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,   332618, 332413, 332349)))),'1'), polygons(list(polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437,   179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,   331133, 331623, 332152, 332357, 332373)))),'2'), polygons(list(polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875,   179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),   c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,   329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'), polygons(list(polygon(cbind(c(180304, 180403,179632,179420,180304),   c(332791, 333204, 333635, 333058, 332791)))),'4'))) plot(sr)  #convert in polygon spatial  srdf=spatialpolygonsdataframe(sr, data.frame(row.names=c('1','2','3','4'), pids=1:4)) srdf@data  #create shapefile  writeogr(srdf, getwd(), 'poly', 'esri shapefile')  #read shapefile  contorno_line_x <- readshapelines ("poly.shp")    #plot  plot(contorno_line_x)  #try create convex hull  df.data = as.data.frame(contorno_line_x)  ch <- chull(df.data) lines(ch, col="red") ## doesn't work 

here approach:

library(dismo) library(rgdal)  sr <- spatialpolygons(list( polygons(list(polygon(cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 332618, 332413, 332349)))),'1'), polygons(list(polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 331133, 331623, 332152, 332357, 332373)))),'2'), polygons(list(polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110), c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'), polygons(list(polygon(cbind(c(180304, 180403,179632,179420,180304), c(332791, 333204, 333635, 333058, 332791)))),'4'))) srdf=spatialpolygonsdataframe(sr, data.frame(row.names=c('1','2','3','4'), pids=1:4))  shapefile(srdf, 'poly.shp') contorno <- shapefile("poly.shp")   g <- geom(contorno) ids <- unique(g[,1])  hulls <- list() (i in ids) {    d <- g[g[,1] == i, ]    hulls[[i]] <- polygons(convhull(d[, c('x', 'y')])) }  h <- do.call(bind, hulls) plot(h, col='red') plot(contorno, add=true, border='blue', lwd=3) 

Comments