Summary statistical functions for sfg, sfc and sf objects

st_geometry_summary(x)

st_area_sum(x, value, digits, ...)

st_length_sum(x, value, digits, ...)

st_perimeter_sum(x, value, digits, ...)

st_perimeter_2d_sum(x, value, digits)

Arguments

x

object of class sfg, sfc or sf

value

object of class units or symbolic_units (see examples section of units() <- value), optional: if unspecified, the default unit of the output is returned

digits

integer indicating the number of decimal places to be used (see round), optional: if unspecified, the output is not rounded

...

passed on to s2_distance or s2_distance_matrix

Value

st_geometry_summary() returns a vector with counts of the geometry types include in x, which is named by the occurring geometry type(s). If a function of the type st_measure_sum() is executed, the sum of these very measures from all geometries included in x is returned, if specified in a certain unit and rounded by given digits (see st_area, st_length, st_perimeter resp. st_perimeter_2d).

Examples

library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
# some demo data
mat      <- rbind(0:3, log(4:1))*1000 + rep(c(26, 12), 4)*10^5
points   <- st_sfc(lapply(data.frame(mat), st_point)) %>% st_set_crs(2056)
polygons <- points %>% st_buffer(., seq_along(.)*150)

mix <- c(points, polygons)
st_geometry_summary(mix)
#>   POINT POLYGON 
#>       4       4 

st_area_sum(polygons)
#> 2119606 [m^2]
st_area_sum(polygons, "ha", 2)
#> 211.96 [ha]
st_area_sum(polygons, "km^2", 3)
#> 2.12 [km^2]

# if geometries' dimension and measure don't fit:
st_length_sum(polygons)
#> 0 [m]

st_perimeter_sum(polygons, "km", 1)
#> 9.4 [km]

# specifying only units doesn't effect comparison:
st_area_sum(polygons, "km^2") == st_area_sum(polygons, "ha")
#> [1] FALSE
# but setting units and rounding by digits argument can mess up comparison:
st_area_sum(polygons, "km^2") == st_area_sum(polygons, "ha", 0)
#> [1] FALSE
# if equivalent units-digits-specifying is done, comparison is feasible:
st_area_sum(polygons, "km^2", 2) == st_area_sum(polygons, "ha", 0)
#> [1] TRUE
# but to avoid a mess don't specify the digits before comparing

# similarly if returns are used in further calculations then don't specify
# the digits to avoid passing on rounding errors
(ratio <- st_perimeter_sum(polygons) / st_area_sum(polygons, "ha"))
#> 44.45968 [m/ha]
# rounding can be done later on
round(ratio)
#> 44 [m/ha]