按固定距离缩小 sf 多边形
Shrink sf polygon by fixed distance
我正在尝试将 sf 多边形缩小固定距离。
以这个简单的多边形为例:
coords <- list(rbind(c(0,0), c(2,0), c(2,2), c(1,2), c(1,1), c(0,1), c(0,0)))
p <- sf::st_polygon(x = coords)
ggplot() + geom_sf(data = p, fill = "white", color = "black")
我想制作一个形状相同的多边形,但边与原始多边形的距离正好为 0.1 个单位。这是目标:
target.coords <- list(rbind(c(0.1,0.1), c(1.9,0.1), c(1.9,1.9), c(1.1,1.9), c(1.1,0.9), c(0.1,0.9), c(0.1,0.1)))
target <- sf::st_polygon(x = target.coords)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = target, fill = "green", color = "dark green")
我以为 sf::st_buffer()
会把我带到那里,但外面的角落不太匹配。
p1 <- sf::st_buffer(x = p, dist = -0.1)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = p1, fill = "red", color = "dark red")
是否有 sf::st_buffer()
的替代设置可以让我实现目标多边形?或者是否有替代功能可以实现这一目标?偏移量需要固定距离,而不是将多边形缩小一个相对量(例如 90%)。
如果问题只与那个角有关,我认为您可以使用参数 joinStyle
和 mitreLimit
更改 st_buffer
的默认行为。例如:
# packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(ggplot2)
# data
coords <- list(rbind(c(0,0), c(2,0), c(2,2), c(1,2), c(1,1), c(0,1), c(0,0)))
p <- sf::st_polygon(x = coords)
p1 <- st_buffer(x = p, dist = -0.1, joinStyle = "MITRE", mitreLimit = 2)
# plot
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = p1, fill = "red", color = "dark red")
# Check
target.coords <- list(rbind(c(0.1,0.1), c(1.9,0.1), c(1.9,1.9), c(1.1,1.9), c(1.1,0.9), c(0.1,0.9), c(0.1,0.1)))
(target <- sf::st_polygon(x = target.coords))
#> POLYGON ((0.1 0.1, 1.9 0.1, 1.9 1.9, 1.1 1.9, 1.1 0.9, 0.1 0.9, 0.1 0.1))
st_equals(p1, target)
#> Sparse geometry binary predicate list of length 1, where the predicate was `equals'
#> 1: 1
由 reprex package (v2.0.0)
于 2021 年 6 月 17 日创建
有关 st_buffer()
的更多示例,另请参阅 here。
我正在尝试将 sf 多边形缩小固定距离。
以这个简单的多边形为例:
coords <- list(rbind(c(0,0), c(2,0), c(2,2), c(1,2), c(1,1), c(0,1), c(0,0)))
p <- sf::st_polygon(x = coords)
ggplot() + geom_sf(data = p, fill = "white", color = "black")
我想制作一个形状相同的多边形,但边与原始多边形的距离正好为 0.1 个单位。这是目标:
target.coords <- list(rbind(c(0.1,0.1), c(1.9,0.1), c(1.9,1.9), c(1.1,1.9), c(1.1,0.9), c(0.1,0.9), c(0.1,0.1)))
target <- sf::st_polygon(x = target.coords)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = target, fill = "green", color = "dark green")
我以为 sf::st_buffer()
会把我带到那里,但外面的角落不太匹配。
p1 <- sf::st_buffer(x = p, dist = -0.1)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = p1, fill = "red", color = "dark red")
是否有 sf::st_buffer()
的替代设置可以让我实现目标多边形?或者是否有替代功能可以实现这一目标?偏移量需要固定距离,而不是将多边形缩小一个相对量(例如 90%)。
如果问题只与那个角有关,我认为您可以使用参数 joinStyle
和 mitreLimit
更改 st_buffer
的默认行为。例如:
# packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(ggplot2)
# data
coords <- list(rbind(c(0,0), c(2,0), c(2,2), c(1,2), c(1,1), c(0,1), c(0,0)))
p <- sf::st_polygon(x = coords)
p1 <- st_buffer(x = p, dist = -0.1, joinStyle = "MITRE", mitreLimit = 2)
# plot
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = p1, fill = "red", color = "dark red")
# Check
target.coords <- list(rbind(c(0.1,0.1), c(1.9,0.1), c(1.9,1.9), c(1.1,1.9), c(1.1,0.9), c(0.1,0.9), c(0.1,0.1)))
(target <- sf::st_polygon(x = target.coords))
#> POLYGON ((0.1 0.1, 1.9 0.1, 1.9 1.9, 1.1 1.9, 1.1 0.9, 0.1 0.9, 0.1 0.1))
st_equals(p1, target)
#> Sparse geometry binary predicate list of length 1, where the predicate was `equals'
#> 1: 1
由 reprex package (v2.0.0)
于 2021 年 6 月 17 日创建有关 st_buffer()
的更多示例,另请参阅 here。