Skip to content

Instantly share code, notes, and snippets.

@coolbutuseless
Created February 18, 2020 10:53
Show Gist options
  • Save coolbutuseless/4ee309cb4a3fcca5fd782126d0e27a78 to your computer and use it in GitHub Desktop.
Save coolbutuseless/4ee309cb4a3fcca5fd782126d0e27a78 to your computer and use it in GitHub Desktop.
Find points close to an outline. Help needed!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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)
@agila5
Copy link

agila5 commented Feb 18, 2020

Hope it makes sense:

# packages
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

# define some points
N <- 10
point_coords <- cbind(
  rep(seq(0, 1, length.out = N), times = N),
  rep(seq(0, 1, length.out = N), each  = N)
)
multi_points <- st_sfc(st_multipoint(point_coords))
points <- st_cast(multi_points, "POINT")

# define a LINESTRING boundary
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 <- st_linestring(cbind(xs, ys))

# filter only points within a buffer of 0.1
in_points <- points[st_intersects(points, st_buffer(poly, 0.1), sparse = FALSE)]
plot(in_points)
plot(poly, add = TRUE, col = "red")

# define a POLYGONAL boundary
poly <- st_polygon(list(cbind(xs, ys)))
plot(poly, col = "red")

# extract the boundary
poly_boundary <- st_boundary(poly)

# repeat the same procedure
in_points <- points[st_intersects(points, st_buffer(poly_boundary, 0.1), sparse = FALSE)]
plot(in_points)
plot(poly_boundary, add = TRUE, col = "red")

Created on 2020-02-18 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment