-
Notifications
You must be signed in to change notification settings - Fork 7
/
split-trees.js
90 lines (76 loc) · 2.29 KB
/
split-trees.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
/**
* This script expects full-populated tree data. It should only be run as the final
* step in the pipeline, as it writes files directly instead of to stdout.
*
* Its job is to split a single master data file into two parts:
* - A trimmed-down version of the main file with only the components necessary
* to render the top-level map view
* - A series of single JSON files that contain the full details of a single tree,
* which could be used to render the tree detail view
*/
const parse = require('csv-parse/lib/sync')
const fs = require('fs')
const log = require('./util.js').log
const readFile = require('./util.js').readFile
const mkdir = require('./util.js').mkdir
const stdin = require('./util.js').stdin
function main() {
if (process.argv.length !== 3) {
console.error('Usage: node split-trees.js <output directory>')
process.exit()
}
const rootDirectory = __dirname + '/' + process.argv[2]
const treeDirectory = rootDirectory + '/trees'
const trees = JSON.parse(stdin())
const mapData = [];
mkdir(treeDirectory)
trees.forEach(function(tree) {
mapData.push(getMapData(tree))
writeTree(treeDirectory, tree)
});
fs.writeFileSync(`${rootDirectory}/map.json`, JSON.stringify(mapData, null, 2))
}
function getMapData(tree) {
return copyFields(tree, [
// 'diameter_max_in',
// 'diameter_min_in',
'family_name_botanical',
'family_name_common',
// 'height_max_ft',
// 'height_min_ft',
// 'ipc_rating',
// 'irrigation_requirements',
'iucn_status',
'latitude',
'longitude',
'name_botanical',
'name_common',
'nativity',
// 'shade_production',
'tree_id',
// 'pruning_year',
// 'pruning_zone',
'heritage'
])
}
function writeTree(directory, tree) {
fs.writeFileSync(`${directory}/${tree.tree_id}.json`, JSON.stringify(tree, null, 2))
}
function copyFields(object, fields) {
const output = {}
fields.forEach(function(field) {
if (object[field] !== undefined) {
output[field] = object[field]
}
})
return output
}
function toMap(data, key) {
const map = {}
data.forEach(d => map[d[key]] = d)
return map
}
function getOrDefault(map, key, field, defaultValue) {
return map[key] && map[key][field] ? map[key][field] : defaultValue
}
main()