-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
221 lines (189 loc) · 5.42 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
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
"use strict";
var fs = require("fs"),
path = require("path"),
url = require("url"),
util = require("util");
var _ = require("underscore"),
Bridge = require("@mapbox/tilelive-bridge"),
carto = require("carto"),
mapnik = require("mapnik"),
mapnikref = require('mapnik-reference').load(mapnik.versions.mapnik),
yaml = require("js-yaml");
var tm = {};
// Named projections.
tm.srs = {
'WGS84': '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs',
'900913': '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over'
};
tm.extent = {
'WGS84': [-180, -90, 180, 90],
'900913': [-20037508.34, -20037508.34, 20037508.34, 20037508.34]
};
// Return an augmented uri object from url.parse with the pathname
// transformed into an unescaped dirname.
tm.parse = function(str) {
var uri = url.parse(str);
if (uri.pathname) uri.dirname = unescape(uri.pathname);
return uri;
};
// Return true/false depending on whether a path is absolute.
tm.absolute = function(str) {
if (str.charAt(0) === '/') return true;
if ((/^[a-z]\:/i).test(str)) return true;
return false;
};
var defaults = {
name:'',
description:'',
attribution:'',
mtime:+new Date,
minzoom:0,
maxzoom:6,
center:[0,0,3],
Layer:[],
_prefs: {
saveCenter: true,
disabled: [],
inspector: false
}
};
var deflayer = {
id:'',
srs:'',
description:'',
fields: {},
Datasource: {},
properties: {
minzoom:0,
maxzoom:22,
'buffer-size':0
}
};
// Initialize defaults and derived properties on source data.
var normalize = function(data) {
data = _(data).defaults(defaults);
// Initialize deep defaults for _prefs, layers.
data._prefs = _(data._prefs).defaults(defaults._prefs);
data.Layer = data.Layer.map(function(l) {
l = _(l).defaults(deflayer);
// @TODO mapnikref doesn't distinguish between keys that belong in
// layer properties vs. attributes...
l.properties = _(l.properties).defaults(deflayer.properties);
// Ensure datasource keys are valid.
l.Datasource = _(l.Datasource).reduce(function(memo, val, key) {
if (!mapnikref.datasources[l.Datasource.type]) return memo;
if (key === 'type') memo[key] = val;
if (key in mapnikref.datasources[l.Datasource.type]) memo[key] = val;
// Set a default extent value for postgis based on the SRS.
if (l.Datasource.type === 'postgis' && key === 'extent' && !val) {
_(tm.srs).each(function(srs, id) {
if (l.srs !== srs) return;
memo[key] = tm.extent[id];
});
}
return memo
}, {});
return l;
});
// Format property to distinguish from imagery tiles.
data.format = 'pbf';
// Construct vector_layers info from layer properties if necessary.
data.vector_layers = data.Layer.map(function(l) {
var info = {};
info.id = l.id;
if ('description' in l) info.description = l.description;
if ('minzoom' in l.properties) info.minzoom = l.properties.minzoom;
if ('maxzoom' in l.properties) info.maxzoom = l.properties.maxzoom;
info.fields = [];
var opts = _(l.Datasource).clone();
if (opts.file && !tm.absolute(opts.file)) opts.base = tm.parse(data.id).dirname;
var fields = new mapnik.Datasource(opts).describe().fields;
info.fields = _(fields).reduce(function(memo, type, field) {
memo[field] = l.fields[field] || type;
return memo;
}, {});
return info;
});
return data;
};
var toXML = function(data, callback) {
// Include params to be written to XML.
var opts = [
"name",
"description",
"attribution",
"bounds",
"center",
"format",
"minzoom",
"maxzoom"
].reduce(function(memo, key) {
if (key in data) {
memo[key] = data[key];
}
return memo;
}, {});
opts.srs = tm.srs['900913'];
opts.Layer = data.Layer.map(function(l) {
l.srs = l.srs || tm.srs["900913"];
return l;
});
opts.json = JSON.stringify({
vector_layers: data.vector_layers
});
try {
return callback(null, new carto.Renderer().render(opts));
} catch(err) {
if (Array.isArray(err)) {
err.forEach(function(e) {
carto.writeError(e, options);
});
} else {
return callback(err);
}
}
};
var TMSource = function(uri, callback) {
var self = this;
if (typeof uri === 'string') {
uri = url.parse(uri);
}
if (uri.yaml) {
return self.init(uri, uri.yaml, callback);
}
uri.pathname = path.resolve(uri.hostname + uri.pathname);
uri.hostname = "";
var filename = path.join(uri.hostname + uri.pathname, "data.yml");
return fs.readFile(filename, "utf8", function(err, data) {
if (err) {
return callback(err);
}
return self.init(uri, data, callback);
});
};
TMSource.prototype.init = function(uri, yamlData, callback) {
var self = this;
try {
self.info = yaml.load(yamlData);
self.info.id = url.format(uri);
self.info = normalize(self.info);
} catch (err) {
return callback(err);
}
return toXML(self.info, function(err, xml) {
if (err) {
return callback(err);
}
uri.xml = xml.data;
uri.base = uri.pathname;
return Bridge.call(self, uri, callback);
});
};
util.inherits(TMSource, Bridge);
TMSource.registerProtocols = function(tilelive) {
tilelive.protocols["tmsource:"] = this;
};
module.exports = function(tilelive, options) {
TMSource.registerProtocols(tilelive);
return TMSource;
};