0

I have a large dataframe of point coordinates and I am trying to make a polygon out of the edge of the coordinates. Some of the points fall near the middle, so I do not want those points to shape the boundary of the polygon created. These are spatial data and I do convert them into sf or terra objects, but in the simplest form they are just (x,y) coordinates.

I've looked and I can't find any already developed function for this task.

Here's a basic set of coordinates that replicate the problem:

lat <- c(-34.8861,-34.9845,-34.9839,-34.4555,-34.3272,-34.922,-34.7)
lon <- c(118,117.721,117.118,116.789,115.785,115.843,116.5)

plot(lon,lat)

Any ideas for creating a polygon out of the outermost points of these coordinates?

Ryan Utz
  • 51
  • 1
  • 6

2 Answers2

2

something like this:

library(sf)
df <- data.frame(x = lon, y = lat)
pts1 <- st_as_sf(x = df, coords = c('x', 'y'))
my_hull <- st_convex_hull(st_union(pts1))
plot(my_hull)
Chris
  • 1,647
  • 1
  • 18
  • 25
2

With 'terra' it goes like this:

lat <- c(-34.8861,-34.9845,-34.9839,-34.4555,-34.3272,-34.922,-34.7)
lon <- c(118,117.721,117.118,116.789,115.785,115.843,116.5)

library(terra)
v <- vect(cbind(lon, lat), crs="+proj=longlat")
x <- convHull(v)

plot(x, col="red")
points(v, cex=2, col="blue")
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63