简体   繁体   English

使用 R 中的简单要素库识别多边形的公共边界

[英]Identifying common borders of polygons using the Simple Features library from R

I'm trying to identify the common borders of two different polygons using the sf_intersection() function from the sf package.我正在尝试使用sf包中的sf_intersection()函数来识别两个不同多边形的公共边界。

I tried this simple approach in my data, which comes from a shapefile, but it's not working exactly as I expected.我在来自 shapefile 的数据中尝试了这种简单的方法,但它的工作方式并不完全符合我的预期。

My data is the shapefile names "zones" from this repository , and this is what I've tried:我的数据是来自这个存储库的 shapefile 名称“区域”,这是我尝试过的:

library(sf)
library(ggplot2)

zones <- st_read('./Data/zones.shp')
zones$id <- seq(nrow(zones))
borders <- st_intersection(zones, zones)
borders <- borders[borders$id != borders$id.1, ]

ggplot() +
  geom_sf(data = zones, color='red', fill=NA) +
  geom_sf(data = borders, color = 'navy')

The final plot yields this result:最终图产生了这个结果:

在此处输入图片说明

If you look carefully, you'll note that there are some portions of the inner line of the polygons that are not part of the line in borders (they are red and not blue).如果仔细观察,您会注意到多边形内线的某些部分不属于borders线的一部分(它们是红色而不是蓝色)。 I don't know why this is happening.我不知道为什么会这样。 Any hint or advice will be much appreciated.任何提示或建议将不胜感激。 Thanks!谢谢!

It is local imprecision in the borders.这是边界的局部不精确。 With most vector data formats, shared POLYGON borders are duplicated in each of the neighbours.对于大多数矢量数据格式,共享的 POLYGON 边界在每个邻居中都是重复的。 It doesn't take very much for slight differences in the coordinates to make the intersection of the two borders incomplete.坐标的微小差异不需要太多就可以使两个边界的交集不完整。

That's not a solution, I'm afraid.恐怕这不是解决办法。

This section shows the kind of problem.本节显示了问题的类型。 The view window is:视图窗口是:

> par('usr')
[1]  764968.2  765650.8 2945266.2 2945890.9

示例差距

That sliver is only about 3 metres wide.那条条子只有大约3米宽。

EDIT: Just to add my attempt at a solution using st_snap .编辑:只是为了使用st_snap添加我对解决方案的尝试。 This seems to do the trick in some places but not consistently.这似乎在某些地方行得通,但并非始终如一。 It doesn't feel like it is working as intended.感觉它没有按预期工作。 Also, just to note the projection uses US feet as units, which confused me.另外,请注意投影使用美国英尺作为单位,这让我感到困惑。

z1 <- st_geometry(zones[1,])
z2 <- st_geometry(zones[2,])

z1 <- st_cast(z1, 'LINESTRING')
z2 <- st_cast(z2, 'LINESTRING')

z1s <- st_snap(z1, z2, 1000)

border <- st_intersection(z1s, z2)

That snap tolerance is way over the top - the gaps between the zone seem to be < 10 feet - but even with this huge tolerance the actual border has missing sections.卡扣容差远远超过顶部 - 区域之间的间隙似乎小于 10 英尺 - 但即使有如此巨大的容差,实际边界仍有缺失的部分。 More oddly, the result has a totally unexpected extension that heads off >6500 feet from the actual intersection.更奇怪的是,结果有一个完全出乎意料的延伸,距离实际交叉路口 >6500 英尺。

在此处输入图片说明

@David_O identifies the issue - the POLYGON borders don't actually touch throughout the shared boundary so st_intersection won't identify them as such. @David_O 确定了问题 - POLYGON 边界实际上并没有触及整个共享边界,因此st_intersection不会这样识别它们。

One workaround may be to st_buffer your zones object before intersecting, although this is admittedly a crude workaround:一种解决方法可能是在相交之前st_buffer您的区域对象,尽管这无疑是一种粗略的解决方法:

borders <- st_intersection(st_buffer(zones, 5), st_buffer(zones, 5))

borders <- borders[borders$id != borders$id.1, ]

ggplot() +
  geom_sf(data = zones, color='red', fill="transparent") +
  geom_sf(data = borders, color = 'navy')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM