Créer un objet contenant les points de p qui intersectent le polygone a, à partir du code suivant :

library(sf)
library(tidyverse)
# polygone (a - orange)
a_poly <- st_polygon(list(rbind(c(-1, -1), c(1, -1), c(1, 1), c(-1, -1))))
a <- st_sfc(a_poly)
# multi-points (p - noirs)
p_matrix <- matrix(c(0.5, 1, -1, 0, 0, 1, 0.5, 1), ncol = 2)
p_multi <- st_multipoint(x = p_matrix)
p <- st_cast(st_sfc(p_multi), "POINT")
library(sf)
library(tidyverse)
# polygone (a - orange)
a_poly <- st_polygon(list(rbind(c(-1, -1), c(1, -1), c(1, 1), c(-1, -1))))
a <- st_sfc(a_poly)
# multi-points (p - noirs)
p_matrix <- matrix(c(0.5, 1, -1, 0, 0, 1, 0.5, 1), ncol = 2)
p_multi <- st_multipoint(x = p_matrix)
p <- st_cast(st_sfc(p_multi), "POINT")

Résultat attendu :

# solution st_filter
res <- st_sf(p) %>% 
  st_filter(a)
# solution tidyverse
res <- st_sf(p) %>% 
  filter(st_intersects(., a, sparse = FALSE))
# solution crochets
res <- st_sf(p)[a, , op = st_intersects]

res


MTES-MCT/parcours_r_exercices documentation built on Dec. 3, 2024, 1:29 p.m.