Select 个多边形中的点
Select points in a polygon
我对一些对我来说似乎很简单的事情感到生气。
我想 select 带有 sf 包的多边形内的点。两者具有相同的 CRS:EPSG:27572“NTF(巴黎)/ Lambert zone II。我无法提供可复制的示例,因为数据是机密的...
我的观点:
Classes ‘sf’ and 'data.frame': 5033 obs. of 6 variables:
$ date : chr "18/04/2014" "17/04/2014" "21/04/2014" "22/04/2014" ...
$ id : chr "Auberta" "Auberta" "Auberta" "Auberta" ...
$ sex : chr "F" "F" "F" "F" ...
$ age : chr "0" "0" "0" "0" ...
$ year : chr "2014" "2014" "2014" "2014" ...
$ geometry:sfc_POINT of length 5033; first list element: 'XY' num 472270 1752852
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA
..- attr(*, "names")= chr [1:5] "date" "id" "sex" "age" ...
我的多边形:
Classes ‘sf’ and 'data.frame': 1 obs. of 2 variables:
$ zone2 : chr "30T"
$ geometry:sfc_POLYGON of length 1; first list element: List of 1
..$ : num [1:29, 1:2] 425644 422338 419034 415729 412423 ...
..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
..- attr(*, "names")= chr "zone2"
我尝试了 st_join 函数,但无论我在 join 参数中输入什么,所有点都是子集。我附上截图。
请帮忙!
我在这里展示如何 select 给定多边形内的点,基本上我过滤了初始点 selecting 只包含在盒子上(最终结果在 point_in_pol
):
library(mapSpain)
library(sf)
library(dplyr)
library(ggplot2)
box <- esp_get_prov("Asturias") %>%
st_transform(27572) %>%
st_bbox() %>%
st_as_sfc()
points <- esp_get_capimun(region = c("Asturias", "Galicia")) %>%
st_transform(27572)
p <- ggplot(box) +
geom_sf() +
geom_sf(data = points)
p
# Point on polygon
# Logical stating if the polint intersect the polygon
logi_point_in_pol <- st_intersects(box, points, sparse = FALSE)
point_in_pol <- points[as.vector(logi_point_in_pol), ]
p +
geom_sf(data = point_in_pol, color = "red")
我对一些对我来说似乎很简单的事情感到生气。 我想 select 带有 sf 包的多边形内的点。两者具有相同的 CRS:EPSG:27572“NTF(巴黎)/ Lambert zone II。我无法提供可复制的示例,因为数据是机密的...
我的观点:
Classes ‘sf’ and 'data.frame': 5033 obs. of 6 variables:
$ date : chr "18/04/2014" "17/04/2014" "21/04/2014" "22/04/2014" ...
$ id : chr "Auberta" "Auberta" "Auberta" "Auberta" ...
$ sex : chr "F" "F" "F" "F" ...
$ age : chr "0" "0" "0" "0" ...
$ year : chr "2014" "2014" "2014" "2014" ...
$ geometry:sfc_POINT of length 5033; first list element: 'XY' num 472270 1752852
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA
..- attr(*, "names")= chr [1:5] "date" "id" "sex" "age" ...
我的多边形:
Classes ‘sf’ and 'data.frame': 1 obs. of 2 variables:
$ zone2 : chr "30T"
$ geometry:sfc_POLYGON of length 1; first list element: List of 1
..$ : num [1:29, 1:2] 425644 422338 419034 415729 412423 ...
..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
..- attr(*, "names")= chr "zone2"
我尝试了 st_join 函数,但无论我在 join 参数中输入什么,所有点都是子集。我附上截图。
请帮忙!
我在这里展示如何 select 给定多边形内的点,基本上我过滤了初始点 selecting 只包含在盒子上(最终结果在 point_in_pol
):
library(mapSpain)
library(sf)
library(dplyr)
library(ggplot2)
box <- esp_get_prov("Asturias") %>%
st_transform(27572) %>%
st_bbox() %>%
st_as_sfc()
points <- esp_get_capimun(region = c("Asturias", "Galicia")) %>%
st_transform(27572)
p <- ggplot(box) +
geom_sf() +
geom_sf(data = points)
p
# Point on polygon
# Logical stating if the polint intersect the polygon
logi_point_in_pol <- st_intersects(box, points, sparse = FALSE)
point_in_pol <- points[as.vector(logi_point_in_pol), ]
p +
geom_sf(data = point_in_pol, color = "red")