-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
149 lines (117 loc) · 3.38 KB
/
index.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
"use strict";
var fs = require("fs"),
path = require("path"),
url = require("url");
var async = require("async"),
carto = require("carto");
var PREFIX = "carto+";
module.exports = function(tilelive, options) {
var Carto = function(uri, callback) {
uri = url.parse(uri, true);
uri.protocol = uri.protocol.replace(PREFIX, "");
this.uri = null;
var version = 0;
// watch the directory containing the style and recompile if anything
// changes
if (uri.query.cache === false || uri.query.cache === "false") {
fs.watch(path.resolve(path.dirname(path.join(uri.hostname, uri.pathname))), {
persistent: false
}, function(event, filename) {
return Carto.compile(uri, function(err, sourceURI) {
if (err) {
console.warn(err.stack);
return;
}
this.uri = sourceURI;
this.uri.query.version = ++version;
}.bind(this));
}.bind(this));
}
return Carto.compile(uri, function(err, sourceURI) {
if (err) {
return callback(err);
}
this.uri = sourceURI;
this.uri.query.version = version;
return callback(null, this);
}.bind(this));
};
Carto.compile = function(uri, callback) {
callback = callback || function() {};
var filename = path.join(uri.hostname, uri.pathname);
return fs.readFile(filename, function(err, mml) {
if (err) {
return callback(err);
}
try {
mml = JSON.parse(mml);
} catch (e) {
return callback(e);
}
return async.map(mml.Stylesheet, function(mss, done) {
if (typeof(mss) === "object") {
// stylesheet was inlined, by millstone or otherwise
return done(null, mss);
}
return fs.readFile(path.join(path.dirname(filename), mss), "utf8", function(err, style) {
if (err) {
return done(err);
}
return done(null, {
id: mss,
data: style
});
});
}, function(err, styles) {
if (err) {
return callback(err);
}
mml.Stylesheet = styles;
var xml;
try {
xml = new carto.Renderer().render(mml);
} catch (err) {
if (Array.isArray(err)) {
err.forEach(function(e) {
// TODO what's this?
carto.writeError(e, options);
});
} else {
return callback(err);
}
}
var sourceURI = {
protocol: "mapnik:",
xml: xml,
query: uri.query
};
return callback(null, sourceURI);
});
});
};
Carto.prototype.getInfo = function(callback) {
return tilelive.load(this.uri, function(err, source) {
if (err) {
return callback(err);
}
return source.getInfo(callback);
});
};
Carto.prototype.getTile = function(z, x, y, callback) {
return tilelive.load(this.uri, function(err, source) {
if (err) {
return callback(err);
}
return source.getTile(z, x, y, callback);
});
};
Carto.prototype.close = function(callback) {
callback = callback || function() {};
return setImmediate(callback);
};
Carto.registerProtocols = function(tilelive) {
tilelive.protocols[PREFIX + "file:"] = this;
};
Carto.registerProtocols(tilelive);
return Carto;
};