比较空间多边形并保留或删除 R 中的公共边界 [英] Compare spatial polygons and keep or delete common boundaries in R

查看:29
本文介绍了比较空间多边形并保留或删除 R 中的公共边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 R 在

我使用以下方法导入并绘制比利时各省的边界:

省份 <-readRDS("gadm36_BEL_2_sp.rds")情节(省)

这给了我:

我想要的是一个 dataframe 与不在比利时边界之外的省份边界:

我尝试使用 over()intersect() 等,但还没有找到一种方法来做到这一点.我猜可以使用几种方法:

  • 将比利时边界减去省份数据集;
  • 只在省份数据集中工作,只保留公地边界;
  • 还有吗?

如果您有解决方案,谢谢.格雷戈尔

解决方案

我从那个站点下载了 sf 格式的文件(

您在这里所做的是寻找省份与其他省份的空间交叉点.然后你过滤掉只是一个省份重叠的交叉点(n.overlaps == 1).这样,您只能获得一个或多个省份与另一个省份接触的内部边界(n.overlaps > 1),而不是单独的任何省份(这将是外部边界).

这是这个出色答案的更新版本:https://stackoverflow.com/a/47761959/3330437

<小时>

要删除地图和数据集中的圆圈点(3个省的交点),您可以使用:

interiors %>% filter(!st_is(., "POINT"))

I use to plot maps of Belgium available on GADM (select Belgium) using R.

I import and plot the outside boundary of Belgium using :

belgium <-readRDS("gadm36_BEL_0_sp.rds")
plot(belgium)

Which gives me :

I import and plot the boundaries of provinces of Belgium using :

provinces <-readRDS("gadm36_BEL_2_sp.rds")
plot(provinces)

Which gives me :

What i'm trying to have is a dataframe with boundaries of provinces that are NOT outside boundaries of Belgium :

I tried using over(), intersect(), etc but did not founded yet a method to do that. Several approach can be used I guess :

  • substract belgium boundary to the province dataset;
  • Work only in the provinces dataset and keep only commons boundaries;
  • else?

Thanks if you have a solution. Grégoire

解决方案

I downloaded the sf formatted files from that site (https://www.gadm.org/download_country_v3.html), since the sf package is a bit easier to deal with.

library(dplyr)
library(sf)

provinces <- readRDS("gadm36_BEL_2_sf.rds")

interiors <- st_intersection(provinces) %>% 
  filter(n.overlaps > 1)

interiors

# Number of columns truncated for clarity:
#   interiors %>% select(VARNAME_2, geometry, n.overlaps)

Simple feature collection with 30 features and 2 fields
geometry type:  GEOMETRY
dimension:      XY
bbox:           xmin: 2.851679 ymin: 49.8004 xmax: 6.033082 ymax: 51.35568
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
First 10 features:
                                                                                                            VARNAME_2
1                                                                            Amberes|Antuérpia|Antwerp|Anvers|Anversa
2                                                                            Amberes|Antuérpia|Antwerp|Anvers|Anversa
3  Brussel Hoofstadt|Brusselse Hoofdstedelijke Gewest|Brüssel|Bruxelas|Région de Bruxelles-Capitale|Brussels|Bruselas
4                                                                                                   Limbourg|Limburgo
5                   Flandres Oriental|Fiandra Orientale|Flandes Oriental|Flandre orientale|East Flanders|Ost Flandern
6                                                                            Amberes|Antuérpia|Antwerp|Anvers|Anversa
7                                                                            Amberes|Antuérpia|Antwerp|Anvers|Anversa
8                                                                            Amberes|Antuérpia|Antwerp|Anvers|Anversa
9                   Flandres Oriental|Fiandra Orientale|Flandes Oriental|Flandre orientale|East Flanders|Ost Flandern
10                                                Brabant Flamand|Brabante Flamenco|Brabante Flamengo|Flemish Brabant
   n.overlaps                       geometry
1           2 MULTILINESTRING ((5.239571 ...
2           2 MULTILINESTRING ((4.327078 ...
3           2 MULTILINESTRING ((4.403365 ...
4           2 MULTILINESTRING ((5.117446 ...
5           2 MULTILINESTRING ((4.243931 ...
6           3       POINT (4.994605 51.0414)
7           3      POINT (4.243931 51.04332)
8           2 MULTILINESTRING ((4.994605 ...
9           2 MULTILINESTRING ((3.466959 ...
10          2 MULTILINESTRING ((5.025736 ...

To check with a plot:

plot(interiors$geometry)

What you're doing here is looking for the spatial intersection of the provinces with every other province. Then you filter out the intersections where it's just a province overlapping itself (n.overlaps == 1). That way you only get the interior borders where one or more provinces touches another (n.overlaps > 1), but not any province alone (which would be an external border).

This is an updated version of this excellent answer: https://stackoverflow.com/a/47761959/3330437


To remove the circled points (intersections of 3 provinces) in the map and dataset, you can use:

interiors %>% filter(!st_is(., "POINT"))

这篇关于比较空间多边形并保留或删除 R 中的公共边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆