使用R中的sf合并一些多边形边界。

huangapple go评论85阅读模式
英文:

Merging some of the polygon boundaries using sf in R

问题

我正在尝试从这个geojson文件中绘制边界。

我可以使用R中的sf绘制所有边界。然而,我想将一些多边形合并在一起 - 例如,POL_STN_NM = PS RAJ PARK, PS SULTAN PURI, PS MANGOL PURI - 应该绘制为一个区域。

但是,使用st_union未能移除内部边界。

能有人帮助我吗?我是空间映射的新手。

谢谢! 使用R中的sf合并一些多边形边界。

英文:

I am trying to plot the boundaries from this geojson file.

I am able to plot all the boundaries using sf in R. However, I would like to merge some polygons together - e.g., POL_STN_NM = PS RAJ PARK, PS SULTAN PURI, PS MANGOL PURI - should be plotted as having one area.

However, using st_union is not removing the internal boundaries.

Could someone please help me with this? I am new to spatial mappings.

Thank you! 使用R中的sf合并一些多边形边界。

答案1

得分: 2

以下是翻译好的部分:

首先,该联合体的第一个环是一个外部环,您可以提取它并将其转换为多边形和SFC以获得没有空洞的多边形:

library(sf)
#> 链接到 GEOS 3.9.3、GDAL 3.5.2、PROJ 8.2.1;sf_use_s2() 为 TRUE
library(dplyr)

pls <- st_read("https://gist.githubusercontent.com/Vonter/a1f0f9d50a587ce059ddcfb086fc0fac/raw/fcf1b53987b15bb9ee37820e655ae874b8e214fa/Police%2520Station%2520Boundary.geojson")
with_holes <- pls[pls$POL_STN_NM %in% c("PS RAJ PARK", "PS SULTAN PURI", "PS MANGOL PURI"), ] %>%
  st_union()
ext_ring <- st_polygon(with_holes[[1]][1]) |> st_sfc(crs = st_crs(pls))

par(mfrow=c(1,2))
plot(with_holes)
plot(ext_ring)

对于一些额外的上下文:

多边形:具有正面积(二维)的几何图形;点的序列形成一个封闭的、非自交的环;第一个环表示外部环,零个或多个后续环表示此外部环中的空洞
https://r-spatial.github.io/sf/articles/sf1.html#simple-feature-geometry-types

简单要素几何图形是使用以下规则作为 R 本机数据实现的:

  1. 单个点是一个数值向量
  2. 一组点,例如 LINESTRING 或 POLYGON 的环,是一个矩阵,每一行包含一个点
  3. 任何其他集合都是一个列表

https://r-spatial.github.io/sf/articles/sf1.html#sfg-simple-feature-geometry

英文:

First ring of that union is an exterior ring, you can extract it and turn it to polygon & sfc to get a polygon without holes:

library(sf)
#&gt; Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)

pls &lt;- st_read(&quot;https://gist.githubusercontent.com/Vonter/a1f0f9d50a587ce059ddcfb086fc0fac/raw/fcf1b53987b15bb9ee37820e655ae874b8e214fa/Police%2520Station%2520Boundary.geojson&quot;)
with_holes &lt;- pls[pls$POL_STN_NM %in% c(&quot;PS RAJ PARK&quot;, &quot;PS SULTAN PURI&quot;, &quot;PS MANGOL PURI&quot;), ] |&gt;
  st_union()
ext_ring &lt;- st_polygon(with_holes[[1]][1]) |&gt; st_sfc(crs = st_crs(pls))

par(mfrow=c(1,2))
plot(with_holes)
plot(ext_ring)

使用R中的sf合并一些多边形边界。<!-- -->

<sup>Created on 2023-05-13 with reprex v2.0.2</sup>

For some additional context:

> POLYGON : geometry with a positive area (two-dimensional); sequence of points form a closed, non-self intersecting ring; the first ring denotes the exterior ring, zero or more subsequent rings denote holes in this exterior ring
> https://r-spatial.github.io/sf/articles/sf1.html#simple-feature-geometry-types

> Simple feature geometries are implemented as R native data, using the
> following rules
> 1. a single POINT is a numeric vector
> 2. a set of points, e.g. in a LINESTRING or ring of a POLYGON is a matrix, each row containing a point
> 3. any other set is a list
>
> https://r-spatial.github.io/sf/articles/sf1.html#sfg-simple-feature-geometry

huangapple
  • 本文由 发表于 2023年5月13日 18:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定