Skip to content

Instantly share code, notes, and snippets.

@mhweber
Created November 6, 2024 21:53
Show Gist options
  • Select an option

  • Save mhweber/1913d7dc26097aebfabd638cc63d9faf to your computer and use it in GitHub Desktop.

Select an option

Save mhweber/1913d7dc26097aebfabd638cc63d9faf to your computer and use it in GitHub Desktop.
Filter overlapping sf polygon features in a spatial data frame in R
# poly is any polygon spatial data frame
overlap <- st_intersection(poly)
overlap <- overlap[overlap$n.overlaps > 1, ]
# The result of st_intersection contains a column called n.overlaps
# that represents the number of overlaps for the resulting geometry.
# If n.overlaps > 1 then multiple polygons are overlaping.
overlap <- overlap |>
dplyr::filter(grepl("POLYGON", st_geometry_type(geom))) |>
dplyr::filter(as.numeric(st_area(overlap)) > 100)
# This filtering reduces what is returned to only results that are of
# polygon geometry type and with a significant area
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment