-
Notifications
You must be signed in to change notification settings - Fork 31
/
tile.js
931 lines (827 loc) · 23.5 KB
/
tile.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/*
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 Pool = require('pg-pool');
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('fs.extra');
// workaround to emulate browser properties
var window = new Object;
window.devicePixelRatio = 1;
Image = Canvas.Image;
// workaround to emulate browser frame method
window.requestAnimationFrame = (
function()
{
return function(callback)
{
callback();
};
}
)();
// workaround to emulate dom functions
var document = new Object;
document.createElement = function()
{
return new Canvas();
}
// include necessary libraries
logger.trace('Including KothicJS...');
eval(fs.readFileSync('kothic/kothic.js')+'');
eval(fs.readFileSync('kothic/renderer/path.js')+'');
eval(fs.readFileSync('kothic/renderer/line.js')+'');
eval(fs.readFileSync('kothic/renderer/polygon.js')+'');
eval(fs.readFileSync('kothic/renderer/shields.js')+'');
eval(fs.readFileSync('kothic/renderer/path.js')+'');
eval(fs.readFileSync('kothic/renderer/texticons.js')+'');
eval(fs.readFileSync('kothic/renderer/path.js')+'');
eval(fs.readFileSync('kothic/renderer/text.js')+'');
eval(fs.readFileSync('kothic/style/mapcss.js')+'');
eval(fs.readFileSync('kothic/style/style.js')+'');
eval(fs.readFileSync('kothic/utils/collisions.js')+'');
eval(fs.readFileSync('kothic/utils/geom.js')+'');
logger.trace('KothicJS loaded.');
// include rendering styles
for (var i=0; i<configuration.styles.length; i++)
{
eval(fs.readFileSync(configuration.styledir+'/'+configuration.styles[i]+'.js')+'');
MapCSS.preloadSpriteImage(configuration.styles[i], configuration.styledir+"/"+configuration.styles[i]+".png");
}
Tile = function(zoom, x, y, style)
{
this.z = parseInt(zoom) || 0;
this.x = parseInt(x) || 0;
this.y = parseInt(y) || 0;
this.data = null;
this.style = style || "vector";
};
Tile.prototype =
{
// Tile numbers of given zoom level to EPSG:4326 bbox of a tile
getBbox: function()
{
var a = this.getCoords(this.z, this.x, this.y);
var b = this.getCoords(this.z, this.x+1, this.y+1);
return new Array(a[0], b[1], b[0], a[1]);
},
// Converts (z,x,y) to coordinates of corner of a tile
getCoords: function(z, x, y)
{
var normalizedTile = new Array(x/Math.pow(2.0, z), 1.0-(y/Math.pow(2.0, z)));
var projectedBounds = this.from4326To3857(new Array(-180.0, -85.0511287798, 180.0, 85.0511287798));
var maxp = new Array(projectedBounds[2]-projectedBounds[0], projectedBounds[3]-projectedBounds[1]);
var projectedCoords = new Array((normalizedTile[0]*maxp[0])+projectedBounds[0], (normalizedTile[1]*maxp[1])+projectedBounds[1]);
return this.from3857To4326(projectedCoords);
},
debug: function(text)
{
logger.debug('z'+this.z+'x'+this.x+'y'+this.y+' '+text);
},
info: function(text)
{
logger.info('z'+this.z+'x'+this.x+'y'+this.y+' '+text);
},
error: function(text)
{
logger.error('z'+this.z+'x'+this.x+'y'+this.y+' '+text);
},
trace: function(text)
{
logger.trace('z'+this.z+'x'+this.x+'y'+this.y+' '+text);
},
warn: function(text)
{
logger.warn('z'+this.z+'x'+this.x+'y'+this.y+' '+text);
},
// use multiple files as hardlink targets to overcome maximum hardlink
// limit of filesystems
emptyTileLinkLoop: function(extension, file, callback, cnt)
{
var modFactor = 128;
var linkTry = cnt % modFactor;
var linkTarget = configuration.tiledir+'/emptytile' + linkTry + extension;
var linkFunc = function()
{
fs.link(linkTarget, file, function(err)
{
// if this target already has too many links try the next one
if (err == 'EMLINK')
{
self.debug('Hardlink limit reached for ' + linkTarget);
return process.nextTick(function()
{
self.emptyTileLinkLoop(extension, file, callback, cnt + 1);
});
}
else
return callback(err);
});
}
fs.exists(linkTarget, function(exists)
{
if (!exists)
return fs.copy('emptytile' + extension, linkTarget, { replace: false }, linkFunc);
else
return linkFunc();
});
},
emptyTileLink: function(extension, file, callback)
{
return this.emptyTileLinkLoop(extension, file, callback, this.y + 1);
},
// stores a vector tile in the vector tile directory
saveVectorData: function(callback)
{
// don't cache tiles in zoom levels higher than maxCached
if (this.z > configuration.maxCached)
{
this.debug('Tile of this zoom level is not cached.');
return process.nextTick(function()
{
callback(false);
});
}
var filepath = configuration.tiledir+'/tiles/'+this.z+'/'+this.x+'/';
var file = filepath + this.y + '.json';
this.debug('Creating path '+filepath+'...');
var self = this;
mkdirp(filepath, function(err)
{
if (err)
{
self.error('Cannot create path: ' + filepath + err);
return process.nextTick(function()
{
callback(err);
});
}
if (self.data.features.length === 0)
{
self.debug('Created path. Linking empty vector tile to: ' + file);
fs.exists(file, function(exists)
{
if (!exists)
{
self.emptyTileLink('.json', file, function(err)
{
if (!err)
self.debug('Empty vector tile was stored.');
else
self.debug('Could not link empty vector file.' + err);
return process.nextTick(function()
{
callback(err);
});
});
}
else
{
return process.nextTick(function()
{
callback(err);
});
}
});
return;
}
self.debug('Created path. Saving vector tile at path: ' + file);
fs.exists(file, function(exists)
{
if (exists)
fs.unlinkSync(file);
fs.writeFile(file, JSON.stringify(self.data), {mode: 0666}, function(err)
{
if (err)
self.error('Cannot save vector tile at path: ' + file + err);
else
self.debug('Saved vector tile at path: ' + file);
return process.nextTick(function()
{
callback(err);
});
});
});
});
},
// returns the content of a vector tile as a string
readVectorData: function(callback)
{
var path = configuration.tiledir+'/tiles/'+this.z+'/'+this.x+'/'+this.y+'.json';
this.debug('Opening vector tile at path: '+path);
var self = this;
fs.readFile(path, function(err, data)
{
if (!err && data)
{
self.debug('Loaded data from vector tile: '+path);
return process.nextTick(function()
{
// catch JSON parsing errors
try
{
self.data = JSON.parse(data);
data = null;
callback(err, self.data);
}
catch (err)
{
data = null;
self.data = null;
callback(err, null);
}
});
}
else
{
self.debug('Cannot read vector tile: '+path);
return process.nextTick(function()
{
callback(err);
});
}
});
},
// adds a callback function to the data to make it usable for browser rendering
getDataString: function()
{
return "onKothicDataResponse("+JSON.stringify(this.data)+","+this.z+","+this.x+","+this.y+");";
},
// renders a certain tile and calls the callback with the ready-rendered canvas when finished
render: function(callback)
{
this.debug('Rendering data...');
// start bitmap rendering
var canvas = new Canvas(configuration.tileSize, configuration.tileSize);
canvas.style = new Object();
MapCSS.invalidateCache();
var self = this;
Kothic.render(canvas, this.data, this.z+configuration.zoomOffset,
{
styles: [this.style],
onRenderComplete: function()
{
self.debug('Finished rendering bitmap tile.');
return process.nextTick(function()
{
callback(false, canvas);
});
}
});
},
// removes all rendering styles of a bitmap tile from the cache
removeBitmap: function(selectedStyle)
{
var selectedStyle = selectedStyle || 0;
if (selectedStyle > configuration.styles.length)
return;
var filepath = configuration.tiledir+'/bitmap-tiles/'+configuration.styles[selectedStyle]+'/'+this.z+'/'+this.x+'/'+this.y+'.png';
fs.exists(filepath, function(exists)
{
if (exists)
{
fs.unlink(filepath, function(err)
{
this.removeBitmap(this.z, this.x, this.y, selectedStyle+1);
});
}
});
},
// returns true if the tile was marked as expired, otherwise false is returned
isExpired: function(callback)
{
this.getModifyTime(function(err, mtime)
{
return process.nextTick(function()
{
callback((err || mtime.getFullYear() == "1970") ? true : false);
});
});
},
// marks a tile and all it's subtiles and parent tiles as expired or deletes them if necessary
expire: function()
{
if (this.z <= configuration.maxCached)
this.markExpired();
// remove if zoom level higher than maxcached
else
{
var filepath = configuration.tiledir+'/tiles/'+this.z+'/'+this.x+'/'+this.y+'.json';
var self = this;
fs.exists(filepath, function(exists)
{
if (exists)
{
fs.unlink(filepath, function(err) {
self.removeBitmap();
});
}
});
}
},
// marks a tile as expired
markExpired: function()
{
var filepath = configuration.tiledir+'/tiles/'+this.z+'/'+this.x+'/'+this.y+'.json';
fs.exists(filepath, function(exists)
{
if (exists)
touch.sync(filepath, {time: new Date(10)});
});
},
// return the timestamp of last modification of a tile
getModifyTime: function(callback)
{
var filepath = configuration.tiledir+'/tiles/'+this.z+'/'+this.x+'/'+this.y+'.json';
fs.exists(filepath, function(exists)
{
if (!exists)
{
return process.nextTick(function()
{
callback(!exists);
});
}
fs.stat(filepath, function(err, stats)
{
if (err || typeof stats == undefined)
return process.nextTick(function()
{
callback(err);
});
return process.nextTick(function()
{
callback(err, stats.mtime);
});
});
});
},
// returns a list of all tiles in lower zoomlevels that contain a certain tile
getParents: function()
{
var zoom = this.z;
var x = this.x;
var y = this.y;
var tiles = new Array();
while (zoom >= configuration.minExpiring)
{
zoom--;
x = parseInt(x/2);
y = parseInt(y/2);
tiles.push(new Tile(zoom, x, y));
}
return tiles;
},
// returns the subtiles of a certain tile
subTiles: function()
{
var zoom = this.z+1;
var x = this.x*2;
var y = this.y*2;
return new Array(
new Tile(zoom, x, y),
new Tile(zoom, x+1, y),
new Tile(zoom, x, y+1),
new Tile(zoom, x+1, y+1)
);
},
// returns a list of all subtiles of a certain tile
getChildren: function()
{
var zoom = this.z+1;
var x = this.x;
var y = this.y;
var tiles = new Array();
var tilesize = 1;
for (var z=zoom; z<=configuration.maxCached; z++)
{
x = x*2;
y = y*2;
tilesize = tilesize*2;
for (var i=x; i<(x+tilesize); i++)
{
for (var j=y; j<(y+tilesize); j++)
tiles.push(new Tile(z, i, j));
j -= tilesize;
}
i -= tilesize;
}
return tiles;
},
// requests objects for a certain tile and returns the data as an object
getVectorData: function(callback, errcallback)
{
var bbox = this.getBbox();
var bbox_p = this.from4326To3857(bbox);
var self = this;
logger.trace('Connecting to database...');
pool.connect(function(err, client, done)
{
if (err)
{
self.error('Fetching client from database pool failed: ' + err);
return process.nextTick(function()
{
errcallback(err);
});
}
else
self.debug('Connected to database.');
// request data
self.trace('Requesting data...');
client.query(self.getDatabaseQuery(bbox_p), function(err, results)
{
self.trace('All database queries finished, generating JSON data object.');
done();
var content = new Object();
content.features = self.getJSONFeatures(results);
content.granularity = configuration.intscalefactor;
content.bbox = bbox;
// catch tiles without data
if (content.features.length === 0)
{
self.debug('Vector tile contains no data.');
}
self.invertYAxe(content);
self.data = content;
self.debug('Generated vector data.');
return process.nextTick(function()
{
callback(content);
});
});
});
},
// rerenders all style versions of a tile
rerenderBitmap: function(selectedStyle)
{
var selectedStyle = selectedStyle || 0;
if (selectedStyle >= configuration.styles.length)
return;
this.style = configuration.styles[selectedStyle];
this.trace('MapCSS style loaded.');
var filepath = configuration.tiledir+'/bitmap-tiles/'+this.style+'/'+this.z+'/'+this.x;
var self = this;
this.render(function(err, image)
{
if (err)
{
self.warn('Bitmap tile could not be rendered. Returning.');
self.rerenderBitmap(selectedStyle+1);
self = null;
image = null;
return;
}
self.debug('Bitmap tile successfully rendered.');
self.debug('Creating path '+filepath+'...');
mkdirp(filepath, function(err)
{
if (err)
{
self.error('Cannot create path '+filepath+'. Returning.');
self.rerenderBitmap(selectedStyle+1);
self = null;
image = null;
return;
}
var fname = filepath + '/' + self.y + '.png';
self.debug('Saving bitmap tile at path: ' + fname);
fs.unlink(fname, function(err)
{
var out = fs.createWriteStream(fname, {mode: 0666});
var stream = image.createPNGStream();
// write PNG data stream
stream.on('data', function(data)
{
out.write(data);
});
// PNG data stream ended
stream.on('end', function()
{
out.end();
self.debug('Bitmap tile was saved.');
self.rerenderBitmap(selectedStyle+1);
self = null;
image = null;
stream = null;
out = null;
return;
});
});
});
});
},
// Converts l pixels on tiles into length on zoom z
pixelSizeAtZoom: function(l)
{
l = l || 1;
return l*20037508.342789244 / 256*2 / Math.pow(2, this.z);
},
// returns a database sql query string
getDatabaseQuery: function(bbox)
{
var zoom = this.z+configuration.zoomOffset;
var cond = configuration.filterconditions[zoom] || "";
var buffer = this.pixelSizeAtZoom(configuration.pxtolerance);
var tolerance = this.pixelSizeAtZoom(configuration.tileBoundTolerance);
var st_bbox = "ST_SetSRID('BOX3D(" + bbox[0] + " " + bbox[1] + "," + bbox[2] + " " + bbox[3] + ")'::box3d, 3857)";
var xy_wh = "" + (-bbox[0]) + ", " + (-bbox[1]) + ", " + (configuration.intscalefactor / (bbox[2] - bbox[0])) + ", " + (configuration.intscalefactor / (bbox[3] - bbox[1]));
return "SELECT\
ST_AsGeoJSON(ST_TransScale(ST_ForceRHR(ST_Intersection(" + configuration.geomcolumn + ", " + st_bbox + ")), " + xy_wh + "), 0) AS " + configuration.geomcolumn + ", \
hstore_to_json(tags) AS tags,\
ST_AsGeoJSON(ST_TransScale(ST_ForceRHR(ST_PointOnSurface(" + configuration.geomcolumn + ")), " + xy_wh + "), 0) AS reprpoint\
FROM\
(\
SELECT (ST_Dump(ST_Multi(ST_SimplifyPreserveTopology(ST_Buffer(" + configuration.geomcolumn + " ,-" + buffer + "), " + buffer + ")))).geom AS " + configuration.geomcolumn + ", tags\
FROM\
(\
SELECT ST_Union(" + configuration.geomcolumn + ") AS " + configuration.geomcolumn + ", tags\
FROM\
(\
SELECT ST_Buffer(" + configuration.geomcolumn + ", " + buffer + ") AS " + configuration.geomcolumn + ", tags AS tags\
FROM " + configuration.prefix + "_polygon\
WHERE " + configuration.geomcolumn + " && " + st_bbox + " AND way_area > " + (Math.pow(buffer, 2) / configuration.pxtolerance) + " " + cond + "\
) p\
GROUP BY tags\
) p\
WHERE ST_Area(" + configuration.geomcolumn + ") > " + Math.pow(buffer, 2) + "\
ORDER BY ST_Area("+configuration.geomcolumn+")\
) p\
UNION ALL\
SELECT\
ST_AsGeoJSON(ST_TransScale(ST_Intersection(" + configuration.geomcolumn+", " + st_bbox + "), " + xy_wh + "), 0) AS " + configuration.geomcolumn + ",\
hstore_to_json(tags) AS tags,\
NULL AS reprpoint\
FROM\
(\
SELECT (ST_Dump(ST_Multi(ST_SimplifyPreserveTopology(ST_LineMerge(" + configuration.geomcolumn + "), " + this.pixelSizeAtZoom(configuration.pxtolerance) + ")))).geom AS " + configuration.geomcolumn + ", tags\
FROM\
(\
SELECT ST_Union(" + configuration.geomcolumn + ") AS " + configuration.geomcolumn+", tags AS tags\
FROM " + configuration.prefix + "_line\
WHERE " + configuration.geomcolumn + " && " + st_bbox + " " + cond + "\
GROUP BY tags\
) p\
) p\
UNION ALL\
SELECT ST_AsGeoJSON(ST_TransScale(" + configuration.geomcolumn + ", " + xy_wh + "), 0) AS " + configuration.geomcolumn + ",\
hstore_to_json(tags) AS tags,\
NULL AS reprpoint\
FROM " + configuration.prefix + "_point\
WHERE " +
configuration.geomcolumn + " && ST_SetSRID('BOX3D(" + (bbox[0] - tolerance) + " " + (bbox[1] - tolerance) + "," + (bbox[2] + tolerance) + " " + (bbox[3] + tolerance) + ")'::box3d, 3857) " + cond;
},
// equivalent of tanh in PHP
tanh: function(i)
{
return (Math.exp(i) - Math.exp(-i)) / (Math.exp(i) + Math.exp(-i));
},
// equivalent of rad2deg in PHP
rad2deg: function(angle)
{
return angle/(Math.PI/180.0);
},
// equivalent of deg2rad in PHP
deg2rad: function(angle)
{
return angle*(Math.PI/180.0);
},
// Wrapper around transform call for convenience. Transforms line from EPSG:3857 to EPSG:4326
// line - a list of [lat0,lon0,lat1,lon1,...] or [(lat0,lon0),(lat1,lon1),...]
from3857To4326: function(line)
{
var serial = false;
if (!Array.isArray(line[0]))
{
serial = true;
var l1 = new Array();
for (var i=0; i<line.length; i=i+2)
l1.push(new Array(line[i], line[i+1]));
line = l1;
}
var ans = new Array();
for (var i=0; i<line.length; i++)
{
var xtile = line[i][0]/111319.49079327358;
var ytile = this.rad2deg(Math.asin(this.tanh(line[i][1]/20037508.342789244*Math.PI)));
if (serial)
{
ans.push(xtile);
ans.push(ytile);
}
else
ans.push(new Array(xtile, ytile));
}
return ans;
},
// Wrapper around transform call for convenience. Transforms line from EPSG:4326 to EPSG:3857
// line - a list of [lat0,lon0,lat1,lon1,...] or [(lat0,lon0),(lat1,lon1),...]
from4326To3857: function(line)
{
var serial = false;
if (!Array.isArray(line[0]))
{
serial = true;
var l1 = new Array();
for (var i=0; i<line.length; i=i+2)
l1.push(new Array(line[i], line[i+1]));
var line = l1;
}
var ans = new Array();
for (var i=0; i<line.length; i++)
{
var latRad = this.deg2rad(line[i][1]);
var xtile = line[i][0]*111319.49079327358;
var ytile = Math.log(Math.tan(latRad) + (1 / Math.cos(latRad))) / Math.PI * 20037508.342789244;
if (serial)
{
ans.push(xtile);
ans.push(ytile);
}
else
ans.push(new Array(xtile, ytile));
}
return ans;
},
// helper function to invert the y axe of the data
invertYAxe: function(data)
{
var type, coordinates, tileSize = data.granularity, i, j, k, l, feature;
for (i = 0; i < data.features.length; i++)
{
feature = data.features[i];
coordinates = feature.coordinates;
type = data.features[i].type;
if (type === 'Point')
coordinates[1] = tileSize - coordinates[1];
else if (type === 'MultiPoint' || type === 'LineString')
for (j = 0; j < coordinates.length; j++)
coordinates[j][1] = tileSize - coordinates[j][1];
else if (type === 'MultiLineString' || type === 'Polygon')
for (k = 0; k < coordinates.length; k++)
for (j = 0; j < coordinates[k].length; j++)
coordinates[k][j][1] = tileSize - coordinates[k][j][1];
else if (type === 'MultiPolygon')
for (l = 0; l < coordinates.length; l++)
for (k = 0; k < coordinates[l].length; k++)
for (j = 0; j < coordinates[l][k].length; j++)
coordinates[l][k][j][1] = tileSize - coordinates[l][k][j][1];
else
throw "Unexpected GeoJSON type: " + type;
if (feature.hasOwnProperty('reprpoint'))
feature.reprpoint[1] = tileSize - feature.reprpoint[1];
}
},
// converts raw JSON features from database response to objects
getJSONFeatures: function(data)
{
var features = new Array();
if (typeof data == undefined || data == null)
return features;
for (var i=0; i<data.rows.length; i++)
{
// catch JSON parsing errors
try
{
var geojson = JSON.parse(data.rows[i][configuration.geomcolumn]);
}
catch (err)
{
break;
}
if (geojson.type == "GeometryCollection")
continue;
try
{
if (data.rows[i].reprpoint)
{
var reprpoint = JSON.parse(data.rows[i].reprpoint);
geojson.reprpoint = reprpoint.coordinates;
}
geojson.properties = data.rows[i].tags;
}
catch (err)
{
continue;
}
features.push(geojson);
}
return features;
},
// returns the cached bitmap image of a tile
readBitmapData: function(callback)
{
fs.readFile(configuration.tiledir+'/bitmap-tiles/'+this.style+'/'+this.z+'/'+this.x+'/'+this.y+'.png', function(err, data)
{
return process.nextTick(function()
{
callback(err, data);
});
});
},
// stores a bitmap tile in the bitmap tile directory
saveBitmapData: function(image, callback)
{
// don't cache tiles in zoom levels higher than maxCached
if (this.z > configuration.maxCached)
{
this.debug('Tile of this zoom level is not cached.');
return process.nextTick(function()
{
callback(false);
});
}
var self = this;
var filepath = configuration.tiledir+'/bitmap-tiles/'+this.style+'/'+this.z+'/'+this.x;
this.debug('Rendering successful.');
this.debug('Saving bitmap tile at path: '+filepath);
mkdirp(filepath, function(err)
{
self.trace('Creating path '+filepath+'...');
if (err)
{
self.error('Cannot create path: '+filepath);
return process.nextTick(function()
{
callback(err);
});
}
var fname = filepath + '/' + self.y + '.png';
fs.exists(fname, function(exists)
{
if (exists)
fs.unlinkSync(fname);
// store empty tile if no image could be rendered
if (image == null)
{
self.debug('Bitmap tile empty.');
self.emptyTileLink('.png', fname, function(err)
{
if (!err)
self.debug('Empty bitmap tile was stored.');
else
self.debug('Could not link empty bitmap file.');
return process.nextTick(function()
{
callback(true);
});
});
}
else
{
self.trace('Saving bitmap data...');
var out = fs.createWriteStream(fname, {mode: 0666});
var stream = image.createPNGStream();
// write PNG data stream
stream.on('data', function(data)
{
out.write(data);
});
// PNG data stream ended
stream.on('end', function()
{
out.end();
image = null;
stream = null;
self.debug('Bitmap tile was stored.');
return process.nextTick(function()
{
callback(false);
});
});
}
});
});
},
// indicates if a bitmap tile in this style is already cached
bitmapIsCached: function(callback)
{
fs.exists(configuration.tiledir+'/bitmap-tiles/'+this.style+'/'+this.z+'/'+this.x+'/'+this.y+'.png', function(exists)
{
return process.nextTick(function()
{
callback(exists);
});
});
},
// destroys the object
destroy: function()
{
self = null;
}
};
module.exports = Tile;