【问题标题】:Remove intersecting points from polygon从多边形中删除相交点
【发布时间】:2016-07-31 17:48:41
【问题描述】:

我正在尝试从两个 sp 几何图形中的 gDifference 创建一组新的 SpatialPoints。假设如下:

你有两个SpatialPolygons:

library(rgeos)
library(sp)

#Create SpatialPlygons objects
polygon1 <- readWKT("POLYGON((-190 -50, -200 -10, -110 20, -190 -50))")          
#polygon 1
polygon2 <- readWKT("POLYGON((-180 -20, -140 55, 10 0, -140 -60, -180 -20))") #polygon 2

#Plot both polygons
par(mfrow = c(1,2)) #in separate windows
plot(polygon1, main = "Polygon1") #window 1
plot(polygon2, main = "Polygon2") #window 2

现在,你想得到它们之间的gDifference

polygon_set <- readWKT(paste("POLYGON((-180 -20, -140 55, 10 0, -140 -60, -180 -20),",
                             "(-190 -50, -200 -10, -110 20, -190 -50))"))

par(mfrow = c(1,1)) #now, simultaneously
plot(polygon_set, main = "Polygon1 & Polygon2")


clip <- gDifference(polygon2, polygon1, byid = TRUE, drop_lower_td = T) #clip polygon 2 with polygon 1
plot(clip, col = "red", add = T)

我怎样才能得到一个只有polygon2 的非相交点的sp 几何图形(即下图中的红点)?

提前致谢!

【问题讨论】:

  • 把它们变成单独的线并测试相交然后选择那些不相交的?
  • 我已经尝试过了,但返回了整个 polygon2。我假设它只在整条线被“包含”时才考虑交集
  • determine the points of intersection 之后,您需要确定生成所需多边形的顺序。调整顺序后,ggplot2::fortify 函数将提供一个不错的 data.frame 供您在绘图中使用。

标签: r clip polygons sp


【解决方案1】:

我认为clippolygon2 的坐标比较给了你不相交的点。

library(ggplot2)                         # as @shayaa commented, ggplot2::fortify is useful.

clip_coords <- fortify(clip)[,1:2]          # or, clip@polygons[[1]]@Polygons[[1]]@coords
polygon2_coords <- fortify(polygon2)[,1:2]  # or, polygon2@polygons[[1]]@Polygons[[1]]@coords
duplicated_coords <- merge(clip_coords, polygon2_coords) 
  # duplicated_coords is the non-intersecting points of the polygon2
res <- SpatialPoints(duplicated_coords)

plot(clip)
plot(res, col="red", pch=19, add=T)

[ 附加代码:版本。相交点(假设上面的代码已经运行)]

## an independent method
library(ggplot2); library(dplyr)

clip2 <- gIntersection(polygon2, polygon1, byid = TRUE, drop_lower_td = T)
res2.1 <- fortify(clip2)[,1:2] %>% setdiff(polygon2_coords) %>%   # not_duplicated_coords
  SpatialPoints()


## a method usign a gDifference.sp.coords
res2.2 <- fortify(clip2)[,1:2] %>% merge(clip_coords) %>% 
  distinct() %>% SpatialPoints()    # res2.2 is equal to res2.1 in elements.


plot(polygon_set, main = "Polygon1 & Polygon2")
plot(clip2, col="red", add=T)
plot(res2.1, col="blue", pch=19, add=T)

【讨论】:

  • 我使用了tidy 而不是fortify,结果保持不变。我一直在尝试使用相同的代码来获取gIntersection 而不是gDifference,但生成的sp 对象是剪辑而不是多边形的SpatialPointsmerge(clip_coords, polygons2_coords, all.x = T) 不工作。
  • 好的,我编辑了。这是你希望的答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2020-10-24
  • 2016-11-18
相关资源
最近更新 更多