diff --git a/lib/distribute/writers/csv-bal-with-lang.cjs b/lib/distribute/writers/csv-bal-with-lang.cjs new file mode 100644 index 00000000..cbd69660 --- /dev/null +++ b/lib/distribute/writers/csv-bal-with-lang.cjs @@ -0,0 +1,49 @@ +/* eslint camelcase: off */ +const {promisify} = require('util') +const {join} = require('path') +const {createWriteStream} = require('fs') +const {createGzip} = require('zlib') +const finished = promisify(require('stream').finished) +const {ensureDir} = require('fs-extra') +const csvWriter = require('csv-write-stream') +const pumpify = require('pumpify') + +const {prepareAdresses} = require('../../formatters/csv-bal.cjs') + +function createCsvWriteStream(file) { + return pumpify.obj( + csvWriter({separator: ';'}), + createGzip(), + file + ) +} + +function waitForDrain(stream) { + if (stream.writableLength > stream.writableHighWaterMark) { + return new Promise(resolve => { + stream.once('drain', resolve) + }) + } +} + +async function createWriter(outputPath, departement) { + await ensureDir(outputPath) + const adressesFile = createWriteStream(join(outputPath, `adresses-${departement}.csv.gz`)) + const adressesStream = createCsvWriteStream(adressesFile) + + return { + async writeAdresses({voies, numeros}) { + prepareAdresses({voies, numeros}, {includesAlt: true}) + .forEach(a => adressesStream.write(a)) + + await waitForDrain(adressesStream) + }, + + async finish() { + adressesStream.end() + await finished(adressesFile) + } + } +} + +module.exports = {createWriter} diff --git a/lib/formatters/csv-bal.cjs b/lib/formatters/csv-bal.cjs index 902b2d5e..80053386 100644 --- a/lib/formatters/csv-bal.cjs +++ b/lib/formatters/csv-bal.cjs @@ -1,5 +1,6 @@ /* eslint camelcase: off */ const {groupBy} = require('lodash') +const languesRegionales = require('@ban-team/shared-data/langues-regionales.json'); const {harmlessProj} = require('../util/geo.cjs') const {idsIdentifier} = require('../util/digest-ids-from-bal-uids.cjs') @@ -83,17 +84,11 @@ function buildRow(voie, numero, position, {includesAlt = false, version = '1.3'} date_der_maj: numero.dateMAJ || '', certification_commune: numero.certifie ? '1' : '0' } - if (includesAlt) { - if (voie.nomVoieAlt) { - Object.keys(voie.nomVoieAlt).forEach(o => { - row['voie_nom_' + o] = voie.nomVoieAlt[o] - }) - } - if (numero.lieuDitComplementNomAlt) { - Object.keys(numero.lieuDitComplementNomAlt).forEach(o => { - row['lieudit_complement_nom_' + o] = numero.lieuDitComplementNomAlt[o] - }) + if (includesAlt) { + for (const {code} of languesRegionales) { + row['voie_nom_' + code] = voie.nomVoieAlt && voie.nomVoieAlt[code] + row['lieudit_complement_nom_' + code] = numero.lieuDitComplementNomAlt && numero.lieuDitComplementNomAlt[code] } } diff --git a/package.json b/package.json index 175d0773..c16a996c 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "dependencies": { "@ban-team/fantoir": "^0.15.0", "@ban-team/gazetteer": "^2.0.0", + "@ban-team/shared-data": "^1.2.0", "@ban-team/validateur-bal": "^2.17.0", "@etalab/adresses-util": "^0.8.2", "@etalab/decoupage-administratif": "^3.0.0",