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

查看:115
本文介绍了比较空间多边形并保留或删除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天全站免登陆