Skip to contents

Returns 'comarcas' of Spain as polygons,as provided by the INE (Instituto Nacional de Estadistica).

Usage

esp_get_comarca(
  region = NULL,
  comarca = NULL,
  moveCAN = TRUE,
  epsg = "4258",
  update_cache = FALSE,
  cache_dir = NULL,
  verbose = FALSE
)

Source

INE: PC_Axis files.

Arguments

region

A vector of names and/or codes for provinces or NULL to get all the comarcas., See Details.

comarca

A name or regex expression with the names of the required comarcas. NULL would not produce any filtering.

moveCAN

A logical TRUE/FALSE or a vector of coordinates c(lat, lon). It places the Canary Islands close to Spain's mainland. Initial position can be adjusted using the vector of coordinates. See Displacing the Canary Islands.

epsg

projection of the map: 4-digit EPSG code. One of:

  • "4258": ETRS89

  • "4326": WGS84

  • "3035": ETRS89 / ETRS-LAEA

  • "3857": Pseudo-Mercator

update_cache

A logical whether to update cache. Default is FALSE. When set to TRUE it would force a fresh download of the source file.

cache_dir

A path to a cache directory. See About caching.

verbose

Logical, displays information. Useful for debugging, default is FALSE.

Value

A sf polygon object.

Details

'Comarcas' (English equivalent: district, county, area or zone) does not always have a formal legal status. They correspond mainly to natural areas (valleys, river basins etc.) or even to historical regions or ancient kingdoms.

When using region you can use and mix names and NUTS codes (levels 1, 2 or 3), ISO codes (corresponding to level 2 or 3) or "cpro" (see esp_codelist).

When calling a superior level (Province, Autonomous Community or NUTS1) , all the comarcas of that level would be added.

The use of the information contained on the INE website may be carried out by users or re-use agents, at their own risk, and they will be the sole liable parties in the case of having to answer to third parties due to damages arising from such use.

About caching

You can set your cache_dir with esp_set_cache_dir().

Sometimes cached files may be corrupt. On that case, try re-downloading the data setting update_cache = TRUE.

If you experience any problem on download, try to download the corresponding .geojson file by any other method and save it on your cache_dir. Use the option verbose = TRUE for debugging the API query.

Displacing the Canary Islands

While moveCAN is useful for visualization, it would alter the actual geographic position of the Canary Islands. When using the output for spatial analysis or using tiles (e.g. with esp_getTiles() or addProviderEspTiles()) this option should be set to FALSE in order to get the actual coordinates, instead of the modified ones. See also esp_move_can() for displacing stand-alone sf objects.

Examples

# \donttest{
comarcas <- esp_get_comarca(moveCAN = FALSE)

library(ggplot2)

ggplot(comarcas) +
  geom_sf()


# Comarcas of Castille and Leon

comarcas_cyl <- esp_get_comarca("Castilla y Leon")

ggplot(comarcas_cyl) +
  geom_sf(aes(fill = ine.prov.name)) +
  labs(fill = "Province")


# Comarcas with Mountains or Alt(o,a) in the name

comarcas_alto <- esp_get_comarca(
  comarca = "Montaña|Monte|Sierra|Alt",
  epsg = 3857
)

ggplot(comarcas_alto) +
  geom_sf(aes(fill = ine.ccaa.name)) +
  geom_sf_text(aes(label = name), check_overlap = TRUE) +
  labs(fill = "CCAA")

# }