library(sf); library(dplyr); library(ggplot2)

1 DEFINITION DE LA GRILLE EN REGION BRETAGNE CONTINENTALE

grille_10x10 <- sf::st_read(
  paste0(wd$data, "masques/Grille_10x10/Grille_10X10.shp")
)
## Reading layer `Grille_10X10' from data source 
##   `/home/onyxia/work/AnalyseDonneesOpportunisteGMB/data/masques/Grille_10x10/Grille_10X10.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 9625 features and 2 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -280000 ymin: 5880000 xmax: 1310000 ymax: 7170000
## Projected CRS: RGF93 v1 / Lambert-93
RegionBretagneConti <- sf::st_read(
  paste0(wd$data, "masques/RegionBretagneConti/RegionBretagneConti.shp")
)
## Reading layer `RegionBretagneConti' from data source 
##   `/home/onyxia/work/AnalyseDonneesOpportunisteGMB/data/masques/RegionBretagneConti/RegionBretagneConti.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 123927.4 ymin: 6650496 xmax: 404382.1 ymax: 6882056
## CRS:           NA
RegionBretagneConti <- RegionBretagneConti%>%
  transforme_carte() %>%
  sf::st_transform(2154) %>%
  sf::st_buffer(dist = 200) %>%
  transforme_carte()

grille_10x10 <- grille_10x10 %>%
  transforme_carte() %>%
  sf::st_intersection(RegionBretagneConti) %>%
  dplyr::arrange(CODE_10KM) %>%
  dplyr::select(CODE_10KM)
## Warning: attribute variables are assumed to be spatially constant throughout all geometries
grille_10x10 <- grille_10x10 %>%
  dplyr::filter(sf::st_is(grille_10x10,c("POLYGON","MULTIPOLYGON")))

2 VARIABLES STRUCTURANTES

var_noms <- c("Indice_Diversite_500m", "Densite_Cultures_500m", 
              "Distance_EcotoneArbore", "Distance_Littoral", "Distance_Eau")

for(var in var_noms) {
  raster_path <- paste0(wd$data, "masques/VariablesStructurates/", var, ".tif")
  rast <- terra::rast(raster_path)
  terra::crs(rast) <- "EPSG:2154"
  grille_10x10 <- sf::st_transform(grille_10x10, crs = "EPSG:2154")
  vals <- exactextractr::exact_extract(rast, grille_10x10, 'mean')
  grille_10x10[[var]] <- vals
}

Suppression car hors du territoire -> Ca nous corrige le problème qu’on avait avec les individus dans la partie bluffée du 44.

grille_10x10 <- grille_10x10%>%
  dplyr::filter(!is.na(Distance_Littoral))
var_noms <- c("Indice_Diversite_500m", "Densite_Cultures_500m", 
              "Distance_EcotoneArbore", "Distance_Littoral", "Distance_Eau")

for(var in var_noms) {
  print(grille_10x10%>%
    ggplot()+
    geom_sf(aes(fill = .data[[var]]))
    )
}

3 FAMILLES DE PAYSAGES

On choisis la famille la plus fréquente dans la cellule?

famille_paysage <- sf::st_read(
  paste0(wd$data, "masques/famille_paysage/familles_paysages.shp")
)
## Reading layer `familles_paysages' from data source 
##   `/home/onyxia/work/AnalyseDonneesOpportunisteGMB/data/masques/famille_paysage/familles_paysages.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 39 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 99226.49 ymin: 6701141 xmax: 412224.8 ymax: 6881501
## Projected CRS: RGF93 v1 / Lambert-93
famille_paysage <- famille_paysage %>%
  dplyr::rename(Nom_paysage = NOM,
                Famille_paysage = FAMILLE)%>%
  dplyr::mutate_if(is.character,as.factor)%>%
  dplyr::select(Nom_paysage, Famille_paysage)

jointure <- sf::st_join(grille_10x10, famille_paysage, left = FALSE)

mode_cellule <- jointure %>%
  sf::st_drop_geometry() %>%  
  dplyr::group_by(CODE_10KM) %>%   
  dplyr::summarise(
    Famille_paysage = Mode(Famille_paysage),
    Nom_paysage = Mode(Nom_paysage)
    )

grille_10x10 <- grille_10x10 %>%
  dplyr::left_join(mode_cellule, by = "CODE_10KM")

On a une case sans de valeurs, donc on lui attribut la valeur la plus fréquente parmi ses voisins.

index <- sf::st_touches(grille_10x10, grille_10x10)

attribut_val_voisine <- function(val, voisins) {
    if (is.na(val)) {
      val_voisins <- valeur[voisins]
      val_voisins <- val_voisins[!is.na(val_voisins)]
      if (length(val_voisins) == 0) {
        return(NA)
      } else {
        return(Mode(val_voisins))
      }
    } else {
      return(val)
    }
  }

for (col in c("Famille_paysage", "Nom_paysage")) {
  valeur <- grille_10x10[[col]]
  nvx_valeur <- purrr::map2(valeur, index, attribut_val_voisine)
  grille_10x10[[col]] <- factor(unlist(nvx_valeur), 
                                levels = levels(valeur))
}
grille_10x10 %>%
  ggplot() + geom_sf(aes(fill=Famille_paysage))

4 TAILLE DE LA CELLULE

grille_10x10 <- grille_10x10 %>%
  sf::st_transform(2154) %>%
  dplyr::mutate(aire_km2 = as.numeric(sf::st_area(grille_10x10)/1000000)) %>%
  transforme_carte()%>%
  dplyr::filter(aire_km2 > 2)
grille_10x10%>%
  ggplot()+geom_sf(aes(fill=aire_km2))

5 COORDONNEES DES CENTROIDS

centroids <- sf::st_centroid(grille_10x10)
## Warning: st_centroid assumes attributes are constant over geometries
centroid_coords <- sf::st_coordinates(centroids)

grille_10x10_centroids <- grille_10x10 %>%
  dplyr::mutate(
    X_10km = centroid_coords[, "X"],
    Y_10km = centroid_coords[, "Y"]
  ) %>%
  dplyr::select(CODE_10KM, X_10km, Y_10km)

grille_10x10 <- grille_10x10 %>%
  dplyr::left_join(
    as.data.frame(grille_10x10_centroids),
    by = "CODE_10KM",
    relationship = "many-to-many"
  )

grille_10x10 <- grille_10x10%>%
  dplyr::rename(Code_10km = CODE_10KM, 
                Ind_Diversite = Indice_Diversite_500m,
                Dnst_Cultures = Densite_Cultures_500m,
                Dist_EcotoneArbore = Distance_EcotoneArbore,
                Dist_Littoral = Distance_Littoral,
                Dist_Eau = Distance_Eau)

6 EXPORTS

summary(grille_10x10)
##   Code_10km         Ind_Diversite   Dnst_Cultures      Dist_EcotoneArbore Dist_Littoral    
##  Length:328         Min.   :1.445   Min.   :0.007704   Min.   : 23.80     Min.   : 0.2196  
##  Class :character   1st Qu.:1.923   1st Qu.:0.288156   1st Qu.: 40.38     1st Qu.: 2.7277  
##  Mode  :character   Median :2.051   Median :0.388762   Median : 48.53     Median :15.1991  
##                     Mean   :2.076   Mean   :0.376411   Mean   : 58.12     Mean   :20.9589  
##                     3rd Qu.:2.218   3rd Qu.:0.466718   3rd Qu.: 59.67     3rd Qu.:34.7019  
##                     Max.   :3.061   Max.   :0.694237   Max.   :766.34     Max.   :79.8731  
##                                                                                            
##     Dist_Eau                                   Famille_paysage                        Nom_paysage 
##  Min.   :  77.86   Paysage cultive a ragosses          :93     Reliefs des Landes de Lanvaux: 36  
##  1st Qu.: 204.78   Paysage boise et de bosquets        :57     Plateau de Penthievre        : 22  
##  Median : 237.46   Paysage cultive avec talus          :56     Armor morbihannais           : 19  
##  Mean   : 257.46   Paysage de bocage a maille elargie  :41     Bassin de Pontivy-Loudeac    : 18  
##  3rd Qu.: 293.17   Paysage de bocage dense sur collines:34     Bassin de Rennes             : 17  
##  Max.   :1399.09   Paysage de littoral urbanise        :29     Cornouaille interieure       : 16  
##                    (Other)                             :18     (Other)                      :200  
##     aire_km2           X_10km           Y_10km              geometry.x          geometry.y 
##  Min.   :  2.294   Min.   :-4.752   Min.   :47.41   MULTIPOLYGON : 21   MULTIPOLYGON : 21  
##  1st Qu.: 78.606   1st Qu.:-3.668   1st Qu.:47.92   POLYGON      :307   POLYGON      :307  
##  Median :100.000   Median :-2.907   Median :48.18   epsg:4326    :  0   epsg:4326    :  0  
##  Mean   : 84.662   Mean   :-2.896   Mean   :48.18   +proj=long...:  0   +proj=long...:  0  
##  3rd Qu.:100.000   3rd Qu.:-2.140   3rd Qu.:48.45                                          
##  Max.   :100.000   Max.   :-1.021   Max.   :48.87                                          
## 

Export sous forme shapefile

grille_10x10%>%
  dplyr::select(-geometry.y, -geometry.x)%>%
  sf::st_write(paste0(wd$data, "derived/VariablesSite.shp"), 
               delete_dsn = TRUE)
## Warning in abbreviate_shapefile_names(obj): Field names abbreviated for ESRI Shapefile driver
## Deleting source `/home/onyxia/work/AnalyseDonneesOpportunisteGMB/data/derived/VariablesSite.shp' using driver `ESRI Shapefile'
## Writing layer `VariablesSite' to data source 
##   `/home/onyxia/work/AnalyseDonneesOpportunisteGMB/data/derived/VariablesSite.shp' using driver `ESRI Shapefile'
## Writing 328 features with 11 fields and geometry type Unknown (any).

Export sous forme csv

grille_10x10%>%
  dplyr::select(-geometry.y)%>%
  sf::st_drop_geometry()%>%
  utils::write.csv(paste0(wd$data, "derived/VariablesSite.csv"))