From ba8886f90cc379ab24dcf8e5f296bd350dc6c8be Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Mon, 13 Dec 2021 12:06:25 +0100
Subject: [PATCH] Map

---
 plotting/panel.R | 117 +++++++++++++++++++++++++++++++++++++++++------
 script_install.R |   4 +-
 2 files changed, 106 insertions(+), 15 deletions(-)

diff --git a/plotting/panel.R b/plotting/panel.R
index bffa737..ba0d72b 100644
--- a/plotting/panel.R
+++ b/plotting/panel.R
@@ -1096,13 +1096,10 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s
     # df_river = tibble(fortify(river))
 
     nbp = length(list_df2plot)
-    
+  
     # Get all different stations code
     Code = levels(factor(df_meta$code))
     nCode = length(Code)
-
-
-
     
     df_trend = list_df2plot[[1]]$trend
     
@@ -1206,10 +1203,17 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s
     maxTrendMean = apply(TrendMean_code, c(1, 2), max, na.rm=TRUE)    
 
     
-    for (j in 1:nbp) {
+    for (i in 1:nbp) {
+        
+        outname = paste('map_', i, sep='')
+        print(paste('map :', outname))
+
+        type = list_df2plot[[i]]$type
         
-        map = ggplot() + theme_void() +
+        map = ggplot() + theme_ash +
             
+            theme(panel.border = element_blank()) +
+        
             coord_fixed() +
             
             geom_polygon(data=df_france,
@@ -1217,7 +1221,7 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s
                          color=NA, fill="grey97") +
             
             # geom_path(data=df_river,
-            # aes(x=long, y=lat, group=group),
+            # aes(x=lon, y=lat, group=group),
             # color="grey85", size=0.15) +
             
             geom_polygon(data=df_bassin,
@@ -1227,20 +1231,105 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s
             geom_polygon(data=df_france,
                          aes(x=long, y=lat, group=group),
                          color="grey40", fill=NA, size=0.2)
-        
-        # marker color="grey50"
-                   
-        # for (code in Code) {
+
+        map = map +
+            # scale_x_continuous(expand=c(0, 0)) +
             
-        # }
+            # scale_y_continuous(expand=c(0, 0)) +
+            
+            coord_sf(xlim=c(280000, 790000),
+                     ylim=c(6110000, 6600000),
+                     expand=FALSE)
+
+        map = map +
+            theme(plot.margin=margin(0, 0, 0, 5, unit="mm"))
+
+        lon = c()
+        lat = c()
+        fill = c()
         
+        for (code in Code) {
+            
+            df_data = list_df2plot[[i]]$data
+            df_trend = list_df2plot[[i]]$trend
+            p_threshold = list_df2plot[[i]]$p_threshold
+            
+            df_data_code = df_data[df_data$code == code,] 
+            df_trend_code = df_trend[df_trend$code == code,]
+
+            Imin = 10^99
+            for (j in 1:nPeriod_max) {
+                Start = Start_code[Code_code == code][[1]][j]
+                End = End_code[Code_code == code][[1]][j]
+
+                I = interval(Start, End)
+                if (I < Imin) {
+                    idImin = j
+                }
+            }
+            
+            Start = Start_code[Code_code == code][[1]][idImin]
+            End = End_code[Code_code == code][[1]][idImin]
+            
+            df_data_code_per =
+                df_data_code[df_data_code$Date >= Start 
+                             & df_data_code$Date <= End,]
+            
+            df_trend_code_per = 
+                df_trend_code[df_trend_code$period_start == Start 
+                              & df_trend_code$period_end == End,]
+            
+            Ntrend = nrow(df_trend_code_per)
+            if (Ntrend > 1) {
+                df_trend_code_per = df_trend_code_per[1,]
+            }
+            
+            dataMean = mean(df_data_code_per$Qm3s, na.rm=TRUE)
+            trendMean = df_trend_code_per$trend / dataMean
+            
+            if (df_trend_code_per$p <= p_threshold){
+                color_res = get_color(trendMean, 
+                                      minTrendMean[idImin, i],
+                                      maxTrendMean[idImin, i],
+                                      palette_name='perso',
+                                      reverse=TRUE)
+                
+                filltmp = color_res$color
+            } else { 
+                filltmp = 'grey97'
+            }
+
+            lontmp =
+                df_meta[df_meta$code == code,]$L93X_m_BH            
+            lattmp =
+                df_meta[df_meta$code == code,]$L93Y_m_BH 
+
+            lon = c(lon, lontmp)
+            lat = c(lat, lattmp)
+            fill = c(fill, filltmp)
+
+            plot_map = tibble(lon=lon, lat=lat, fill=fill)
+
+        }
+
+        for (i in nCode) {
+            map = map +
+                geom_point(data=plot_map,
+                           aes(x=lon, y=lat),
+                           shape=21, size=3,
+                           color='grey50', fill=fill)
+        }
 
+        Map = list(map)
         
+        plot = grid.arrange(grobs=Map, layout_matrix=
+                                           matrix(c(1, 1, 2, 1, 1, 3),
+                                                  nrow=2, byrow=TRUE))
         
         # Saving matrix plot
-        ggsave(plot=map,
+        ggsave(plot=plot,
                path=outdirTmp,
-               filename='map.pdf',
+               filename=paste(outname, '.pdf', sep=''),
                width=29.7, height=21, units='cm', dpi=100)
     }
 }
diff --git a/script_install.R b/script_install.R
index 8c28e6a..c319b23 100644
--- a/script_install.R
+++ b/script_install.R
@@ -17,9 +17,11 @@ install.packages('trend')
 
 # linux
 # sudo apt-get install gdal-bin proj-bin libgdal-dev libproj-dev
-
 install.packages("rgdal")
 
+# linux
+# sudo apt-get -y update && sudo apt-get install -y  libudunits2-dev libgdal-dev libgeos-dev libproj-dev
+install.packages("sf")
 
 library(devtools)
 install_github("https://github.com/benRenard/BFunk") #type '1'
-- 
GitLab