Giulia Ciminelli
Giulia Ciminelli

Reputation: 49

how to check if points are in a specific polygon, iterated for many polygons?

I have a data frame df_poly with a list of polygons coordinate:

     x   y        point     Group_Name
1   479 165     top_left        G03
2   161 713  bottom_left        G03
3   795 713 bottom_right        G03
4   785 291    top_right        G03
5   373 477     top_left        G04
6   340 717  bottom_left        G04
7  1075 653 bottom_right        G04
8   823 274    top_right        G04
9   508 300     top_left        G06
10  208 712  bottom_left        G06
11  862 709 bottom_right        G06
12  874 456    top_right        G06

I have a data frame df_point with a list of points:

         Group_Name xcentre ycentre
1             G03   278.0   139.5
2             G03   216.0   139.5
4             G03   685.5    24.5
5             G03   680.5    25.5
6             G03   217.5   131.0
7             G03   670.5    22.0
8             G03   330.0   264.5
9             G03   552.5    72.5
10            G03   329.5   267.5
13            G03   391.5   682.0
14            G03   587.0   644.5
15            G04   483.5   562.0
16            G04   456.0   340.5
17            G06   619.0   651.0
18            G06   456.0   340.5
19            G06   455.5   338.5
20            G06  328.0   271.0

I would like to check, for each group (Group_Name) if the points in that group (i.e. G03) are in the polygon for that specific group.

I tried with pip2d and group_by:

df_point%>%group_by(Group_Name)%>%summarise(inside=pip2d(cbind(df_polyI$x,df_poly$y),

cbind(df_point$xcentre,df_point$ycentre)))

but it returns a data frame with way too many rows...

Upvotes: 0

Views: 34

Answers (1)

Wimpel
Wimpel

Reputation: 27792

library(sf)
library(sfheaders)
library(ggplot2)

mypoly <- sfheaders::sf_polygon(mydata, x = "x", y = "y", polygon_id = "Group_Name")
mypoint <- sf::st_as_sf(mydata_point, coords = c("xcentre", "ycentre"))

final <- st_join(mypoint, mypoly, st_within)

now you can easily check if group_Name.x matches group_Name.y

# Simple feature collection with 25 features and 2 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 216 ymin: 22 xmax: 685.5 ymax: 682
# CRS:           NA
#     Group_Name.x Group_Name.y            geometry
# 1             G03         <NA>   POINT (278 139.5)
# 2             G03         <NA>   POINT (216 139.5)
# 4             G03         <NA>  POINT (685.5 24.5)
# 5             G03         <NA>  POINT (680.5 25.5)
# 6             G03         <NA>   POINT (217.5 131)
# 7             G03         <NA>    POINT (670.5 22)
# 8             G03         <NA>   POINT (330 264.5)
# 9             G03         <NA>  POINT (552.5 72.5)
# 10            G03         <NA> POINT (329.5 267.5)
# 13            G03          G03   POINT (391.5 682)
# 13.1          G03          G04   POINT (391.5 682)
# 13.2          G03          G06   POINT (391.5 682)
# 14            G03          G03   POINT (587 644.5)
# 14.1          G03          G04   POINT (587 644.5)
# 14.2          G03          G06   POINT (587 644.5)
# 15            G04          G03   POINT (483.5 562)
# 15.1          G04          G04   POINT (483.5 562)
# 15.2          G04          G06   POINT (483.5 562)
# 16            G04          G03   POINT (456 340.5)
# 17            G06          G03     POINT (619 651)
# 17.1          G06          G04     POINT (619 651)
# 17.2          G06          G06     POINT (619 651)
# 18            G06          G03   POINT (456 340.5)
# 19            G06          G03 POINT (455.5 338.5)
# 20            G06         <NA>     POINT (328 271)

visual confirmation

ggplot() + geom_sf(data = mypoly, alpha = 0.25, aes(fill = Group_Name)) + 
geom_sf(data = mypoint, aes(color = Group_Name))

enter image description here

sample data

mydata_poly <- read.table(text = "     x   y        point     Group_Name
1   479 165     top_left        G03
2   161 713  bottom_left        G03
3   795 713 bottom_right        G03
4   785 291    top_right        G03
5   373 477     top_left        G04
6   340 717  bottom_left        G04
7  1075 653 bottom_right        G04
8   823 274    top_right        G04
9   508 300     top_left        G06
10  208 712  bottom_left        G06
11  862 709 bottom_right        G06
12  874 456    top_right        G06", header = TRUE)

mydata_point <-  read.table(text = "Group_Name xcentre ycentre
1             G03   278.0   139.5
2             G03   216.0   139.5
4             G03   685.5    24.5
5             G03   680.5    25.5
6             G03   217.5   131.0
7             G03   670.5    22.0
8             G03   330.0   264.5
9             G03   552.5    72.5
10            G03   329.5   267.5
13            G03   391.5   682.0
14            G03   587.0   644.5
15            G04   483.5   562.0
16            G04   456.0   340.5
17            G06   619.0   651.0
18            G06   456.0   340.5
19            G06   455.5   338.5
20            G06  328.0   271.0", header = TRUE)

Upvotes: 3

Related Questions