forked from osmlab/osm-community-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_dist.js
104 lines (86 loc) · 2.78 KB
/
build_dist.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
/* eslint-disable no-console */
const colors = require('colors/safe');
const fs = require('fs');
const locationToFeature = require('./lib/locationToFeature.js');
const prettyStringify = require('json-stringify-pretty-compact');
const shell = require('shelljs');
const features = require('./dist/features.json').features;
const resources = require('./dist/resources.json').resources;
buildAll();
function buildAll() {
const START = '🏗 ' + colors.yellow('Building dist...');
const END = '👍 ' + colors.green('dist built');
console.log('');
console.log(START);
console.time(END);
// Start clean
shell.rm('-f', [
'dist/combined.geojson',
'dist/combined.min.geojson',
'dist/features.min.geojson',
'dist/resources.min.geojson'
]);
const combined = generateCombined(features, resources);
// Save individual data files
fs.writeFileSync('dist/combined.geojson', prettyStringify(combined) );
fs.writeFileSync('dist/combined.min.geojson', JSON.stringify(combined) );
fs.writeFileSync('dist/features.min.json', JSON.stringify({ features: features }) );
fs.writeFileSync('dist/resources.min.json', JSON.stringify({ resources: resources }) );
console.timeEnd(END);
}
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
// Generate a combined GeoJSON FeatureCollection
// containing all the features w/ resources stored in properties
//
// {
// type: 'FeatureCollection',
// features: [
// {
// type: 'Feature',
// id: 'ghana',
// geometry: { ... },
// properties: {
// 'area': 297118.3,
// 'resources': {
// 'osm-gh-facebook': { ... },
// 'osm-gh-twitter': { ... },
// 'talk-gh': { ... }
// }
// }
// }, {
// type: 'Feature',
// id: 'madagascar',
// geometry: { ... },
// properties: {
// 'area': 964945.85,
// 'resources': {
// 'osm-mg-facebook': { ... },
// 'osm-mg-twitter': { ... },
// 'talk-mg': { ... }
// }
// }
// },
// ...
// ]
// }
//
function generateCombined(features, resources) {
let keepFeatures = {};
Object.keys(resources).forEach(resourceId => {
const resource = resources[resourceId];
resource.includeLocations.forEach(location => {
const featureId = location.toString();
let keepFeature = keepFeatures[featureId];
if (!keepFeature) {
const origFeature = locationToFeature(location, features).feature;
keepFeature = deepClone(origFeature);
keepFeature.properties.resources = {};
keepFeatures[featureId] = keepFeature;
}
keepFeature.properties.resources[resourceId] = deepClone(resource);
});
});
return { type: 'FeatureCollection', features: Object.values(keepFeatures) };
}