为 shapefile 创建一个凸包
Create a convex hull for shapefile
我需要使用 shapefile 文件为 4 个多边形创建一个凸包。我不使用 SpatialPolygonsDataFrame
对象创建,但我想使用 shapefile 对象,这可能吗?
我的代码:
#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 to create a convex hull
df.data = as.data.frame(contorno_line_X)
ch <- chull(df.data)
lines(ch, col="red") ## Doesn't work
chull
returns 形成凸包的点的 指数 (行号),因此绘制变量的值 ch
是你的问题。尝试绘制 lines(df.data[ch,], col="red")
代替。
这里有一个方法:
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()
for (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)
还有 rgeos 函数:rgeos::gConvexHull(sr, byid = TRUE)
plot(rgeos::gConvexHull(sr, byid = TRUE), col = "grey", border = "grey40", lty = 2)
plot(sr, add = T)
并通过使用 sf
包
library(sf)
srdf <- read_sf("POLY.shp")
plot(srdf)
srdf_chull <- st_convex_hull(srdf)
plot(srdf_chull)
我需要使用 shapefile 文件为 4 个多边形创建一个凸包。我不使用 SpatialPolygonsDataFrame
对象创建,但我想使用 shapefile 对象,这可能吗?
我的代码:
#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 to create a convex hull
df.data = as.data.frame(contorno_line_X)
ch <- chull(df.data)
lines(ch, col="red") ## Doesn't work
chull
returns 形成凸包的点的 指数 (行号),因此绘制变量的值 ch
是你的问题。尝试绘制 lines(df.data[ch,], col="red")
代替。
这里有一个方法:
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()
for (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)
还有 rgeos 函数:rgeos::gConvexHull(sr, byid = TRUE)
plot(rgeos::gConvexHull(sr, byid = TRUE), col = "grey", border = "grey40", lty = 2)
plot(sr, add = T)
并通过使用 sf
包
library(sf)
srdf <- read_sf("POLY.shp")
plot(srdf)
srdf_chull <- st_convex_hull(srdf)
plot(srdf_chull)