From cfb81d0daba3c71f85bbddaec6fe170f2c71e71c Mon Sep 17 00:00:00 2001 From: Ewen Date: Fri, 4 Nov 2022 06:30:54 +0100 Subject: [PATCH] init --- .gitignore | 4 ++ scripts/enporzhiañ.R | 92 ++++++++++++++++++++++++++++++++++++ scripts/enporzhiañ_kartenn.R | 68 ++++++++++++++++++++++++++ scripts/main.R | 89 ++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 scripts/enporzhiañ.R create mode 100644 scripts/enporzhiañ_kartenn.R create mode 100644 scripts/main.R diff --git a/.gitignore b/.gitignore index f26b794..65f0e3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +data/ +tmp/ + + # ---> R # History files .Rhistory diff --git a/scripts/enporzhiañ.R b/scripts/enporzhiañ.R new file mode 100644 index 0000000..917468f --- /dev/null +++ b/scripts/enporzhiañ.R @@ -0,0 +1,92 @@ +library(readODS) +library(magrittr) +library(dplyr) +library(tidyr) +#library(osmdata) +library(tidywikidatar) + +############################## +## Arventennoù +# Cheñch teuliad +source_path <- rstudioapi::getActiveDocumentContext()$path +setwd(dirname(dirname(source_path))) + +# Anv ar c'holonennoù, hervez an niver a dud aterset +anv_kolon <- function(n, gant_lechierezh=TRUE, gant_evezhiou=TRUE) { + anviou <- c("anv_latin") + anviou <- c(anviou, paste0("anv_bzg_", seq(1,n))) + anviou <- c(anviou, "anv_glg") + if (gant_lechierezh) { + anviou <- c(anviou, "lechierezh") + } + if (gant_evezhiou) { + anviou <- c(anviou, "evezhiou") + } + anviou <- c(anviou, "strollad") + return(anviou) +} + +# WikiData +tw_enable_cache() +tw_set_cache_folder(path = fs::path(getwd(), "tmp/tw_data")) +tw_set_language(language = "br") +tw_create_cache_folder(ask = FALSE) + +## Renabl eus ar restroù +# Brieg +brieg1 <- read_ods("./data/Brieg.ods", sheet=1, range="A1:G44") %>% + as_tibble() %>% + mutate(strollad=1) +names(brieg1) <- anv_kolon(3) +brieg2 <- read_ods("./data/Brieg.ods", sheet=2, range="A1:E23") %>% + as_tibble() %>% + mutate(strollad=2) +names(brieg2) <- anv_kolon(1) +gps_brieg <- tw_search(search="Brieg") %>% + tw_filter_first(p="P31", q="Q484170") %>% + tw_get_property(p="P625") %>% + mutate(kumun="Brieg", led=as.numeric(strsplit(value, ",")[[1]][1]), hed=as.numeric(strsplit(value, ",")[[1]][2])) %>% + select(kumun, led, hed) +brieg = bind_rows(brieg1, brieg2) %>% + bind_cols(., gps_brieg) + + +# Louergad +louergad1 <- read_ods("./data/Louergad (Seubil).ods", sheet=1, range="A1:E44") %>% + as_tibble() %>% + mutate(strollad=1) +names(louergad1) <- anv_kolon(2, gant_lechierezh=FALSE) +louergad2 <- read_ods("./data/Louergad (Seubil).ods", sheet=2, range="A1:D23") %>% + as_tibble() %>% + mutate(strollad=2) +names(louergad2) <- anv_kolon(2, gant_lechierezh=FALSE, gant_evezhiou=FALSE) +gps_louergad <- tw_search(search="Louergad") %>% + tw_filter_first(p="P31", q="Q484170") %>% + tw_get_property(p="P625") %>% + mutate(kumun="Louergad", led=as.numeric(strsplit(value, ",")[[1]][1]), hed=as.numeric(strsplit(value, ",")[[1]][2])) %>% + select(kumun, led, hed) +louergad = bind_rows(louergad1, louergad2) %>% + bind_cols(., gps_louergad) + + +# Ploneour Lanwern +ploneour_lanwern1 <- read_ods("./data/Ploneour Lanwern.ods", sheet=1, range="A1:E44") %>% + as_tibble() %>% + mutate(strollad=1) +names(ploneour_lanwern1) <- anv_kolon(1) +ploneour_lanwern2 <- read_ods("./data/Ploneour Lanwern.ods", sheet=2, range="A1:E23") %>% + as_tibble() %>% + mutate(strollad=2) +names(ploneour_lanwern2) <- anv_kolon(1) +gps_ploneour_lanwern <- tw_search(search="Ploneour-Lanwern") %>% + tw_filter_first(p="P31", q="Q484170") %>% + tw_get_property(p="P625") %>% + mutate(kumun="Ploneour-Lanwern", led=as.numeric(strsplit(value, ",")[[1]][1]), hed=as.numeric(strsplit(value, ",")[[1]][2])) %>% + select(kumun, led, hed) +ploneour_lanwern = bind_rows(ploneour_lanwern1, ploneour_lanwern2) %>% + bind_cols(., gps_ploneour_lanwern) + + +data = bind_rows(brieg, + louergad, + ploneour_lanwern) \ No newline at end of file diff --git a/scripts/enporzhiañ_kartenn.R b/scripts/enporzhiañ_kartenn.R new file mode 100644 index 0000000..0eec7a8 --- /dev/null +++ b/scripts/enporzhiañ_kartenn.R @@ -0,0 +1,68 @@ +library("osmdata") +library("sf") + +############################## +## Arventennoù +# Cheñch teuliad +source_path <- rstudioapi::getActiveDocumentContext()$path +setwd(dirname(dirname(source_path))) + + +## Kemer an data +# An harzoù +if (!file.exists("./tmp/kumunioù.RData")) { + kumuniou_22 <- opq(bbox="Côtes-d'Armor") %>% + add_osm_feature(key = 'boundary', value = 'administrative') %>% + add_osm_feature(key = 'admin_level', value = '8') %>% + osmdata_sf + kumuniou_29 <- opq(bbox="Finistère") %>% + add_osm_feature(key = 'boundary', value = 'administrative') %>% + add_osm_feature(key = 'admin_level', value = '8') %>% + osmdata_sf + kumuniou_56 <- opq(bbox="Morbihan") %>% + add_osm_feature(key = 'boundary', value = 'administrative') %>% + add_osm_feature(key = 'admin_level', value = '8') %>% + osmdata_sf + + kumuniou <- c(kumuniou_22, + kumuniou_29, + kumuniou_56 + ) %>% unique_osmdata + + kumuniou_poly <- kumuniou$osm_multipolygons + kumuniou_poly <- kumuniou_poly %>% + mutate(ref.INSEE = as.numeric(as.character(ref.INSEE))) %>% + filter(grepl("^(22|29|56)", ref.INSEE)) + + + ggplot(municipalities_poly)+ + geom_sf() + + + dept_22 <- opq(bbox = "Côtes-d'Armor") %>% + add_osm_feature(key = 'boundary', value = 'administrative') %>% + add_osm_feature(key = 'admin_level', value = '6') %>% + osmdata_sf %>% unique_osmdata + dept_29 <- opq(bbox = "Finistère") %>% + add_osm_feature(key = 'boundary', value = 'administrative') %>% + add_osm_feature(key = 'admin_level', value = '6') %>% + osmdata_sf %>% unique_osmdata + dept_56 <- opq(bbox = "Morbihan") %>% + add_osm_feature(key = 'boundary', value = 'administrative') %>% + add_osm_feature(key = 'admin_level', value = '6') %>% + osmdata_sf %>% unique_osmdata + + dept <- c(dept_22, dept_29, dept_35, dept_44, dept_56) %>% + unique_osmdata + + dept_poly <- dept$osm_multipolygons + dept_poly <- dept_poly %>% + filter(grepl("^(22|29|56)", ref.INSEE)) + + + save(kumuniou, kumuniou_poly, + dept, dept_poly, + file = "./tmp/kumuniou.RData") +} else { + load(file = "../roadennoù/kumuniou.RData") +} diff --git a/scripts/main.R b/scripts/main.R new file mode 100644 index 0000000..ee72888 --- /dev/null +++ b/scripts/main.R @@ -0,0 +1,89 @@ +############################## +## Config +# Cheñch teuliad +source_path <- rstudioapi::getActiveDocumentContext()$path +setwd(dirname(dirname(source_path))) + +############################## +## Enporzhiañ ha treiñ ar roadennoù +# (ar roadennoù kriz = ur restr evit ul lec'h) +# disoc'h = data +source("./scripts/enporzhiañ.R") + +############################## +## Enporzhiañ peadra d'ober ur gartenn +# (harzoù hag all) +# disoc'h = data_osm +source("./scripts/enporzhiañ_kartenn.R") + + + +############################## +## Sevel ur gartenn +anv <- "Agropyron repens" + +sub_data <- data %>% + filter(anv_latin==anv) + + + + + + + + +data <- merge(municipalities_poly, + data_percentages, by.x="ref.INSEE", by.y="ebssa", all.x=TRUE) + +# Rannañ an dregantadoù dre rummad + +data$emdroadur_rummad <- cut(data$emdroadur, breaks=c(-0.05, -0.02, -0.01, -0.005, 0, 0.01, 0.02, 0.03, 0.04, 0.12)) +data$emdroadur2007_rummad <- cut(data$emdroadur2007_2017, breaks=c(-0.05, -0.02, -0.01, -0.005, 0, 0.01, 0.02, 0.03, 0.04, 0.12)) + + +# Evit gwiriañ: peseurt kumun zo hep emdroadur? +data %>% + filter(is.na(emdroadur)) +# => Plevenon ha Sant-Sinforian (poblañs ebet e 1999, nevez krouet int?) + + +#pdf("./kartenn_dregantad_skolioumamm_publik_prevez_diwan.pdf", width = 25, height = 20) + + +data_simple <- st_simplify(data, preserveTopology = TRUE, dTolerance = 0.001) +dept_poly_simple <- st_simplify(dept_poly, preserveTopology = TRUE, dTolerance = 0.001) + +br <- c(-0.02, -0.01, -0.005, 0, 0.01, 0.02, 0.03, 0.04) +## Diskouez +pdf("../kartenn_emdroadur_poblañs.pdf", width=10, height=7) +p <- ggplot(data=data_simple, aes(fill=emdroadur, text=paste0(name, "\nEmdroadur: ", round(emdroadur, 3)*100, "%")))+ + geom_sf(size=0.1, color="#bfbfbf")+ + #scale_fill_manual(values=c("#152a84", + binned_scale(aesthetics="fill", scale_name="stepsn", + palette=function(x) c("#152a84", + "#275da5", + "#00ade2", + "#c6e7f3", + "#f7f4d8", + "#fbea68", + "#f1b85f", + "#e78051", + "#e04c45"), + breaks=br, + limits=c(-0.05, 0.12), + guide="colorsteps", + labels=paste0(br*100, "%"))+ + guides(colour=guide_legend("No data", override.aes=list(colour="grey75")))+ + ggtitle(paste0("Emdroadur poblañs kumunioù Breizh etre 1999 ha 2017"))+ + theme_void()+ + labs(fill="Feur kresk bloaziek ar boblañs etre 1999 ha 2017")+ + guides(fill=guide_colorsteps(title.position="top", ticks=TRUE))+ + theme(plot.title = element_text(face = "bold", hjust=0.5, size=12), + legend.position="bottom", + legend.title=element_text(size=10), + legend.key.width=unit(0.5, "inches"), + legend.key.height=unit(0.1, "inches")) +print(p) +dev.off() + +ggplotly(p)