Skip to contents

The GeoScale object

A GeoScale is an S7 object, a subclass of nestedscales’ NestedScale. S7::prop_names() lists its properties, gs@geoframes or S7::prop(gs, "geoframes") reads one, and str(gs) prints them with their size and purpose (with nestedscales 0.3.0 or later).

gs <- geoscale_example()
S7::prop_names(gs)
#>  [1] "leaftable"  "frames"     "members"    "leaf"       "meta"      
#>  [6] "attributes" "aliases"    "labels"     "geoframes"  "geometry"  
#> [11] "hidden"
property type holds
@leaftable data.frame one row per leaf: one column per geoframe, and the weight columns
@geoframes character the hierarchy, ordered coarsest first (the same value as @frames)
@members named list the ordered code vocabulary at each geoframe
@leaf character the name of the column that identifies the leaves: the finest geoframe, unless another was named
@labels data.frame display names by (geoframe, region, variant); sparse
@geometry sfc or NULL optional leaf polygons, aligned to leaftable rows
@meta list name, desc, weights, default_weight, default_label, crs, source
@attributes, @aliases data.frame inherited from NestedScale: member values and alternative spellings

The object is a definition: the regions, the geoframes that group them and the weights that size them. It holds no data. Data stays in ordinary tables, tied to the object by the region-code columns. Everything else — parent/child tables, ancestry, shares, dissolved shapes — is derived on demand; nothing is cached on the object.

The leaftable

geoscale_leaftable(gs)
#>   country state zone cell  km2 pop
#> 1       N    N1   N1   A1  100  10
#> 2       N    N1   N1   A2  200  90
#> 3       N    N2   ZB   A3  300  30
#> 4       N    N2   ZB   A4  400  70
#> 5       S    S1   ZB   A5  500  50
#> 6       S    S1   ZC   A6  600  50
#> 7    <NA>  <NA> <NA>  ROW 1000   0

Each row is one leaf. The finest geoframe (cell here) is the authored node ID (unique, non-missing) and the leaf column, geoscale_leaf_column(gs); there is no separate identifier column. Coarser geoframe columns may hold NA — a leaf with no code at a coarser geoframe (a rest-of-world row, offshore areas) simply does not participate at that resolution, and na_action= decides what recasts do about it.

Geoframes, members, ranks

geoscale_geoframes(gs)
#> [1] "country" "state"   "zone"    "cell"
geoscale_geoframes(gs, finest = TRUE)   # the leaf geoframe
#> [1] "cell"
geoscale_regions(gs, "state")           # the members at one geoframe
#> [1] "N1" "N2" "S1"
geoscale_rank(gs, c("zone", "cell"))    # positions, coarsest = 1
#> [1] 3 4

Geoframe names must be syntactically valid column names; CORE_GEOFRAMES is recommended vocabulary, not enforcement:

CORE_GEOFRAMES
#> [1] "GLOBE"     "CONTINENT" "COUNTRY"   "STATE"     "ZONE"      "CELL"
is_valid_geoframe(c("COUNTRY", "reg32", "", "2bad"))
#> [1]  TRUE  TRUE FALSE FALSE

Navigation is derived on demand from the leaftable — and because geoframes may cross-cut, ancestry is computed leaf-mediated, never as a transitive closure:

geoscale_parents(gs, "zone", "ZB", target = "state")   # cross-cut: two parents
#> [1] "N2" "S1"
geoscale_ancestors(gs, "cell", "A5")
#>   geoframe region
#> 1  country      S
#> 2    state     S1
#> 3     zone     ZB
geoscale_descendants(gs, "country", "N")
#>   geoframe region
#> 1    state     N1
#> 2    state     N2
#> 3     zone     N1
#> 4     zone     ZB
#> 5     cell     A1
#> 6     cell     A2
#> 7     cell     A3
#> 8     cell     A4
head(geoscale_ancestry(gs), 4)
#>   parent_geoframe parent child_geoframe child
#> 1         country      N           cell    A1
#> 2         country      N           cell    A2
#> 3         country      N           cell    A3
#> 4         country      N           cell    A4

Weights and meta

geoscale_weights(gs)
#> [1] "km2" "pop"
gs@meta[c("name", "weights", "default_weight")]
#> $name
#> [1] "example"
#> 
#> $weights
#> [1] "km2" "pop"
#> 
#> $default_weight
#> [1] "km2"

meta$name is load-bearing: conversion and attach name their working columns after the object (geoscale_map() label columns, join_geoscale()’s "<name>." prefixes), so both require a named GeoScale. geoscale_build() derives a default name from the geoframes; geoscale_from_leaftable() takes name=.

Labels

Display names are declared per geoframe and per region, in named variants. The table is sparse: here the countries get a name and an alternative one, while states and zones have none and are shown by their codes.

gs_named <- gs |>
  attach_labels_geoscale(
    data.frame(geoframe = "country", region = c("N", "S"),
               label = c("Northland", "Southland")),
    variant = "name"
  ) |>
  attach_labels_geoscale(
    data.frame(geoframe = "country", region = "N", label = "Nordland"),
    variant = "de"
  )
geoscale_labels(gs_named)
#>   geoframe region variant     label
#> 1  country      N    name Northland
#> 2  country      S    name Southland
#> 3  country      N      de  Nordland
geoscale_label(gs_named, c("N", "S"), "country")
#> [1] "Northland" "Southland"
geoscale_label(gs_named, c("N", "S"), "country", variant = "de")
#> [1] "Nordland" "S"
geoscale_label(gs_named, c("N1", "ZB"), "zone")
#> [1] "N1" "ZB"

Subsets keep the labels of the regions that remain, and the plots use the default variant.

What the validator guarantees

Construction (any layer: geoscale_from_provider(), geoscale_build(), geoscale_from_leaftable()) enforces:

  • the leaf column present, character, unique, no missing/empty values;
  • every label row naming a geoframe and a region that exist;
  • every geoframe present as a leaftable column; members exactly the non-NA values of that column, in a stable order;
  • weights numeric, finite, non-negative, with a positive sum; default_weight one of the declared weights;
  • geometry, when attached, aligned 1:1 with leaftable rows (see attach_geometry_geoscale(); the CRS is recorded in meta$crs);
  • a warning (not an error) when geoframes look ordered finer-to-coarser — order carries meaning: aggregation direction follows it.

The registries

Three package-level registries change behavior explicitly — the only way anything global changes:

Rules map value-column names to their aggregation rule (there is no fallback — unregistered columns without rule= error):

GEOSCALE_RULES
#> [1] "sum"           "weighted_mean" "mean"          "copy"         
#> [5] "sd"            "share"         "logshare"
register_geoscale_rule("capacity", "sum")
register_geoscale_rule("eff", "weighted_mean", weight = "pop")
get_geoscale_rule("eff")
#> $rule
#> [1] "weighted_mean"
#> 
#> $weight
#> [1] "pop"
list_geoscale_rules()
#>      param          rule weight
#> 1 capacity           sum   <NA>
#> 2      eff weighted_mean    pop
clear_geoscale_rules()

Maps hold exact crosswalks that short-circuit the leaf-layer derivation in geoscale_map() — for hand-audited concordances:

fake <- data.frame(state = "N1", zone = "ZC", n_from = 1L,
                   n_overlap = 1L, w = 1, w_from = 1)
register_geoscale_map(gs, "state", "zone", fake)
list_geoscale_maps()
#>                   key
#> 1 example:state->zone
get_geoscale_map(gs, "state", "zone")
#>   state zone n_from n_overlap w w_from
#> 1    N1   ZC      1         1 1      1
clear_geoscale_maps()

Providers are named map sources (fetch functions returning a wide sf/data.frame); the package ships one:

list_geoscale_providers()
#>           name                                          desc
#> 1 naturalearth Natural Earth admin-0/admin-1 (rnaturalearth)
get_geoscale_provider("naturalearth")$desc
#> [1] "Natural Earth admin-0/admin-1 (rnaturalearth)"

Further reading