Reputation: 21
I have shapefiles on two states that border each other, I need to isolate the shared border of the two states, and then calculate the distance to the border for a bunch of points. I can't just define one point on the border and measure the distance to that, I need to have each point scan for the closest point on the border, while the border info I have is in shapefile form.
Any suggestions? I dropped a picture in if it helps visualize the problem.
Upvotes: 0
Views: 560
Reputation: 4243
To answer the first part of your question, you can convert the polygons to lines of the perimeters, then sf::st_intersection
to find the overlap of the two perimeters.
For the second part of your question, consider breaking the overlap line into 100 or 1000 points using st_sample
, then finding the distance between those points and the points within the state polygon using st_nearest_feature
. See here for more details: https://gis.stackexchange.com/a/288575/162034
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
# Get state data
us_states <- st_as_sf(maps:::map("state", plot = FALSE, fill = TRUE))
states2 <- subset(us_states, ID %in% c("new jersey", "pennsylvania"))
# Convert polygons to lines
states2_perim <- st_cast(states2, "MULTILINESTRING")
# Find overlap of lines
states2_overlap <- st_intersection(states2_perim)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
# Plot
plot(st_geometry(states2))
plot(st_geometry(subset(states2_overlap, n.overlaps == 2)), col = 'red', lwd = 2, add = TRUE)
Created on 2022-02-14 by the reprex package (v2.0.0)
Upvotes: 1