What to build
The primary new exported function of the WB Boundaries infrastructure. get_country_shapes() fetches WB Official Boundary parquets from the WB Data Catalog, reconstructs sf geometry, runs validation, attaches the pre-computed H3 hex grid, and returns a fully package-ready named list — identical in shape to ukr_shp or rwa_shp — with no manual column renaming required.
This is the function that enables "client app in minutes": a deployer who knows an ISO-3 code can call one function and have a valid shapes.rds in seconds.
Signature
get_country_shapes(
iso3,
levels = 0:2,
level_names = NULL, # NULL → uses registry level_names; override with e.g. c("Country", "Province", "District")
hex_resolution = 6L # only resolution 6 supported in v1
)
What it does (end-to-end)
- Validates
iso3 against the bundled registry; errors with list_country_shapes() hint if not found
- For each level in
levels: reads wb_boundaries_admin{N}.parquet from WB Data Catalog using arrow::open_dataset() with predicate pushdown iso3 == <iso3> — only the target country rows are transferred
- Reconstructs
sf geometry: sf::st_as_sfc(geometry_col, crs = 4326)
- Renames columns to package convention:
pcod → admin<N>Pcod, name → admin<N>Name, attaches parent Pcod columns, ensures area column (from area_km2)
- Assembles named list:
admin0_<level_name>, admin1_<level_name>, admin2_<level_name> — names from registry level_names or level_names argument override
- Fetches pre-computed hex parquet (
wb_hex_r6/iso3={iso3}/data.parquet); attaches as admin9_Hexagon
- Runs
validate_geometries() internally; prints cli summary: "✔ 3 layers | 1 + 5 + 30 polygons + 507 hex cells | all geometries valid"
- Returns the named list invisibly (also prints it so the user sees the structure)
Resulting 01-shapes.qmd pattern
# Option A — WB Official Boundaries (recommended for new deployments)
my_shp <- get_country_shapes(
iso3 = "RWA",
levels = 0:2,
level_names = c("Country", "Province", "District")
)
saveRDS(my_shp, "app-data/shapes.rds")
# Option B — custom boundaries (bring your own GeoJSON)
# [existing manual load + rename + spatial join code, commented out]
Dependencies
arrow (already used in hex pipeline)
sf, h3jsr (already present)
- No new
Imports if these are already listed
Error handling
- Unknown
iso3 → cli_abort() with suggestion to run list_country_shapes()
- Network failure →
cli_abort() with message explaining this requires internet access and suggesting the custom-boundaries fallback (Option B in 01-shapes.qmd)
- Validation failure → surface
validate_geometries() error with the structured result
Acceptance criteria
Blocked by
#145 (Inf-1) — parquets must exist on WB Data Catalog
#146 (Inf-2) — registry YAML must be present
#147 (Inf-3) — list_country_shapes() used in error messages
What to build
The primary new exported function of the WB Boundaries infrastructure.
get_country_shapes()fetches WB Official Boundary parquets from the WB Data Catalog, reconstructssfgeometry, runs validation, attaches the pre-computed H3 hex grid, and returns a fully package-ready named list — identical in shape toukr_shporrwa_shp— with no manual column renaming required.This is the function that enables "client app in minutes": a deployer who knows an ISO-3 code can call one function and have a valid
shapes.rdsin seconds.Signature
What it does (end-to-end)
iso3against the bundled registry; errors withlist_country_shapes()hint if not foundlevels: readswb_boundaries_admin{N}.parquetfrom WB Data Catalog usingarrow::open_dataset()with predicate pushdowniso3 == <iso3>— only the target country rows are transferredsfgeometry:sf::st_as_sfc(geometry_col, crs = 4326)pcod → admin<N>Pcod,name → admin<N>Name, attaches parent Pcod columns, ensuresareacolumn (fromarea_km2)admin0_<level_name>,admin1_<level_name>,admin2_<level_name>— names from registrylevel_namesorlevel_namesargument overridewb_hex_r6/iso3={iso3}/data.parquet); attaches asadmin9_Hexagonvalidate_geometries()internally; printsclisummary:"✔ 3 layers | 1 + 5 + 30 polygons + 507 hex cells | all geometries valid"Resulting
01-shapes.qmdpatternDependencies
arrow(already used in hex pipeline)sf,h3jsr(already present)Importsif these are already listedError handling
iso3→cli_abort()with suggestion to runlist_country_shapes()cli_abort()with message explaining this requires internet access and suggesting the custom-boundaries fallback (Option B in01-shapes.qmd)validate_geometries()error with the structured resultAcceptance criteria
get_country_shapes("RWA")returns a named list passingvalidate_geometries()with Rwanda admin0 + admin1 + admin2 + hex layersadmin<N>Pcod,admin<N>Name,area, parent Pcods)admin9_Hexagonlayer is present and has correct parent Pcod columnslevel_namesargument overrides registry defaultsiso3errors with actionable messagedevtools::check()examples pass (use\dontrun{}for the live fetch)Blocked by
#145 (Inf-1) — parquets must exist on WB Data Catalog
#146 (Inf-2) — registry YAML must be present
#147 (Inf-3) —
list_country_shapes()used in error messages