gpt4 book ai didi

r - 按固定距离缩小 SF 多边形

转载 作者:行者123 更新时间:2023-12-04 07:29:51 25 4
gpt4 key购买 nike

我正在尝试按固定距离缩小 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")
enter image description here
我想制作一个形状相同的多边形,但边缘都与原始多边形相距 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")
enter image description here
我以为 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")
enter image description here
是否有替代设置 sf::st_buffer()那可以让我实现目标多边形吗?或者是否有替代功能可以实现这一目标?偏移量需要固定距离,而不是将多边形缩小相对量(例如 90%)。

最佳答案

如果问题只是与那个角落有关,我认为您可以更改 st_buffer 的默认行为使用参数 joinStylemitreLimit .例如:

# 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
创建于 2021-06-17 由 reprex package (v2.0.0)
另见 here有关 st_buffer() 的更多示例.

关于r - 按固定距离缩小 SF 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68006296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com