2

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

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)

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

谢谢

4

2 回答 2

3

您可以使用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)

阴谋

于 2015-03-11T07:50:20.033 回答
1

除了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
于 2015-03-12T07:20:57.187 回答