Introduction

Objectif du tutoriel

Il s’agit ici de décrire comment accéder simplement au découpage administratif de la France (voire d’autres pays pour les faire par exemple apparaître en zones frontalières) pour différents usages.

Selon ces usages, la précision requise diffère. Un découpage grossier suffira pour représenter des données nationales à l’échelle départementale. Il faudra en revanche plus de finesse pour les limites communales à l’intérieur d’un EPCI.

Si la donnée représentée est rapportée à un découpage qui évolue dans le temps (par exemple à la commune ou à la région, entités qui ont subi des fusions), il faudra s’assurer de la compatibilité des millésimes entre données et découpage.

Pré-requis

Les étapes de configuration de la machine et d’installation des packages sont considérées comme déjà réalisées. Si ce n’est pas le cas se reporter à ces explications.

Activation des packages :

library(tod)
library(tidyverse)
library(mapview)

Package COGiter

Présentation

COGiter fournit des fonctions, des données et des fonds de carte pour permettre des analyses territoriales sur les collectivités françaises. Il est accompagné d’un tutoriel.

Ses principales fonctionnalités sont :

  • passerelle d’un millésime des codes communes à un autre
  • passerelles d’une échelle à une autre (région > département > EPCI > Commune)
  • fourniture de fonds de carte à ces 4 échelles

Tout ça est extrêmement pratique, mais on peut préférer charger soi-même les données source IGN, OpenStreetMap, Insee, etc. et les archiver, d’où les sections suivantes.

Installation

Si le package n’est pas encore installé, procéder à son téléchargement depuis le dépôt Gitlab de la Dreal des Pays de la Loire.

devtools::install_gitlab("dreal-datalab/COGiter")

Puis l’activer :

library(COGiter)

Fonds de carte

Les fonds de carte disponibles pour les départements et régions d’outre-mer ainsi que de métropole aux échelles mentionnées ci-dessus :

ls("package:COGiter", pattern = "_geo$") # suffixe_geo pour les objets spatiaux
#>  [1] "communes_971_geo"       "communes_972_geo"       "communes_973_geo"      
#>  [4] "communes_974_geo"       "communes_976_geo"       "communes_geo"          
#>  [7] "communes_metro_geo"     "departements_971_geo"   "departements_972_geo"  
#> [10] "departements_973_geo"   "departements_974_geo"   "departements_976_geo"  
#> [13] "departements_geo"       "departements_metro_geo" "epci_971_geo"          
#> [16] "epci_972_geo"           "epci_973_geo"           "epci_974_geo"          
#> [19] "epci_976_geo"           "epci_geo"               "epci_metro_geo"        
#> [22] "filtrer_cog_geo"        "regions_971_geo"        "regions_972_geo"       
#> [25] "regions_973_geo"        "regions_974_geo"        "regions_976_geo"       
#> [28] "regions_geo"            "regions_metro_geo"

Chacun de ces objets est géographique, de classe sf. Il peut être visualisé simplement avec le package ggplot :

ggplot(communes_972_geo) +
  geom_sf()

Pour obtenir les contours sur un périmètre administratif, on utilise la fonction filtrer_cog_geo(). Son aide nous renseigne sur son usage. Elle mermet de filtrer le découpage national selon la commune, l’EPCI, le département ou la région (arguments depcom, epci, dep, `reg``, respectivement). Elle renvoie une liste d’objets spatiaux correspondant à chacune des échelles.

Exemple d’utilisation : carte des communes des Bouches du Rhône avec les limites des EPCI en bleu

dept_13 <- filtrer_cog_geo(dep = "13")
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): longer object length is not a multiple of shorter object length
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): longer object length is not a multiple of shorter object length
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

ggplot(data = dept_13$communes,
       aes(fill = DEPCOM)) +
  geom_sf() +
  geom_sf(data = dept_13$epci,
          col = "blue",
          fill = alpha("red", 0),
          size = 1) +
  theme(legend.position = "none")

Si l’on veut les EPCI de la région avec leurs noms au survol, on applique à la carte créée par ggplot la fonction ggplotly() du package plotly:

paca <- filtrer_cog_geo(reg = "93") %>% # code de la région PACA = 93
  .$epci %>% # récupération du slop epci
  left_join(y = epci) %>% # ajout des informations contenues dans le dataframe epci du package
  select(EPCI, NOM_EPCI, geometry) # sélection des colonnes qui nous intéressent
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): longer object length is not a multiple of shorter object length
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): longer object length is not a multiple of shorter object length
#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing

#> Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex =
#> opts(pattern)): argument is not an atomic vector; coercing
#> Joining, by = "EPCI"

paca_carte <- ggplot(data = paca,
       aes(fill = EPCI,
           text = NOM_EPCI)) +
  geom_sf() +
  theme(legend.position = "none")

plotly::ggplotly(paca_carte)

Tables de correspondance

Le package contient les tables de passage entre échelles.

communes %>% head() %>% DT::datatable()
#> Warning in instance$preRenderHook(instance): It seems your data is too big
#> for client-side DataTables. You may consider server-side processing: https://
#> rstudio.github.io/DT/server.html
epci %>% head() %>% DT::datatable()
departements %>% head() %>% DT::datatable()
regions %>% head() %>% DT::datatable()

Source GADM

Le projet GADM vise à cartographier le découpage administratif de l’ensemble des pays du monde, à chacun des niveaux de finesse. Ses données sont en libre accès sauf pour usage commercial et le package GADMTools simplifie l’utilisation de ses données. Un tutoriel en français existe pour ce package.

La fonction gadm_sf_loadCountries() permet de charger la carte pour un (ou plusieurs) pays en spécifiant la finesse du découpage par l’argument level. Le niveau level = 0 retourne les frontières du pays. Pour la France, level = 1, retourne les régions, level = 2 les départements, etc.

Les fichiers sont légers donc le téléchargement est rapide mais la précision peut manquer pour certain s usages.

library(GADMTools)
depts_fr <- gadm_sf_loadCountries("FRA",
                                  level = 2,
                                  basefile = "../raw_data/")

La visualisation de l’objet retourné prévue dans le package :

gadm_plot(depts_fr)
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()

Cet objet comprend plusieurs éléments :

names(depts_fr)
#> [1] "basename" "sf"       "level"    "hasBGND"

On peut donc extraire le slot nommé sf qui est un objet spatial facilement manipulable avec le package sf et visualisable avec mapview().

mapview::mapview(depts_fr$sf)
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()

Source OpenStreetMap

Ces données sont diffusées sur la plateforme [www.data.gouv.fr]’https://www.data.gouv.fr/fr/organizations/openstreetmap/). Pour certains de ces fichiers, la précision est meilleure que pour le projet GADM et les fichiers à télécharger sont nettement plus lourds (200Mo pour les communes).

On note en particulier :

Pour y accéder, on utilise la fonction tgeo_osm_adm() du package tod qui permet de :

Départements

Les contours sont proposés à plusieurs niveaux de précision (de 1 à 22Mo) pour plusieurs millésimes. Par exemple pour télécharger l’export de mars 2014 - vérifié et simplifié à 100m :

url <- "https://www.data.gouv.fr/fr/datasets/r/3096e551-c68d-40ce-8972-a228c94c0ad1"
depts <- tgeo_osm_adm(url = url,
                      repertoire_donnees_brutes = "../raw_data/depts")
#> OGR data source with driver: ESRI Shapefile 
#> Source: "C:\Users\pascal.irz\Documents\projets\sources_open_data\package\tod_test\raw_data\depts\departements-20140306-100m.shp", layer: "departements-20140306-100m"
#> with 101 features
#> It has 4 fields

Affichage :

depts %>%
  filter(str_length(code_insee) == 2) %>%  # périmètre métropole
  ggplot() +
    geom_sf()

Pour une projection en Lambert 93 (code EPSG 2154) :

depts %>%
  sf::st_transform(crs = 2154) %>% 
  filter(str_length(code_insee) == 2) %>%  # périmètre métropole
  ggplot() +
    geom_sf()

Communes

Le jeu de données est disponible sur cette page.


url <- "https://www.data.gouv.fr/fr/datasets/r/17062524-991f-4e13-9bf0-b410cc2216fd"
com <- tgeo_osm_adm(url = url,
                    repertoire_donnees_brutes = "../raw_data/com")
#> OGR data source with driver: ESRI Shapefile 
#> Source: "C:\Users\pascal.irz\Documents\projets\sources_open_data\package\tod_test\raw_data\com\communes-20210101.shp", layer: "communes-20210101"
#> with 34969 features
#> It has 4 fields

Affichage de la carte interactive des communes de la Corse-du-Sud, avec mapview :

com %>% 
  filter(str_sub(insee, 1, 2) == "2A") %>% 
  mapview::mapview()

Rattachement des départements aux régions :

corres <- ttab_geoapi_dept_reg()

depts <- depts %>%
  left_join(y = corres,
            by = c('code_insee' = 'code_dept'))

regs <- depts %>%
  filter(str_length(code_insee) == 2) %>% 
  ugeo_aggr(variable_aggregation = "nom_reg")

ggplot(regs) + geom_sf()

L’API Géo

Cette API développée par la mission Etalab propose au téléchargement des données sur le découpage administratif de la France.