How do I calculate the area between two lines using R?
The two lines have the same start and end location, but the number of vertices for each line can vary.
I've tried to create a polygon from the coordinates of the vertices from the two line and then calculate the area by using rgeos::gArea but the resultant SpatialPolygons (sps) is incorrect.
x1 <- c(1,5,4,50)
y1 <- c(1,3,4,50)
line1 <- sp::SpatialLines(list(sp::Lines(sp::Line(cbind(x1,y1)), ID='a')))
x2 <- c(1,5,5,50)
y2 <- c(1,4,6,50)
line2 <- sp::SpatialLines(list(sp::Lines(sp::Line(cbind(x2,y2)), ID='b')))
plot(line1)
plot(line2, add = T, col = "red")
line_pts <- methods::as(line1, "SpatialPoints")
comparison_pts <- methods::as(line2, "SpatialPoints")
line_coords <- sp::coordinates(line_pts)
comparison_coords <- sp::coordinates(comparison_pts)
start <- line_coords[1, ]
end <- line_coords[base::nrow(line_coords), ]
coords <- base::rbind(line_coords, comparison_coords)
p = sp::Polygon(coords)
ps = sp::Polygons(list(p), 1)
sps = sp::SpatialPolygons(list(ps))
plot(sps, add = T, col ="black")