Mise en forme des données pour l’analyse de la « Typologie des bâtiments résidentiels à l’échelle de la France métropolitaine »

R
géomatique
logement

Visualisation à échelle départementale de la répartition des bâtiments et des logements en zone inondable selon la typologie du GT-AMC

Auteur·rice
Date de publication

28 novembre 2025

Objectif

Cette méthodologie vise à mettre en forme les données afin de servir de base à l’analyse de la typologie des logements en France métropolitaine. Cette mise en forme a pour objectif de proposer une vision de la typologie des logements à échelle nationale par département.

Logiciels et bibliothèques requis

Installation des logiciels et bibliothèques requis

Le code est destiné à être exécuté avec R. Il est donc nécessaire que R soit installé sur votre système.

Ce tutoriel utilise les bibliothèques R suivantes :

  • floodam.data qui permet de réaliser des opérations adaptées aux données de l’observatoire.
  • so.ii qui permet notamment de réaliser des cartes centrées sur le territoire de l’observatoire.

Il est également supposé que vous disposez d’un dossier « data » dans « l’accueil » de votre ordinateur.

Déroulement de la méthode

Avertissement

Il est également nécessaire d’avoir appliqué la méthode de Génération et analyse de la base de données « Typologie des bâtiments résidentiels à l’échelle de la France métropolitaine » avant de continuer.

Cette méthode n’est pas complètement reproductible en l’état pour des personnes extérieures à l’équipe so-ii, en raison des contraintes d’accès aux données de tiers.

Définition du millésime et des chemins

Premièrement il faut définir la version de la BD-Topo® pour laquelle on souhaite effectuer l’analyse et définir les chemins d’accès à vos fichiers et dossiers.

# define millesime
millesime = "2025-06-15"

# set up paths
# origin = tempdir()
path_table = file.path("table", "logement-bd-topo")

Définition des fonctions utilisateur

Nous devons également définir la fonction aggregate_rename_reshape() qui nous permet de mette en forme notre base de données pour analyse.

## aggregate_rename_reshape ()
# - aggregate data by group
# - rename columns
# - reshape data to wide format
aggregate_rename_reshape = function(to_group, group_by, FUN = "sum"){

    replacements = c("_", "eaip_", "dwelling_", "dwelling_eaip_")
    pattern = c("building_all", "building_eaip", "dwelling_all", "dwelling_eaip")

    result = aggregate(to_group, by = group_by, FUN = FUN)

    for (i in seq_along(pattern)) {
      names(result) = gsub(pattern[i], replacements[i], names(result), fixed = TRUE)
    }

    result = reshape(result, idvar="department", timevar="type", direction="wide", sep="", drop = FALSE)

    names(result) = 
        gsub("indefinite", "NC",
            gsub("^_", "",
                names(result)
            )
        )
    
    return(result)
}

Mise en forme de de la base de données

Une fois la configuration terminée, il suffit de charger les données, d’appliquer la fonction aggregate_rename_reshape() et d’enregistrer la base de données mise en forme.

# read and substitue NA and "empty"
dataset = data.table::fread(file.path(path_table, sprintf("national-%s.csv.gz", millesime)), data.table = FALSE)
dataset[["type_2"]][is.na(dataset[["type_2"]]) | dataset[["type_2"]] == ""] = "NC"

# use aggregate_rename_reshape() function
result = aggregate_rename_reshape(
    to_group = dataset[,c("building_all", "building_eaip", "dwelling_all", "dwelling_eaip")], 
    group_by = list(department = dataset[["department"]], type = dataset[["type_2"]])
)

Écriture des modifications apportées à la base de données « Typologie des bâtiments résidentiels à l’échelle de la France métropolitaine ».

# save data
data.table::fwrite(
    result,    
    file.path(path_table, sprintf("bdtopo-dwelling-%s-national.csv.gz", millesime)),
    yaml = TRUE,
)

Sauvegarde des metadata

Il est également possible de venir sauvegarder les metadata concernant la mise en forme de la base de données « Typologie des bâtiments résidentiels pour le territoire de France métropolitaine »“.

# configuration of metadata
metadata_file_name = "metadata_bdtopo-dwelling-fr.yml"
    metadata = c("data: typology of buildings of residential use", 
        paste0("processed_with: floodam.data v", utils::installed.packages()["floodam.data", 
            "Version"]), paste0("vintage: BD-Topo\\uc2ae ", vintage), 
        "data_curator: so.ii", "contact: so-ii@so-ii.org", paste0("citation: \"Observatoire so-ii (", 
            format(Sys.Date(), "%Y"), ") : Analyse du parc immobilier \\uc3a0 destination r\\uc3a9sidentielle du territoire de France m\\uc3a9tropolitaine \\uc3a0 partir de la BD-Topo\\uc2ae, mill\\uc3a9sime ", 
            vintage, "\""), "column_meaning:", "   commune: INSEE code", 
        "   AB: number of buildings with more than 10 dwellings with common areas", 
        "   MFH: number of buildings with less than 10 dwellings with common areas", 
        "   NC: number of unclassified buildings", "   SFH: number of single-dwelling house with no common wall with adjacent houses", 
        "   TH: number of single-dwelling house with one or more common walls with adjacent houses", 
        "   eaip_AB: number of buildings of type AB in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   eaip_MFH: number of buildings of type MFH in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   eaip_NC: number of buildings of type NC in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   eaip_SFH: number of buildings of type SFH in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   eaip_TH: number of buildings of type TH in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   dwelling_AB: number of dwellings by building of type AB", 
        "   dwelling_MFH: number of dwellings by building of type MFH", 
        "   dwelling_NC: number of dwellings by building of type NC", 
        "   dwelling_SFH: number of dwellings by building of type SFH", 
        "   dwelling_TH: number of dwellings by building of type TH", 
        "   dwelling_eaip_AB: number of dwellings in buildings of type AB in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   dwelling_eaip_MFH: number of dwellings in buildings of type MFH in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   dwelling_eaip_NC: number of dwellings in buildings of type NC in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   dwelling_eaip_SFH: number of dwellings in buildings of type SFH in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "   dwelling_eaip_TH: number of dwellings in buildings of type TH in flooding area according to the \\\"Enveloppe Approch\\uc3a9e des inondations Potentielles (Estimated Envelope of Potential Flooding)\\\"", 
        "column_units:", "   commune: no unit | character", "   AB: units (buildings) | integer", 
        "   MFH: units (buildings) | integer", "   NC: units (buildings) | integer", 
        "   SFH: units (buildings) | integer", "   TH: units (buildings) | integer", 
        "   eaip_AB: units (buildings) | integer", "   eaip_MFH: units (buildings) | integer", 
        "   eaip_NC: units (buildings) | integer", "   eaip_SFH: units (buildings) | integer", 
        "   eaip_TH: units (buildings) | integer", "   dwelling_AB: units (dwellings) | integer", 
        "   dwelling_MFH: units (dwellings) | integer", "   dwelling_NC: units (dwellings) | integer", 
        "   dwelling_SFH: units (dwellings) | integer", "   dwelling_TH: units (dwellings) | integer", 
        "   dwelling_eaip_AB: units (dwellings) | integer", "   dwelling_eaip_MFH: units (dwellings) | integer", 
        "   dwelling_eaip_NC: units (dwellings) | integer", "   dwelling_eaip_SFH: units (dwellings) | integer", 
        "   dwelling_eaip_TH: units (dwellings) | integer", paste0("R_info: \"to load this data base in R as data.frame: data.table::fread(\\\"path/to/file/bdtopo-dwelling-", 
            vintage, "-fr.csv\\\", data.table = FALSE, yaml = TRUE)\""))

Écriture des metadata

# save metadata
writeLines(metadata, file.path(path_table, metadata_file_name))
Retour au sommet