-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-district-color.js
executable file
·51 lines (45 loc) · 1.37 KB
/
get-district-color.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Script used by `create-warming-maps.sh` to get a color value given a year and a geojson feature
* I probably could have just written everything as a node script but I just had too much fun creating
* maps using commandline tools...
*/
const fs = require('fs');
const d3 = require('d3');
const {csvParse, mean} = require('d3');
const path = require('path');
const {
FIRST_REF_YEAR,
LAST_REF_YEAR,
SCALE_DEVIATION,
WARMING_COLORS,
FAULTY_COLOR
} = require('./constants');
const scale = d3.scaleQuantize().range(WARMING_COLORS);
const districts = csvParse(
fs.readFileSync(
path.resolve(__dirname, `./gemeinden-data/gemeinden-temperature.csv`),
'utf-8',
),
).map((d, i) => {
const years = Object.keys(d)
.map((key) => parseInt(key))
.filter((key) => !isNaN(key))
.map((key) => parseFloat(d[key]));
const refYears = years.slice(FIRST_REF_YEAR, LAST_REF_YEAR);
return {
years,
id: d.id,
name: d.name,
mean: mean(refYears),
};
});
module.exports = (f, i) => {
const d = districts.find((d) => d.id === f.properties.RS);
// Because of very weird bug we have to check every time if d exists. Not sure what causes this
const value = d ? d.years[i] : 0;
const domain = d
? [d.mean - SCALE_DEVIATION, d.mean + SCALE_DEVIATION]
: [0, 1];
const color = isNaN(value) ? FAULTY_COLOR : scale.domain(domain)(value);
return color;
};