forked from osmlab/osm-community-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
272 lines (224 loc) · 8.75 KB
/
build.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* eslint-disable no-console */
const colors = require('colors/safe');
const fs = require('fs');
const glob = require('glob');
const precision = require('geojson-precision');
const rewind = require('geojson-rewind');
const Validator = require('jsonschema').Validator;
const shell = require('shelljs');
const prettyStringify = require('json-stringify-pretty-compact');
const YAML = require('js-yaml');
const geojsonSchema = require('./schema/geojson.json');
const featureSchema = require('./schema/feature.json');
const resourceSchema = require('./schema/resource.json');
let v = new Validator();
v.addSchema(geojsonSchema, 'http://json.schemastore.org/geojson.json');
buildAll();
function buildAll() {
console.log('building data');
console.time(colors.green('data built'));
// Start clean
shell.rm('-f', ['dist/*.json', 'dist/*.js', 'i18n/en.yaml']);
let tstrings = {}; // translation strings
const features = generateFeatures();
const resources = generateResources(tstrings, features);
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.json', prettyStringify({ features: features }) );
fs.writeFileSync('dist/features.min.json', JSON.stringify({ features: features }) );
fs.writeFileSync('dist/resources.json', prettyStringify({ resources: resources }) );
fs.writeFileSync('dist/resources.min.json', JSON.stringify({ resources: resources }) );
fs.writeFileSync('i18n/en.yaml', YAML.safeDump({ en: tstrings }, { lineWidth: -1 }) );
console.timeEnd(colors.green('data built'));
}
function generateFeatures() {
let features = {};
let files = {};
process.stdout.write('Features:');
glob.sync(__dirname + '/features/**/*.geojson').forEach(function(file) {
const contents = fs.readFileSync(file, 'utf8');
let parsed;
try {
parsed = JSON.parse(contents);
} catch (jsonParseError)
{
console.error(colors.red('Error - ' + jsonParseError.message + ' in:'));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
let feature = precision(rewind(parsed, true), 5);
let fc = feature.features;
// A FeatureCollection with a single feature inside (geojson.io likes to make these).
if (feature.type === 'FeatureCollection' && Array.isArray(fc) && fc.length === 1) {
if (feature.id && !fc[0].id) {
fc[0].id = feature.id;
}
feature = fc[0];
}
validateFile(file, feature, featureSchema);
prettifyFile(file, feature, contents);
const id = feature.id;
if (files[id]) {
console.error(colors.red('Error - Duplicate feature id: ') + colors.yellow(id));
console.error(' ' + colors.yellow(files[id]));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
features[id] = feature;
files[id] = file;
process.stdout.write(colors.green('✓'));
});
process.stdout.write(Object.keys(files).length + '\n');
return features;
}
function generateResources(tstrings, features) {
let resources = {};
let files = {};
process.stdout.write('Resources:');
glob.sync(__dirname + '/resources/**/*.json').forEach(function(file) {
let contents = fs.readFileSync(file, 'utf8');
let resource;
try {
resource = JSON.parse(contents);
} catch (jsonParseError) {
console.error(colors.red('Error - ' + jsonParseError.message + ' in:'));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
validateFile(file, resource, resourceSchema);
prettifyFile(file, resource, contents);
let resourceId = resource.id;
if (files[resourceId]) {
console.error(colors.red('Error - Duplicate resource id: ') + colors.yellow(resourceId));
console.error(' ' + colors.yellow(files[resourceId]));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
let featureId = resource.featureId;
if (featureId && !features[featureId]) {
console.error(colors.red('Error - Unknown feature id: ') + colors.yellow(featureId));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
if (!featureId && !/\/resources\/world/.test(file)) {
console.error(colors.red('Error - feature id is required for non-worldwide resource:'));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
resources[resourceId] = resource;
files[resourceId] = file;
// collect translation strings for this resource
tstrings[resourceId] = {
name: resource.name,
description: resource.description
};
if (resource.extendedDescription) {
tstrings[resourceId].extendedDescription = resource.extendedDescription;
}
// Validate event dates and collect strings from upcoming events (where `i18n=true`)
if (resource.events) {
let estrings = {};
for (let i = 0; i < resource.events.length; i++) {
let event = resource.events[i];
// check date
const d = new Date(event.when);
if (isNaN(d.getTime())) {
console.error(colors.red('Error - Bad date: ') + colors.yellow(event.when));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
if (!event.i18n) continue;
if (estrings[event.id]) {
console.error(colors.red('Error - Duplicate event id: ') + colors.yellow(event.id));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
estrings[event.id] = {
name: event.name,
description: event.description,
where: event.where
};
}
if (Object.keys(estrings).length) {
tstrings[resourceId].events = estrings;
}
}
process.stdout.write(colors.green('✓'));
});
process.stdout.write(Object.keys(files).length + '\n');
return resources;
}
// Generate a combined GeoJSON FeatureCollection
// containing all the features w/ resources stored in properties
//
// {
// type: 'FeatureCollection',
// features: [
// {
// type: 'Feature',
// id: 'ghana',
// geometry: { ... },
// properties: {
// 'osm-gh-facebook': { ... },
// 'osm-gh-twitter': { ... },
// 'talk-gh': { ... }
// }
// }, {
// type: 'Feature',
// id: 'madagascar',
// geometry: { ... },
// properties: {
// 'osm-mg-facebook': { ... },
// 'osm-mg-twitter': { ... },
// 'talk-mg': { ... }
// }
// },
// ...
// ]
// }
//
//
function generateCombined(features, resources) {
let keepFeatures = {};
Object.keys(resources).forEach(resourceId => {
const resource = resources[resourceId];
const featureId = resource.featureId;
if (!featureId) return; // note: this will exclude worldwide resources
const origFeature = features[featureId];
if (!origFeature) return;
let keepFeature = keepFeatures[featureId];
if (!keepFeature) {
keepFeature = deepClone(origFeature);
keepFeature.properties = {};
keepFeatures[featureId] = keepFeature;
}
keepFeature.properties[resourceId] = deepClone(resource);
});
return { type: 'FeatureCollection', features: Object.values(keepFeatures) };
}
function validateFile(file, resource, schema) {
const validationErrors = v.validate(resource, schema).errors;
if (validationErrors.length) {
console.error(colors.red('Error - Schema validation:'));
console.error(' ' + colors.yellow(file + ': '));
validationErrors.forEach(function(error) {
if (error.property) {
console.error(' ' + colors.yellow(error.property + ' ' + error.message));
} else {
console.error(' ' + colors.yellow(error));
}
});
process.exit(1);
}
}
function prettifyFile(file, object, contents) {
const pretty = prettyStringify(object);
if (pretty !== contents) {
fs.writeFileSync(file, pretty);
}
}
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}