按固定距离缩小 sf 多边形

Kev*_*vin 4 r polygon r-sf

我正在尝试将 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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想制作一个形状相同的多边形,但其边距原始多边形正好 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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我以为这样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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

是否有其他设置sf::st_buffer()可以让我实现目标多边形?或者是否有替代功能可以实现此目的?偏移需要是固定距离,而不是通过将多边形缩小相对量(例如90%)。

agi*_*ila 7

如果问题仅与该角有关,我认为您可以更改st_buffer使用参数joinStyle和 的默认行为mitreLimit。例如:

# 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")
Run Code Online (Sandbox Code Playgroud)

# 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
Run Code Online (Sandbox Code Playgroud)

reprex 包(v2.0.0)于 2021-06-17 创建

另请参阅此处了解更多有关 的示例st_buffer()