I have one dataframe with the lat/long coords of different snakes, like this:
df1 <- data.frame(snake = c("snake_1", "snake_2"),
lat = c(40.68491, 40.8),
long = c(-83.34485, -83.3)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
> df1
Simple feature collection with 2 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -83.34485 ymin: 40.68491 xmax: -83.3 ymax: 40.8
Geodetic CRS: WGS 84
snake geometry
1 snake_1 POINT (-83.34485 40.68491)
2 snake_2 POINT (-83.3 40.8)
I have a second dataframe of sf polygons, like this:
df2 <- data.frame(lat = c(40.68477,
40.68477,
40.9923,
40.9923,
40.68477),
long = c(-83.51883,
-83.11233,
-83.11233,
-83.51883,
-83.51883)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast("POLYGON") %>%
mutate(region = "region_1")
> df2
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -83.51883 ymin: 40.68477 xmax: -83.11233 ymax: 40.9923
Geodetic CRS: WGS 84
geometry region
1 POLYGON ((-83.51883 40.6847... region_1
As you can see, both snake_1 and snake_2 should be within the bounds of the polygon, though snake_1 is close to the minimum latitude. You can see the two points in a ggplot:
ggplot() +
geom_sf(data = df2, fill = "lightblue") +
geom_sf(data = df1, color = "red", size = 3) +
theme_minimal()
My goal is to see whether each point is inside the polygon. When I do so with the following code, the resulting dataframe indicates that snake_1 is not inside the bounding box. How can this be? That point is definitely inside of the bounds of the polygon. Is there a different function I should be using besides st_join?
test <- st_join(df1, df2, join = st_within)
> test
Simple feature collection with 2 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -83.34485 ymin: 40.68491 xmax: -83.3 ymax: 40.8
Geodetic CRS: WGS 84
snake region geometry
1 snake_1 <NA> POINT (-83.34485 40.68491)
2 snake_2 region_1 POINT (-83.3 40.8)

It can get bit tricky when dealing with polygons that are defined with just 4 corners while using an unprojected coordinate system. For example, one might assume that latitude values along the bottom edge of that
df2polygon are constant (i.e. bottom section is parallel to parallels), though it's not always the case:One option is to add more vertices to polygons, though care should be taken when and how to do that. Here we'll just drop CRS for a moment so lat/lon values would be treated as planar coordinates, segmentize, and then restore original CRS.
Here's another attempt to visualize this. Let's scale up our box, move it up north and use conical projection. It's not the best visual, but we can still see how 4-corner polygon differs from the one with more vertices.
Created on 2024-03-16 with reprex v2.1.0