# load libraries
library(floodam.data)
library(sf); sf::sf_use_s2(FALSE)
# set up paths
# origin = tempdir()
= file.path("~", "data", "data-local", "floodam-data")
path_common = file.path(path_common, "original", "bd-topo")
origin = file.path(path_common, "adapted", "bd-topo")
destination = file.path(path_common,"adapted","eaip")
path_eaip = file.path("table", "logement-bd-topo") path_output
Génération des données pour l’analyse de la « Typologie des bâtiments résidentiels sur le territoire so-ii »
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 »
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
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.
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
= floodam.data::download_bd_topo(
download_info
origin, department = 34,
type = "GPKG"
)= floodam.data::analyse_archive(download_info[["task"]][2])["vintage"] 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
::extract_building(
floodam.data
origin, destination = file.path(destination, vintage, "building"),
path_eaip = path_eaip
)
= floodam.data::extract_dwelling(
dataset 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
= sf::st_drop_geometry(
dataset "commune"]] %in% so.ii::so_ii_collectivity[["commune"]], ]
dataset[dataset[[
)"commune"]] = droplevels(dataset[["commune"]])
dataset[[
# relabeling NA data in typology variable as NC (not classified)
"typology"]][is.na(dataset[["typology"]])] = "NC" dataset[[
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
= table(dataset[["commune"]], dataset[["typology"]], useNA = "ifany")
building = data.frame(
building matrix(
building, nrow = dim(building)[1],
dimnames = dimnames(building)
)
)"commune"]] = rownames(building)
building[[
= table(
building_eaip "commune"]],
dataset[["typology"]],
dataset[["eaip"]],
dataset[[useNA = "ifany"
)= data.frame(
building_eaip array(
"TRUE"],
building_eaip[, ,dim = dim(building_eaip)[1:2],
dimnames = dimnames(building_eaip)[1:2]
)
)colnames(building_eaip) = sprintf("eaip_%s", colnames(building_eaip))
"commune"]] = rownames(building_eaip)
building_eaip[[
= aggregate(
dwelling ~ commune + typology,
dwelling
dataset,
sum, na.rm = TRUE,
drop = FALSE
)= reshape(
dwelling
dwelling, direction = "wide",
timevar = "typology",
idvar = "commune",
v.names = "dwelling",
sep = "_"
)
= aggregate(
dwelling_eaip ~ commune + typology + eaip,
dwelling
dataset,
sum, na.rm = TRUE,
drop = FALSE
)= dwelling_eaip[dwelling_eaip[["eaip"]] == TRUE,]
dwelling_eaip "eaip"]] = NULL
dwelling_eaip[[= reshape(
dwelling_eaip
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
= merge(
building ::commune_so_ii["commune"],
floodam.data
building, all.x = TRUE
)= merge(
building_eaip ::commune_so_ii["commune"],
floodam.data
building_eaip, all.x = TRUE
)= merge(
dwelling ::commune_so_ii["commune"],
floodam.data
dwelling, all.x = TRUE
)= merge(
dwelling_eaip ::commune_so_ii["commune"],
floodam.data
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
= 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"]])
result[[
# save database
= "bdtopo-dwelling-so-ii.csv.gz"
data_file_name ::fwrite(
data.table
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_bdtopo-dwelling-so-ii.yml"
metadata_file_name = c(
metadata '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))