Skip to content

Commit

Permalink
Add prepare-localisants script
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Sep 15, 2020
1 parent 42d4aae commit 3b1d2db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
40 changes: 40 additions & 0 deletions bin/prepare-localisants.js
@@ -0,0 +1,40 @@
#!/usr/bin/env node
require('dotenv').config()
const {join} = require('path')
const {promisify} = require('util')
const zlib = require('zlib')
const {groupBy} = require('lodash')
const {remove} = require('fs-extra')
const Keyv = require('keyv')
const {getRemoteFeatures} = require('../lib/cadastre')

const gzip = promisify(zlib.gzip)

const dbPath = join(__dirname, '..', 'localisants.sqlite')
const millesimeLocalisants = process.env.LOCALISANTS_MILLESIME
const departements = process.env.LOCALISANTS_DEPARTEMENTS.split(',')

function getDepartementFileUrl(codeDepartement) {
return `https://cadastre.data.gouv.fr/data/ign-localisants/${millesimeLocalisants}/geojson/localisants-${codeDepartement}.geojson.gz`
}

async function main() {
await remove(dbPath)
const db = new Keyv(`sqlite://${dbPath}`)

for (const codeDepartement of departements) {
console.log(`Chargement du département ${codeDepartement}`)
const localisants = await getRemoteFeatures(getDepartementFileUrl(codeDepartement))
const localisantsCommunes = groupBy(localisants, l => l.properties.IDU.slice(0, 5))

await Promise.all(Object.keys(localisantsCommunes).map(async codeCommune => {
const data = await gzip(Buffer.from(JSON.stringify(localisantsCommunes[codeCommune])))
await db.set(codeCommune, data)
}))
}
}

main().catch(error => {
console.error(error)
process.exit(1)
})
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -14,7 +14,8 @@
"lint": "xo",
"test-unit": "ava",
"test": "yarn lint && yarn test-unit",
"build": "./bin/build.js"
"build": "./bin/build.js",
"prepare-localisants": "node --max-old-space-size=2048 bin/prepare-localisants"
},
"dependencies": {
"@etalab/decoupage-administratif": "^0.8.0",
Expand Down Expand Up @@ -50,7 +51,8 @@
"semicolon": false,
"space": 2,
"rules": {
"unicorn/no-fn-reference-in-iterator": "off"
"unicorn/no-fn-reference-in-iterator": "off",
"no-await-in-loop": "off"
}
},
"engines": {
Expand Down

0 comments on commit 3b1d2db

Please sign in to comment.