Created
February 18, 2020 10:53
-
-
Save coolbutuseless/4ee309cb4a3fcca5fd782126d0e27a78 to your computer and use it in GitHub Desktop.
Find points close to an outline. Help needed!
This file contains hidden or 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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# A bunch of points | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
N <- 10 | |
point_coords <- cbind( | |
rep(seq(0, 1, length.out = N), times = N), | |
rep(seq(0, 1, length.out = N), each = N) | |
) | |
points <- sf::st_multipoint(point_coords) | |
plot(points) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# An outline of a polygon (making a rect to keep thing simple) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
xs <- c(0.1, 0.9, 0.9, 0.1, 0.1) | |
ys <- c(0.1, 0.1, 0.9, 0.9, 0.1) | |
poly <- sf::st_linestring(cbind(xs, ys)) | |
plot(poly) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# simple plot of the polygon outline and the points | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
coll <- st_geometrycollection(list(points, poly)) | |
plot(coll) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Which points are close to the outline? | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
points_sfc <- sf::st_cast(st_sfc(points), 'POINT') | |
close_indices <- sf::st_is_within_distance(points_sfc, poly, dist = 0.1) | |
close_bool <- lengths(close_indices) > 0 | |
close_points_sfc <- points_sfc[close_bool] | |
close_coords <- sf::st_coordinates(close_points_sfc) | |
close_points <- sf::st_multipoint(close_coords) | |
plot(close_points) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# simple plot of the polygon outline and the points | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
coll <- st_geometrycollection(list(close_points, poly)) | |
plot(coll) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hope it makes sense:
Created on 2020-02-18 by the reprex package (v0.3.0)