【问题标题】:For a series of points and a set of SpatialPolygons that are irregularly shaped, how do I detect which of the points sit inside which Polygon? [duplicate]对于一系列不规则形状的点和一组 SpatialPolygon,我如何检测哪些点位于哪个多边形内? [复制]
【发布时间】:2015-03-11 05:42:13
【问题描述】:

如果我有一组这样创建的 SpatialPolygon:

library(sp)

Sr2 = Polygon(cbind(c(2,2,1,2,4,4,2),c(2,6,7,7,5,3,2)))
Sr3 = Polygon(cbind(c(4,4,2,5,10,4),c(5,3,2,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs2, Srs3), 1:2)
plot(SpP, col = 2:3, pbg = "white")

看起来像这样:

以及由以下人员创建的点向量:

x <- c(2, 1, 3, 4, 2, 2)
y <- c(2, 4, 2, 2, 3, 4)
points <- cbind(x, y)

如果有的话,我如何检测哪些点位于哪个多边形内?

谢谢

【问题讨论】:

  • OP 正在尝试找出哪个点在哪个多边形内。你会说这与提议的副本@Pascal 有很大不同吗?
  • @Pascal,我正在四处寻找,看看由于重复,这是否确实是一个值得我近距离投票的问题。正如我目前看到的那样,它是微不足道的,因此我进行了调查。
  • @RomanLuštrik 抱歉误解了您的评论。也许不是重复的,是的。我应该指出这个问题,作为可能的帮助。
  • 谢谢你们的回复,帕斯卡我的问题确实是那个问题的重复。我为加倍道歉并感谢您为我指出正确的方向并因此回答我的问题。

标签: r geometry polygons sp


【解决方案1】:

你可以使用over():

over(SpatialPoints(points), SpP)
# 1  2  3  4  5  6 
# 2 NA  2  2  1  1 

plot(SpP, col = 2:3, pbg = "white", axes=TRUE)
plot(SpatialPoints(points), pch=as.character(1:6), add=TRUE)

【讨论】:

    【解决方案2】:

    除了over,你还可以使用rgeos的函数:

    library(rgeos)
    pts1 <- gIntersection(SpatialPoints(points), SpP)
    plot(SpP, col = 2:3, pbg = "white", axes=TRUE)
    points(pts1, pch = 20, col = "blue")
    

    如果您想知道每个点和/或多边形的交点,您可以使用:

    gIntersects(SpatialPoints(points), SpP, byid = TRUE)
    ##        1     2     3     4     5     6
    ## s2   TRUE FALSE FALSE FALSE  TRUE  TRUE
    ## s3/4 TRUE FALSE  TRUE  TRUE FALSE FALSE
    

    如果多边形不映射,您可以使用任何一种:

    apply(gIntersects(SpatialPoints(points), SpP, byid = TRUE), 2, any)
    ##     1     2     3     4     5     6 
    ##  TRUE FALSE  TRUE  TRUE  TRUE  TRUE 
    

    gIntersects(SpatialPoints(points), gUnaryUnion(SpP), byid = TRUE)
    ##      1     2    3    4    5    6
    ## 1 TRUE FALSE TRUE TRUE TRUE TRUE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      相关资源
      最近更新 更多