-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbitmaptilerequest.js
224 lines (198 loc) · 5.64 KB
/
bitmaptilerequest.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
/*
node-tileserver Copyright (C) 2014 Alexander Matheisen
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under certain conditions.
See https://github.com/rurseekatze/node-tileserver for details.
*/
// include necessary modules
var cluster = require('cluster');
var os = require('os');
var rbush = require('rbush');
var assert = require('assert');
var url = require("url");
var mkdirp = require('mkdirp');
var pg = require('pg');
var toobusy = require('toobusy-js');
var byline = require('byline');
var touch = require("touch");
var Canvas = require('canvas');
var events = require('events');
var log4js = require('log4js');
var fs = require('graceful-fs');
BitmapTilerequest = function(self)
{
this.tile = self.tile;
this.response = self.response;
this.command = self.command;
this.queue = self.queue;
this.requestModified = self.requestModified;
};
BitmapTilerequest.prototype =
{
// save the rendered image to disk and send it to the client
renderCallback: function(err, image)
{
var self = this;
if (err)
self.tile.debug('Vectortile was empty.');
self.tile.saveBitmapData(image, function(err)
{
if (err || image == null)
{
self.response.writeHead(500, {'Content-Type': 'text/plain'});
self.response.end();
self.tile.debug('Empty bitmap tile was responded to the request.');
self.tile.debug('Finished request.');
return;
}
self.tile.getModifyTime(function(err, mtime)
{
var header = self.getHeader();
if (!err)
header['Last-Modified'] = mtime.toUTCString();
self.tile.trace('Responding bitmap data...');
var stream = image.createPNGStream();
self.response.writeHead(200, header);
// write PNG data stream
stream.on('data', function(data)
{
self.response.write(data);
});
// PNG data stream ended
stream.on('end', function()
{
self.response.end();
self.tile.debug('Bitmap tile was responded to the request.');
self.tile.debug('Finished request.');
return;
});
});
});
},
// serves a bitmap tile
getTile: function()
{
var self = this;
self.tile.debug('Bitmap tile requested.');
self.tile.bitmapIsCached(function(exists)
{
// if tile is already rendered, return the cached image
if (exists && typeof self.command == "undefined")
{
self.tile.debug('Bitmap tile already rendered, returning cached data...');
self.tile.readBitmapData(function(err, data)
{
if (err)
{
self.abortRequest('Cannot read cached bitmap tile. Returning status 500.' + err);
return;
}
self.tile.trace('Returning bitmap tile...');
// check if tile is expired and add it to the queue if necessary
self.tile.isExpired(function(expired)
{
if (expired)
self.queue.add(self.tile);
self.tile.getModifyTime(function(err, mtime)
{
var header = {
'Content-Type': 'image/png'
};
if (!err)
header['Last-Modified'] = mtime.toUTCString();
if (expired)
header['Cache-Control'] = 'max-age=0';
else
header['Cache-Control'] = 'public, max-age=3600';
if (!err && self.requestModified.getTime() == mtime.getTime() && !expired)
{
self.response.writeHead(304, header);
self.response.end();
self.tile.debug('Bitmap tile cached on client.');
}
else
{
self.response.writeHead(200, header);
self.response.end(data);
self.tile.debug('Bitmap tile returned.');
}
self.tile.debug('Finished request.');
});
});
});
}
// otherwise render the tile
else
{
self.tile.debug('Bitmap tile not cached...');
self.tile.trace('MapCSS style successfully loaded.');
self.tile.readVectorData(function(err, data)
{
if (err || self.command == "dirty")
{
if (err)
self.tile.debug('Vectortile not cached, needs to be created...');
if (self.command == "dirty")
self.tile.debug('Vectortile dirty, needs to be refreshed...');
self.tile.getVectorData(function(data)
{
self.tile.debug('Vector tile created successfully, saving vector tile...');
self.tile.saveVectorData(function(err)
{
if (err)
self.tile.warn('Vector tile could not be saved.' + err);
if (data.features.length === 0)
{
self.tile.debug('Vector tile without features, serving empty PNG tile for style ' + self.tile.style);
self.renderCallback(true, null);
}
else
{
self.tile.debug('Rendering bitmap tile with style ' + self.tile.style);
self.tile.render(function(err, image)
{
self.renderCallback(err, image);
});
}
});
}, function(err)
{
self.abortRequest('Vectortile could not be created. Aborting.' + err);
});
}
else
{
// check if tile is expired and add it to the queue if necessary
self.tile.isExpired(function(expired)
{
if (expired)
self.queue.add(self.tile);
self.tile.debug('Rendering bitmap tile with style '+self.tile.style);
self.tile.render(function(err, image)
{
self.renderCallback(err, image);
});
});
}
});
}
});
},
// sends a 500 error response
abortRequest: function(msg)
{
this.tile.warn(msg);
this.response.writeHead(500, {'Content-Type': 'text/plain'});
this.response.end();
return;
},
getHeader: function(msg)
{
return {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=3600',
'Server': 'node-tileserver/0.3'
};
}
};
module.exports = BitmapTilerequest;