I have a set of coordinates that I want to convert into a polygon (to calculate its area) using sf
. However, the polygon generated by the code most people suggest for this looks like a scribble between the points, not an outline.
Here are the real data:
library(sf)
library(tidyverse)
lon <- c(-71.616667, -71.5, -71.716667, -71.9, -72.4, -72.65, -72.383333,
-72.4, -72.383333, -72.65, -72.6, -72.7, -73.033333, -73.2, -73.933333,
-73.9, -74, -74.1, -74.183333, -74.183333, -74.45, -74.566667,
-74.533333, -73.55, -73.733333, -73.833333, -73.683333, -73.283333,
-72.883333, -73, -69.966667, -69.8, -73.183333, -73.85, -73.533333,
-73.983333, -74.1, -74.3, -73.916667, -73.983333, -73.933333,
-74.183333, -74.35, -74.366667, -74.516667, -74.583333, -73.283333,
-73.15, -72.8, -72.583333)
lat <- c(39.916667, 39.916667, 39.85, 39.95, 39.816667, 39.766667, 39.633333,
39.6, 39.316667, 39.266667, 39.15, 39.116667, 39.05, 39.033333,
39.983333, 39.816667, 39.7, 39.516667, 39.35, 39.283333, 39.116667,
39.066667, 39.033333, 39.2, 39.333333, 39.333333, 39.616667,
39.616667, 39.566667, 39.85, 39.95, 39.933333, 39.8, 39.683333,
39.333333, 39.15, 39.15, 39.066667, 39.85, 39.733333, 39.65,
39.266667, 39.216667, 39.183333, 39.1, 39.033333, 39.1, 39.15,
39.45, 39.383333)
tmp <- data.frame(lon, lat)
plot(lon, lat)
Here is how those points look:
OK, let's convert this to a sf object and make it into a polygon:
polygon <- tmp %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast("POLYGON")
plot(
This makes beautiful abstract art but is not a simple outline of the points:
How do I explain to sf that I just want an outline polygon, as if you took a pen and connected the dots such that all the points were inside?
this worked:
polygon <- st_as_sf(tmp, coords = c("lon", "lat"), crs = 4326) %>%
summarise() %>%
st_concave_hull(., ratio=0.3)
h/t to L Tyrone and SamR