Let's say you have two sf objects made of polygons. In my case, one is administrative units (ADM) and the other is buildings (SHAPES). I would like to calculate the sum of the surface area of SHAPES by administrative units. Administrative units have an identifier which is ADM$ID
Here is a reproducible example with two administrative units and some shapes within those.
structure(list(geometry = structure(list(structure(list(structure(c(6.14080991669186,
6.13947697270692, 6.16645297360113, 6.16780165721562, 6.14080991669186,
43.3518254218006, 43.3317355383674, 43.3309488103689, 43.351153846062,
43.3518254218006), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(6.16890637059613, 6.16740543717258,
6.19429519052069, 6.19526756384816, 6.16890637059613, 43.3510636393805,
43.3309787504817, 43.3300719009479, 43.3504615102039, 43.3510636393805
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), class = c("sfc_POLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 6.13947697270692,
ymin = 43.3300719009479, xmax = 6.19526756384816, ymax = 43.3518254218006
), class = "bbox")), ID = 1:2), row.names = 1:2, sf_column = "geometry", agr = structure(c(ID = NA_integer_), class = "factor", .Label = c("constant",
"aggregate", "identity")), class = c("sf", "data.frame"))
structure(list(geometry = structure(list(structure(list(structure(c(6.14610631720318,
6.1444416896701, 6.14698673458045, 6.14872736274246, 6.14610631720318,
43.3486637269545, 43.3472082709604, 43.3459195725539, 43.3473149345871,
43.3486637269545), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(6.14685762308362, 6.14559823469476,
6.14883926645652, 6.15082025971966, 6.14685762308362, 43.3421058187015,
43.3408121210182, 43.3392139218913, 43.3406600795448, 43.3421058187015
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(6.15692525266947, 6.15575738447906, 6.15884632257924,
6.16050796769147, 6.15692525266947, 43.3436054388965, 43.3425405435208,
43.3410621584899, 43.3424596372303, 43.3436054388965), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(6.17287816794718, 6.17142829167592, 6.17667469587113,
6.17837798466806, 6.17287816794718, 43.3368337767714, 43.3349675471364,
43.3338302197931, 43.3359781405233, 43.3368337767714), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), class = c("sfc_POLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 6.1444416896701,
ymin = 43.3338302197931, xmax = 6.17837798466806, ymax = 43.3486637269545
), class = "bbox"))), row.names = c(NA, 4L), class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(integer(0), class = "factor", .Label = c("constant",
"aggregate", "identity"), .Names = character(0)))
ADM are the two orange squares, and SHAPES are the black polygons.
I expected this simple pipe to work, I remember seing somewhere that with dyplr, it's not mandatory to use intersect functions from the sf package.
ADM %>%
group_by(ID) %>%
sum(st_area(SHAPES))
Yet, it's not working.