Downloads either a simple features (sf), SpatialPolygonDataFrame or a data_frame preprocessed using broom::tidy().

get_eurostat_geospatial(
  output_class = "sf",
  resolution = "60",
  nuts_level = "all",
  year = "2016",
  cache = TRUE,
  update_cache = FALSE,
  cache_dir = NULL,
  crs = "4326",
  make_valid = FALSE
)

Arguments

output_class

A string. Class of object returned, either sf simple features, df (data_frame) or spdf (SpatialPolygonDataFrame)

resolution

Resolution of the geospatial data. One of

  • "60" (1:60million),

  • "20" (1:20million)

  • "10" (1:10million)

  • "03" (1:3million) or

  • "01" (1:1million).

nuts_level

Level of NUTS classification of the geospatial data. One of "0", "1", "2", "3" or "all" (mimics the original behaviour)

year

NUTS release year. One of "2003", "2006", "2010", "2013", "2016" or "2021"

cache

a logical whether to do caching. Default is TRUE. Affects only queries from the bulk download facility.

update_cache

a logical whether to update cache. Can be set also with options(eurostat_update = TRUE)

cache_dir

a path to a cache directory. The directory have to exist. The NULL (default) uses and creates 'eurostat' directory in the temporary directory from tempdir(). Directory can also be set with option eurostat_cache_dir.

crs

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

  • "4326" - WGS84

  • "3035" - ETRS89 / ETRS-LAEA

  • "3857" - Pseudo-Mercator

make_valid

logical; ensure that valid (multi-)polygon features are returned if output_class="sf", see Details. Current default FALSE, will be changed in the future.

Value

a sf, data_frame or SpatialPolygonDataFrame.

Details

The data source URL is https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/administrative-units-statistical-units. The source provides feature collections as line strings (GeoJSON format), not as (multi-)polygons which, in some cases, yields invalid self-intersecting (multi-)polygon geometries (for some years/resolutions). This can cause problems, e.g., when using these geometries as input argument to sf::st_interpolate_aw(). make_valid = TRUE makes sure that only valid (multi-)polygons are returned, example included below.

See also

Other geospatial: eurostat_geodata_60_2016

Author

Markus Kainu markuskainu@gmail.com

Examples

# \donttest{
sf <- get_eurostat_geospatial(
  output_class = "sf",
  resolution = "60",
  nuts_level = "all"
)
#> Loading required namespace: sf
#> sf at resolution 1:60 read from local file
#> Warning: Default of 'make_valid' for 'output_class="sf"' will be changed in the future (see function details).
df <- get_eurostat_geospatial(
  output_class = "df",
  resolution = "20",
  nuts_level = "0"
)
#> Object cached at /tmp/RtmpNcFzcA/r_cache2/df20020164326.RData
#> No encoding supplied: defaulting to UTF-8.
#> Regions defined for each Polygons
#> Joining, by = "id"
#> data_frame at resolution 1: 20  cached at:  /tmp/RtmpNcFzcA/r_cache2/df20020164326.RData
#> Warning: Default of 'make_valid' for 'output_class="sf"' will be changed in the future (see function details).
# }

if (FALSE) {
spdf <- get_eurostat_geospatial(
  output_class = "spdf",
  resolution = "10",
  nuts_level = "3"
)
}

if (FALSE) {
# -------------------------------------------------------------------
# Minimal example to demonstrate reason/effect of 'make_valid = TRUE'
# Spatial data set; rectangle spanning the entire globe with a constant value of 1L.
# Requires the R package sf.
library("sf")
d <- c(-180, -90, -180, 90, 180, 90, 180, -90, -180, -90)
poly <- st_polygon(list(matrix(d, ncol = 2, byrow = TRUE)))
data <- st_sf(data.frame(geom = st_sfc(poly), data = 1L),
  crs = st_crs(4326)
)

# Causing an error: Self-intersection of some points of the geometry
NUTS2_A <- get_eurostat_geospatial("sf", 60,
  nuts_level = 2, year = 2013,
  crs = 4326, make_valid = FALSE
)
res <- tryCatch(st_interpolate_aw(data, NUTS2_A, extensive = FALSE),
  error = function(e) e
)
print(res)

# Resolving the problem using
# make_valid = TRUE. 'extensive = FALSE' returns
# average over each area, thus resulting in a
# constant value of 1 for each geometry in NUTS2_B.
NUTS2_B <- get_eurostat_geospatial("sf", 60,
  nuts_level = 2, year = 2013,
  crs = 4326, make_valid = TRUE
)
res <- st_interpolate_aw(data, NUTS2_B, extensive = FALSE)
print(head(res))
}