notebooks/germany.R

# /Library/Frameworks/R.framework/Resources/Rscript -e "source('notebooks/germany.R')"

### Steps to replicate hexagons (takes a while to run):

library("hexscape")

## 1) Set path to folder with raw spatial data and intermediate objects:

set_storage_folder("~/Documents/Resources/Datasets/hexscape")
# then put clc_legend.csv and u2018_clc2018_v2020_20u1_geoPackage into the raw_data folder

## 2) Process and cache corine data:

# Downloads and caches high res map:
map <- extract_map("DE")
# Processes raw corine data and saves processed_data/corine_XX.rds
land_use <- extract_corine("DE", verbose=2L)
# Note: the second time these are run they load cached data

## 3) Group and summarise corine data

clc <- extract_clc() %>%
  mutate(Category = case_when(
    CLC_LABEL2 == "Urban fabric" ~ "Impassable",
    CLC_LABEL3 == "Industrial or commercial units" ~ "Impassable",
    CLC_LABEL1 == "Artificial surfaces" ~ "Passable",
    CLC_CODE %in% c("243", "324", "322", "411", "421", "412", "321", "222") ~ "Medium",
    CLC_LABEL1 == "Water bodies" ~ "Passable",
    CLC_LABEL2 == "Open spaces with little or no vegetation" ~ "Passable",
    CLC_CODE %in% c("243","244") ~ "High",
    CLC_LABEL1 == "Agricultural areas" ~ "Low",
    CLC_LABEL2 == "Forests" ~ "High",
    CLC_LABEL3 == "Transitional woodland-shrub" ~ "High",
    CLC_LABEL2 == "Scrub and/or herbaceous vegetation associations" ~ "Low",
    CLC_LABEL1 == "Wetlands" ~ "Low",
    CLC_LABEL1 == "Unknown" ~ "Passable",
    TRUE ~ NA_character_
  )) %>%
  mutate(Category = ordered(Category, levels=c("Impassable","Passable","Low","Medium","High"))) %>%
  arrange(Category, CLC_CODE)
stopifnot(all(!is.na(clc$Category)))

land_use_processed <- land_use %>%

  # East Germany (Berlin, Brandenberg, Mecklenburg, Sachsen):
  filter(str_detect(NUTS_ID, "DE3.*") | str_detect(NUTS_ID, "DE4.*") | str_detect(NUTS_ID, "DE8.*") | str_detect(NUTS_ID, "DED.*")) %>%

  left_join(clc %>% select(CLC_CODE, Category), by="CLC_CODE") %>%
  filter(CLC_LABEL1 != "Unknown") %>%
  group_by(Category) %>%
  summarise(AREA_HA = sum(AREA_HA), Shape = sf::st_union(Shape), .groups="drop")

ggplot(land_use_processed) + geom_sf()

# save(land_use_processed, file="tempfile.rda")

# Limit to

# Simplify land use boundaries for computational reasons:
land_use_processed <- land_use_processed %>%
  rmapshaper::ms_simplify(keep=0.25, keep_shapes=TRUE, explode=TRUE, method="dp")

## 3) Generate hexagon patches from this map:
patches <- generate_patches(map, hex_width=2000, land_use=land_use_processed)

## 4) Generate neighbours:

neighbours <- generate_neighbours(patches, calculate_border=TRUE)

save(patches, neighbours, map, file="patches/patches_germany.rda")
ku-awdc/HexScape documentation built on Jan. 10, 2025, 5:24 p.m.