forked from erikandre/mapbox-gl-style-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style-editor.js
executable file
·279 lines (256 loc) · 7.35 KB
/
style-editor.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env node
var tilelive = require('tilelive');
var path = require('path');
var http = require("http");
var https = require('https');
var url = require('url');
var fs = require("fs");
var qs = require('querystring');
var styleConv = require('./style-conv');
var layerSplitter = require('./split-layers');
require('tilelive-bridge').registerProtocols(tilelive);
//var host = 'http://192.168.0.2:8080';
var host = 'http://localhost:8080';
var port = 8080;
var args = process.argv;
args.splice(0, 2); // Remove 'node' and the name of the script
if (args.length < 1) {
console.error('Usage:');
console.error('tile-serve <Mapbox gl json style>');
console.error('or');
console.error('tile-serve <mapnik xlm file>');
process.exit(1);
}
var staticStyle = null;
var mapFile = null;
if (args[0].endsWith('xml')) {
mapFile = path.resolve(__dirname, args[0]);
} else if (args[0].endsWith('json')) {
staticStyle = path.resolve(__dirname, args[0]);
}
var cacheDir = 'cache/';
var cachePrefix;
if (mapFile == null) {
cachePrefix = 'disabled'; // No caching if this is not a generated style
} else {
cachePrefix = path.parse(mapFile).name;
}
// Create Mapnik setups for each of the layers
if (mapFile != null) {
fs.readFile(mapFile, 'utf8', function(err, file) {
layerSplitter.splitLayers(file, cacheDir, cachePrefix, function() {
startServer();
});
});
} else {
startServer();
}
function startServer() {
if (mapFile != null) {
console.log('Serving tiles from ' + mapFile + " on port " + port);
} else {
console.log('Serving style data on port: ' + port);
}
http.createServer(function(request, response) {
if (request.method == 'GET') {
handleGet(request, response);
} else if (request.method == 'POST') {
handlePost(request, response);
}
}).listen(port);
}
function handlePost(request, response) {
console.log('POST URL: ' + request.url);
var parsedUrl = url.parse(request.url);
console.log('Path: ' + parsedUrl.pathname);
if (parsedUrl.pathname == '/save') {
saveStyle(request, response);
}
}
function saveStyle(request, response) {
var file;
if (staticStyle == null) {
file = path.resolve(__dirname, 'output.json');
staticStyle = file; // To avoid overwriting saved changes
} else {
file = staticStyle;
}
var out = fs.createWriteStream(file);
request.on('data', function(chunk) {
out.write(chunk)
});
request.on('end', function() {
out.end();
response.writeHead(200);
response.end();
});
}
function handleGet(request, response) {
console.log('GET URL: ' + request.url);
var parsedUrl = url.parse(request.url);
console.log('Path: ' + parsedUrl.pathname);
if (parsedUrl.pathname == '/') {
// Serve the main html page
serveMapPage(response);
} else if (parsedUrl.pathname == '/style.json') {
// Serve the map style
serveStyle(response, mapFile);
} else if (parsedUrl.pathname == '/favicon.ico') {
// Ignored
response.writeHead(404);
response.end();
} else if (parsedUrl.pathname.startsWith('/proxy/')) {
serveProxyFile(parsedUrl, response);
} else if (parsedUrl.pathname.startsWith('/file/')) {
serveLocalFile(parsedUrl, response);
} else if (parsedUrl.pathname == '/font') {
serveFont(response, parsedUrl);
} else {
serveTile(parsedUrl, response);
}
}
function serveLocalFile(url, response) {
var relPath = url.pathname.substr(6);
console.log('Serving local file: ' + relPath);
var file = path.resolve(__dirname, './files/' + relPath);
fs.readFile(file, 'binary', function(err, data) {
if (err) {
response.writeHead(404);
} else {
response.writeHeader(200);
response.write(data, 'binary');
}
response.end();
});
}
function serveProxyFile(url, response) {
// TODO: Don't use hard coded url
var proxyUrl = 'http://d2zne8rmvh5alv.cloudfront.net/' + url.pathname.substr(7);
console.log('Serving proxy file: ' + proxyUrl);
var request = http.request(proxyUrl, function(res) {
if (res.statusCode != 200) {
response.writeHead(res.statusCode);
response.end();
return;
}
var headers = {
'Content-Type' : 'application/x-protobuf',
'Content-Encoding' : 'gzip',
'x-tilelive-contains-data' : true
};
response.writeHead(200, 'OK', headers);
res.on('data', function(chunk) {
response.write(chunk, 'binary');
});
res.on('end', function() {
response.end();
});
});
request.on('error', function(e) {
console.log('Failed to load proxied file: ' + url + ', e: ' + e);
response.writeHead(500);
response.end();
});
request.end();
}
function serveTile(url, response) {
var query = url.query;
var params = qs.parse(query);
console.log('Requested z=' + params.z + ', x=' + params.x + ', y=' + params.y + ' of source=' + params.source);
var cacheKey = getCacheKey(params.source, params.z, params.x, params.y);
loadFromCache(cacheKey, function(cachedTile) {
console.log('Loaded tile from cache');
var headers = {
'Content-Type' : 'application/x-protobuf',
'Content-Encoding' : 'gzip',
'x-tilelive-contains-data' : true
};
response.writeHead(200, 'OK', headers);
response.write(cachedTile, 'binary');
response.end();
}, function() {
// Cache miss
openTileSource(params.source, function(source) {
source.getTile(params.z, params.x, params.y, function(err, tile, headers) {
if (err) {
response.writeHead(200);
response.end();
console.error(err);
return;
}
writeToCache(getCacheKey(params.source, params.z, params.x, params.y), tile, function() {
// Return response after saving to cache
response.writeHead(200, 'OK', headers);
response.write(tile, 'binary');
response.end();
});
});
});
});
}
function openTileSource(source, callback) {
var start = Date.now();
var url = 'bridge://' + path.resolve(__dirname, cacheDir + cachePrefix + '-' + source + '.xml');
tilelive.load(url, function(err, source) {
if (err)
throw err;
console.log('Opened source in ' + (Date.now() - start) + 'ms');
callback(source);
});
}
function getCacheKey(source, z, x, y) {
return cacheDir + cachePrefix + '-' + source + '-' + z + '-' + x + '-' + y + '.pbf';
}
function serveFont(response, url) {
var query = url.query;
var params = qs.parse(query);
var fileName = path.resolve(__dirname, 'font/' + params.name.split(',')[0] + '/' + params.range);
fs.readFile(fileName, 'binary', function(err, file) {
if (err) {
//throw err;
response.writeHead(404);
response.end();
return;
}
response.writeHead(200);
response.write(file, 'binary');
response.end();
});
}
function loadFromCache(key, hit, miss) {
var fileName = path.resolve(__dirname, key);
fs.readFile(fileName, 'binary', function(err, file) {
if (err) {
miss();
} else {
hit(file);
}
});
}
function writeToCache(key, tile, callback) {
var fileName = path.resolve(__dirname, key);
fs.writeFile(fileName, tile, 'binary', callback);
}
function serveStyle(response, mapFile) {
if (staticStyle != null) {
fs.readFile(path.resolve(__dirname, staticStyle), 'utf8', function(err, file) {
serveStringResponse(response, file);
});
} else {
styleConv.convertStyle(mapFile, host, '/map', function(jsonStyle) {
serveStringResponse(response, jsonStyle);
});
}
}
function serveMapPage(response) {
fs.readFile(path.resolve(__dirname, 'index.html'), 'utf8', function(err, file) {
if (err)
throw err;
serveStringResponse(response, file);
});
}
function serveStringResponse(response, data) {
response.writeHead(200);
response.write(data, 'utf8');
response.end();
}