Skip to content

Commit

Permalink
Ajout des anciens codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Apr 8, 2019
1 parent 92a6212 commit 0c4e824
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
49 changes: 48 additions & 1 deletion build/cog.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getRangChefLieu(codeCommune, chefsLieuxArrondissement, chefsLieuxDepart
return 0
}

async function extractCommunes(path, arrondissements, departements, regions) {
async function extractCommunes(path, arrondissements, departements, regions, historiqueCommunes) {
const rows = await readCsvFile(path)
const chefsLieuxRegion = regions.map(e => e.chefLieu)
const chefsLieuxDepartement = departements.map(r => r.chefLieu)
Expand Down Expand Up @@ -113,7 +113,54 @@ async function extractCommunes(path, arrondissements, departements, regions) {
}
})

expandWithAnciensCodes(communes, historiqueCommunes)

return communes
}

function expandWithAnciensCodes(communes, historiqueCommunes) {
const historiqueCommunesActuelles = historiqueCommunes
.filter(h => ['COMA', 'COMD', 'COM'].includes(h.type) && !h.dateFin)

const historiqueCommunesActuellesIndex = keyBy(historiqueCommunesActuelles, h => `${h.type}-${h.code}`)

communes.forEach(commune => {
if (commune.type === 'arrondissement-municipal') {
return
}

const key = `${getPrefix(commune)}-${commune.code}`
const entreeHistorique = historiqueCommunesActuellesIndex[key]
const codes = getAllCodes(entreeHistorique)
codes.delete(commune.code)
if (codes.size > 0) {
commune.anciensCodes = [...codes]
}
})
}

function getPrefix(commune) {
if (commune.type === 'commune-actuelle') {
return 'COM'
}

if (commune.type === 'commune-deleguee') {
return 'COMD'
}

if (commune.type === 'commune-associee') {
return 'COMA'
}
}

function getAllCodes(historiqueRecord, acc = new Set()) {
acc.add(historiqueRecord.code)

if (!historiqueRecord.predecesseur) {
return acc
}

return getAllCodes(historiqueRecord.predecesseur, acc)
}

module.exports = {extractDepartements, extractRegions, extractArrondissements, extractCommunes}
6 changes: 3 additions & 3 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function buildArrondissements(arrondissements) {
await writeData('departements', arrondissements)
}

async function buildCommunes(regions, departements, arrondissements, population) {
const data = await extractCommunes(getSourceFilePath('communes-2019.csv.gz'), arrondissements, departements, regions)
async function buildCommunes(regions, departements, arrondissements, population, historiqueCommunes) {
const data = await extractCommunes(getSourceFilePath('communes-2019.csv.gz'), arrondissements, departements, regions, historiqueCommunes)

data.forEach(commune => {
if (['commune-actuelle', 'arrondissement-municipal'].includes(commune.type)) {
Expand Down Expand Up @@ -72,7 +72,7 @@ async function main() {
await buildRegions(regions)
await buildDepartements(departements)
await buildArrondissements(arrondissements)
await buildCommunes(regions, departements, arrondissements, population.communes)
await buildCommunes(regions, departements, arrondissements, population.communes, historiqueCommunes)
await buildHistoriqueCommunes(historiqueCommunes)
await buildEPCI()
}
Expand Down

0 comments on commit 0c4e824

Please sign in to comment.