Search code examples
rpolygonr-sp

How to draw Polygon for lots of lat/long coordinates and calculate surface are?


I have the following data: https://ufile.io/p9s0le6g ...which consists of 485 lat/long observations. I ultimately want to calculate the surface area (in m2 or km2) of this figure. I wanted to create a polygon with the data points (the outer points) and then calculate the surface are. However, I cannot come up with a nice polygon. My data points as coordinates look like: enter image description here

I want a polygon which would look like that (or similarly): enter image description here Here is what I tried:

library(sp)
df <- read.csv('data.csv')
p = Polygon(df)
ps = Polygons(list(p),1)
sps = SpatialPolygons(list(ps))
plot(sps)

This results in (clearly not a nice polygon): enter image description here I also tried to implement the answer of Create polygon from set of points distributed but this results in just a rectangular for me: enter image description here

Anyone an idea how I can optimize my polygon to get a figure which looks like the outer shape of my data points and how to then calculate the surface are of that optimized polygon?


Solution

  • Using concaveman and sf we can create a concave hull around your set of points:

    library(sf)
    library(concaveman)
    
    pts <- st_as_sf(df, coords=c('LONG','LAT'), crs=4326 )
    
    conc <- concaveman(pts)
    
    conc %>% st_area()
    # 4010443 [m^2]
    
    library(ggplot2)
    ggplot() +
      geom_sf(data=pts, col = 'red', pch=3)  +
      geom_sf(data = conc, fill = NA)
    

    enter image description here