I want to create a sf from bounding box coordinates and compare it to another sf to see if it intersects. When I use the code below it doesn't seem to work:
I can't plot the sf as I get the error
Error: Invalid index: field name 'x_start' not found
andst_intersects gives me
[,1] [,2] [,3] [,4] [1,] FALSE FALSE FALSE FALSE
even though I know they should intersect.
Here is my code:
# Read in UK shapefile
uk.shapefile <- file.path(uk.shapefile_path) %>%
st_read() %>%
st_transform(crs = 27700)
# Define the bounding box
bbox_coords <- c(-7.84000, 49.68000, -6.52750, 51.10625)
# Create a polygon from the bounding box
bbox_polygon <- st_as_sf(st_sfc(st_polygon(list(matrix(c(
bbox_coords[1], bbox_coords[2],
bbox_coords[3], bbox_coords[2],
bbox_coords[3], bbox_coords[4],
bbox_coords[1], bbox_coords[4],
bbox_coords[1], bbox_coords[2]
), ncol = 2, byrow = TRUE))), crs = 27700))
# Plot the bounding box - **returns error**
ggplot() +
geom_sf(data = bbox_polygon, fill = "blue", alpha = 0.3)
# Find intersection - **returns FALSE**
st_intersects(bbox_polygon, uk.shapefile, sparse = FALSE)