Created
August 1, 2020 23:46
-
-
Save paleolimbot/2fe424e1e2f81c2d3ad00d4b3d204b8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
poly_df <- read.csv( | |
url("https://gist.github.com/will-r-chase/ac1c1b3ba96a761a7d8d70af860bf663/raw/97c68537ad35254721ed55b12b696af6e424cd4d/overlapping_polys.csv") | |
) | |
# geos approach | |
poly_geos <- geos::geos_make_polygon( | |
poly_df$x, poly_df$y, | |
feature_id = poly_df$id | |
) | |
# to use sf, you could use st_as_sfc(poly_geos) and use st_intersects() | |
poly_sf <- sf::st_as_sf(poly_geos) | |
# set up loop variables | |
poly_geos_iter <- poly_geos | |
# st_intersects will give you the same object | |
poly_geos_intersections <- geos::geos_intersects_matrix( | |
poly_geos_iter, | |
poly_geos_iter | |
) | |
max_iter <- 1000 | |
for (i in seq_len(max_iter)) { | |
n_intersections <- vapply(poly_geos_intersections, length, integer(1)) | |
if (all(n_intersections == 1)) { | |
break; | |
} | |
poly_geos_iter <- poly_geos_iter[-which.max(n_intersections)] | |
poly_geos_intersections <- geos::geos_intersects_matrix( | |
poly_geos_iter, | |
poly_geos_iter | |
) | |
} | |
plot(poly_geos) | |
plot(poly_geos_iter, border = "red", add = T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2020-08-01 by the reprex package (v0.3.0)