library(datalibaba)
library(dplyr)
library(lubridate)
library(sf)
library(yaml)
À faire impérativement : configurer ou vérifier les paramètres du fichier config.yml avant chargement.
Les paramètres suivants sont ensuite récupérés :
# Charger le fichier YAML
config <- yaml::read_yaml("../config.yml")
# Récupérer les variables
fichier_gpkg <- config$fichier_gpkg
database <- config$database
schema_name <- config$schema_name
primary_key <- config$primary_key
role <- config$role
emprise <- config$emprise
Récupération du mois et de l’année de l’édition d’ADMIN EXPRESS téléchargée à partir du nom du fichier GPKG :
# Extraire la date après '-ED' dans le nom de fichier
date_part <- sub(".*-ED([0-9]{4}-[0-9]{2}-[0-9]{2})\\.gpkg$", "\\1",
basename(fichier_gpkg))
# Définir la date
date <- lubridate::ymd(date_part)
# Extraire le mois et l'année
edition <- format(date, "%B %Y")
# 1. Lister les couches du GPKG
if (!file.exists(fichier_gpkg)) {
stop("Le fichier GPKG n'existe pas. Vérifiez le chemin.")
}
couches <- sf::st_layers(fichier_gpkg)
if (nrow(couches) == 0) {
stop("Aucune couche trouvée dans le GPKG.")
}
# 2. Initialiser un vecteur pour stocker les noms des dataframes créés
noms_dataframes <- character(length = nrow(couches))
# 3. Boucle pour créer un dataframe par couche
for (i in seq_along(couches$name)) {
table_name <- couches$name[i]
tryCatch({
# Lecture de la couche
df <- sf::st_read(fichier_gpkg, layer = table_name, quiet = TRUE)
# Assignation dynamique du dataframe
assign(table_name, df, envir = .GlobalEnv)
# Stocker le nom du dataframe créé
noms_dataframes[i] <- table_name
# Message de confirmation
cat("Couche '" , table_name, "' chargée dans un dataframe.\n")
}, error = function(e) {
warning(paste("Échec du chargement de la couche", table_name, ":", e$message))
noms_dataframes[i] <- NA # Marquer l'échec
})
}
# 4. Créer un dataframe contenant les noms des dataframes créés
df_noms <- data.frame(
nom_dataframe = noms_dataframes,
couche_source = couches$name, # Nom original de la couche dans le GPKG
statut = ifelse(is.na(noms_dataframes), "Échec", "Succès") # Statut de chargement
)
cleabs et
geometrieUtilisation de la variable name du dataframe
couches :
for (df_name in couches$name) {
tryCatch({
df <- get(df_name, envir = .GlobalEnv)
# Renommer la colonne cleabs en id (ne fait rien si elle n'existe pas déjà)
if ("cleabs" %in% names(df)) {
df <- dplyr::rename(df, id = cleabs)
}
# Renommer la géométrie si besoin
cur_geom <- attr(df, "sf_column") # détecte la colonne de géométrie active
if (!is.null(cur_geom) && cur_geom != "the_geom") {
names(df)[names(df) == cur_geom] <- "the_geom"
sf::st_geometry(df) <- "the_geom"
}
# Remettre à jour la variable dans l'environnement global
assign(df_name, df, envir = .GlobalEnv)
message(sprintf("Renommage terminé pour : %s", df_name))
}, error = function(e) {
message(sprintf("Erreur lors du traitement de %s : %s", df_name, e$message))
})
}
Les tables France entière sont chargées uniquement dans la base
referentiels, sauf la table n_region_exp_000
qui est également publiée dans la base si_eau.
referentiels# Lister toutes les couches avec une géométrie
df_names <- couches$name[sapply(couches$name, function(nom) {
obj <- get(nom, envir = .GlobalEnv)
inherits(obj, "sf")
})]
for (df_name in df_names) {
# Récupérer le dataframe par son nom sous forme d'objet
df <- get(df_name, envir = .GlobalEnv)
# Construire dynamiquement le nom de la table à charger en base
table_name <- paste0("n_", df_name, "_exp_", emprise)
tryCatch({
poster_data(
data = df,
table = table_name,
schema = schema_name,
db = database,
pk = primary_key,
user = role,
overwrite = TRUE
)
message(sprintf("Table publiée avec succès : %s.%s",
schema_name, table_name))
}, error = function(e) {
message(sprintf("Erreur lors de la publication de %s.%s : %s",
schema_name, table_name, e$message))
})
}
si_eaudatalibaba::poster_data(data = region,
table = "n_region_exp_000",
schema = schema_name,
db = "si_eau",
pk = primary_key,
user = role,
overwrite = TRUE)
# Commentaire sur la table
comment_arrondissement <-
paste0("Arrondissements - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_arrondissement,
table = "n_arrondissement_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_arrondissement.R")
datalibaba::post_dico_attr(dico = dico_arrondissement,
table = "n_arrondissement_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_canton <-
paste0("Cantons - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_canton,
table = "n_canton_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_canton.R")
datalibaba::post_dico_attr(dico = dico_canton,
table = "n_canton_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_chef_lieu_d_arrondissement <-
paste0("Chefs-lieux d'arrondissement - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_chef_lieu_d_arrondissement,
table = "n_chef_lieu_arrondissement_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_chef_lieu_d_arrondissement.R")
datalibaba::post_dico_attr(dico = dico_chef_lieu_d_arrondissement,
table = "n_chef_lieu_arrondissement_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_chef_lieu_d_epci <-
paste0("Chefs-lieux d'EPCI - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_chef_lieu_d_epci,
table = "n_chef_lieu_epci_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_chef_lieu_d_epci.R")
datalibaba::post_dico_attr(dico = dico_chef_lieu_d_epci,
table = "n_chef_lieu_epci_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_chef_lieu_de_canton <-
paste0("Chefs-lieux de canton - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_chef_lieu_de_canton,
table = "n_chef_lieu_canton_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_chef_lieu_de_canton.R")
datalibaba::post_dico_attr(dico = dico_chef_lieu_de_canton,
table = "n_chef_lieu_canton_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_chef_lieu_de_commune <-
paste0("Chefs-lieux de commune - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_chef_lieu_de_commune,
table = "n_chef_lieu_commune_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_chef_lieu_de_commune.R")
datalibaba::post_dico_attr(dico = dico_chef_lieu_de_commune,
table = "n_chef_lieu_commune_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_chef_lieu_de_departement <-
paste0("Chefs-lieux du département - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_chef_lieu_de_departement,
table = "n_chef_lieu_departement_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_chef_lieu_de_departement.R")
datalibaba::post_dico_attr(dico = dico_chef_lieu_de_departement,
table = "n_chef_lieu_departement_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_chef_lieu_de_region <-
paste0("Chefs-lieux de région - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_chef_lieu_de_region,
table = "n_chef_lieu_region_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_chef_lieu_de_region.R")
datalibaba::post_dico_attr(dico = dico_chef_lieu_de_region,
table = "n_chef_lieu_region_p_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_commune <-
paste0("Communes - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_commune,
table = "n_commune_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_commune.R")
datalibaba::post_dico_attr(dico = dico_commune,
table = "n_commune_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_commune_associee_ou_deleguee <-
paste0("Communes associées ou déléguées - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_commune_associee_ou_deleguee,
table = "n_commune_associee_ou_deleguee_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_commune_associee_ou_deleguee.R")
datalibaba::post_dico_attr(dico = dico_commune_associee_ou_deleguee,
table = "n_commune_associee_ou_deleguee_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_departement <-
paste0("Départements - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_departement,
table = "n_departement_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_departement.R")
datalibaba::post_dico_attr(dico = dico_departement,
table = "n_departement_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_epci <-
paste0("EPCI - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_epci,
table = "n_epci_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_epci.R")
datalibaba::post_dico_attr(dico = dico_epci,
table = "n_epci_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaire sur la table
comment_region <-
paste0("Régions - France entière (ADMIN-EXPRESS édition ",
edition, ")")
datalibaba::commenter_table(comment_region,
table = "n_region_exp_000",
schema = schema_name,
db = database,
user = role)
# Commentaires sur les champs
source("../data/dico_region.R")
datalibaba::post_dico_attr(dico = dico_region,
table = "n_region_exp_000",
schema = schema_name,
db = database,
user = role)
# Connection à la base de données
connexion <- datalibaba::connect_to_db(db = database, user = role)
query_grant_csd_adl <- sprintf(
"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA %s TO %s", schema_name, role)
DBI::dbExecute(connexion, query_grant_csd_adl)
query_grant_writer_referentiels <- sprintf(
"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA %s TO writer_referentiels",
schema_name)
DBI::dbExecute(connexion, query_grant_writer_referentiels)
query_grant_reader_referentiels <- sprintf(
"GRANT SELECT ON ALL TABLES IN SCHEMA %s TO reader_referentiels",
schema_name)
DBI::dbExecute(connexion, query_grant_reader_referentiels)
# Fermeture de la connection
DBI::dbDisconnect(connexion)