I am trying to calculate the surface described by various coordinates (described below). For this I use the following code
library(sf)
tbl <- readr::read_table2(
Code Latitude Longitude
C1 40.051302 -3.593308
C2 40.043193 -3.599064
H1 40.04933 -3.477574
H2 40.085899 -3.214026
H3 40.245852 -3.253943
M1 40.03184 -3.34522
M1 40.03184 -3.34522
M2 40.050578 -3.50304
M3 40.0459 -3.125
M4 40.032298 -3.182801
Z1 40.050052 -3.476076
Z2 40.04472 -3.131
L1 40.03185 -3.3453
L2 40.048758 -3.476462
L3 40.050352 -3.125794
L4 40.245459 -3.252617
Q1 39.59552 -3.39183
Q2 40.225155 -3.530613
Q3 40.320294 -3.205903
Q4 40.345348 -3.137145
E1 40.059138 -3.500323
E2 40.234966 -3.536191
E3 40.089637 -3.196057
E4 40.494167 -3.131139")
tbl[, c(3,2)] %>%
as.matrix() %>%
list() %>%
st_polygon() %>%
st_sfc() %>%
st_set_crs(4326) %>%
st_area()
but I get the following error:
Error in MtrxSet(x, dim, type = "POLYGON", needClosed = TRUE) :
polygons not (all) closed
How can I solve the error or what alternatives are there?
My points draw this area, I would like to know what surface they have.
First of all, from the data you show, it would not be the points of a polygon but individual points, so your first problem is how to find an area that "encloses" all the points. What occurs to me is to use
chull()
to select those more external points, and on these yes, build the area or polygon.However, it is still necessary to "close" the area, the easiest trick is to replicate the first point, and now yes:
The graphic representation would be: