forked from osmlab/osm-community-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_dist.js
100 lines (83 loc) · 2.68 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
const colors = require('colors/safe');
const fs = require('fs');
const LocationConflation = require('@ideditor/location-conflation');
const prettyStringify = require('json-stringify-pretty-compact');
const shell = require('shelljs');
const featureCollection = require('./dist/features.json');
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.json',
'dist/resources.min.json'
]);
const combined = generateCombined(resources, featureCollection);
// 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(featureCollection) );
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: 'Q117',
// geometry: { ... },
// properties: {
// 'area': 297118.3,
// 'resources': {
// 'osm-gh-facebook': { ... },
// 'osm-gh-twitter': { ... },
// 'talk-gh': { ... }
// }
// }
// }, {
// type: 'Feature',
// id: 'Q1019',
// geometry: { ... },
// properties: {
// 'area': 964945.85,
// 'resources': {
// 'osm-mg-facebook': { ... },
// 'osm-mg-twitter': { ... },
// 'talk-mg': { ... }
// }
// }
// },
// ...
// ]
// }
//
function generateCombined(resources, featureCollection) {
let keepFeatures = {};
const loco = new LocationConflation(featureCollection);
Object.keys(resources).forEach(resourceId => {
const resource = resources[resourceId];
const feature = loco.resolveLocationSet(resource.locationSet);
let keepFeature = keepFeatures[feature.id];
if (!keepFeature) {
keepFeature = deepClone(feature);
keepFeature.properties.resources = {};
keepFeatures[feature.id] = keepFeature;
}
keepFeature.properties.resources[resourceId] = deepClone(resource);
});
return { type: 'FeatureCollection', features: Object.values(keepFeatures) };
}