GitLab at IIASA

01_PreparePolygonFiles.R 4.95 KiB
Newer Older
Martin Jung's avatar
Martin Jung committed
library(sf)
library(tidyverse)
library(stringr)
source("Scripts/00_Functions.R") # Some Convenience functions

# Paths
path_input <- "Inputs/" # Note that inputs and other files need to be downloaded separately
path_output <- "Outputs/"
path_scripts <- "Scripts/"

# CRS proj4strings
crs.latlong <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" # Latitude longitude
crs.laea <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs"
crs.mollweide <- "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs" # For global equal area projections

# Vector of European countries (origin: GADM Sovereign)
vec_europe <- c("Finland",  "Andorra", 
                "Netherlands", "Austria", "Belgium",
                "Bosnia and Herzegovina", "Bulgaria",
                "France", "Croatia", "Cyprus", "Czechia", "Denmark",
                "Estonia","Germany", "Greece", "Hungary", "Ireland",
                "Italy", "Kosovo", "Latvia", "Liechtenstein", "Lithuania",
                "Luxembourg", "Malta",  "Slovakia", "Slovenia",
                "Northern Cyprus", "Poland", "Portugal", "Romania",
                "Spain", "Sweden", "Switzerland",  "Vatican City", "San Marino"
                )
# Accession countries
vec_accession <- c("Albania","Moldova", "Ukraine","North Macedonia","Montenegro","Kosovo", "Serbia",  "Turkey",
                   "Bosnia and Herzegovina", "Georgia")
# Other Friends not in the EU
vec_neighbor <- c("United Kingdom", "Norway", "Switzerland")

Martin Jung's avatar
Martin Jung committed
# ------------- #
#### Download EEA reference grids ####
# Ideally we ensure that the reference grid is consistent with the 
# EEA reference grid https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2

# Since the reference grid is for Europe only available at a 10km resolution, 
# we will use the original EEA grid and split it into smaller parts.

# Download European reference grids
# Source: https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/grids
for(grain in c("1km", "2km", "5km", "10km", "20km", "50km", "100km")){
  print(grain)
  url <- paste0("https://gisco-services.ec.europa.eu/grid/grid_",grain,"_surf.gpkg")
  curl::curl_download(
    url,
    paste0(path_input, basename(url) )
  )
}


Martin Jung's avatar
Martin Jung committed

#### Prepare OCT countries, including the UK and Norway ####
# We need an outline of the OCT countries. Ideally this also include
# those countries not in the EU, but which are included in the EU modelling.

gadm <- read_sf("/mnt/pdrive/bec_data/100_rawdata/GADM_AdminBoundaries/gadm_410.gpkg", quiet = T) |> 
  # Get all countries 
  # We exclude Antarctica and thus the French Southern and Antarctic Lands
  dplyr::filter(NAME_0 != 'Antarctica') %>% 
  sf::st_cast("MULTIPOLYGON")

# Now identify those regions that are part of European countries somehow 
# owing to historical reasons (injustices).
target_areas <- gadm |> 
  dplyr::filter(SOVEREIGN %in% c(vec_europe, vec_accession, vec_neighbor))
Martin Jung's avatar
Martin Jung committed

# First get all those regions that are accession regions
target_accession <- target_areas |> dplyr::filter(SOVEREIGN %in% vec_accession)

# Get all those bits that are part of another European soveraign
target_oct <- target_areas |> 
  dplyr::filter(SOVEREIGN %in% vec_europe) |> 
  dplyr::filter(NAME_0 != SOVEREIGN)

# Finally get the OCTs from Neighbor countries in case we want to consider them as well
# (optional)
target_oct_noneu <- target_areas |> 
  dplyr::filter(SOVEREIGN %in% vec_neighbor) |> 
  dplyr::filter(NAME_0 != SOVEREIGN)
Martin Jung's avatar
Martin Jung committed


# --- #
# Produce outputs for each of those above
# Outputs to be produced at highest level of detail and joined by country sovereign
path_output_vec <- paste0(path_output, "vector")

# Project to global Mollweide
oc <- target_oct |> sf::st_transform(crs = crs.mollweide) |> 
  # Summarize by feature and top-level name and save
  dplyr::group_by(NAME_0) |> dplyr::summarise() |> 
  ungroup() |> sf::st_cast("MULTIPOLYGON")
Martin Jung's avatar
Martin Jung committed

write_sf(oc, paste0("~", "/PG_gadm_octsaccession_name0_mollweide.gpkg"), layer = "EU_octs", quiet = TRUE)

# Accession countries
oc <- target_accession |> sf::st_transform(crs = crs.mollweide) |> 
  # Summarize by feature and top-level name and save
  dplyr::group_by(NAME_0) |> dplyr::summarise() |> 
  ungroup() |> sf::st_cast("MULTIPOLYGON")

write_sf(oc, paste0("~", "/PG_gadm_octsaccession_name0_mollweide.gpkg"), layer = "EU_accession", quiet = TRUE)

# Non-EU OCTs
oc <- target_oct_noneu |> sf::st_transform(crs = crs.mollweide) |> 
  # Summarize by feature and top-level name and save
  dplyr::group_by(NAME_0) |> dplyr::summarise() |> 
  ungroup() |> sf::st_cast("MULTIPOLYGON")
write_sf(oc, paste0("~", "/PG_gadm_octsaccession_name0_mollweide.gpkg"), layer = "NonEU_OCTs", quiet = TRUE)

# --- #
# Copy the file from the homedrive to the target folder
file.copy(from = paste0("~", "/PG_gadm_octsaccession_name0_mollweide.gpkg"),
          to = paste0(path_output_vec, "/PG_gadm_octsaccession_name0_mollweide.gpkg"))
file.remove(paste0("~", "/PG_gadm_octsaccession_name0_mollweide.gpkg"))
# --- #