在 R 中,找到包含一个点的多边形 - Shapefile
In R, find the polygon containing a point - Shapefile
我有一个 shapefile,我想找到包含点列表的多边形。例如,
rio <- readShapeSpatial("setores_rio.shp")
bairrorio.fort<- fortify(rio , region = "neighborhood")
head(bairrorio.fort)
long lat order hole piece group id
1 -43.17769 -22.91814 1 FALSE 1 330455705001.1 330455705001
2 -43.17771 -22.91814 2 FALSE 1 330455705001.1 330455705001
3 -43.17771 -22.91808 3 FALSE 1 330455705001.1 330455705001
4 -43.17793 -22.91811 4 FALSE 1 330455705001.1 330455705001
5 -43.17811 -22.91768 5 FALSE 1 330455705001.1 330455705001
6 -43.17802 -22.91766 6 FALSE 1 330455705001.1 330455705001
假设 p = c(long, lat) 是一个定位为经纬度的点。我想找到包含点 p 的 id(neighborhood)(参见 bairrorio.fort)。
您可以使用来自 sp:
的 point.in.polygon
library(sp)
library(magrittr)
bairrorio.fort %>%
split(.$id) %>%
sapply(function(x) point.in.polygon(p[1], p[2], x$long, x$lat) > 0) %>%
names(.)[.]
我有一个 shapefile,我想找到包含点列表的多边形。例如,
rio <- readShapeSpatial("setores_rio.shp")
bairrorio.fort<- fortify(rio , region = "neighborhood")
head(bairrorio.fort)
long lat order hole piece group id
1 -43.17769 -22.91814 1 FALSE 1 330455705001.1 330455705001
2 -43.17771 -22.91814 2 FALSE 1 330455705001.1 330455705001
3 -43.17771 -22.91808 3 FALSE 1 330455705001.1 330455705001
4 -43.17793 -22.91811 4 FALSE 1 330455705001.1 330455705001
5 -43.17811 -22.91768 5 FALSE 1 330455705001.1 330455705001
6 -43.17802 -22.91766 6 FALSE 1 330455705001.1 330455705001
假设 p = c(long, lat) 是一个定位为经纬度的点。我想找到包含点 p 的 id(neighborhood)(参见 bairrorio.fort)。
您可以使用来自 sp:
的point.in.polygon
library(sp)
library(magrittr)
bairrorio.fort %>%
split(.$id) %>%
sapply(function(x) point.in.polygon(p[1], p[2], x$long, x$lat) > 0) %>%
names(.)[.]