【问题标题】:st_buffer "missing" part of multipolygon using sf in R在 R 中使用 sf 的 st_buffer “缺少”多面体的一部分
【发布时间】:2022-01-12 20:56:07
【问题描述】:

我正在使用 R 中的 sf 包和 arcpullr 包从 ArcGIS REST 服务中提取数据并将其作为 sf 对象使用。我遇到了 MULTIPOLYGON 的问题,其中sf 仅缓冲 MULTIPOLYGON 的一部分(即,它缓冲一个多边形,但只缓冲另一个多边形)。在缓冲找到here 的示例时,我无法复制该问题。

这是一个 MRE(抱歉,如果没有,您必须安装 arcpullr)。

# install.packages("arcpullr")
library(arcpullr)
library(sf) # redundant since loaded with arcpullr, but here for brevity
library(ggplot2)

tax_parcel_url <- paste0(
  "https://mapservices.legis.wisconsin.gov/arcgis/rest/services/", 
  "WLIP/Parcels/FeatureServer/0"
)
parcel <- 
  get_spatial_layer(tax_parcel_url, where = "PARCELID = 'HA-11'") %>%
  st_transform(crs = 3071)
parcel_buffer <- st_buffer(parcel, dist = 10)

# map of parcel
ggplot(data = parcel) + geom_sf() # this is correct

# map of parcel and buffer - buffer "misses" part of multipolygon
ggplot(data = parcel_buffer) + 
  geom_sf(color = "blue") + 
  geom_sf(data = parcel, color = "red", alpha = 0.3) + 
  theme_bw()

【问题讨论】:

  • 嗨!我只想指出,运行 get_spatial_layer 时出现“已达到超时”错误。您可以尝试重新运行代码吗?或者可能以其他方式共享对象?
  • 谢谢@agila。我的运行时间为 0.41 秒。运行它时可能存在服务器问题。再试一次?

标签: r sf arcpullr


【解决方案1】:

您可以通过管道将st_make_valid 输入到parcel,这将使用sf 对象更正几何(即,使几何有效)。这将允许缓冲区在多面体的所有部分上正确绘制。

library(arcpullr)
library(sf)
library(ggplot2)

parcel <-
  arcpullr::get_spatial_layer(tax_parcel_url, where = "PARCELID = 'HA-11'") %>%
  sf::st_transform(crs = 3071) %>%
  sf::st_make_valid()

parcel_buffer <- sf::st_buffer(parcel, dist = 10)

ggplot(data = parcel_buffer) +
  geom_sf(color = "blue") +
  geom_sf(data = parcel,
          color = "red",
          alpha = 0.3) +
  theme_bw()

输出

地图视图

你可以看到mapview中的缓冲区也是清空的。

library(mapview)

mapview::mapview(parcel_buffer) + mapview::mapview(parcel)

【讨论】:

  • 谢谢,安德鲁。完美运行!
猜你喜欢
  • 2020-04-07
  • 1970-01-01
  • 2022-01-05
  • 2017-05-07
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
相关资源
最近更新 更多