Skip to content

Commit

Permalink
Merge pull request #35 from etalab/com-dep-reg-and-flatten
Browse files Browse the repository at this point in the history
Manage COM as dept and region, flatten key collectiviteOutremer
  • Loading branch information
ThomasG77 committed Jun 22, 2022
2 parents 19b9451 + 315ab53 commit 1fa722d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
36 changes: 33 additions & 3 deletions build/cog.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ async function extractDepartements(path) {
const rows = await readCsvFile(path)

return rows.map(row => {
let zone
if (row.ZONE) {
zone = row.ZONE
} else if (row.DEP.length > 2) {
zone = 'drom'
} else {
zone = 'metro'
}

return {
code: row.DEP,
region: row.REG,
chefLieu: row.CHEFLIEU,
nom: row.LIBELLE,
typeLiaison: parseTypeLiaison(row.TNCC)
typeLiaison: parseTypeLiaison(row.TNCC),
zone
}
})
}
Expand All @@ -23,11 +33,21 @@ async function extractRegions(path) {
const rows = await readCsvFile(path)

return rows.map(row => {
let zone
if (row.ZONE) {
zone = row.ZONE
} else if (['01', '02', '03', '04', '06'].includes(row.REG)) {
zone = 'drom'
} else {
zone = 'metro'
}

return {
code: row.REG,
chefLieu: row.CHEFLIEU,
nom: row.LIBELLE,
typeLiaison: parseTypeLiaison(row.TNCC)
typeLiaison: parseTypeLiaison(row.TNCC),
zone
}
})
}
Expand Down Expand Up @@ -116,10 +136,20 @@ async function extractCommunes(communesPath, mouvementsCommunesPath, arrondissem
const anciensCodesIndex = computeAnciensCodesCommunes(communesRows, mouvementsRows)

const communes = communesRows.map(row => {
let zone
if (row.zone) {
zone = row.zone
} else if (row.DEP.length > 2) {
zone = 'drom'
} else {
zone = 'metro'
}

const commune = {
code: row.COM,
nom: row.LIBELLE,
typeLiaison: parseTypeLiaison(row.TNCC)
typeLiaison: parseTypeLiaison(row.TNCC),
zone
}

if (row.TYPECOM === 'COM') {
Expand Down
44 changes: 39 additions & 5 deletions build/collectivites-outremer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
const {readCsvFile} = require('./util')

const chefLieuCOM = {
'975': '97502',
'977': '97701',
'978': '97801',
'984': '97502',
'986': '98613',
'987': '98735',
'988': '98818',
'989': '98901'
}

async function extractCommunesCOM(path) {
const rows = await readCsvFile(path)

return rows.map(row => {
const commune = {
code: row.code_commune,
nom: row.nom_commune,
collectiviteOutremer: {
code: row.code_collectivite,
nom: row.nom_collectivite
},
departement: row.code_collectivite,
region: row.code_collectivite,
zone: 'com',
type: 'commune-actuelle'
}

Expand All @@ -30,4 +40,28 @@ async function extractCommunesCOM(path) {
})
}

module.exports = {extractCommunesCOM}
async function generateDepartementsAndRegionsCOM(path) {
const rows = await readCsvFile(path)

const departements = Object.values(rows.reduce((acc, curr) => {
if (!(curr.code_collectivite in acc)) {
acc[curr.code_collectivite] = {
code: curr.code_collectivite,
region: curr.code_collectivite,
chefLieu: chefLieuCOM[curr.code_collectivite],
nom: curr.nom_collectivite,
typeLiaison: 0,
zone: 'com'
}
}
return acc
}, {}))

const regions = JSON.parse(JSON.stringify(departements)).map(el => {
delete el.region
return el
});
return [departements, regions]
}

module.exports = {extractCommunesCOM, generateDepartementsAndRegionsCOM}
9 changes: 6 additions & 3 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {extractEPCI} = require('./epci')
const {extractPopulation, computeMLPPopulation} = require('./population')
const {getCodesPostaux, computeMLPCodesPostaux} = require('./codes-postaux')
const {MLP_CODES} = require('./mlp')
const {extractCommunesCOM} = require('./collectivites-outremer')
const {extractCommunesCOM, generateDepartementsAndRegionsCOM} = require('./collectivites-outremer')
const {extractDepartements, extractRegions, extractArrondissements, extractCommunes} = require('./cog')
const {writeData, getSourceFilePath} = require('./util')

Expand Down Expand Up @@ -70,8 +70,11 @@ async function main() {
communes: {...populationHorsMayotte.communes, ...populationMayotte.communes}
}
const arrondissements = await extractArrondissements(getSourceFilePath('arrondissements.csv'))
const departements = await extractDepartements(getSourceFilePath('departements.csv'))
const regions = await extractRegions(getSourceFilePath('regions.csv'))
const departementsMetroAndDrom = await extractDepartements(getSourceFilePath('departements.csv'))
const [departementsCom, regionsCom] = await generateDepartementsAndRegionsCOM(getSourceFilePath('collectivites-outremer.csv'))
const departements = [...departementsMetroAndDrom, ...departementsCom]
const regionsMetroAndDrom = await extractRegions(getSourceFilePath('regions.csv'))
const regions = [...regionsMetroAndDrom, ...regionsCom]

await buildRegions(regions)
await buildDepartements(departements)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etalab/decoupage-administratif",
"version": "2.0.0",
"version": "2.1.0",
"description": "API JavaScript permettant d'interroger le découpage administratif français",
"main": ".",
"repository": "https://github.com/etalab/decoupage-administratif",
Expand Down

0 comments on commit 1fa722d

Please sign in to comment.