I want to remove the points that are close to a boarder when building a grid, i am using the sf library from R.


shp <- getData('GADM', country = 'aut', level = 0) %>% st_as_sf()

grid <- st_make_grid(shp, n=c(25, 25), 
                     what = "centers", 
                     square=TRUE) %>% st_intersection(shp)  

ggplot() +
  geom_sf(data = shp) +
  geom_sf(data = grid, col ="red")

Example of the problem:

enter image description here

1 Answer 1


Compute distance from grid points to shape border. Border is converted to linear geometry because distance from point to polygon for points inside polygon=0.

> d = st_distance(grid, st_cast(shp,"MULTILINESTRING"))

Select grid points further than 10km from border:

> gridin2 = grid[d[,1]>units::set_units(10,km)]


> plot(shp$geometry)
> plot(gridin2,add=TRUE)

You could also do it by a negative buffer and an intersection, but st_buffer would need a coordinate transformation first to work in real units rather than degrees. The st_distance approach works in proper distances.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.