如何解决按固定距离缩小 SF 多边形
我正在尝试按固定距离缩小 sf 多边形。
以这个简单的多边形为例:
coords <- list(rbind(c(0,0),c(2,2),c(1,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,1.9),c(1.1,0.9),c(0.1,0.1)))
target <- sf::st_polygon(x = target.coords)
ggplot() +
geom_sf(data = p,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,color = "black") +
geom_sf(data = p1,fill = "red",color = "dark red")
是否有替代 sf::st_buffer()
的设置可以让我实现目标多边形?或者是否有替代功能可以实现这一目标?偏移量需要固定的距离,而不是将多边形缩小相对量(例如 90%)。
解决方法
如果问题仅与那个角落有关,我认为您可以使用参数 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,2),c(1,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,1.9),c(1.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 日创建
另请参阅 here 以获取有关 st_buffer()
的更多示例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。