Perform a "dissolve" (a.k.a. aggregation) operation on a vector object according to a grouping variable, also aggregate values of the attribute table for all other columns over the same grouping variable.

Aggregation of factor or character columns is done as follows: - If all polygons that are aggregated to a common dissolved polygon had the same value for the column, then the value is preserved; - If polygons had different values, the column takes the value "variable" or NA (depending on var_as_NA )

Aggregation of factor or character columns is done as follows:

- If the column represent a "quantity" (e.g., Population), the value for each
  dissolved is computed as the sum of the values of the original
  polygons belonging to it;

TODO - If the column represents a "density" (e.g., ton ha-1), the value for each dissolved is computed as the average of the values of the original polygons belonging to it, weighted by their area; STILL TO BE IMPLEMENTED ! CURRENTLY ALL COLUMNS ARE COMPUTED SIMPLY AS THE SUM !!!!

dissolve_vect(in_vect, dissolve_var, var_as_NA = FALSE,
  out_file = NULL, out_type = "vectobject", overwrite = FALSE,
  verbose = TRUE)

Arguments

in_vect

Either a R vector object (sf or sp), or the path to a valid vector file

dissolve_var

character column of the attribute table of in_vect on to which the dissolve has to be performed.

var_as_NA

logical TO BE DESCRIBED - see examples for now!!!, Default: FALSE

out_file

character output file name

out_type

character ["vectfile" | "vectobject"] If "vectfile", and out_file is not NULL, the function returns the name of the saved shapefile. Otherwise, it returns the reprojected vector object, with the format specified by out_class, Default: 'vectobject'

overwrite

logical if TRUE, output file will be overwritten if existing, Default: FALSE

verbose

logical if TRUE, provide messages on processing, Default: TRUE

Value

an *sf object. The object is also saved to disk as a shapefile if out_file is provided

Examples

library(sprawl.data) library(maps) in_vect <- get(load(system.file("extdata/shapes","poly_lomb.RData", package = "sprawl.data"))) plot_vect(in_vect, fill_var = "NAME_2")
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
plot_vect(in_vect, fill_var = "population", palette_name = "RdYlGn")
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
# Dissolve the vector to provinces diss <- dissolve_vect(in_vect, "NAME_2")
#> Joining, by = "NAME_2"
#> Warning: Column `NAME_2` joining factor and character vector, coercing into character vector
diss
#> Simple feature collection with 12 features and 4 fields #> geometry type: GEOMETRY #> dimension: XY #> bbox: xmin: 8.49941 ymin: 44.6809 xmax: 11.42868 ymax: 46.63806 #> epsg (SRID): 4326 #> proj4string: +proj=longlat +datum=WGS84 +no_defs #> # A tibble: 12 x 5 #> NAME_2 NAME_1 year population geometry #> <chr> <fct> <chr> <dbl> <GEOMETRY [°]> #> 1 Bergamo Lombar… varia… 28 POLYGON ((10.10427 46.08951, 10.10582 4… #> 2 Brescia Lombar… varia… 25 POLYGON ((10.50012 46.33554, 10.51088 4… #> 3 Como Lombar… varia… 39 POLYGON ((9.295499 46.23737, 9.302701 4… #> 4 Cremona Lombar… varia… 38 POLYGON ((9.551826 45.46125, 9.57864 45… #> 5 Lecco Lombar… varia… 41 POLYGON ((9.491518 46.07167, 9.49338 46… #> 6 Lodi Lombar… varia… 116 POLYGON ((9.532625 45.16629, 9.529924 4… #> 7 Mantua Lombar… varia… 18 POLYGON ((10.69031 45.42357, 10.69465 4… #> 8 Milano Lombar… varia… 16 POLYGON ((8.955621 45.6264, 8.956942 45… #> 9 Monza and… Lombar… varia… 23 POLYGON ((9.260717 45.74068, 9.261759 4… #> 10 Pavia Lombar… varia… 14 MULTIPOLYGON (((8.814569 45.01818, 8.81… #> 11 Sondrio Lombar… varia… 51 POLYGON ((10.25107 46.6226, 10.2514 46.… #> 12 Varese Lombar… varia… 13 POLYGON ((8.772502 46.10313, 8.774683 4…
plot_vect(diss, fill_var = "NAME_2")
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
provinces <- get_boundaries("ITA", 2)
#> get_boundaries --> Downloading data for: "ITA", Level: 2
plot_vect(diss, fill_var = "population", palette_name = "RdYlBu", leg_position = "bottom")
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.