-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojson-to-csv.js
executable file
·110 lines (96 loc) · 2.95 KB
/
geojson-to-csv.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const fs = require('fs');
const path = require('path');
const topojson = require('topojson');
const {csvParse, mean} = require('d3');
const {createArrayCsvWriter} = require('csv-writer');
const {
YEAR_RANGE: [START_YEAR],
FAULTY_DISTRICTS,
FAULTY_YEARS,
} = require('./constants');
const SOURCE_FOLDER = './gemeinden-years-topo';
const DEST_FOLDER = './gemeinden-data';
const ID_KEY = 'RS';
if (!fs.existsSync(DEST_FOLDER)) {
fs.mkdirSync(DEST_FOLDER);
}
// Read all files in directory and extract data to be written to csv
const filenames = fs.readdirSync(SOURCE_FOLDER);
const header = [
'id',
'name',
'zone',
'min',
'max',
'minYear',
'maxYear',
'mean',
...Array.from(Array(filenames.length).keys()).map((i) => i + START_YEAR),
];
const districts = {};
const getValue = (props, key) => {
return props[key] !== null ? Math.round(props[key] * 10) / 100 : null;
};
const parseFeature = (feature, year) => {
const {properties} = feature;
const id = properties[ID_KEY];
const isFaultyDistrict = FAULTY_DISTRICTS.indexOf(id) >= 0;
const isFaultyYear = year - START_YEAR < FAULTY_YEARS;
const isFaulty = isFaultyDistrict && isFaultyYear;
const value = isFaulty ? null : getValue(properties, '_mean');
if (!districts[id]) {
districts[id] = {
id,
name: properties.GEN,
values: [value],
min: value === null ? Infinity : value,
max: value === null ? -Infinity : value,
minYear: year,
maxYear: year,
};
} else {
const district = districts[id];
const newMin = value === null ? false : value < district.min;
const newMax = value === null ? false : value > district.max;
district.values.push(value);
district.min = newMin ? value : district.min;
district.max = newMax ? value : district.max;
district.minYear = newMin ? year : district.minYear;
district.maxYear = newMax ? year : district.maxYear;
}
};
const writeCsv = (header, data) => {
const fileName = `gemeinden-temperature.csv`;
const folderName = './';
const formatted = data.map((d) => [
d.id,
d.name,
d.zone,
parseFloat(d.min).toFixed(1),
parseFloat(d.max).toFixed(1),
d.minYear,
d.maxYear,
parseFloat(mean(d.values)).toFixed(1),
...d.values,
]);
createArrayCsvWriter({
path: path.join(DEST_FOLDER, fileName),
header: header,
})
.writeRecords(formatted)
.then(() => console.log(`Wrote ${fileName} to ${folderName}`));
};
filenames.forEach((filename, i) => {
console.log(`Processing ${filename}`);
const raw = fs.readFileSync(`./${SOURCE_FOLDER}/${filename}`, 'utf-8');
const parsed = JSON.parse(raw);
const geojson = topojson.feature(parsed, parsed.objects['districts']);
geojson.features.forEach((f, j) => {
parseFeature(f, i + START_YEAR);
});
});
const sortedDistricts = Object.keys(districts)
.map((key) => districts[key])
.filter((d) => d.values.some((v) => v !== null))
.sort((a, b) => b.lat - a.lat);
writeCsv(header, sortedDistricts);