Commit 493c2a80 authored by Grelot Frederic's avatar Grelot Frederic :swimmer_tone5:
Browse files

Merge branch 'master' into 2-add-hydrography-data

- map_so_ii
	- gestion des options, de l'aide
	- ajout de la légende pour theme hydro
- data.r
	- gestion de la fusion

0 errors :heavy_check_mark: | 0 warnings :heavy_check_mark: | 0 notes :heavy_check_mark:

Refs #2
Showing with 241 additions and 7 deletions
+241 -7
```{r data-recup}
library(sf)
# Récupération des données (à faire une fois)
## Récupération du périmètre (déjà pré-traité)
so_ii = readRDS("data-common/data/so-ii/so-ii_perim.rds")
## Récupération de CLC (déjà pré-traité)
so_ii_clc = readRDS("data-common/data/so-ii/so-ii_clc.rds")
## Récupération des cours d'eau
river = sf::read_sf("data-common/data/so-ii/topage-2021-09/cours-eau-so-ii.shp")
river = sf::st_transform(river, crs = sf::st_crs(so_ii))
selection = c(
"le Salaison",
"la Lironde",
"la Mosson",
"le Terrieu",
"le Lez",
"le Bérange",
"le Lirou",
"la Cadoule",
"Ruisseau du Coulazou"
)
river = river[river[["TopoOH"]] %in% selection, ]
```
```{r logo}
## Définition du nom du fichier de sauvegarde
file_path = file.path("data-common/figure/so-ii/pdf/so_ii.pdf")
# Génération de la carte
## Ouverture du fichier de sauvegarde
# png(file_path, height = 800, width = 800)
pdf(file_path)
## Initialisation de la carte avec le périmètre de so-ii
par(mai = c(.65, .60, .50, .15))
# so_ii_epure = sf::st_simplify(so_ii, dTolerance = 333)
# plot(so_ii_epure, lwd = 4, col = scales::alpha("orange", .333))
so_ii_epure = rmapshaper::ms_simplify(so_ii, keep = 0.04)
plot(so_ii_epure, lwd = 6, col = scales::alpha("#e95d0fff", .5), border = "#004494ff")
# ## Ajout du fond CLC avec le niveau 1 du code_18
# color_clc = scales::alpha(
# c("#FF2A2A", "#008400", "#008400", "#008400", "#002fa7ff"),
# c(.5, .25, .35, .15, 1)
# )
# # color_clc = scales::alpha(c("black", "darkolivegreen3", "darkolivegreen3", "lightblue", "lightblue"), .5)
# color = as.character(cut(
# as.integer(substr(so_ii_clc[["code_18"]], 1, 1)),
# breaks = 5,
# labels = color_clc))
# plot(so_ii_clc$geometry, border = NA, col = color, add = TRUE)
# plot(river$geometry, lwd = 3, col = "#002fa7ff", add = TRUE)
dev.off()
```
# Data to be included in library
so_ii = readRDS("data-common/data/so-ii/so-ii_perim.rds")
so_ii_clc = readRDS("data-common/data/so-ii/so-ii_clc.rds")
# Map
map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE, path = NULL, ...) {
if (!is.null(path)) {
switch(
EXPR = tolower(tools::file_ext(path)),
"pdf" = pdf(path),
"png" = png(path),
error(sprintf("%s not recognized", tolower(tools::file_ext(path))))
)
}
## Init map
par(mai = c(.65, .60, .50, .15))
plot(so_ii, axes = TRUE)
plot(so_ii, lwd = 2, add = TRUE)
if ("clc" %in% theme) {
color_clc = scales::alpha(c("red3", "darkolivegreen3", "darkgreen", "#4C90B4", "lightblue"), .2)
color = as.character(cut(
as.integer(substr(so_ii_clc[["code_18"]], 1, 1)),
breaks = 5,
labels = color_clc))
plot(so_ii_clc$geometry, border = NA, col = color, add = TRUE)
}
plot(dataset$geometry, add = TRUE, ...)
if (bar == TRUE) {
terra::sbar(10, c(3.55, 43.47), type = "bar", below = "km", label = c(0, 5, 10), cex = .8)
}
if (!is.null(dataset_legend)) {
dataset_legend = c(
x = "bottomright",
cex = .8,
bg = "white",
inset = 0.01,
dataset_legend)
do.call(legend, dataset_legend)
}
if (!is.null(path)) invisible(dev.off())
}
# Data to be plot
dataset = rio::import("data-common/table/so-ii/rex-2020.ods", which = 1)
dataset = dataset[!is.na(dataset$latitude), ]
dataset = sf::st_as_sf(dataset, coords = c("longitude", "latitude"), crs = "WGS84")
# Color
bg = rep(NA, nrow(dataset))
bg[dataset[["viticulture"]]] = "deeppink4"
bg[dataset[["habitant"]]] = "cornflowerblue"
col = "black"
cex = 1.4
pch = 21
# Legend
dataset_legend = list(
title = "Enquêtes du REX 19 septembre 2020",
legend = c("Agriculteurs", "Habitants membre ROI"),
pch = 21,
pt.bg = c("deeppink4", "cornflowerblue"),
pt.cex = 1.4
)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, cex = cex, col = col, pch = pch)
map_so_ii(dataset, dataset_legend, path = "toto.png", bg = bg, cex = cex, col = col, pch = pch)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22)
\ No newline at end of file
...@@ -12,17 +12,23 @@ bg[dataset[["habitant"]]] = "cornflowerblue" ...@@ -12,17 +12,23 @@ bg[dataset[["habitant"]]] = "cornflowerblue"
col = "black" col = "black"
cex = 1.4 cex = 1.4
pch = 21 pch = 21
pch[dataset[["viticulture"]]] = 21
pch[dataset[["habitant"]]] = 22
# Legend definition # Legend definition
dataset_legend = list( dataset_legend = list(
title = "Enquêtes du REX 19 septembre 2020", title = "Enquêtes du REX 19 septembre 2020",
legend = c("Agriculteurs", "Habitants membre ROI"), legend = c("Agriculteur", "Habitant"),
pch = 21, pch = c(21, 22),
pt.bg = c("deeppink4", "cornflowerblue"), pt.bg = c("deeppink4", "cornflowerblue"),
pt.cex = 1.4 pt.cex = cex
) )
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, cex = cex, col = col, pch = pch) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "clc", legend_theme = TRUE)
map_so_ii(dataset, dataset_legend, path = "toto.png", bg = bg, cex = cex, col = col, pch = pch) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "catnat", year = 2020, legend_theme = TRUE)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "catnat", year = 2020, hazard = "nappe", legend_theme = TRUE)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22, theme = "gaspar") map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "population", legend_theme = TRUE)
\ No newline at end of file
map_so_ii(dataset, dataset_legend, path = "rex-clc.pdf", bg = bg, pch = pch, theme = "clc", year = 2020, legend_theme = TRUE)
map_so_ii(dataset, dataset_legend, path = "rex-catnat.pdf", bg = bg, pch = pch, theme = "catnat", year = 2020, legend_theme = TRUE)
map_so_ii(dataset, dataset_legend, path = "rex-population.pdf", bg = bg, pch = pch, theme = "population", legend_theme = TRUE)
\ No newline at end of file
# Functions
plot_chronicle = function(catnat, file_path = "catnat-chronicle.pdf") {
year.month = format(catnat[["date.start"]], "%Y-%m")
n.ym = table(year.month)
d.ym = as.Date(paste(names(n.ym), "01", sep = "-"))
pdf(file_path, width = 20, height = 10)
plot(d.ym, as.integer(n.ym), type = "h", xaxt = "n", xlab = "", ylab = "n", col = "royalblue3")
axis.Date(1, format = "%Y-%m", las = 2, at = d.ym)
axis.Date(1, format = "%Y-%m", las = 2, label = FALSE,
at = seq(range(d.ym)[1], range(d.ym)[2], by = "month"))
dev.off()
}
plot_month = function(catnat, file_path = "catnat-month.pdf") {
month = format(seq(as.Date("2020-01-01"), length.out = 12, by = "month"), "%m")
month = table(factor(format(catnat[["date.start"]], "%m"), levels = month))
pdf(file_path, width = 20, height = 10)
barplot(height = month, ylab = "n", xlab = "month", col = "royalblue3")
dev.off()
}
# Data
## Preparing files & version (today)
today = as.character(Sys.Date())
dir.create("data-common/data/gaspar/archive", showWarnings = FALSE)
archive = sprintf("data-common/data/gaspar/archive/gaspar-%s", today)
## Download uptodate data
utils::download.file(
url = "https://files.georisques.fr/GASPAR/gaspar.zip",
destfile = file.path(sprintf("%s.zip", archive)),
method = "wget")
utils::unzip(sprintf("%s.zip", archive), exdir = archive)
unlink(archive, recursive = TRUE)
catnat = rio::import(file.path(archive, "catnat_gaspar.csv"))[-c(1, 10)]
## Formating data + scope
names(catnat) = c(
"commune",
"commune_name",
"alea_code",
"alea_jo",
"date.start",
"date.end",
"date.arrete",
"date.jo"
)
# write.csv2(unique(catnat[c("alea_code", "alea")]), "lib_risque_jo.csv", row.names = FALSE)
# catnat = catnat[ grep("nondation", catnat[["alea"]]), ]
so_ii_scope = geau::so_ii_scope
catnat_so_ii = merge(
catnat[ catnat[["commune"]] %in% so_ii_scope, ],
read.csv2("data-common/so-ii/gaspar/catnat_alea_jo.csv")
)
write.csv2(
catnat_so_ii,
sprintf("data-common/so-ii/gaspar/catnat-%s.csv", today),
row.names = FALSE
)
alea_scope = c("inondation", "nappe", "submersion")
catnat_so_ii = catnat_so_ii[ catnat_so_ii[["alea"]] %in% alea_scope, ]
catnat_so_ii[["commune"]] = factor(
catnat_so_ii[["commune"]],
levels = so_ii_scope
)
catnat_so_ii[["alea"]] = factor(
catnat_so_ii[["alea"]],
levels = alea_scope
)
# Treatments
plot_chronicle(catnat, "figure/catnat-france-chronicle.pdf")
plot_month(catnat, "figure/catnat-france-month.pdf")
plot_chronicle(catnat_so_ii, "catnat-so-ii-chronicle.pdf")
plot_month(catnat_so_ii, "catnat-so-ii-month.pdf")
write.csv2(catnat_year, sprintf("data-common/so-ii/gaspar/catnat_year_n-%s.csv", today))
write.csv2(catnat_month, sprintf("data-common/so-ii/gaspar/catnat_month_n-%s.csv", today))
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment