This repository was archived by the owner on Jun 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhistory.js
More file actions
79 lines (69 loc) · 2.43 KB
/
Copy pathhistory.js
File metadata and controls
79 lines (69 loc) · 2.43 KB
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
'use strict';
const _ = require('lodash');
const tilebelt = require('tilebelt');
const path = require('path');
const JSONStream = require('JSONStream');
const fs = require('fs');
class NullHistoryStore {
end() {}
store() {}
}
// Stores history of tiles in files grouped by parent tile index
// this is used to generate JSON files that can be hosted
// on GitHub pages
class HistoryStore {
constructor(directory, storeZoomLevel) {
this.directory = path.normalize(directory);
this.storeZoomLevel = storeZoomLevel;
this.tileFiles = {};
this.idxDimension = Math.pow(2, this.storeZoomLevel);
this.zoomDirectory = path.join(this.directory, this.storeZoomLevel.toString());
fs.mkdirSync(this.directory);
fs.mkdirSync(this.zoomDirectory);
// The size of the x and y dimensions
_.range(0, this.idxDimension).forEach(x => {
this.tileFiles[x] = {};
fs.mkdirSync(path.join(this.zoomDirectory, x.toString()));
_.range(0, this.idxDimension).forEach(y => {
const fileStream = fs.createWriteStream(path.join(this.zoomDirectory, x.toString(), y.toString() + ".json"));
const jsonStream = JSONStream.stringifyObject();
jsonStream.pipe(fileStream);
this.tileFiles[x][y] = jsonStream;
});
});
JSONStream.stringify()
}
// Close all tile streams
end() {
_.range(0, this.idxDimension).forEach(x => {
_.range(0, this.idxDimension).forEach(y => {
this.tileFiles[x][y].end();
});
});
}
findAncestor(tile) {
let parent = tilebelt.getParent(tile);
if (parent[2] <= this.storeZoomLevel) {
return parent;
} else {
return this.findAncestor(parent);
}
}
// Store a feature history in the appropriate JSON file
// of the parent
store(feature) {
const props = feature.properties;
const parentTile = this.findAncestor(props.tile);
const x = parentTile[0];
const y = parentTile[1];
const stream = this.tileFiles[x][y];
//NOTE: Probably as fast as a multidimensional dict since JavaScript turns integer keys
//into strings anyway
const tileIdx = props.tile.join('/');
stream.write([tileIdx, props.months]);
}
}
module.exports = {
HistoryStore: HistoryStore,
NullHistoryStore, NullHistoryStore
}