Skip to content

Commit

Permalink
feat: export csv bal with lang
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Apr 29, 2024
1 parent abb2fc4 commit af557f6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
49 changes: 49 additions & 0 deletions lib/distribute/writers/csv-bal-with-lang.cjs
Original file line number Diff line number Diff line change
@@ -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}
15 changes: 5 additions & 10 deletions lib/formatters/csv-bal.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint camelcase: off */
const {groupBy} = require('lodash')
const languesRegionales = require('@ban-team/shared-data/langues-regionales.json');

Check failure on line 3 in lib/formatters/csv-bal.cjs

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon.
const {harmlessProj} = require('../util/geo.cjs')
const {idsIdentifier} = require('../util/digest-ids-from-bal-uids.cjs')

Expand Down Expand Up @@ -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]
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit af557f6

Please sign in to comment.