From 342bb4e50b7ce957980737ae9007280f29bebc79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Grelot?= <frederic.grelot@irstea.fr>
Date: Wed, 19 Jan 2022 14:31:39 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20des=20fonctions=20pour=20les=20cartes?=
 =?UTF-8?q?=20et=20documentation=20des=20donn=C3=A9es?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 geau/R/current_version.r                | 25 ++++++++++
 geau/R/data.r                           | 49 +++++++++++++++++++
 geau/R/{kable_units.R => kable_units.r} |  0
 geau/R/map_so_ii.r                      | 65 +++++++++++++++++++++++++
 4 files changed, 139 insertions(+)
 create mode 100644 geau/R/current_version.r
 create mode 100644 geau/R/data.r
 rename geau/R/{kable_units.R => kable_units.r} (100%)
 create mode 100644 geau/R/map_so_ii.r

diff --git a/geau/R/current_version.r b/geau/R/current_version.r
new file mode 100644
index 0000000..bbff4ec
--- /dev/null
+++ b/geau/R/current_version.r
@@ -0,0 +1,25 @@
+#' @title Find the current version of some data
+#' 
+#' @param path character path to a directory where to find data
+#' @param ... some extra parameters that will be passed to dir(path, ...)
+#'
+#' @return character path of the file corresponding the current version of the data
+#'
+#' @export
+#'
+#' @encoding UTF-8
+#' @author Frédéric Grelot
+#' 
+#' @examples
+#' 
+#' \dontrun{
+#' # To be added (soon)
+#' }
+ 
+current_version = function(path, ...) {
+    path = as.character(path)[1]
+    file.path(
+        path,
+        utils::head(sort(dir(path, ...), decreasing = TRUE), 1)
+    )
+}
diff --git a/geau/R/data.r b/geau/R/data.r
new file mode 100644
index 0000000..63f3aba
--- /dev/null
+++ b/geau/R/data.r
@@ -0,0 +1,49 @@
+#' Local collectivities included in so-ii
+#'
+#' A dataset containing the INSEE code of all local collectivities
+#' (communes) included in so-ii
+#'
+#' @format a vector of 69 INSEE code
+"so_ii_scope"
+
+#' List of all collectivities included in so-ii
+#'
+#' A dataset containing the INSEE code of all local collectivities
+#' included in so-ii
+#' 
+#' Basically this dataset is obtained as a selection from the layer
+#' COMMUNE in ADMIN EXPRESS, more a renaming of variables.
+#'
+#' @format sf data.frame 69 rows, 7 variables
+#' \describe{
+#'   \item{id}{id, from IGN ADMIN EXPRESS}
+#'   \item{commune}{character, official name of the commune}
+#'   \item{commune_majuscule}{character, official capitalized name of the commune}
+#'   \item{code}{character, INSEE code of the commune}
+#'   \{statut}{character, "statut" of the commune}
+#'   \{pop_yyy}{integer, official population of year yyyy in the commune}
+#'   \{epci}{characeter, INSEE ID of the EPCI of the commune}
+#' }
+#' @source \url{https://www.data.gouv.fr/fr/datasets/admin-express/}
+"so_ii_commune"
+
+#' Spatial perimeter of so-ii
+#'
+#' A dataset containing the perimeter of so-ii.
+#' 
+#' Basically, this dataset is obtained as
+#' \code{sf::st_union(so_ii_commune)}
+#'
+#' @format sfc_POLYGON of length 1
+"so_ii_limit"
+
+#' CLC information for so-ii
+#'
+#' A dataset containing the Corine Land Cover information on so-ii.
+#'
+#' @format sf data.frame 1337 rows, 2 variables
+#' \describe{
+#'   \item{clc_2018}{character, classification from CLC 2018}
+#'   \item{color}{character, default color to be used to plot so_ii_clc}
+#' }
+"so_ii_limit"
\ No newline at end of file
diff --git a/geau/R/kable_units.R b/geau/R/kable_units.r
similarity index 100%
rename from geau/R/kable_units.R
rename to geau/R/kable_units.r
diff --git a/geau/R/map_so_ii.r b/geau/R/map_so_ii.r
new file mode 100644
index 0000000..df6a9a2
--- /dev/null
+++ b/geau/R/map_so_ii.r
@@ -0,0 +1,65 @@
+#' @title Plot a thematic map of so-ii
+#' 
+#' @param dataset sf objectf, data to be plotted
+#' @param dataset_legend list of parameters to be passed to legend
+#' @param theme character, choice for the theme (if any)
+#' @param bar logical, should a bar be plotted
+#' @param path character, the name of the file to save the plot
+#' @param ...  some parameters that will be used by plot (from sf)
+#'
+#' @return Nothing useful.
+#' 
+#' @export
+#'
+#' @encoding UTF-8
+#' @author Frédéric Grelot
+#' 
+#' @examples
+#' 
+#' \dontrun{
+#' # To be added (soon)
+#' }
+
+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" = grDevices::pdf(path),
+            "png" = grDevices::png(path),
+            stop(sprintf("%s not recognized", tolower(tools::file_ext(path))))
+        )
+    }
+
+    ## Init map
+    graphics::par(mai = c(.65, .60, .50, .15))
+    plot(geau::so_ii_limit, axes = TRUE)
+    plot(geau::so_ii_limit, lwd = 2, add = TRUE)
+
+    if ("clc" %in% theme) {
+        plot(
+            geau::so_ii_clc[["geometry"]],
+            border = NA,
+            col = geau::so_ii_clc[["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(graphics::legend, dataset_legend)
+    }
+
+    if (!is.null(path)) invisible(grDevices::dev.off())
+}
\ No newline at end of file
-- 
GitLab