knitr::opts_chunk$set(echo = TRUE, warning = TRUE, message = TRUE,
                      fig.width = 9, fig.height = 6, dpi = 500, 
                      retina = 1, fig.showtext = TRUE)

Setup

# devtools::install_github("EcoDynIZW/d6geodata")

sapply(list("sf", "tidyverse", "terra", "here" , "d6geodata"), 
       library, character.only = TRUE, logical.return = TRUE)

create folder

data_name <- d6geodata::data_name()

path <- here("data", unlist(str_split(data_name, "_"))[2], data_name)

dir.create(path = here::here(path))

Data

(germany_3035_sf <- st_read(here("data", 
                                 "germany",
                                 "administrativ-units_germany_2020_polygon_28532_gpkg",
                                 "administrativ-units_germany_2020_polygon_28532.gpkg")) %>% 
   dplyr::select(GEN) %>% 
   st_transform(3035)
 )

berlin_3035_sf <-
  st_read(
    here(
      "data",
      "berlin",
      "districs_berlin_2022_poly_03035_gpkg",
      "districs_berlin_2022_poly_03035.gpkg"
    )
  )

processing

data <- 

sf::st_write(data, here(path, stringi::stri_replace_last_fixed(data_name, "_", ".")), 
             delete_layer = TRUE)

create meta data file

meta <- dplyr::tibble(
  folder_name = data_name,
  name = stringi::stri_replace_last_fixed(data_name, "_", "."),
  epsg = ,
  crs = st_crs()$proj4string,
  year_of_data = ,
  units_of_data = "",
  resolution = "",
  type_of_data = d6geodata:::fun_type(),
  type_of_file = d6geodata:::fun_file(),
  date_of_compile = as.character(Sys.Date()),
  source = d6geodata:::fun_source(),
  short_description = "",
  modified = NA
) |> 
  mutate(link_of_source = d6geodata:::fun_source_link(x = source),
          copyright = d6geodata:::get_copyright(source))


write.table(meta, base::paste0(path, "/meta-data_", 
    data_name, ".csv"), row.names = FALSE, sep = ",")

plot file

col_nam <- names(data)[utils::menu(names(data), "Enter column name:")]

data_sf <- data %>% dplyr::select(all_of(col_nam))


if(meta$type_of_data %in% c('continuous_numeric', 'discrete_numeric')) {

  p_map <- ggplot() + 
    geom_sf(data = data_sf, aes(fill = .data[[{{ col_nam }}]]), color = NA) + 
    rcartocolor::scale_fill_carto_c(palette = "Emrld", 
        name = meta$units_of_data) + ggplot2::guides(fill = ggplot2::guide_legend(label.position = "bottom",
                                                                                     title.position = "top")) +
                                                           theme(legend.title = element_text(hjust = 0.5))
}
if(meta$type_of_data %in% c('unordered_categorical', 'ordered_categorical')) {

  breaks <- table((data_sf %>% data.frame())[, col_nam]) %>% 
                  data.frame() %>%
                  dplyr::mutate(Var2 = 1:n()) %>% 
                  dplyr::filter(Var2 %in% base::seq(1, max(Var2), round(max(Var2)/5)))

  data_sf <- data_sf |>
    group_by(.data[[col_nam]]) |>
    mutate(label = if_else(.data[[{{ col_nam }}]] %in% droplevels(breaks$Var1)[-1] ,.data[[{{ col_nam }}]] , "other"))

p_base_map <-
  ggplot() + geom_sf(data = data_sf, aes(fill = label), color = NA)   

  p_map <- 
   p_base_map +
    scale_fill_manual(
      breaks = unique(data_sf$label),
      labels = unique(data_sf$label),
      values = rev(c("#7F3C8D", "#11A579", "#3969AC", "#F2B701", "#E73F74", "#A5AA99")),
      name = meta$units_of_data
    ) + 
    ggplot2::guides(fill = ggplot2::guide_legend(label.position = "bottom",
                                                   title.position = "top")) +
                            theme(legend.title = element_text(hjust = 0.5))
}

saveRDS(p_map, here(path, paste0(data_name, ".rds")))

if(meta$type_of_data %in% c('binary_categorical')) {

  data_sf$label <- "" # add label or column to be displayed

p_base_map <-
  ggplot() + geom_sf(data = data_sf, aes(fill = label), color = "black")  

 p_map <- 
   p_base_map +
    scale_fill_manual(
      breaks = unique(data_sf$label),
      labels = unique(data_sf$label),
      values = rev(c("#A5AA99")),
      name = "" # if column added enter name of column
    ) + 
    ggplot2::guides(fill = ggplot2::guide_legend(label.position = "bottom",
                                                   title.position = "top")) +
                            theme(legend.title = element_text(hjust = 0.5))

}

saveRDS(p_map, here(path, paste0(data_name, ".rds")))

create html file

d6geodata::create_report(path_name = data_name, out_path = paste0(stringi::stri_c((
      unlist(stringi::stri_split(path, regex = "/"))[-c(1:which(unlist(stringi::stri_split(path, regex = "/")) %in% "GeoData")-1)]
    ), collapse = "/")), cats = c())

Session Info

## DO NOT REMOVE!
Sys.time()
#git2r::repository() ## uncomment if you are using GitHub
sessionInfo()



EcoDynIZW/d6geodata documentation built on Sept. 30, 2024, 2:15 p.m.