public_library_map/process.js

36 lines
1.1 KiB
JavaScript

import csv from 'csv-parser'
import fs from 'fs'
const data_directory = '/srv/public_library_map/website/data'
// read json file into memory
const mapData = fs.readFileSync(`${data_directory}/boundaries.topo.json`)
let data = JSON.parse(mapData)
let geometries = data.objects['boundaries.geo'].geometries
// read csv file
// 'data' should be a single line (excl headers)
fs.createReadStream(`${data_directory}/library_services_information.csv`)
.pipe(csv())
.on('data', (data) => {
for (let feature in geometries) {
if (geometries[feature].properties.name == data.name) {
for (let k in data) {
if (k != "long_name" && k != "") {
geometries[feature].properties[k] = data[k]
}
}
}
}
})
.on('end', () => {
// write back to object
data.objects['boundaries.geo'].geometries = geometries
// write back to file
try {
fs.writeFileSync(`${data_directory}/boundaries.topo.json`, JSON.stringify(data))
} catch (err) {
console.err('ERROR writing topo file')
}
console.log('topo updated')
})