rgeos::gBuffer 收缩不失分

标签 r gis sp geos rgeo-shapefile

我需要能够在不丢失点的情况下缩小纬度/经度数据的多边形;更重要的是,我需要在正确的方向上有效地“消除”这些点。通常,gBuffer 工作正常,但不能保证点的数量和它们的相对间距。最终,每个点都有我需要保留的属性,而 gBuffer 和多边形增长/收缩的样条、平滑和其他“不错的效率”不允许我足够自信地保留这些属性一对一的映射。

例子:

library(rgeos)   # gBuffer

dat <- structure(list(x = c(6, 5.98, 5.94, 5.86, 5.75, 5.62, 5.47, 5.31, 5.13, -4.87, -5.04, -5.22, -5.39, -5.55, -5.69, -5.81, -5.9, -5.96, -6, -6, -6, -5.96, -5.9, -5.81, -5.69, -5.55, -5.39, -5.22, -5.04, -3.04, -2.87, -2.69, -2.53, -2.38, -2.25, -2.14, -2.06, -2.02, -2, -2, -1.96, -1.9, -1.81, -1.69, -1.55, -1.39, -1.22, -1.04, -0.87, 1.13, 1.31, 1.47, 1.62, 1.75, 1.86, 1.94, 1.98, 2, 2, 2, 2.04, 2.1, 2.19, 2.31, 2.45, 2.61, 2.78, 2.96, 4.96, 5.13, 5.31, 5.47, 5.62, 5.75, 5.86, 5.94, 5.98, 6), y = c(5, 5.18, 5.35, 5.51, 5.66, 5.78, 5.88, 5.95, 5.99, 5.99, 6, 5.97, 5.92, 5.83, 5.72, 5.59, 5.43, 5.27, 5.09, -4.91, -5.09, -5.27, -5.43, -5.59, -5.72, -5.83, -5.92, -5.97, -6, -6, -5.99, -5.95, -5.88, -5.78, -5.66, -5.51, -5.35, -5.18, -5, -1.91, -1.73, -1.57, -1.41, -1.28, -1.17, -1.08, -1.03, -1, -1.01, -1.01, -1.05, -1.12, -1.22, -1.34, -1.49, -1.65, -1.82, -2, -4.91, -5.09, -5.27, -5.43, -5.59, -5.72, -5.83, -5.92, -5.97, -6, -6, -5.99, -5.95, -5.88, -5.78, -5.66, -5.51, -5.35, -5.18, 5)), row.names = c(NA, -78L), class = "data.frame")

# "shrink-wrap"
sp <- sp::SpatialPolygons(list(sp::Polygons(list(sp::Polygon( as.matrix(dat) )), "dat")))
sp2 <- gBuffer(sp, width = -0.5)
dat2 <- as.data.frame(sp2@polygons[[1]]@Polygons[[1]]@coords)

c(nrow(dat), nrow(dat2))
# [1] 78 97

我们立即看到点数的变化。我认识到,大多数时候,这是 gBuffer 的理想特性,因此 rgeos 可能不是这种转换的最佳工具。

library(ggplot2) # just for vis here
ggplot(dat, aes(x, y)) +
  geom_path() + geom_point() +
  geom_path(data = dat2, color = "red") + geom_point(data = dat2, color = "red")  

enter image description here

这张图片对我想要的整体形状有效果,但是增加了点数,也就是说我不能再依赖与原来点的1对1关系。

一般来说,多边形不是对称的,许多多边形都有像这样的内部裁剪,其中许多将点“拉”到特定方向的方法会产生偏差或方向错误。

我在 gBuffer 中找不到任何选项,在 rgeos 中也找不到其他函数能够保留点的数量和基本空间关系。我不需要“完美”收缩,如果它改变了事情,但它不应该显着偏离。

最佳答案

如果您对当前缩小多边形的方式感到满意,这可能会奏效。它以此为基础获得从旧(大)点到新(小)多边形的 1:1 点映射。

library(rgeos)   # gBuffer
library(sf)
library(tidyverse)
dat <- structure(list(x = c(6, 5.98, 5.94, 5.86, 5.75, 5.62, 5.47, 5.31, 5.13, -4.87, -5.04, -5.22, -5.39, -5.55, -5.69, -5.81, -5.9, -5.96, -6, -6, -6, -5.96, -5.9, -5.81, -5.69, -5.55, -5.39, -5.22, -5.04, -3.04, -2.87, -2.69, -2.53, -2.38, -2.25, -2.14, -2.06, -2.02, -2, -2, -1.96, -1.9, -1.81, -1.69, -1.55, -1.39, -1.22, -1.04, -0.87, 1.13, 1.31, 1.47, 1.62, 1.75, 1.86, 1.94, 1.98, 2, 2, 2, 2.04, 2.1, 2.19, 2.31, 2.45, 2.61, 2.78, 2.96, 4.96, 5.13, 5.31, 5.47, 5.62, 5.75, 5.86, 5.94, 5.98, 6), y = c(5, 5.18, 5.35, 5.51, 5.66, 5.78, 5.88, 5.95, 5.99, 5.99, 6, 5.97, 5.92, 5.83, 5.72, 5.59, 5.43, 5.27, 5.09, -4.91, -5.09, -5.27, -5.43, -5.59, -5.72, -5.83, -5.92, -5.97, -6, -6, -5.99, -5.95, -5.88, -5.78, -5.66, -5.51, -5.35, -5.18, -5, -1.91, -1.73, -1.57, -1.41, -1.28, -1.17, -1.08, -1.03, -1, -1.01, -1.01, -1.05, -1.12, -1.22, -1.34, -1.49, -1.65, -1.82, -2, -4.91, -5.09, -5.27, -5.43, -5.59, -5.72, -5.83, -5.92, -5.97, -6, -6, -5.99, -5.95, -5.88, -5.78, -5.66, -5.51, -5.35, -5.18, 5)), row.names = c(NA, -78L), class = "data.frame")

# "shrink-wrap"
sp <- sp::SpatialPolygons(list(sp::Polygons(list(sp::Polygon( as.matrix(dat) )), "dat")))
sp2 <- gBuffer(sp, width = -0.5)
dat2 <- as.data.frame(sp2@polygons[[1]]@Polygons[[1]]@coords)

## New methods begin here
# change objects to type `sf`
sp_sf <- st_as_sf(sp)
sp2_sf <- st_as_sf(sp2)

dat_sf <- dat %>% st_as_sf(coords = c('x', 'y'))
dat2_sf <- dat2 %>% st_as_sf(coords = c('x', 'y'))

# The plot so far, saved for building on further down
p <- ggplot() + 
  geom_sf(data = sp_sf, color = 'blue', fill = NA) + 
  geom_sf(data = dat_sf, color = 'blue') +
  geom_sf(data = sp2_sf, color = 'red', fill = NA) + 
  geom_sf(data = dat2_sf, color = 'red')




# Using st_nearest_points original points to new small polygon
#  results in (perpendicular?) lines from old points to new small polygon
near_lines <- st_nearest_points(dat_sf, sp2_sf)

#plotted together:
p + geom_sf(data = near_lines, color = 'black')


## Zooming in on a problem area
p + geom_sf(data = near_lines, color = 'black') + 
  coord_sf(xlim = c(-3, 0), ylim = c(-2,0))


# Get only 1:1 points for shrunken polygon
# a small buffer had to be added, as some points were not showing up
# you may need to adjust the buffer, depending on your data & projection
new_points <- st_intersection(st_buffer(near_lines, .001), sp2_sf)

# All together now:
p + geom_sf(data = near_lines, color = 'black') + 
  geom_sf(data = new_points, color = 'green', size = 4) +
  coord_sf(xlim = c(-3, 0), ylim = c(-2,0))

reprex package 创建于 2020-12-20 (v0.3.0)

关于rgeos::gBuffer 收缩不失分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65366110/

相关文章:

r - 从命名列表或矢量创建调色板功能?

gis - 使用 Mapnik 将 osm 数据转换为 png

用 dplyr 中的 data_frame() 替换 data.frame 并用 bind_cols() 替换 cbind

c++ - Rcpp:字符 vector 大小

r - 使用 qqmath 或 dotplot : How to make it look fancy? 绘制来自 lmer(lme4 包)的随机效应

linux - 光栅制图转换库。 'Unprojected' 到任何

r - 使用 R 对 geojson 数据进行子集化

r - spplot图例在 map 中的自定义位置

r - 将矢量图像置于网格中以在 R 中进行克里金法

r - 如何绘制具有不同行数的向量的多个 CDF 图