Compute the area and perimeter of a polygon at each vertex, showing how the measurements accumulate as vertices are added.

polygon_area_cumulative(x, polyline = FALSE)

Arguments

x

A two-column matrix or data frame of coordinates (longitude, latitude) in decimal degrees defining polygon vertices, or a list with longitude and latitude components.

polyline

Logical. If FALSE (default), compute area and perimeter of a closed polygon. If TRUE, compute only the length of a polyline (area will be meaningless).

Value

A data frame with columns:

  • lon: Longitude of vertex

  • lat: Latitude of vertex

  • area: Cumulative area in square meters up to this vertex

  • perimeter: Cumulative perimeter in meters up to this vertex

Details

This function is useful for understanding how polygon area accumulates and for debugging polygon vertex order issues.

Examples

# Watch area accumulate as vertices are added
pts <- cbind(
  lon = c(0, -74, -43, 28),
  lat = c(52, 41, -23, -26)
)
polygon_area_cumulative(pts)
#>   lon lat         area perimeter
#> 1   0  52 0.000000e+00         0
#> 2 -74  41 0.000000e+00  11111781
#> 3 -43 -23 2.653936e+13  22634340
#> 4  28 -26 6.569003e+13  29506941