R6 class for downloading, processing, and caching Portuguese geographic
boundaries from CAOP (Carta Administrativa Oficial de Portugal), and for
filtered queries to DGT's OGC API. get_censos() and get_map() use one for
their downloads too.
Failed downloads
When a download fails, because a server can't be reached or a file has
moved or changed, the method stops with an error saying which and why. The
error has class geopt_download_error, so a script can catch download
failures alone with tryCatch(..., geopt_download_error = ...).
Usage
client <- GeoClient$new()
sf <- client$get_caop("continente", "municipio")Active bindings
cache_dirCharacter. Path to the cache directory. Defaults to
tools::R_user_dir("geopt", "cache").use_cacheLogical. Whether to read and write cached results (default
TRUE). WithFALSE, cached results are neither read nor written: each call builds its result again, and the latest CAOP year is looked up online instead of taken from the cache. Downloaded files (CAOP and census GeoPackages) are kept and reused either way;$clear_cache("raw")removes them.crsInteger EPSG code for the default coordinate reference system. Defaults to 4326 (WGS84). Output can be transformed to any CRS via the
crsargument of$get_caop().
Methods
GeoClient$new()
Create a new GeoClient.
Usage
GeoClient$new(cache_dir = NULL, use_cache = TRUE, crs = DEFAULT_CRS)GeoClient$get_caop()
Get geographic boundaries as an sf object.
Arguments
regionOne of "continente", "raa", "ram".
levelOne of "freguesia", "municipio", "distrito", "nuts1", "nuts2", "nuts3".
yearCAOP year. If NULL, resolves to the latest available.
crsOutput EPSG code. If NULL, uses the client default.
detailSimplification level: "full", "high", "medium", "low" or "very_low".
GeoClient$get_ogc()
Get features from a DGT OGC API collection.
Queries https://ogcapi.dgterritorio.gov.pt/ and returns the response
as an sf object. Use this for filtered or bounded subsets (single
municipio, bbox around a city, COS land-cover by area). For bulk
country-wide downloads of CAOP, $get_caop() is faster.
Usage
GeoClient$get_ogc(
collection,
bbox = NULL,
bbox_crs = 4326L,
properties = NULL,
cql_filter = NULL,
crs = NULL,
limit = OGC_DEFAULT_LIMIT,
max_features = Inf
)Arguments
collectionCollection id, e.g. "freguesias", "municipios", "nuts3", "cos2023v1". See
$list_ogc_collections(); an id geopt doesn't know is an error that lists the ones it does. The large collections ("cos2018v3","cos2023v1","cadastro") need abbox,propertiesorcql_filter.bboxNumeric length-4 vector
c(xmin, ymin, xmax, ymax)or an sfbboxobject. If NULL, no spatial filter is applied.bbox_crsInteger EPSG code that
bboxis expressed in. Defaults to 4326. Only sent to the server when not 4326.propertiesNamed list of equality filters on attribute columns, e.g.
list(distrito = "Lisboa"). Usecql_filterfor more complex predicates.cql_filterOptional CQL-Text filter string, passed through verbatim. Example:
"distrito_ilha = 'Lisboa'". DGT's server accepts CQL1 syntax (cql-text), not CQL2.crsOutput EPSG code. If NULL, uses the client default. Requested server-side to avoid client-side reprojection.
limitPage size sent to the server. Default 1000.
max_featuresStop pagination after this many features. Defaults to
Inf(fetch everything).
GeoClient$list_ogc_collections()
List collections reported live by the DGT OGC API.
Hits /collections?f=json. Used for discovery; the package itself
validates against the hardcoded OGC_COLLECTIONS registry.
GeoClient$download_caop()
Download boundaries to cache without loading into memory.
GeoClient$export_boundaries()
Export boundaries to a file format suitable for dashboards.
GeoClient$clear_cache()
Clear cached data.
Usage
GeoClient$clear_cache(type = c("all", "processed", "raw"))