-
Notifications
You must be signed in to change notification settings - Fork 10
/
wmajt.js
1106 lines (1003 loc) · 33.6 KB
/
wmajt.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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var wmajt = (function () {
var w = 128, h = 128 // tile size
, UILang = 'en'
, minzoom = 12, buildingzoom = 14
, cache = {}
, ref_sd = {}, ref_z = {}
, zbuild = {}
, trackstartdate = false, trackzbuild = true
, bx1, by1, bx2, by2, bw, bh // used by update and mouse pointer interaction (current tile coords)
, lineWidthMult = 1 // lineWidth multiplier
, glProgram, glBufList, glBufSize, glI, glO, gl = null // used to build the WebGL building buffers (glI is index to current buffer (last in list))
, dash = null
, style = {
Polygon: [
['natural', { land_polygons: 1 },
[{ fillStyle: "rgb(250,250,208)" }]
],
['railway', { platform: 1 },
[{ fillStyle: "rgb(220,220,220)" }]
],
['landuse', { industrial: 1, retail: 1, commercial: 1, residential: 1 },
[{ fillStyle: "rgb(208,208,208)" }]
],
['landuse', { reservoir: 1 },
[{ fillStyle: "rgb(200,200,224)" }]
],
['landuse', { military: 1, railway: 1 },
[{ fillStyle: "rgb(224,200,200)" }]
],
['landuse', { cemetery: 1, recreation_ground: 1 },
[{ fillStyle: "rgb(190,214,190)" }]
],
['landuse', { grass: 1 },
[{ globalAlpha: 0.3, fillStyle: "rgb(0,160,0)" }, { globalAlpha: 1 }]
],
['waterway', { riverbank: 1, dock: 1 },
[{ fillStyle: "rgb(158,199,243)" }]
],
['waterway', { dam: 1 },
[{ fillStyle: "rgb(150,150,150)" }]
],
['leisure', {
park: 1, orchard: 1, meadow: 1, village_green: 1, golf_course: 1, track: 1,
forrest: 1, recreation_ground: 1, dog_park: 1
},
[{ fillStyle: "rgb(200,224,200)" }]
],
['leisure', {
garden: 1, pitch: 1, stadium: 1
},
[{ fillStyle: "rgb(180,224,180)" }]
],
['leisure', { swimming_pool: 1 },
[{ fillStyle: "rgb(200,200,224)" }]
],
['natural', { beach: 1, sand: 1 },
[{ fillStyle: "rgb(250,242,175)" }]
],
['natural', { wetland: 1, mud: 1 },
[{ fillStyle: "rgb(200,218,224)" }]
],
['natural', { grassland: 1, fell: 1 },
[{ fillStyle: "rgb(200,224,200)" }]
],
['natural', { scrub: 1 },
[{ fillStyle: "rgb(150,214,150)" }]
],
['natural', { wood: 1 },
[{ fillStyle: "rgb(100,204,100)" }]
],
['natural', { water: 1, bay: 1 },
[{ fillStyle: "rgb(158,199,243)" },
{ lineWidth: 1, strokeStyle: "rgb(158,199,243)" }]
],
['natural', { reef: 1 },
[{ fillStyle: "rgb(148,189,223)" },
{ lineWidth: 1, strokeStyle: "rgb(158,199,243)" }]
],
['man_made', { wastewater_plant: 1, breakwater: 1, pier: 1 },
[{ fillStyle: "rgb(168,178,178)" },
{ lineWidth: 1, strokeStyle: "rgb(140,145,145)" }]
],
['natural', { glacier: 1 },
[{ fillStyle: "rgb(230,245,255)" },
{ lineWidth: 1, strokeStyle: "rgb(255,255,255)" }]
],
['amenity', { university: 1, school: 1 },
[{ lineWidth: 0.5, strokeStyle: "rgb(240,225,183)" }]
],
['amenity', { parking: 1 },
[{ fillStyle: "rgb(240,235,193)" }]
],
['highway', { pedestrian: 1 },
[{ fillStyle: "rgb(255,255,255)" },
{ lineWidth: 2, strokeStyle: "rgb(168,148,148)" }]
],
['tourism', true,
[{ dash: [3, 3], lineWidth: 2, strokeStyle: "rgb(255,255,0)" }]
],
['aeroway', { terminal: 1 },
[{ fillStyle: "rgb(190,210,190)" },
{ lineWidth: 1, strokeStyle: "rgb(127,137,127)" }]
],
['historic', { memorial: 1, monument: 1, fort: 1, castle: 1 },
[{ fillStyle: "rgb(255,190,190)" },
{ lineWidth: 1, strokeStyle: "rgb(167,120,120)" }]
],
['historic', { ship: 1, wreck: 1 },
[{ fillStyle: "rgb(255,190,235)" }]
],
['railway', { station: 1 },
[{ fillStyle: "rgb(210,195,195)" },
{ lineWidth: 1, strokeStyle: "rgb(127,127,127)" }]
],
/*['building', {yes: 1, block: 1, office: 1, courthouse: 1, church: 1, school: 1, cathedral: 1, residential: 1, house: 1, hut: 1,
university: 1, hospital: 1, bunker: 1, train_station: 1, chapel: 1, industrial: 1, commercial: 1, retail: 1, hotel: 1,
apartments: 1, synagogue: 1},
[ { fillStyle: "rgb(200,200,200)" },
{ lineWidth: 1, strokeStyle: "rgb(127,127,127)" } ]
], */
['building', true,
[{ fillStyle: "rgb(200,200,200)" },
{ lineWidth: 1, strokeStyle: "rgb(127,127,127)" }]
],
['building:part', true,
[{ fillStyle: "rgb(200,200,200)" },
{ lineWidth: 1, strokeStyle: "rgb(127,127,127)" }]
],
['aeroway', { runway: 1, taxiway: 1, apron: 1 },
[{ fillStyle: "rgb(100,130,100)" },
{ lineWidth: 1, strokeStyle: "rgb(150,180,150)" }]
]
/*['building:height', true,
[ { lineWidth: 5, strokeStyle: "rgb(255,0,0)" } ]
],
['building:levels', true,
[ { lineWidth: 3, strokeStyle: "rgb(0,255,0)" } ]
],
['building:min_level', true,
[ { lineWidth: 1.5, strokeStyle: "rgb(0,0,255)" } ]
],
['start_date', true,
[ { dash: [2,5], lineWidth: 3, strokeStyle: "rgb(255,0,0)" } ]
]*/
],
LineString: [
['natural', { coastlines: 1 },
[{ lineWidth: 1, strokeStyle: "rgb(125,125,104)" }]
],
['waterway', { canal: 1 },
[{ lineCap: 'butt', lineWidth: 3, strokeStyle: "rgb(158,199,243)" }]
],
['waterway', { river: 1 },
[{ lineWidth: 1.5, strokeStyle: "rgb(126,159,194)" }]
],
['waterway', { stream: 1 },
[{ dash: [2, 2], lineWidth: 1.5, strokeStyle: "rgb(126,159,194)" }]
],
['route', { ferry: 1 },
[{ dash: [4, 4], lineWidth: 2, strokeStyle: "rgb(126,159,194)" }]
],
['highway', { footway: 1, pedestrian: 1, path: 1, cycleway: 1 },
[{ lineWidth: 2, strokeStyle: "rgb(198,178,178)" }]
//[ { lineWidth: 2, strokeStyle: "rgb(168,148,148)" } ]
],
['highway', { steps: 1 },
[{ dash: [1.5, 1.5], lineWidth: 3, strokeStyle: "rgb(168,148,148)" }]
],
['highway', { service: 1, bus_guideway: 1 },
[{ lineWidth: 4, strokeStyle: "rgb(168,168,168)" },
{ lineWidth: 2.5, strokeStyle: "rgb(208,208,208)" }]
],
['highway', { track: 1 },
[{ lineWidth: 3.5, strokeStyle: "rgb(168,168,168)" },
{ lineWidth: 2.5, strokeStyle: "rgb(250,250,208)" }]
],
['highway', { residential: 1, unclassified: 1 },
[{ lineWidth: 4, strokeStyle: "rgb(200,200,200)" },
{ lineWidth: 2.5, strokeStyle: "rgb(255,255,255)" }]
],
['highway', { tertiary: 1 },
[{ lineWidth: 5, strokeStyle: "rgb(200,200,200)" },
{ lineWidth: 3.5, strokeStyle: "rgb(255,255,235)" }]
],
// border
['railway', { subway: 1 },
[{ globalAlpha: 0.2, lineWidth: 3, strokeStyle: "rgb(100,100,100)" },
{ globalAlpha: 1 }]
],
['railway', { rail: 1, preserved: 1, monorail: 1, narrow_gauge: 1 },
[{ lineWidth: 3, strokeStyle: "rgb(100,100,100)" }]
],
['highway', { secondary: 1, secondary_link: 1, primary: 1, primary_link: 1 },
[{ lineCap: 'round', lineWidth: 6, strokeStyle: "rgb(171,158,137)" }]
],
['highway', { motorway: 1, motorway_link: 1, trunk: 1, trunk_link: 1 },
[{ lineWidth: 7, strokeStyle: "rgb(188,149,28)" }]
],
['aeroway', { runway: 1 },
[{ lineWidth: 10, strokeStyle: "rgb(100,130,100)" }]
],
['aeroway', { taxiway: 1 },
[{ lineWidth: 4.5, strokeStyle: "rgb(100,130,100)" }]
],
// fill
['railway', { subway: 1 },
[{ lineCap: 'butt', globalAlpha: 0.3, dash: [3, 3], lineWidth: 1.5, strokeStyle: "rgb(255,255,255)" },
{ globalAlpha: 1 }]
],
['railway', { rail: 1, narrow_gauge: 1 },
[{ dash: [3, 3], lineWidth: 1.5, strokeStyle: "rgb(255,255,255)" }]
],
['railway', { preserved: 1 },
[{ dash: [3, 3], lineWidth: 1.5, strokeStyle: "rgb(200,200,200)" }]
],
['railway', { monorail: 1 },
[{ dash: [1, 2, 4, 2], lineWidth: 1.5, strokeStyle: "rgb(200,200,200)" }]
],
['highway', { bus_guideway: 1 },
[{ dash: [3, 3], lineWidth: 2, strokeStyle: "rgb(150,150,255)" }]
],
['highway', { secondary: 1, secondary_link: 1 },
[{ lineCap: 'round', lineWidth: 4.5, strokeStyle: "rgb(255,250,115)" }]
],
['highway', { primary: 1, primary_link: 1 },
[{ lineWidth: 4, strokeStyle: "rgb(255,230,95)" }]
],
['highway', { motorway: 1, motorway_link: 1, trunk: 1, trunk_link: 1 },
[{ lineWidth: 5, strokeStyle: "rgb(242,191,36)" }]
],
['aeroway', { runway: 1 },
[{ lineWidth: 8, strokeStyle: "rgb(150,180,150)" }]
],
['aeroway', { taxiway: 1 },
[{ lineWidth: 2.5, strokeStyle: "rgb(150,180,150)" }]
],
['railway', { tram: 1 },
[{ globalAlpha: 0.4, dash: [3, 3], lineWidth: 1.5, strokeStyle: "rgb(0,0,0)" },
{ globalAlpha: 1 }]
],
// access overlay
['access', { permissive: 1 },
[{ dash: [1, 2], lineWidth: 1, strokeStyle: "rgb(100,200,100)" }]
],
['access', { 'private': 1, residents: 1 },
[{ dash: [1, 2], lineWidth: 1, strokeStyle: "rgb(200,100,100)" }]
],
['building:part', true,
[{ lineWidth: 1, strokeStyle: "rgb(127,127,255)" }]
]
/*['start_date', true,
[ { dash: [2,5], lineWidth: 3, strokeStyle: "rgb(255,0,0)" } ]
]*/
]
};
function union(a1, a2) {
var a = a1.concat(a2), r = [], s = {};
for (i in a) {
if (a.hasOwnProperty(i)) {
if (!(a[i] in s)) {
s[a[i]] = true;
r.push(a[i]);
}
}
}
return r;
};
function hash(x, y, z) {
return x + '_' + y + '_' + z;
}
function gotData(data) {
// TODO sort accorsing to layer and tunnel flag (but that kills the index!)
var idx, lay = { 0: 1 }, d, i, j;
// insert response into cache
if (data === null) return; // server error
d = data.data;
// index of objects by tag
if (data.v && data.v >= 2)
idx = data.idx;
else {
// generate index client side
idx = {};
for (i = 0; i < d.length; ++i) {
for (j in d[i].tags) {
if (j in idx)
idx[j].push(i);
else
idx[j] = [i];
}
}
}
// list of all layers in this tile
for (i = 0; i < d.length; ++i) {
if ('layer' in d[i].tags)
lay[d[i].tags['layer']] = 1;
}
// build cache entry
cache[hash(data.x, data.y, data.z)] = {
data: data.data,
building: {},
f: data.f || {},
idx: idx,
lay: lay
};
// propagate buildings to low zoom levels above the building threshold
var d = data.data, zz, xx = data.x, yy = data.y, ca;
if (data.z >= buildingzoom) {
for (zz = data.z; zz >= minzoom; --zz) {
ca = cache[hash(xx, yy, zz)];
if (zz < buildingzoom && ca && ca.data) {
// iterate over all data entries and insert buildings into higher cache level
for (i = 0; i < d.length; ++i) {
// check against shape type and tags
if ('osm_id' in d[i].tags &&
('building' in d[i].tags || 'building:part' in d[i].tags) &&
!(d[i].tags['osm_id'] in ca.building)) {
// update the index
for (j in d[i].tags) {
if (j in ca.idx)
ca.idx[j].push(ca.data.length);
else
ca.idx[j] = [ca.data.length];
}
ca.data.push(d[i]);
ca.building[d[i].tags['osm_id']] = 1;
}
}
}
xx = Math.floor(xx / 2);
yy = Math.floor(yy / 2);
}
}
else {
// TODO: look for lower zoom levels with building data, but there may be A LOT!
// SOLUTION: just look at building zoom
}
// (re-)draw the tile
update(this.csx, this.csy, this.csz, this);
}
// create path from coordinate data in g
function drawPath(g, c) {
if (dash) {
// iterate over all nodes
if (g && g.length > 0) {
var px = (g[0][0] - bx1) * 128.0 / bw
, py = 128.0 - (g[0][1] - by1) * 128.0 / bh
, dx, dy, di = 0, dl = dash.length, dc = 0.0
, mx, my, r, rr, j, done;
for (j = 1; j < g.length; ++j) {
// move to start
c.moveTo(px, py);
rr = -dc;
// destination point
dx = (g[j][0] - bx1) * 128.0 / bw;
dy = 128.0 - (g[j][1] - by1) * 128.0 / bh;
// stepvector and length
mx = dx - px;
my = dy - py;
r = Math.sqrt(mx * mx + my * my);
mx /= r;
my /= r;
// loop over segment
done = false;
while (true) {
rr += dash[di] * lineWidthMult;
if (rr > r) {
done = true;
dc = rr - r;
rr = r;
}
c[di % 2 ? 'moveTo' : 'lineTo'](rr * mx + px, rr * my + py);
if (done) break;
di = (di + 1) % dl;
}
// new starting point
px = dx;
py = dy;
}
}
}
else {
// iterate over all nodes
if (g && g.length > 0) {
c.moveTo((g[0][0] - bx1) * 128 / bw, 128 - (g[0][1] - by1) * 128 / bh);
for (j = 1; j < g.length; ++j)
c.lineTo((g[j][0] - bx1) * 128 / bw, 128 - (g[j][1] - by1) * 128 / bh);
}
}
}
// detect mouse pointer proximity
function detectPointer(e, tile) {
var rmin = null, mmin = {}
, o = tile.div.offset()
, mx = e.pageX - o.left
, my = e.pageY - o.top
, d = tile.csca.data || []
, m, i, t, s = '';
function detectPath(g, c, m) {
var px, py, r;
// iterate over all nodes
if (!g) return;
for (j = 1; j < g.length; ++j) {
px = (g[j][0] - bx1) * 128 / bw - mx;
py = 128 - (g[j][1] - by1) * 128 / bh - my;
r = px * px + py * py;
if ((rmin === null || r < rmin) && (('name:' + UILang) in m.tags || 'name' in m.tags || 'addr:street' in m.tags)) {
rmin = r;
mmin = m;
}
}
}
// set globals for current tile coordinates
bx1 = tile.csx * 60.0 / (1 << tile.csz)
by1 = 90.0 - (((tile.csy + 1.0) * 60.0) / (1 << tile.csz))
bx2 = (tile.csx + 1) * 60.0 / (1 << tile.csz)
by2 = 90.0 - ((tile.csy * 60.0) / (1 << tile.csz))
bw = bx2 - bx1
bh = by2 - by1
if (bx1 > 180.0) bx1 -= 360;
// loop over tag index objects
for (i = 0; i < d.length; ++i) {
m = d[i];
processShape(m, 'Polygon', null, detectPath);
if (m.geo.type == 'GeometryCollection')
processShape(m, 'Line', null, detectPath);
}
t = mmin.tags || {};
// has name
if (('name' in t) || (('name:' + UILang) in t)) {
s = t[('name:' + UILang)] || t.name;
if ('loc_name' in t)
s += ' "' + t.loc_name + '"';
if ('artist_name' in t)
s += ' (' + t.artist_name + ')';
return s;
}
// has address
if ('addr:street' in t) {
s = t['addr:street'];
if ('addr:housenumber' in t)
s = t['addr:housenumber'] + ' ' + s;
return s;
}
// misc
var tags = ['landuse', 'historic', 'highway', 'building'];
for (i = 0; i < tags.length; ++i) {
if (tags[i] in t)
return tags[i] + ' ' + t[tags[i]];
}
return null;
}
// quick hack for shape type
function processShape(m, s, c, path) {
var k, l, g;
switch (m.geo.type) {
case 'Polygon':
// TODO
path(m.geo.coordinates[0], c, m);
//path(m.geo.coordinates, c, m);
break;
case 'LineString':
path(m.geo.coordinates, c, m);
break;
case 'MultiLineString':
for (k = 0; k < m.geo.coordinates.length; ++k) {
path(m.geo.coordinates[k], c, m);
}
break;
case 'MultiPolygon':
for (k = 0; k < m.geo.coordinates.length; ++k) {
path(m.geo.coordinates[k][0], c, m);
}
break;
case 'GeometryCollection':
g = m.geo.geometries;
for (l = 0; l < g.length; ++l) {
if (s === 'Polygon') {
switch (g[l].type) {
case 'Polygon':
// TODO
path(g[l].coordinates[0], c, m);
break;
case 'MultiPolygon':
for (k = 0; k < g[l].coordinates.length; ++k) {
path(g[l].coordinates[k][0], c, m);
}
break;
}
}
else {
switch (g[l].type) {
case 'LineString':
path(g[l].coordinates, c, m);
break;
case 'MultiLineString':
for (k = 0; k < g[l].coordinates.length; ++k) {
path(g[l].coordinates[k], c, m);
}
break;
}
}
}
break;
}
}
function update(x, y, z, tile, purge) {
var c = tile.ctx, glRedraw = false, bldgh, bldgm, g, roofh, roofs;
// set globals for current tile coordinates
bx1 = x * 60.0 / (1 << z)
by1 = 90.0 - (((y + 1.0) * 60.0) / (1 << z))
bx2 = (x + 1) * 60.0 / (1 << z)
by2 = 90.0 - ((y * 60.0) / (1 << z))
bw = bx2 - bx1
bh = by2 - by1
if (bx1 > 180.0)
bx1 -= 360;
// set global lineWidth multiplier
lineWidthMult = Math.pow(1.25, z - 13);
// draw the data
function drawGeoJSON(ca) {
var i, j, k, g, s, o, d = ca.data, m, idx;
c.lineWidth = 1.0;
// TODO: handle coastlines properly!!
c.fillStyle = 'rgb(158,199,243)';
c.fillRect(0, 0, 128, 128);
for (s in style) {
for (o = 0; o < style[s].length; ++o) {
c.beginPath();
// skip styles whose tag does not occur
if (!(style[s][o][0] in ca.idx)) continue;
// set dash style global
dash = style[s][o][2][0].dash || null;
// loop over tag index objects
idx = ca.idx[style[s][o][0]];
for (i = 0; i < idx.length; ++i) {
m = d[idx[i]];
// check against shape type and tags
if ((s !== m.geo.type && ("Multi" + s) !== m.geo.type && m.geo.type !== 'GeometryCollection') ||
!(style[s][o][1] === true || m.tags[style[s][o][0]] in style[s][o][1])) continue;
processShape(m, s, c, drawPath);
}
// iterate over the style components
g = style[s][o][2]
for (i = 0; i < g.length; ++i) {
for (j in g[i]) {
if (j === 'lineWidth')
c.lineWidth = g[i].lineWidth * lineWidthMult;
else if (j !== 'dash')
c[j] = g[i][j];
}
if ('strokeStyle' in g[i])
c.stroke();
if ('fillStyle' in g[i])
c.fill();
}
}
}
}
function parseHeight(s) {
var m;
if (s === undefined)
return null;
// meters (or implicit meters)
m = /^(\d+(\.\d*)?)(\s*m)?$/.exec(s);
if (m !== null)
return parseFloat(m[1]);
// feet and inches
m = /^((\d+(\.\d*)?)')?((\d+(\.\d*)?)")?$/.exec(s);
if (m !== null)
return parseFloat(m[2] || '0') * 0.3048 + parseFloat(m[5] || '0') * 0.0254;
return 0.0;
}
// store coords in tile context
tile.csx = x;
tile.csy = y;
tile.csz = z;
// seach cache for data
var zz, xx = x, yy = y, ca, d, v, idx;
for (zz = z; zz >= minzoom; --zz) {
ca = cache[hash(xx, yy, zz)];
if (ca && ca.data && !purge) {
// subtract old tile from reference counters
if (tile.csca) {
d = tile.csca.data;
if (trackstartdate && 'start_date' in tile.csca.idx) {
idx = tile.csca.idx['start_date']
for (i = 0; i < idx.length; ++i) {
v = d[idx[i]].tags['start_date'];
if (v in ref_sd) {
ref_sd[v]--;
if (ref_sd[v] == 0)
delete ref_sd[v];
}
}
}
// union index
if (trackzbuild && z > buildingzoom) {
idx = union(tile.csca.idx['building:levels'] || [], tile.csca.idx['height'] || []);
//if (z>buildingzoom && 'building:levels' in tile.csca.idx) {
// idx = tile.csca.idx['building:levels']
for (i = 0; i < idx.length; ++i) {
if ('osm_id' in d[idx[i]].tags) {
v = d[idx[i]].tags['osm_id'];
if (v in ref_z) {
ref_z[v]--;
if (ref_z[v] == 0)
delete ref_z[v];
}
}
}
}
}
// draw tile contents
drawGeoJSON(ca);
// link current cache object to current tile
tile.csca = ca;
// add to reference counters
d = ca.data;
// this maintains a list of all distinct start dates in the current field of view
if (trackstartdate && 'start_date' in ca.idx) {
idx = ca.idx['start_date']
for (i = 0; i < idx.length; ++i) {
v = d[idx[i]].tags['start_date'];
ref_sd[v] = (ref_sd[v] || 0) + 1;
}
}
// this maintains a list of all buildings with height data in the field of view
// and a lookup table of buildings by osm_id
//if (z>buildingzoom && 'building:levels' in ca.idx) {
// union index
if (trackzbuild && z > buildingzoom) {
idx = union(ca.idx['building:levels'] || [], ca.idx['height'] || []);
//idx = ca.idx['building:levels']
for (i = 0; i < idx.length; ++i) {
if ('osm_id' in d[idx[i]].tags) {
v = d[idx[i]].tags['osm_id'];
ref_z[v] = (ref_z[v] || 0) + 1;
if (!(v in zbuild))
zbuild[v] = d[idx[i]];
}
}
}
// Hook for WebGL buildings:
// just increment ref_z if ref_z is 0 (before the increment)
// add the building to the WebGL buffer
// otherwise do nothing
if (gl !== null) {
idx = union(ca.idx['building:levels'] || [], ca.idx['height'] || []);
for (i = 0; i < idx.length; ++i) {
if ('osm_id' in d[idx[i]].tags) {
v = d[idx[i]].tags['osm_id'];
if (!(v in ref_z)) {
ref_z[v] = true;
v = d[idx[i]];
bldgh = parseHeight(v.tags['height']) || (v.tags['building:levels'] * 3);
bldgm = parseHeight(v.tags['min_height']) || (v.tags['building:min_level'] * 3) || 0;
if (v.geo.type === 'Polygon')
g = v.geo.coordinates;
else if (v.geo.type === 'LineString')
g = [v.geo.coordinates];
else
continue;
glRedraw = true;
// old style pyramid
if (v.tags['building:shape'] === 'pyramid') {
shapePyramid(g, bldgm, bldgh);
continue;
}
// roofs
if (('roof:shape' in v.tags || 'building:roof:shape' in v.tags) &&
('roof:height' in v.tags || 'building:roof:height' in v.tags)) {
roofh = parseHeight(v.tags['roof:height'] || v.tags['building:roof:height']);
roofs = v.tags['roof:shape'] || v.tags['building:roof:shape'];
// pyramidal
if (roofs === 'pyramidal') {
if (roofh < bldgh)
triangulate(g, bldgm, bldgh - roofh, true);
shapePyramid(g, bldgh - roofh, bldgh);
continue;
}
// gabled and hilted
if (g[0].length == 5) {
if (roofs === 'gabled' || roofs === 'hilted') {
if (roofh < bldgh)
triangulate(g, bldgm, bldgh - roofh, true);
shapeGabled(g, bldgh - roofh, bldgh);
continue;
}
}
}
triangulate(g, bldgm, bldgh);
}
}
}
}
tile.can.show();
if (glRedraw)
renderWebGLBuildingData();
if (z < buildingzoom || zz >= buildingzoom)
return;
}
xx = Math.floor(xx / 2);
yy = Math.floor(yy / 2);
}
// request data
tile.debug.html('/tiles/jsontile.php?x=' + x + '&y=' + y + '&z=' + z);
$.ajax({
url: '/tiles/jsontile.php?x=' + x + '&y=' + y + '&z=' + z + (purge === true ? '&action=purge' : ''),
dataType: 'json',
success: gotData,
context: tile
});
}
function registerWebGLBuildingData(triangleNum, context, program) {
glArrList = [];
glBufList = [];
glBufSize = triangleNum; // *9 floats
gl = context;
glProgram = program;
// setup first buffer array
var va, na;
glI = 0;
glO = 0;
va = new Float32Array(glBufSize * 9);
na = new Float32Array(glBufSize * 9);
glArrList.push({ v: va, n: na });
// switch off the visible building tracking needed for canvas
trackzbuild = false;
}
function renderWebGLBuildingData() {
var i, l, vb, nb, s;
// new data to be copied
if (glArrList.length > glBufList.length || glI > glO) {
// copy data, add buffers (maybe more arrays than buffers!)
// start at last entry in glBufList loop up till
s = glBufList.length - 1;
s = s < 0 ? 0 : s;
for (i = s; i < glArrList.length; ++i) {
// create new buffer
if (i >= glBufList.length) {
vb = gl.createBuffer();
nb = gl.createBuffer();
glBufList.push({ v: vb, n: nb });
}
else {
vb = glBufList[i].v;
nb = glBufList[i].n;
}
gl.bindBuffer(gl.ARRAY_BUFFER, vb);
gl.bufferData(gl.ARRAY_BUFFER, glArrList[i].v, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, nb);
gl.bufferData(gl.ARRAY_BUFFER, glArrList[i].n, gl.STATIC_DRAW);
}
glO = glI;
}
l = glBufList.length - 1;
for (i = 0; i <= l; ++i) {
gl.bindBuffer(gl.ARRAY_BUFFER, glBufList[i].n);
gl.vertexAttribPointer(glProgram.normalPosAttrib, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, glBufList[i].v);
gl.vertexAttribPointer(glProgram.vertexPosAttrib, 3, gl.FLOAT, false, 0, 0);
// number of vertices = glBufSize*3
gl.drawArrays(gl.TRIANGLES, 0, ((i == l) ? glO : glBufSize) * 3);
}
}
// push vertex coordinates and normals into the Float32Arrays
function vnPush(v, n) {
// assert v.length = n.length
var l = v.length / 9, s = glBufSize, i, j, k, n;
// entire array fits into current buffer
i = glArrList.length - 1;
if (glI + l <= s) {
glArrList[i].v.set(v, glI * 9);
glArrList[i].n.set(n, glI * 9);
glI += l;
}
else {
// copy as much as fits, then make new array and continue
for (j = 0; j < l; ++j) {
// copy triangle
for (k = 0; k < 9; ++k)
glArrList[i].v[glI * 9 + k] = v[j * 9 + k];
// increment pointer
glI++;
// end of array
if (glI == s) {
glI = 0;
glArrList.push({ v: new Float32Array(glBufSize * 9), n: new Float32Array(glBufSize * 9) });
i++;
}
}
}
// is last buffer filled up?
if (glI == s) {
glI = 0;
glArrList.push({ v: new Float32Array(glBufSize * 9), n: new Float32Array(glBufSize * 9) });
}
}
function shapeGabled(d, b, h, hilted) {
var c, i, j, k, l, ls = [], cx = [], cy = [],
dx1, dy1, dx2, dy2, dz2, nx, ny, nz;
// assumes a 4 corner poly
c = d[0];
// side lengths
for (i = 0; i < 4; ++i) {
dx1 = c[i][0] - c[i + 1][0];
dy1 = c[i][1] - c[i + 1][1];
ls[i] = Math.sqrt(dx1 * dx1 + dy1 * dy1);
}
// first node of long edge
j = (ls[0] + ls[2] > ls[1] + ls[3]) ? 0 : 1;
// roofline ends
cx[0] = 0.5 * (c[j][0] + c[j + 3][0]);
cy[0] = 0.5 * (c[j][1] + c[j + 3][1]);
cx[1] = 0.5 * (c[j + 1][0] + c[j + 2][0]);
cy[1] = 0.5 * (c[j + 1][1] + c[j + 2][1]);
// TODO: modify for hilted roof
dz2 = h - b;
// rectangular roof surfaces
for (k = 0; k < 2; ++k) {
i = k * 2 + j;
dx1 = c[i][0] - c[i + 1][0];
dy1 = c[i][1] - c[i + 1][1];
dx2 = cx[k] - c[i][0];
dy2 = cy[k] - c[i][1];
nx = -dy1 * dz2;
ny = dx1 * dz2;
nz = dy1 * dx2 - dx1 * dy2;
r = Math.sqrt(nx * nx + ny * ny + nz * nz);
nx /= r;
ny /= r;
nz /= r;
// triangle at base level
vnPush([c[i][0], c[i][1], b,
c[i + 1][0], c[i + 1][1], b,
cx[k], cy[k], h],
[nx, ny, nz,
nx, ny, nz,
nx, ny, nz]);
// triangle at roof level
vnPush([cx[k], cy[k], h,
cx[(k + 1) % 2], cy[(k + 1) % 2], h,
c[i + 1][0], c[i + 1][1], b],
[nx, ny, nz,
nx, ny, nz,
nx, ny, nz]);
}
// triangular roof surfaces
}
function shapePyramid(d, b, h) {
var c, i, j, l, cx = 0, cy = 0,
dx1, dy1, dx2, dy2, dz2, nx, ny, nz;
// pyramids only need outer contours
// winding order and center
c = d[0];
l = c.length - 1; area = 0;
if (l < 3) return;
for (i = 0; i < l; ++i) {
area += (c[i][0] * c[i + 1][1]) - (c[i + 1][0] * c[i][1]);
cx += c[i][0];
cy += c[i][1];
}
if (area > 0)
c.reverse();
cx /= l;
cy /= l;
dz2 = h - b;
for (i = 0; i < l; ++i) {
// normal vector (dx, dy, 0) x (0, 0, 1)
dx1 = c[i][0] - c[i + 1][0];
dy1 = c[i][1] - c[i + 1][1];
dx2 = cx - c[i][0];
dy2 = cy - c[i][1];
nx = -dy1 * dz2;
ny = dx1 * dz2;
nz = dy1 * dx2 - dx1 * dy2;
r = Math.sqrt(nx * nx + ny * ny + nz * nz);
nx /= r; ny /= r; nz /= r;
// triangle base to top
vnPush([c[i][0], c[i][1], b,
c[i + 1][0], c[i + 1][1], b,
cx, cy, h],
[nx, ny, nz,
nx, ny, nz,
nx, ny, nz]);
}
}
function triangulate(d, b, h, noroof) {
var tr, d0, c, i, j, l, good, area, dx, dy;
noroof = ~~noroof;
// enforce winding orders
for (j = 0; j < d.length; ++j) {
c = d[j];
l = c.length - 1;
area = 0;
if (l < 3) return;
for (i = 0; i < l; ++i)
area += (c[i][0] * c[i + 1][1]) - (c[i + 1][0] * c[i][1]);
area *= (j == 0) ? 1 : -1;
if (area > 0)
c.reverse();
}
// setup walls
for (j = 0; j < d.length; ++j) {