-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
115 lines (82 loc) · 2.57 KB
/
render.js
File metadata and controls
115 lines (82 loc) · 2.57 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
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
var fs = require("fs"),
wp = require("webpage"),
system = require("system"),
page = wp.create();
var config = JSON.parse(fs.read("config.json"));
//Choose a format
if ("format" in config) {
config.format = config.format.toLowerCase();
if (["gif","png","jpg","jpeg","pdf"].indexOf(config.format) === -1) {
console.log("Invalid value for 'format', using 'png' instead.");
config.format = "png";
}
if (config.format == "jpeg") {
config.format = "jpg";
}
} else {
config.format = "png";
}
//Use empty prefix if none set
config.outputPrefix = config.outputPrefix || "";
//Page dimensions
page.clipRect = { top: 0, left: 0, width: config.width, height: config.height };
page.viewportSize = { width: config.width, height: config.height};
page.open(config.url, function (status) {
var waited = 0,
timeout = 10000,
interval = 100,
loop;
//generate.html loads async data, need to check until it's ready
loop = setInterval(function() {
waited += interval;
var isReady = page.evaluate(function(){
return mapReady;
});
if (isReady) {
ready();
clearInterval(loop);
} else if (waited > timeout) {
console.log("Maximum timeout of "+Math.round(timeout/1000)+"s exceeded. generate.html is having trouble loading.");
console.log("Make sure it's accessible at the URL listed in config.json, and that you have a working internet connection.");
clearInterval(loop);
phantom.exit();
}
},interval);
function ready() {
//Get the number of districts
var maps = page.evaluate(function(propName){
var data = [],
skip = [];
//Use data() if propName has been configured,
//otherwise use the feature's index
foreground.data().forEach(function(d,i){
if (propName) {
data.push(d.properties[propName] || null);
if (!(propName in d.properties)) {
skip.push(i);
}
} else {
data.push(i);
}
});
return {
"data": data,
"skip": skip
}
},config.propName);
//Generate a map for each
maps.data.forEach(function(datum,i){
if (maps.skip.indexOf(i) !== -1) {
console.log("'"+config.propName+"' not found for feature "+i+". Skipping.");
} else {
page.evaluate(function(d){
highlight(d);
},datum);
var filename = config.outputPrefix+datum+"."+config.format;
page.render(filename,{format:config.format});
console.log("Saved "+filename);
}
});
phantom.exit();
}
});