【问题标题】:Shrink sf polygon by fixed distance将 sf 多边形缩小固定距离
【发布时间】:2021-06-16 16:03:33
【问题描述】:

我正在尝试将 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%)。

【问题讨论】:

  • 可以进行仿射变换,同时保留确切的形状(= 您明确排除的 90% 的收缩)。但是,恕我直言,深红色内部多边形的行为符合预期:外角周围的圆形曲线描述了距原始点单位距离 1/10 的所有点。你的绿色多边形的内角实际上比那个远 1 sqrt(2)...
  • @JindraLacko 我也注意到关于角落的问题,虽然我很好奇为什么凸角是方形的(因此与白色角落的距离为 sqrt(2))但是凹一个不是
  • 这不是特定于 R 的,但之前的一篇文章一般介绍了这种类型的算法,可能会对 stackoverflow.com/q/1109536/5325862 有所帮助
  • @camille 凸点在负缓冲区中很容易;有两个边可以测量距离,所以没问题。它们会在正缓冲情况下带来麻烦/凹点很容易出现问题。所以选择你想要的东西,但你不能两全其美:)

标签: r polygon sf


【解决方案1】:

如果问题只是与那个角落有关,我认为您可以使用参数joinStylemitreLimit 更改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-06-17 创建

有关st_buffer() 的更多示例,另请参阅here

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2010-10-28
    • 2012-06-14
    • 1970-01-01
    • 2018-07-04
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多