Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict'
var topojson = require('topojson');
var fs = require('fs');
var path = require('path');
var percentile = require('stats-percentile');
var topojsonFilename = path.join(__dirname, 'municipality.topojson');
var csvFilePath = path.join(__dirname, '20161021111834699019927InntektStruk15-utf8.csv');
var outputFile = path.join(__dirname, 'municipality-enriched-2014.topojson');
// Global array to calculate percentile.
var allValues = [];
function loadCsvFile (filenameCsv) {
return new Promise((resolve, reject) => {
var municipalityData = {};
fs.readFile(filenameCsv, 'utf8', (err, data) => {
var lines = data.split('\n');
lines.forEach(line => {
var columns = line.split(';');
var municipality = columns[0];
var totalEmployed = parseInt(columns[1], 10);
allValues.push(totalEmployed);
// Remove additional info and artifacts.
municipality = municipality.replace(/"/g, '');
municipality = municipality.replace(/ - /g, ' ');
municipality = municipality.replace(/\s+/g, ' ');
municipality = municipality.replace(/\s+\(.+\)/g, '');
municipality = municipality.replace(/\s+kommune/gi, '');
if (typeof municipalityData[municipality] === 'undefined') {
municipalityData[municipality] = {};
}
municipalityData[municipality]['totalEmployed'] = totalEmployed;
});
resolve(municipalityData);
});
});
}
function loadTopojsonFile (topojsonFilename) {
return new Promise((resolve, reject) => {
fs.readFile(topojsonFilename, 'utf8', function (err, data) {
if (err) {
return reject(err);
}
try {
var norge = JSON.parse(data);
} catch (error) {
return reject(error);
}
resolve(norge);
});
});
}
function enrichTopojson(dataSets) {
return new Promise((resolve, reject) => {
var csvData = dataSets[0];
var topojsonData = dataSets[1];
var percentile95th = percentile.calc(allValues, 95);
console.log('95th percentile employed in a municipality:', percentile95th);
var kommuner = topojson.feature(topojsonData, topojsonData.objects.kommuner);
kommuner.features.forEach((municipalityObject, i) => {
var municipality = municipalityObject.properties.navn;
// Remove unwanted chars.
municipality = municipality.replace(/[^a-zæøåá\- ]+/gi, '');
// Truncate whitespaces.
municipality = municipality.replace(/\s+/g, ' ');
if (typeof csvData[municipality] === 'object') {
municipalityObject.properties.employed = csvData[municipality].totalEmployed;
// Format the maps
var fillColor = '#0000ff';
municipalityObject.properties.fill = fillColor;
municipalityObject.properties.stroke = '#000000';
var diffFromMax = Math.abs(percentile95th - csvData[municipality].totalEmployed);
municipalityObject.properties['fill-opacity'] = calcOpacity(diffFromMax / percentile95th);
// console.log(municipality, 'opacity:', municipalityObject.properties['fill-opacity']);
municipalityObject.properties['stroke-opacity'] = .2;
municipalityObject.properties['stroke-width'] = 1;
} else {
console.log('NOT FOUND!', municipality);
}
});
resolve(topojsonData);
});
}
function writeTopojson(topojsonData, topojsonFilename) {
return new Promise((resolve, reject) => {
var wstream = fs.createWriteStream(topojsonFilename);
wstream.write(JSON.stringify(topojsonData));
wstream.end();
resolve('File saved as: ' + topojsonFilename);
});
}
function calcOpacity (value) {
if (value > 1) {
value = 0;
}
value = 1 - value;
if (value > .9) {
value = .9;
}
return value;
}
Promise.all([loadCsvFile(csvFilePath), loadTopojsonFile(topojsonFilename)])
.then(enrichTopojson)
.then(topojsonData => {
return writeTopojson(topojsonData, outputFile);
})
.then(console.log)
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.