-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrenderAll.js
80 lines (63 loc) · 2.19 KB
/
renderAll.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
'use strict';
const buildConfig = require('./lib/buildConfig')();
const fs = require('fs');
const path = require('path');
const mapnik = require('mapnik');
const execSync = require('child_process').execSync;
const TILESIZE = 512;
const merc = new mapnik.Projection('+init=epsg:3857');
// register fonts and datasource plugins
mapnik.register_default_fonts();
mapnik.register_default_input_plugins();
// Add some padding around a highlighted feature
// extent coords are in the form [minx, miny, maxx, maxy]
// or, roughly, (lower-left, top-right)
const padExtent = function(extent) {
const xdiff = extent[2] - extent[0];
const ydiff = extent[3] - extent[1];
return [
extent[0] - xdiff * 0.1,
extent[1] - ydiff * 0.1,
extent[2] + xdiff * 0.1,
extent[3] + ydiff * 0.1
];
};
const renderTile = function(tile) {
// skip rendering tiles without an OCDID, as they won't be visible
// in the product and this avoids rendering ~10K tiles
if (tile.ocdid === undefined) {
return;
}
console.log('Rendering ' + tile.xmlPath);
const map = new mapnik.Map(TILESIZE, TILESIZE);
const xmlPath = tile.xmlPath;
let outPath = path.normalize(path.join(xmlPath, '..', path.basename(xmlPath, '.xml') + '.png'));
map.fromStringSync(fs.readFileSync(xmlPath).toString());
let extent = tile.extent;
extent = padExtent(extent); // add padding around highlighted feature
extent = merc.forward(extent); // convert to Web Mercator coords
map.zoomToBox(extent);
map.renderFileSync(outPath);
outPath = buildConfig.moveToOCDID(outPath, tile.ocdid, tile.level);
// apply 'water' background color
execSync(
`gm convert ${outPath} -background '#d3ddff' -extent 0x0 ${outPath}`,
function(error, stdout, stderr) {
if (error !== undefined) {
console.log(`Failed to set background on ${outPath}`);
console.log(error);
console.log(stderr);
}
}
);
};
exports.renderTile = renderTile;
exports.renderAll = function(filterOcdid) {
const tiles = JSON.parse(fs.readFileSync('build/tiles.json'));
tiles.forEach(function(tile) {
if (filterOcdid && tile.ocdid && tile.ocdid.indexOf(filterOcdid) !== -1) {
return;
}
renderTile(tile);
});
};