Génération des données pour l’analyse de la « Typologie des bâtiments résidentiels sur le territoire so-ii »

R
géomatique
logement

Tutoriel pour la Génération des données pour l’analyse de la « Typologie des bâtiments résidentiels sur le territoire so-ii »

Auteur·rice·s
Date de publication

16 septembre 2025

Objectif

Cette méthodologie vise à créer une base de données à partir de la dernière version de la BD-Topo disponible pour le territoire de SO-II, afin de servir de base à l’analyse de la typologie des logements sur ce territoire.

Paramétrage initial

Installation des logiciels et bibliothèques requis

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

Cette méthode utilise la bibliothèque R floodam.data. Les instructions d’installation sont disponibles sur la page web de la bibliothèque.

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

Premièrement il faut charger les bibliothèques supplémentaires nécessaires et à définir les chemins d’accès à vos fichiers et dossiers.

# load libraries
library(floodam.data)
library(sf); sf::sf_use_s2(FALSE)

# set up paths
# origin = tempdir()
path_common = file.path("~", "data", "data-local", "floodam-data")
origin = file.path(path_common, "original", "bd-topo")
destination = file.path(path_common, "adapted", "bd-topo")
path_eaip = file.path(path_common,"adapted","eaip")
path_output = file.path("table", "logement-bd-topo")

Téléchargement des données de la BD-Topo du département 34

Une fois la configuration terminée, nous devons télécharger BD-Topo®. La fonction que nous utilisons télécharge, par défaut, la dernière version de la base de données depuis le site de l’IGN. Nous enregistrons également les informations relatives au millésime , qui seront utilisées ultérieurement pour enregistrer les fichiers.

# download data from department 34
download_info = floodam.data::download_bd_topo(
    origin, 
    department = 34, 
    type = "GPKG"
)
vintage = floodam.data::analyse_archive(download_info[["task"]][2])["vintage"]

Traitement et sauvegarde des données de la BD-Topo du département 34

A partir de la base de données téléchargée, nous extrayons la couche « bâtiment », la formatons et y ajoutons des informations pertinentes : , typologie des bâtiments, etc. (voir les fichiers d’aide des deux fonctions utilisées pour plus de détails)

# process data from department 34
floodam.data::extract_building(
    origin, 
    destination = file.path(destination, vintage, "building"),
    path_eaip = path_eaip
)

dataset = floodam.data::extract_dwelling(
    origin = file.path(destination, vintage, "building"),
    destination = file.path(destination, vintage, "dwelling"),
    archive = sprintf("bdtopo-building-%s-D034.rds", vintage),
    department = 34,
    map = FALSE,
    retrieve = TRUE
)

Extraction des données pour le territoire so-ii

Une fois la base de données du département traitée, on extrait les communes so-ii et on supprime les géométries, car elles ne seront pas utilisées dans la suite de l’analyse. Nous renommons aussi les données NA dans la variable « typologie » en NC (non classé),

# subset so-ii territory in processed data from department 34
dataset = sf::st_drop_geometry(
    dataset[dataset[["commune"]] %in% so.ii::so_ii_collectivity[["commune"]], ]
)
dataset[["commune"]] = droplevels(dataset[["commune"]])

# relabeling NA data in typology variable as NC (not classified)
dataset[["typology"]][is.na(dataset[["typology"]])] = "NC"

Agrégation des données relatives aux bâtiments et logements par commune de so-ii

Ensuite, nous regroupons les données relatives aux bâtiments et logements par commune de so-ii.

# aggregate data by so-ii's commune
building = table(dataset[["commune"]], dataset[["typology"]], useNA = "ifany")
building = data.frame(
    matrix(
        building, 
        nrow = dim(building)[1], 
        dimnames = dimnames(building)
    )
)
building[["commune"]] = rownames(building)

building_eaip = table(
    dataset[["commune"]], 
    dataset[["typology"]], 
    dataset[["eaip"]], 
    useNA = "ifany"
)
building_eaip =  data.frame(
    array(
        building_eaip[, ,"TRUE"], 
        dim = dim(building_eaip)[1:2],
        dimnames = dimnames(building_eaip)[1:2]
    )
)
colnames(building_eaip) = sprintf("eaip_%s", colnames(building_eaip))
building_eaip[["commune"]] = rownames(building_eaip)

dwelling = aggregate(
    dwelling ~ commune + typology, 
    dataset, 
    sum, 
    na.rm = TRUE, 
    drop = FALSE
)
dwelling = reshape(
    dwelling, 
    direction = "wide", 
    timevar = "typology", 
    idvar = "commune",
    v.names = "dwelling", 
    sep = "_"
)

dwelling_eaip = aggregate(
    dwelling ~ commune + typology + eaip, 
    dataset, 
    sum, 
    na.rm = TRUE, 
    drop = FALSE
)
dwelling_eaip = dwelling_eaip[dwelling_eaip[["eaip"]] == TRUE,]
dwelling_eaip[["eaip"]] = NULL
dwelling_eaip = reshape(
    dwelling_eaip, 
    direction = "wide", 
    timevar = "typology", 
    idvar = "commune",
    v.names = "dwelling", 
    sep = "_eaip_"
)

Calcul des pourcentages de bâtiments et de logements dans chaque commune de so-ii

Nous calculons les pourcentages de bâtiments et d’habitations situés dans des zones inondables (selon l’EAIP) par commune de so-ii

# calculate percentages of buildings & dwellings in eaip by so-ii's commune
building = merge(
    floodam.data::commune_so_ii["commune"], 
    building, 
    all.x = TRUE
)
building_eaip = merge(
    floodam.data::commune_so_ii["commune"], 
    building_eaip, 
    all.x = TRUE
)
dwelling = merge(
    floodam.data::commune_so_ii["commune"],
    dwelling, 
    all.x = TRUE
)
dwelling_eaip = merge(
    floodam.data::commune_so_ii["commune"], 
    dwelling_eaip, 
    all.x = TRUE
)

Construction de la base de données finale

Ensuite, nous construisons la base de données finale en fusionnant toutes les pièces intermédiaires et nous enregistrons la base de données.

# compose result database
result = merge(building, building_eaip, all.x = TRUE)
result = merge(result, dwelling, all.x = TRUE)
result = merge(result, dwelling_eaip, all.x = TRUE)
result[["commune"]] = as.character(result[["commune"]])

# save database
data_file_name = "bdtopo-dwelling-so-ii.csv.gz"
data.table::fwrite(
    result,    
    file.path(path_output, data_file_name),
    yaml = TRUE,
)

Génération et sauvegarde de métadonnées

La dernière étape consiste à générer et enregistrer les métadonnées associées à la base de données que nous venons de créer.

# save metadata
metadata_file_name = "metadata_bdtopo-dwelling-so-ii.yml"
metadata = c(
    'data: typology of buildings of residential use',
    paste0("processed_with: floodam.data v", installed.packages()["floodam.data","Version"]),
    paste0("vintage: BD-Topo® ", vintage),
    'data_curator: so.ii',
    'contact: so-ii@so-ii.org',
    paste0('citation: "Observatoire so-ii (', format(Sys.Date(), "%Y"),') : Analyse du parc immobilier à destination résidentielle du territoire so-ii à partir de la BD-Topo®, millésime ', 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ée des inondations Potentielles (Estimated Envelope of Potential Flooding)\\"',
    '   eaip_MFH: number of buildings of type MFH in flooding area according to the \\"Enveloppe Approchée des inondations Potentielles (Estimated Envelope of Potential Flooding)\\"',
    '   eaip_NC: number of buildings of type NC in flooding area according to the \\"Enveloppe Approchée des inondations Potentielles (Estimated Envelope of Potential Flooding)\\"',
    '   eaip_SFH: number of buildings of type SFH in flooding area according to the \\"Enveloppe Approchée des inondations Potentielles (Estimated Envelope of Potential Flooding)\\"',
    '   eaip_TH: number of buildings of type TH in flooding area according to the \\"Enveloppe Approchée 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ée 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ée 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ée 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ée 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ée 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,'-soii.csv\\", data.table = FALSE, yaml = TRUE)"')
)

writeLines(metadata, file.path(path_output, metadata_file_name))
Retour au sommet