diff --git a/improve-csv.js b/improve-csv.js index 68f980a..f06e3ae 100644 --- a/improve-csv.js +++ b/improve-csv.js @@ -136,7 +136,12 @@ async function main() { const parcelles = await getParcellesCommune(codeCommune) communeRows.forEach(row => { - if (parcelles && row.id_parcelle in parcelles) { + if (row.ancien_id_parcelle in parcelles) { + row.id_parcelle = row.ancien_id_parcelle + row.ancien_id_parcelle = undefined + } + + if (row.id_parcelle in parcelles) { const parcelle = parcelles[row.id_parcelle] const [lon, lat] = truncate(centroid(parcelle), {precision: 6}).geometry.coordinates row.longitude = lon diff --git a/lib/parcelles.js b/lib/parcelles.js index cda8704..6693f23 100644 --- a/lib/parcelles.js +++ b/lib/parcelles.js @@ -3,22 +3,29 @@ const {join} = require('path') const gunzip = promisify(require('zlib').gunzip) const {readFile, pathExists} = require('fs-extra') const {keyBy} = require('lodash') -const {getCodeDepartement} = require('./recog') +const {getCodeDepartement, getAllCodesCommunes} = require('./recog') const communesPath = process.env.CADASTRE_COMMUNES_PATH async function getParcellesCommune(codeCommune) { - const filePath = join(communesPath, getCodeDepartement(codeCommune), codeCommune, `cadastre-${codeCommune}-parcelles.json.gz`) + const codesCommunes = getAllCodesCommunes(codeCommune) + const parcelles = [] - if (!(await pathExists(filePath))) { - return - } + await Promise.all(codesCommunes.map(async code => { + const filePath = join(communesPath, getCodeDepartement(code), code, `cadastre-${code}-parcelles.json.gz`) - const gzippedFileContent = await readFile(filePath) - const fileContent = await gunzip(gzippedFileContent) - const featureCollection = JSON.parse(fileContent) + if (!(await pathExists(filePath))) { + return + } - return keyBy(featureCollection.features, 'properties.id') + const gzippedFileContent = await readFile(filePath) + const fileContent = await gunzip(gzippedFileContent) + const featureCollection = JSON.parse(fileContent) + + parcelles.push(...featureCollection.features) + })) + + return keyBy(parcelles, 'properties.id') } module.exports = {getParcellesCommune} diff --git a/lib/recog.js b/lib/recog.js index 0ccf6b6..d262d03 100644 --- a/lib/recog.js +++ b/lib/recog.js @@ -127,4 +127,9 @@ function getCommuneActuelle(communeEntry, dateValeur) { } } -module.exports = {getCodeDepartement, getCommune, isValidAt, getCommuneActuelle, getCommuneFromCadastre} +function getAllCodesCommunes(codeCommune) { + const commune = getCommune(codeCommune) + return getCodesMembres(commune) +} + +module.exports = {getAllCodesCommunes, getCodeDepartement, getCommune, isValidAt, getCommuneActuelle, getCommuneFromCadastre}