| Title: | Download Administrative Boundary Data from 'geoBoundaries' |
|---|---|
| Description: | Provides tools to download individual country boundaries and global composite boundaries from 'geoBoundaries' <https://www.geoboundaries.org/> across multiple administrative ('ADM') levels. Returns boundaries as 'sf' objects for mapping and spatial analysis. Runfola et al. (2020) <doi:10.1371/journal.pone.0231866> describe the underlying database. |
| Authors: | Diego Hernangómez [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8457-4658>), William & Mary geoLab [dtc] (ROR: <https://ror.org/03hsf0573>, for the geoBoundaries project) |
| Maintainer: | Diego Hernangómez <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.1 |
| Built: | 2026-10-12 23:12:33 UTC |
| Source: | https://github.com/dieghernan/geobounds |
Use this function with caution. It clears cached archives and
configuration by deleting the geobounds configuration directory
returned by tools::R_user_dir(), deleting the active cache directory and
clearing the GEOBOUNDS_CACHE_DIR environment variable. See
base::Sys.getenv().
gb_clear_cache(config = FALSE, cached_data = TRUE, quiet = TRUE)gb_clear_cache(config = FALSE, cached_data = TRUE, quiet = TRUE)
config |
A logical value. If |
cached_data |
A logical value. If |
quiet |
A logical value. If |
This reset restores the cache state of a fresh geobounds
installation. For safety, the function refuses to recursively delete a cache
path that contains the home, working, temporary or package configuration
directory. Use a dedicated cache subdirectory.
Inspect the active directory with gb_detect_cache_dir() before clearing
it. Use gb_set_cache_dir() to configure a cache directory again.
NULL, returned invisibly. This function is called for its
side effects.
Cache management functions:
gb_detect_cache_dir(),
gb_set_cache_dir()
# Caution: this may modify your current state. ## Not run: my_cache <- gb_detect_cache_dir() # Set an example cache directory. ex <- file.path(tempdir(), "example", "cache") gb_set_cache_dir(ex, quiet = TRUE) gb_clear_cache(quiet = FALSE) # Restore the initial cache. gb_set_cache_dir(my_cache) identical(my_cache, gb_detect_cache_dir()) ## End(Not run)# Caution: this may modify your current state. ## Not run: my_cache <- gb_detect_cache_dir() # Set an example cache directory. ex <- file.path(tempdir(), "example", "cache") gb_set_cache_dir(ex, quiet = TRUE) gb_clear_cache(quiet = FALSE) # Restore the initial cache. gb_set_cache_dir(my_cache) identical(my_cache, gb_detect_cache_dir()) ## End(Not run)
Detects the active cache directory. See gb_set_cache_dir().
gb_detect_cache_dir(x = NULL)gb_detect_cache_dir(x = NULL)
x |
An object. Ignored. |
A character scalar containing the path to the active cache directory. The path is also printed as a clickable message. See cli::inline-markup from cli.
Cache management functions:
gb_clear_cache(),
gb_set_cache_dir()
gb_detect_cache_dir()gb_detect_cache_dir()
Returns individual country boundaries that reflect how countries represent their own boundaries, without special identification of disputed areas.
Use gb_get_world() for global composite boundaries that standardize
disputed areas and fill gaps between borders.
Always acknowledge geoBoundaries and follow the boundary's license.
See vignette("metadata", package = "geobounds") for sources and licensing.
The wrappers gb_get_adm0(), gb_get_adm1(), gb_get_adm2(),
gb_get_adm3(), gb_get_adm4() and gb_get_adm5() are also available for
requesting a single ADM level.
gb_get( country, adm_lvl = "adm0", simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL )gb_get( country, adm_lvl = "adm0", simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL )
country |
A character vector of country names or ISO 3166-1 alpha-3
country codes. Use |
adm_lvl |
ADM level. Accepted values are |
simplified |
A logical value. If |
release_type |
A character string, one of |
quiet |
A logical value. If |
overwrite |
A logical value. If |
cache_dir |
A path to a cache directory. If |
These are terrestrial boundaries, not maritime boundaries. For product
selection and mapping workflows, see
vignette("geobounds", package = "geobounds").
This function uses current API metadata and reuses cached archives without
checking for upstream changes. Historical versions cannot be selected. See
vignette("metadata", package = "geobounds") for cache limitations
and provenance.
An sf object from sf containing the requested
boundaries. Returns NULL if no boundaries match the request
or the downloads return no geometries.
Runfola et al. (2020) "geoBoundaries: A global database of political administrative boundaries." PLOS ONE, 15(4), e0231866. doi:10.1371/journal.pone.0231866.
gb_get_metadata() inspects boundary metadata and licensing.
gb_get_max_adm_lvl() checks the ADM levels available for individual
country boundaries.
gb_set_cache_dir() configures where downloaded archives are cached.
Boundary download functions:
gb_get_adm,
gb_get_world()
# Map ADM2 in Sri Lanka. sri_lanka <- gb_get( "Sri Lanka", adm_lvl = 2, simplified = TRUE ) sri_lanka library(ggplot2) ggplot(sri_lanka) + geom_sf() + labs( caption = paste( "Sources: geoBoundaries, OpenStreetMap and Wambacher,", "license: ODbL 1.0" ) )# Map ADM2 in Sri Lanka. sri_lanka <- gb_get( "Sri Lanka", adm_lvl = 2, simplified = TRUE ) sri_lanka library(ggplot2) ggplot(sri_lanka) + geom_sf() + labs( caption = paste( "Sources: geoBoundaries, OpenStreetMap and Wambacher,", "license: ODbL 1.0" ) )
These functions call gb_get() for a single ADM level. gb_get_adm0()
returns country boundaries, gb_get_adm1() returns first-level subnational
boundaries (for example, states in the United States) and gb_get_adm2()
returns second-level subnational boundaries (for example, counties in the
United States). gb_get_adm3(), gb_get_adm4() and gb_get_adm5() return
third-, fourth- and fifth-level administrative boundaries, respectively.
Use gb_get_metadata() to check availability for the requested country,
ADM level and release type.
See gb_get() for download behavior and licensing, and
vignette("geobounds", package = "geobounds") for worked examples.
gb_get_adm0( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm1( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm2( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm3( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm4( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm5( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL )gb_get_adm0( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm1( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm2( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm3( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm4( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL ) gb_get_adm5( country, simplified = FALSE, release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative"), quiet = TRUE, overwrite = FALSE, cache_dir = NULL )
country |
A character vector of country names or ISO 3166-1 alpha-3
country codes. Use |
simplified |
A logical value. If |
release_type |
A character string, one of |
quiet |
A logical value. If |
overwrite |
A logical value. If |
cache_dir |
A path to a cache directory. If |
An sf object from sf containing the requested
boundaries. Returns NULL if no boundaries match the request
or the downloads return no geometries.
Runfola et al. (2020) "geoBoundaries: A global database of political administrative boundaries." PLOS ONE, 15(4), e0231866. doi:10.1371/journal.pone.0231866.
gb_get_metadata() lists the layers available for a country and release
type. gb_get_max_adm_lvl() summarizes the highest available ADM level.
Boundary download functions:
gb_get(),
gb_get_world()
lev2 <- gb_get_adm2( c("Italia", "Suiza", "Austria"), simplified = TRUE ) library(ggplot2) ggplot(lev2) + geom_sf(aes(fill = shapeGroup)) + labs( title = "Second-level administrative boundaries", subtitle = "Selected countries", caption = paste( "Sources: geoBoundaries and the original boundary providers,", "check gb_get_metadata() for licenses" ) )lev2 <- gb_get_adm2( c("Italia", "Suiza", "Austria"), simplified = TRUE ) library(ggplot2) ggplot(lev2) + geom_sf(aes(fill = shapeGroup)) + labs( title = "Second-level administrative boundaries", subtitle = "Selected countries", caption = paste( "Sources: geoBoundaries and the original boundary providers,", "check gb_get_metadata() for licenses" ) )
Returns a summary of selected country codes and their highest available ADM
level in geoBoundaries. ADM0 represents the country boundary. Use
gb_get_metadata() to list available layers in the selected release type.
gb_get_max_adm_lvl( country = "all", release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative") )gb_get_max_adm_lvl( country = "all", release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative") )
country |
A character vector of country names or ISO 3166-1 alpha-3
country codes. Use |
release_type |
A character string, one of |
A tibble from tibble with one row per matching
country. The boundaryISO column contains ISO 3166-1 alpha-3 country codes
and maxBoundaryType contains the highest available ADM level as an integer.
Runfola et al. (2020) "geoBoundaries: A global database of political administrative boundaries." PLOS ONE, 15(4), e0231866. doi:10.1371/journal.pone.0231866.
gb_get() downloads boundaries for the available ADM levels.
The ADM wrappers request a single administrative level.
See vignette("metadata", package = "geobounds") for availability
and metadata fields.
Metadata and licensing functions:
gb_get_metadata()
all <- gb_get_max_adm_lvl() library(dplyr) # Countries whose highest available level is ADM1. all |> filter(maxBoundaryType == 1) # Countries whose highest available level is ADM4. all |> filter(maxBoundaryType == 4)all <- gb_get_max_adm_lvl() library(dplyr) # Countries whose highest available level is ADM1. all |> filter(maxBoundaryType == 1) # Countries whose highest available level is ADM4. all |> filter(maxBoundaryType == 4)
Returns boundary metadata from the geoBoundaries API.
gb_get_metadata( country = "all", adm_lvl = "all", release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative") )gb_get_metadata( country = "all", adm_lvl = "all", release_type = c("gbOpen", "gbHumanitarian", "gbAuthoritative") )
country |
A character vector of country names or ISO 3166-1 alpha-3
country codes. Use |
adm_lvl |
ADM level. Accepted values are |
release_type |
A character string, one of |
This function queries /api/current and does not select historical versions.
See vignette("metadata", package = "geobounds") for column definitions,
type conversions, availability, licensing and reproducible workflows.
A tibble from tibble with one row per matching
boundary. Numeric statistics are converted to numeric values,
sourceDataUpdateDate to POSIXlt in GMT and
buildDate to Date. Literal "nan" values become NA.
Runfola et al. (2020) "geoBoundaries: A global database of political administrative boundaries." PLOS ONE, 15(4), e0231866. doi:10.1371/journal.pone.0231866.
gb_get() downloads the boundaries described by the metadata. The
ADM wrappers request a single administrative level.
This metadata describes individual country boundaries, not the CGAZ layers
returned by gb_get_world().
Metadata and licensing functions:
gb_get_max_adm_lvl()
# Get boundary metadata for ADM4. library(dplyr) gb_get_metadata(adm_lvl = "ADM4") |> glimpse()# Get boundary metadata for ADM4. library(dplyr) gb_get_metadata(adm_lvl = "ADM4") |> glimpse()
Returns global composite boundaries for the requested ADM level. Boundaries are clipped to international borders, with gaps between borders filled.
Always acknowledge geoBoundaries and follow the terms in the CGAZ archive.
gb_get_world( country = "all", adm_lvl = "adm0", quiet = TRUE, overwrite = FALSE, cache_dir = NULL )gb_get_world( country = "all", adm_lvl = "adm0", quiet = TRUE, overwrite = FALSE, cache_dir = NULL )
country |
A character vector of country names or ISO 3166-1 alpha-3
codes. Use |
adm_lvl |
ADM level. Accepted values are levels 0, 1 and 2 ( |
quiet |
A logical value. If |
overwrite |
A logical value. If |
cache_dir |
A path to a cache directory. If |
CGAZ uses extensive simplification and standardizes disputed areas. See
vignette("geobounds", package = "geobounds") for product differences.
Archives come from the repository's main branch and historical versions
cannot be selected. See
vignette("metadata", package = "geobounds") for provenance.
An sf object from sf containing the requested
boundaries. Returns NULL if no boundaries match the request
or the downloads return no geometries.
geoBoundaries global downloads: https://www.geoboundaries.org/globalDownloads.html.
CGAZ release files: https://github.com/wmgeolab/geoBoundaries/tree/main/releaseData/CGAZ.
Runfola et al. (2020) "geoBoundaries: A global database of political administrative boundaries." PLOS ONE, 15(4), e0231866. doi:10.1371/journal.pone.0231866.
gb_get() downloads individual country boundaries instead of CGAZ layers.
gb_set_cache_dir() configures where downloaded archives are cached.
See vignette("geobounds", package = "geobounds") for a comparison
of individual country boundaries and global composites.
Boundary download functions:
gb_get(),
gb_get_adm
# This download may take some time. ## Not run: world <- gb_get_world() library(ggplot2) ggplot(world) + geom_sf() + coord_sf(expand = FALSE) + labs(caption = "Source: geoBoundaries (CGAZ)") ## End(Not run)# This download may take some time. ## Not run: world <- gb_get_world() library(ggplot2) ggplot(world) + geom_sf() + coord_sf(expand = FALSE) + labs(caption = "Source: geoBoundaries (CGAZ)") ## End(Not run)
Sets the active cache directory and optionally saves it for future sessions.
Use gb_detect_cache_dir() to find the active cache directory.
gb_set_cache_dir( cache_dir = NULL, overwrite = FALSE, install = FALSE, quiet = FALSE )gb_set_cache_dir( cache_dir = NULL, overwrite = FALSE, install = FALSE, quiet = FALSE )
cache_dir |
A path to a cache directory. If |
overwrite |
A logical value. If |
install |
A logical value. If |
quiet |
A logical value. If |
By default, when no cache_dir is set, geobounds uses a directory
inside base::tempdir(). Cached archives in this directory are removed when
the R session ends. To reuse a cache directory across R sessions, use
gb_set_cache_dir(cache_dir = "a/path/here", install = TRUE). This saves the
directory in the user configuration path returned by
tools::R_user_dir().
Cached archives are reused without checking for upstream changes. See
vignette("metadata", package = "geobounds") for cache limitations
and provenance.
A character scalar containing the path to the cache directory, returned invisibly.
For occasional use, use the default temporary cache directory.
Set the cache directory for the current session with
gb_set_cache_dir(cache_dir = "a/path/here").
Save a persistent cache directory for future R sessions with
gb_set_cache_dir(cache_dir = "a/path/here", install = TRUE).
Set the cache directory for an individual download with the cache_dir
argument. See gb_get().
gb_get() and gb_get_world() use the configured cache directory for
downloaded boundary archives.
tools::R_user_dir() identifies standard locations for user-specific files.
Cache management functions:
gb_clear_cache(),
gb_detect_cache_dir()
# Caution: this may modify your current state. ## Not run: my_cache <- gb_detect_cache_dir() # Set an example cache directory. ex <- file.path(tempdir(), "example", "cachenew") gb_set_cache_dir(ex) gb_detect_cache_dir() # Restore the initial cache. gb_set_cache_dir(my_cache) identical(my_cache, gb_detect_cache_dir()) ## End(Not run) gb_detect_cache_dir()# Caution: this may modify your current state. ## Not run: my_cache <- gb_detect_cache_dir() # Set an example cache directory. ex <- file.path(tempdir(), "example", "cachenew") gb_set_cache_dir(ex) gb_detect_cache_dir() # Restore the initial cache. gb_set_cache_dir(my_cache) identical(my_cache, gb_detect_cache_dir()) ## End(Not run) gb_detect_cache_dir()