-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdaptiveCompositeMap.js
8721 lines (7430 loc) · 272 KB
/
AdaptiveCompositeMap.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
/* Build Time: January 19, 2015 02:00:24 PM */
/*globals LambertCylindricalEqualArea, ProjectionFactory */
function MapEvents(map) {"use strict";
// enlarge scale by this factor when the map is double tapped
var TOUCH_DOUBLE_TAP_SCALE_FACTOR = 1.8,
// relative change of scale for zooming with mouse scroll wheel
WHEEL_ZOMM_STEP = 1.12,
// If the central latitude is close to the equator, the central latitude snaps to the equator.
// This angle is the tolerance for a relative vertical scale of 1. The angle is
// linearly reduced for larger scales.
SNAP_TOLERANCE_ANGLE = 3 / 180 * Math.PI;
var prevDrag = [],
// map scale when a pinch open or pinch close touch starts
startTransformMapScale = null;
addEvtListener(map.getParent(), 'mousewheel', function(e) {
e = e || window.event;
var node, delta, zoomStep, localX = e.clientX, localY = e.clientY;
// correct for scrolled document
localX += document.body.scrollLeft + document.documentElement.scrollLeft;
localY += document.body.scrollTop + document.documentElement.scrollTop;
// correct for nested offsets in DOM
for ( node = parent; node; node = node.offsetParent) {
localX -= node.offsetLeft;
localY -= node.offsetTop;
}
// mouse wheel events differ depending on the platform
delta = 0;
if (e.wheelDelta) {
delta = e.wheelDelta / 40 / 3;
} else if (e.detail) {
delta = -e.detail / 3;
}
zoomStep = Math.pow(WHEEL_ZOMM_STEP, Math.abs(delta));
map.zoomBy(delta < 0 ? zoomStep : 1 / zoomStep, localX, localY);
// prevent page scroll
e.preventDefault();
});
// animated transition for double clicks and double taps
// zoom and center of the map are changed
function animateTransition(params) {
var anim, startLon, startLat, startScale, startTime, endTime;
startLon = map.getCentralLongitude();
startLat = map.getCentralLatitude();
startScale = map.getZoomFactor();
startTime = new Date().getTime();
endTime = startTime + params.duration;
anim = setInterval(function() {
var currTime, lon0, lat0, mapScale;
currTime = new Date().getTime();
if (currTime >= endTime) {
clearInterval(anim);
map.render();
} else {
lon0 = ((params.endLon - startLon) / (endTime - startTime)) * (currTime - startTime) + startLon;
lat0 = ((params.endLat - startLat) / (endTime - startTime)) * (currTime - startTime) + startLat;
map.setCenter(lon0, lat0);
mapScale = ((params.endScale - startScale) / (endTime - startTime)) * (currTime - startTime) + startScale;
map.setZoomFactor(mapScale);
}
}, 1000 / params.fps);
}
Hammer(map.getParent()).on("doubletap", function(ev) {
var xy, targetOffset, endLonLat, endScale;
xy = ev.gesture.touches[0];
targetOffset = $(xy.target).offset();
endLonLat = map.canvasXY2LonLat(xy.pageX - targetOffset.left, xy.pageY - targetOffset.top);
//if endLonLat are NaN use the current mapCenter for the Transition
if(isNaN(endLonLat[0]) || isNaN(endLonLat[1])) {
endLonLat[0] = map.getCentralLongitude();
endLonLat[1] = map.getCentralLatitude();
}
endScale = map.getZoomFactor() * TOUCH_DOUBLE_TAP_SCALE_FACTOR;
// FIXME
if (endScale > MERCATOR_LIMIT_2) {
endScale += 0.1;
}
animateTransition({
duration : 500,
fps : 30,
endLon : endLonLat[0],
endLat : endLonLat[1],
endScale : endScale,
onEnd : null,
fastRender : true
});
});
Hammer(map.getParent()).on("dragstart", function(ev) {
var xy, projection, canvasXY;
xy = ev.gesture.touches[0];
prevDrag.x = xy.pageX;
prevDrag.y = xy.pageY;
projection = map.updateProjection();
canvasXY = mouseToCanvasCoordinates({
clientX : xy.x,
clientY : xy.y
}, parent);
});
/**
* Adjust the central longitude and latitude. The geographic location under the point at startX/startY
* should move to the point at endX/endY. The points are in "page" coordinates.
*/
function moveMapCenter(startX, startY, endX, endY) {
var projection, startLonLat, endLonLat, dLon, dLat, canSnapToEquator, lon0, lat0, mapHeight, maxLat0, mapCanvasOrigin;
// convert from page coordinates to coordinates relative to the origin of the map DOM element.
mapCanvasOrigin = $(map.getParent()).offset();
startX -= mapCanvasOrigin.left;
startY -= mapCanvasOrigin.top;
endX -= mapCanvasOrigin.left;
endY -= mapCanvasOrigin.top;
projection = map.updateProjection();
startLonLat = map.canvasXY2LonLat(startX, startY);
endLonLat = map.canvasXY2LonLat(endX, endY);
dLon = endLonLat[0] - startLonLat[0];
dLat = endLonLat[1] - startLonLat[1];
if (isNaN(dLon) || isNaN(dLat) || dLon === 0 || dLat === 0) {
// pointer is outside the map graticule, or there is not change
return;
}
// snap to equator
canSnapToEquator = false;
if (map.isEquatorSnapping()) {
if (map.isUsingWorldMapProjection()) {
// A world map projection is used. Only snap if the map can be rotated.
canSnapToEquator = map.isRotateSmallScale();
} else {
// a projection for medium or large scales is used.
// Don't snap when a cylindrical projection is used. A changed central latitude results in a
// vertical shift for these projections.
canSnapToEquator = !( projection instanceof LambertCylindricalEqualArea);
}
// snap tolerance is getting smaller with increasing map scale
if (canSnapToEquator && Math.abs(map.getCentralLatitude() - dLat) < SNAP_TOLERANCE_ANGLE / Math.max(1, map.getZoomFactor())) {
// this will result in lat0 equal to 0
dLat = map.getCentralLatitude();
}
}
lon0 = map.getCentralLongitude() - dLon;
lat0 = map.getCentralLatitude() - dLat;
// the rotation of central latitude has to be limited for world maps that cannot be rotated
// when the user tries moving the globe vertically, the central latitude can take extreme values, but the graticule
// moving vertically. When the user later zooms in, the extreme value of the central latitude will create
// a confusing vertical rotation of the globe.
if (map.isUsingWorldMapProjection() && !map.isRotateSmallScale()) {
mapHeight = map.canvasXYToUnscaledXY(0, 0)[1];
// create a new projection, as the inverse of the default projection converts the current lat0 to a vertical shift.
// FIXME this does not work for most projections as toString is not a good method to identify projections.
// use the projection ID instead
projection = ProjectionFactory.getSmallScaleProjection(projection.toString());
maxLat0 = ProjectionFactory.smallScaleMaxLat0(mapHeight, projection);
if (lat0 > maxLat0) {
lat0 = maxLat0;
} else if (lat0 < -maxLat0) {
lat0 = -maxLat0;
}
}
map.setCenter(lon0, lat0);
}
Hammer(map.getParent()).on("drag", function(ev) {
var xy, endLonLatBefore, endXYAfter, d, counter = 0;
xy = ev.gesture.touches[0];
// FIXME
/*
do {
// geographic position of end position with old projection
endLonLatBefore = map.canvasXY2LonLat(xy.pageX, xy.pageY);
endXYAfter = map.lonLat2Canvas(endLonLatBefore[0], endLonLatBefore[1]);
console.log("test", endLonLatBefore[0] / Math.PI * 180, endLonLatBefore[1] / Math.PI * 180);
console.log(xy.pageX, xy.pageY, endXYAfter[0], endXYAfter[1]);
// move center of map, which can change the projection itself
moveMapCenter(prevDrag.x, prevDrag.y, xy.pageX, xy.pageY);
map.updateProjection();
// location of end position in new projection
endXYAfter = map.lonLat2Canvas(endLonLatBefore[0], endLonLatBefore[1]);
var dx = endXYAfter[0] - xy.pageX;
var dy = endXYAfter[1] - xy.pageY;
prevDrag.x = endXYAfter[0];
prevDrag.y = endXYAfter[1];
d = Math.sqrt(dx * dx + dy * dy);
counter += 1;
console.log(counter, "before", endLonLatBefore[0] / Math.PI * 180, endLonLatBefore[1] / Math.PI * 180, d);
console.log(xy.pageX, xy.pageY, endXYAfter[0], endXYAfter[1]);
}
while (counter < 10 && d > 1)
*/
//map.setCenter() in moveMapCenter renders the map
moveMapCenter(prevDrag.x, prevDrag.y, xy.pageX, xy.pageY);
prevDrag.x = xy.pageX;
prevDrag.y = xy.pageY;
map.getParent().style.cursor = 'move';
});
Hammer(map.getParent()).on("dragend", function(ev) {
map.getParent().style.cursor = 'default';
map.render(false);
});
// FIXME not tested
Hammer(map.getParent()).on("transformstart", function(ev) {
startTransformMapScale = map.getZoomFactor();
});
// FIXME not tested
Hammer(map.getParent()).on("transform", function(ev) {
if (startTransformMapScale === null) {
return;
}
/*
* ev.scale: The distance between two fingers since the start of an event as
* a multiplier of the initial distance. The initial value is 1.0. If less
* than 1.0 the gesture is pinch close to zoom out. If greater than 1.0 the
* gesture is pinch open to zoom in.
*/
map.setZoomFactor(ev.scale * startTransformMapScale);
// transition to the mercator slippy map
// FIXME ?
if (map.getZoomFactor() > MERCATOR_LIMIT_2) {
map.setZoomFactor(map.getZoomFactor() + 0.1);
startTransformMapScale = null;
}
map.render(true);
});
// FIXME not tested
Hammer(map.getParent()).on("transformend", function(ev) {
startTransformMapScale = null;
});
}
function canvasDashedLine(ctx, x, y, x2, y2, da) {
"use strict";
if (!da) {
da = [8, 4];
}
ctx.save();
var dx = (x2 - x),
dy = (y2 - y);
var len = Math.sqrt(dx * dx + dy * dy);
var rot = Math.atan2(dy, dx);
ctx.translate(x, y);
ctx.moveTo(0, 0);
ctx.rotate(rot);
var dc = da.length;
var di = 0,
draw = true;
x = 0;
while (len > x) {
x += da[di++ % dc];
if (x > len) {
x = len;
}
if (draw) {
ctx.lineTo(x, 0);
} else {
ctx.moveTo(x, 0);
}
draw = !draw;
}
ctx.restore();
}
function ProjectionDiagram(parent, diagramChangeListener) {
"use strict";
var DIAGRAM_WIDTH = 800,
DIAGRAM_HEIGHT = 300,
// maximum latitude for web mercator
WEB_MERCATOR_MAX_LAT = 1.4844222297453322,
// top and left margin for diagram lables
XYMARGIN = 25,
// maximum value on abscissa
MAX_SCALE = Math.ceil(MERCATOR_LIMIT_2 + 1),
// vertical distance between grid lines
DEG_DIST = 15;
// relative vertical position of projection names
var NAME_V_POS_REL = 0.45;
// font size and style
var FONT_LARGE_H = 12;
var FONT_MEDIUM_H = 12;
var FONT_SMALL_H = 11;
var FONT_LARGE = 'Bold ' + FONT_LARGE_H + 'px Sans-Serif';
var FONT_MEDIUM = FONT_MEDIUM_H + 'px Sans-Serif';
var FONT_SMALL = FONT_SMALL_H + 'px Sans-Serif';
// height of text line
var LINE_HEIGHT_LARGE = FONT_LARGE_H * 1.5;
var LINE_HEIGHT_MEDIUM = FONT_MEDIUM_H * 1.5;
// colors
var BACKGROUND_COLOR = '#fff',
TEXT_COLOR = '#000',
TEXT_HALO_COLOR = '#fff',
GRID_COLOR = '#eee';
var TEXT_HALO_WIDTH = 8;
// FIXME
var diagram = this;
var canvas = createCanvas('diagramCanvas', parent, DIAGRAM_WIDTH, DIAGRAM_HEIGHT);
var buttonCanvas = createCanvas('diagramCanvasButton', parent, DIAGRAM_WIDTH, DIAGRAM_HEIGHT);
// apply size to parent for proper layout
parent.style.width = DIAGRAM_WIDTH;
parent.style.height = DIAGRAM_HEIGHT;
// FIXME: a duplicate from map.js
// Compute scale factor such that the whole graticule fits onto the canvas.
// This defines mapScale = 1
function referenceScaleFactor() {
//var h = ProjectionFactory.halfCentralMeridianLengthOfSmallScaleProjection(proj);
var graticuleHeight = 2 * 1.5;
// FIXME ProjectionFactory.HALF_SMALL_SCALE_GRATICULE_HEIGHT;
var vScale = canvas.height / graticuleHeight;
return vScale;
}
// FIXME: a duplicate from map.js
var canvasXY2UnscaledXY = function(x, y, mapScale) {
var cx = canvas.width / 2;
var cy = canvas.height / 2;
x -= cx;
y = cy - y;
var scale = referenceScaleFactor() * mapScale;
return [x / scale, y / scale];
};
function verticalLine(ctx, scale) {
var hScale = (canvas.width - XYMARGIN) / MAX_SCALE;
ctx.beginPath();
ctx.moveTo((scale * hScale) + (XYMARGIN), XYMARGIN);
ctx.lineTo((scale * hScale) + (XYMARGIN), canvas.height);
ctx.stroke();
}
function solidLine(ctx, x1, y1, x2, y2, x3, y3) {
var hScale = (canvas.width - XYMARGIN) / MAX_SCALE;
var vScale = (canvas.height - XYMARGIN) / (Math.PI / 2);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
if (x3 && y3) {
ctx.lineTo(x3, y3);
}
ctx.stroke();
}
function dashedLine(ctx, x0, y0, x1, y1) {
ctx.beginPath();
canvasDashedLine(ctx, x0, y0, x1, y1);
ctx.closePath();
ctx.stroke();
}
function drawGrid(conf, ctx) {
var i;
var innerWidth = canvas.width - XYMARGIN;
var innerHeight = canvas.height - XYMARGIN;
// number of grid cells
var hDivisions = Math.floor(MAX_SCALE);
var vDivisions = 90 / DEG_DIST;
// grid cell size
var hgridSpace = innerWidth / hDivisions;
var vgridSpace = innerHeight / vDivisions;
//grid horizontal lines
ctx.fillStyle = GRID_COLOR;
for ( i = 1; i < vDivisions; i++) {
ctx.beginPath();
ctx.lineWidth = 0.25;
ctx.moveTo(XYMARGIN, XYMARGIN + vgridSpace * i);
ctx.lineTo(canvas.width, XYMARGIN + vgridSpace * i);
ctx.stroke();
}
//grid vertical lines
for ( i = 1; i < hDivisions; i++) {
ctx.beginPath();
ctx.lineWidth = 0.25;
ctx.moveTo(XYMARGIN + hgridSpace * i, XYMARGIN);
ctx.lineTo(XYMARGIN + hgridSpace * i, canvas.height);
ctx.stroke();
}
// labels for horizontal lines
ctx.textAlign = "right";
ctx.fillStyle = TEXT_COLOR;
ctx.font = FONT_MEDIUM;
ctx.textBaseLine = 'middle';
for ( i = 0; i <= vDivisions; i++) {
ctx.fillText((vDivisions - i) * DEG_DIST + '\u00B0', XYMARGIN - 2, XYMARGIN + vgridSpace * i);
}
//labels for vertical lines
ctx.textAlign = "center";
ctx.font = FONT_MEDIUM;
ctx.textBaseLine = 'top';
for ( i = 1; i < hDivisions; i++) {
ctx.fillText(i, XYMARGIN + i * hgridSpace, XYMARGIN - 3);
}
// label for last vertical line
ctx.fillText(hDivisions, XYMARGIN + i * hgridSpace - 6, (XYMARGIN - 3));
}
function text(t, x, y, ctx) {
ctx.strokeText(t, x, y);
ctx.fillText(t, x, y);
}
function drawLimitsAndText(conf, ctx) {
var i;
var isLandscape = (conf.canvasHeight / conf.canvasWidth) < conf.formatRatioLimit;
var isPortrait = (conf.canvasHeight / conf.canvasWidth) > 1 / conf.formatRatioLimit;
var isSquare = !(isLandscape || isPortrait);
var hScale = (canvas.width - XYMARGIN) / MAX_SCALE;
var vScale = (canvas.height - XYMARGIN) / (Math.PI / 2);
// diagram area
var innerWidth = canvas.width - XYMARGIN;
var innerHeight = canvas.height - XYMARGIN;
// vertical position of projection names
var tPos = innerHeight * NAME_V_POS_REL + XYMARGIN;
// vertical canvas coordinates
var yPolarUpperLat = XYMARGIN + innerHeight - conf.polarUpperLat * vScale;
var yPolarUpperLatDefault = XYMARGIN + innerHeight - conf.polarUpperLatDefault * vScale;
var yPolarLowerLat = XYMARGIN + innerHeight - conf.polarLowerLat * vScale;
var yCylindricalLowerLat = XYMARGIN + innerHeight - conf.cylindricalLowerLat * vScale;
var yCylindricalUpperLat = XYMARGIN + innerHeight - conf.cylindricalUpperLat * vScale;
var yWebMercatorLat = XYMARGIN + innerHeight - WEB_MERCATOR_MAX_LAT * vScale;
// horizontal canvas coordinates
var xLimit1 = XYMARGIN + (conf.zoomLimit1 * hScale);
var xLimit2 = XYMARGIN + (conf.zoomLimit2 * hScale);
var xLimit3 = XYMARGIN + (conf.zoomLimit3 * hScale);
var xLimit4 = XYMARGIN + (conf.zoomLimit4 * hScale);
var xLimit5 = XYMARGIN + (conf.zoomLimit5 * hScale);
var xMercator1 = XYMARGIN + (conf.mercatorLimit1 * hScale);
var xMercator2 = XYMARGIN + (conf.mercatorLimit2 * hScale);
drawGrid(conf, ctx);
// vertical scale limits
ctx.lineWidth = 2;
ctx.strokeStyle = '#000000';
verticalLine(ctx, conf.zoomLimit1);
verticalLine(ctx, conf.zoomLimit2);
verticalLine(ctx, conf.mercatorLimit1);
solidLine(ctx, xMercator2, canvas.height, xMercator2, yWebMercatorLat);
// oblique Mercator limit at poles
// FIXME
solidLine(ctx, xMercator1 - 50, XYMARGIN, xMercator1, yWebMercatorLat, canvas.width, yWebMercatorLat);
// for landscape format
if (isLandscape) {
// compute latitude on curve separating the azimuthal from the conic
var y = canvasXY2UnscaledXY(canvas.width / 2, 0, conf.zoomLimit4)[1];
var polarUpperLatAtXLimit4 = ProjectionFactory.polarLatitudeLimitForAlbersConic(y, conf.zoomLimit4);
var yPolarUpperLatAtXLimit4 = XYMARGIN + innerHeight - polarUpperLatAtXLimit4 * vScale;
// curved line separating the azimuthal from the conic
ctx.beginPath();
ctx.moveTo(xLimit4, yPolarUpperLatAtXLimit4);
var lat,
xPolarUpperLimitAtDefaultLat,
s = conf.zoomLimit4;
do {
xPolarUpperLimitAtDefaultLat = s * hScale + XYMARGIN;
y = canvasXY2UnscaledXY(canvas.width / 2, 0, s)[1];
lat = ProjectionFactory.polarLatitudeLimitForAlbersConic(y, s);
y = XYMARGIN + innerHeight - lat * vScale;
ctx.lineTo(xPolarUpperLimitAtDefaultLat, y);
s += 0.1;
} while (lat < conf.polarUpperLatDefault);
ctx.stroke();
/*
// FIXME hack: add transformation from azimuthal to cylindrical
//scale limit 3
dashedLine(ctx, xLimit2, XYMARGIN, xLimit4, yPolarUpperLatAtXLimit4);
dashedLine(ctx, xLimit4, yPolarUpperLatAtXLimit4, xLimit3, yPolarLowerLat);
dashedLine(ctx, xLimit3, yPolarLowerLat, xLimit3, yCylindricalUpperLat);
dashedLine(ctx, xLimit3, yCylindricalUpperLat, xLimit4, yCylindricalLowerLat);
// cylindrical upper latitude limit
dashedLine(ctx, xLimit3, yCylindricalUpperLat, xMercator1, yCylindricalUpperLat);
// cylindrical lower latitude limit
solidLine(ctx, xLimit4, yCylindricalLowerLat, xLimit5, yCylindricalLowerLat, xMercator1, yCylindricalLowerLat);
*/
//scale limit 3
dashedLine(ctx, xLimit2, XYMARGIN, xLimit4, yPolarUpperLatAtXLimit4);
dashedLine(ctx, xLimit4, yPolarUpperLatAtXLimit4, xLimit3, yPolarLowerLat);
dashedLine(ctx, xLimit3, yPolarLowerLat, xLimit3, yCylindricalLowerLat);
dashedLine(ctx, xLimit3, yCylindricalLowerLat, xLimit4, canvas.height);
// cylindrical upper latitude limit
dashedLine(ctx, xLimit3, yCylindricalUpperLat, xMercator1, yCylindricalUpperLat);
// cylindrical lower latitude limit
solidLine(ctx, xLimit4, canvas.height, xLimit5, yCylindricalLowerLat, xMercator1, yCylindricalLowerLat);
//scale limit 4
verticalLine(ctx, conf.zoomLimit4);
//scale limit 5
lat = ProjectionFactory.polarLatitudeLimitForAlbersConic(yPolarUpperLatDefault, conf.zoomLimit5);
y = canvasXY2UnscaledXY(canvas.width / 2, 0, conf.zoomLimit5)[1];
lat = ProjectionFactory.polarLatitudeLimitForAlbersConic(y, s);
y = Math.max(XYMARGIN + innerHeight - lat * vScale, yPolarUpperLatDefault);
dashedLine(ctx, xLimit5, y, xLimit5, yCylindricalLowerLat);
//polar upper and lower latitude
ctx.beginPath();
ctx.moveTo(xPolarUpperLimitAtDefaultLat, yPolarUpperLatDefault);
ctx.lineTo(xMercator1, yPolarUpperLatDefault);
ctx.stroke();
dashedLine(ctx, xLimit3, yPolarLowerLat, xMercator1, yPolarLowerLat);
} else if (isPortrait) {
verticalLine(ctx, conf.zoomLimit4);
verticalLine(ctx, conf.zoomLimit5);
}
//Text
ctx.fillStyle = TEXT_COLOR;
ctx.strokeStyle = TEXT_HALO_COLOR;
ctx.lineWidth = TEXT_HALO_WIDTH;
ctx.lineJoin = "bevel";
ctx.font = FONT_MEDIUM;
ctx.textBaseLine = 'top';
//Small-scale Projection Text
ctx.textAlign = "center";
ctx.font = FONT_LARGE;
var cx = conf.zoomLimit1 / 2 * hScale + XYMARGIN;
var cy = tPos - LINE_HEIGHT_LARGE;
var smallScaleProj = ProjectionFactory.getSmallScaleProjection(conf.smallScaleProjectionName);
if (smallScaleProj) {
var str = smallScaleProj.toString();
var strArray = str.split(' ');
for ( i = 0; i < strArray.length; i++) {
text(strArray[i], cx, cy += LINE_HEIGHT_LARGE, ctx);
}
}
ctx.font = FONT_MEDIUM;
cy += LINE_HEIGHT_MEDIUM;
text('normal', cx, cy, ctx);
cy += LINE_HEIGHT_MEDIUM;
text('aspect', cx, cy, ctx);
// Medium-scale projection text
cx = (xLimit2 + ( isSquare ? xMercator1 : xLimit4)) / 2;
cy = tPos;
ctx.font = FONT_LARGE;
text('Lambert', cx, cy, ctx);
text('Azimuthal', cx, cy += LINE_HEIGHT_LARGE, ctx);
ctx.font = FONT_MEDIUM;
text('oblique', cx, cy += LINE_HEIGHT_MEDIUM, ctx);
// Large-scale azimuthal
if (isLandscape) {
ctx.font = FONT_LARGE;
cx = (xLimit4 + xMercator1) / 2;
cy = XYMARGIN + 1.5 * FONT_LARGE_H;
text('Lambert Azimuthal', cx, cy, ctx);
ctx.font = FONT_MEDIUM;
text('polar aspect', cx, cy += LINE_HEIGHT_MEDIUM, ctx);
ctx.font = FONT_SMALL;
ctx.textBaseLine = 'middle';
cx = (xLimit5 + xMercator1) / 2;
text('Adjusted standard parallels', cx, (yPolarUpperLatDefault + yPolarLowerLat) / 2, ctx);
// large scale conic
ctx.font = FONT_LARGE;
cy = tPos;
text('Albers Conic', cx, cy, ctx);
ctx.font = FONT_MEDIUM;
text('normal aspect', cx, cy += LINE_HEIGHT_LARGE, ctx);
ctx.font = FONT_SMALL;
text('Standard parallels at 1/6 and 5/6', cx, cy += LINE_HEIGHT_MEDIUM, ctx);
// large scale cylindrical
ctx.font = FONT_LARGE;
ctx.textBaseLine = 'middle';
text('Lambert Cylindrical', cx, (yCylindricalLowerLat + canvas.height) / 2, ctx);
ctx.font = FONT_SMALL;
text('Adjusted standard parallels', cx, (yCylindricalLowerLat + yCylindricalUpperLat) / 2, ctx);
} else if (isPortrait) {
// large scale azimmuthal
ctx.font = FONT_LARGE;
cx = (xLimit4 + xMercator1) / 2;
cy = tPos;
text('Lambert Cylindrical', cx, cy, ctx);
ctx.font = FONT_MEDIUM;
text('transverse', cx, cy += LINE_HEIGHT_LARGE, ctx);
}
// Mercator text for largest scales
ctx.font = FONT_LARGE;
cx = (conf.mercatorLimit2 + MAX_SCALE) / 2 * hScale + XYMARGIN;
text('Mercator', cx, tPos, ctx);
}
this.renderButton = function(mapScale, lat0) {
buttonCanvas.width = DIAGRAM_WIDTH;
buttonCanvas.height = DIAGRAM_HEIGHT;
var ctx = buttonCanvas.getContext('2d');
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, buttonCanvas.width, buttonCanvas.height);
var r = 6;
var rCenter = 1;
var hScale = (buttonCanvas.width - XYMARGIN) / MAX_SCALE;
var vScale = (buttonCanvas.height - XYMARGIN) / (Math.PI / 2);
var x = (Math.min(MAX_SCALE, mapScale) * hScale) + XYMARGIN;
var y = buttonCanvas.height - Math.abs(lat0) * vScale;
ctx.save();
ctx.beginPath();
ctx.shadowColor = "gray";
ctx.shadowOffsetX = 1.5;
ctx.shadowOffsetY = 1.5;
ctx.shadowBlur = 3;
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI, false);
ctx.fillStyle = "#8ED6FF";
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = '#000000';
ctx.stroke();
ctx.fillStyle = "black";
ctx.fillRect(x - rCenter, y - rCenter, rCenter * 2, rCenter * 2);
ctx.restore();
};
function drawUpperLatitude(conf, ctx, hScale, vScale) {
var i,
n,
lineTo = function(ctx, x, y) {
ctx.moveTo(x, y);
lineTo = function(ctx, x, y) {
ctx.lineTo(x, y);
};
};
ctx.save();
ctx.beginPath();
ctx.lineWidth = 1;
ctx.strokeStyle = 'gray';
for ( i = 0,
n = upperLatitudeLimit.length; i < n; i += 1) {
var scale = MAX_SCALE / N_upperLatitudeLimit * i;
var lat = upperLatitudeLimit[i];
lineTo(ctx, XYMARGIN + hScale * scale, canvas.height - vScale * lat);
}
ctx.stroke();
ctx.restore();
}
this.render = function(conf) {
canvas.width = DIAGRAM_WIDTH;
canvas.height = DIAGRAM_HEIGHT;
var ctx = canvas.getContext('2d');
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!conf) {
return;
}
var hScale = (canvas.width - XYMARGIN) / MAX_SCALE;
var vScale = (canvas.height - XYMARGIN) / (Math.PI / 2);
ctx.fillStyle = BACKGROUND_COLOR;
ctx.fillRect(XYMARGIN, XYMARGIN, canvas.width - XYMARGIN, canvas.height - XYMARGIN);
drawUpperLatitude(conf, ctx, hScale, vScale);
drawLimitsAndText(conf, ctx);
// frame
ctx.beginPath();
ctx.strokeStyle = '#000000';
ctx.lineWidth = 2;
ctx.setTransform(1, 0, 0, 1, 0, 0);
var w = canvas.width - XYMARGIN - ctx.lineWidth / 2;
var h = canvas.height - XYMARGIN - ctx.lineWidth / 2;
ctx.strokeRect(XYMARGIN, XYMARGIN, w, h);
this.renderButton(conf.zoomFactor, conf.lat0);
};
function diagramMouseDown(e) {
function readDiagram(e) {
var hScale = (canvas.width - XYMARGIN) / MAX_SCALE;
var vScale = (canvas.height - XYMARGIN) / (Math.PI / 2);
var canvasXY = mouseToCanvasCoordinates(e, parent);
var mapScale = (canvasXY.x - XYMARGIN) / hScale;
var lat0 = (canvas.height - canvasXY.y) / vScale;
lat0 = Math.max(0, lat0);
diagram.renderButton(mapScale, lat0);
if (diagramChangeListener) {
diagramChangeListener(lat0, mapScale);
}
}
function diagramMouseMove(e) {
readDiagram(e);
canvas.style.cursor = 'move';
}
function diagramMouseUp(e) {
readDiagram(e);
stopDrag();
}
function stopDrag() {
document.body.style.cursor = null;
document.removeEventListener('mousemove', diagramMouseMove, false);
document.removeEventListener('mouseup', diagramMouseUp, false);
canvas.style.cursor = 'default';
}
document.addEventListener('mousemove', diagramMouseMove, false);
document.addEventListener('mouseup', diagramMouseUp, false);
}
// FIXME
var N_upperLatitudeLimit = 100;
var i;
var upperLatitudeLimit = [N_upperLatitudeLimit];
for ( i = 0; i <= N_upperLatitudeLimit; i += 1) {
var scale = MAX_SCALE / N_upperLatitudeLimit * i;
var y = canvasXY2UnscaledXY(canvas.width / 2, 0, scale)[1];
var lat = ProjectionFactory.polarLatitudeLimitForAlbersConic(y, scale);
upperLatitudeLimit[i] = lat;
}
canvas.parentNode.addEventListener('mousedown', diagramMouseDown, false);
// canvas is not selectable. Without this, the mouse changes to a text
// selection cursor while dragging on Safari.
canvas.onselectstart = function() {
return false;
};
}
function SphericalRotation(poleLat) {"use strict";
var sinLatPole, cosLatPole;
sinLatPole = Math.sin(poleLat);
cosLatPole = Math.cos(poleLat);
this.getPoleLat = function() {
return poleLat;
};
this.transform = function(lon, lat, res) {
var sinLon, cosLon, sinLat, cosLat, cosLat_x_cosLon;
sinLon = Math.sin(lon);
cosLon = Math.cos(lon);
sinLat = Math.sin(lat);
cosLat = Math.cos(lat);
cosLat_x_cosLon = cosLat * cosLon;
res[0] = aatan2(cosLat * sinLon, sinLatPole * cosLat_x_cosLon + cosLatPole * sinLat);
sinLat = sinLatPole * sinLat - cosLatPole * cosLat_x_cosLon;
res[1] = aasin(sinLat);
};
this.transformInv = function(lon, lat, res) {
var sinLon = Math.sin(lon), cosLon = Math.cos(lon), sinLat = Math.sin(lat), cosLat = Math.cos(lat);
var cosLat_x_cosLon = cosLat * cosLon;
res[0] = aatan2(cosLat * sinLon, sinLatPole * cosLat_x_cosLon - cosLatPole * sinLat);
res[1] = aasin(sinLatPole * sinLat + cosLatPole * cosLat_x_cosLon);
};
}
// cross-browser requestAnimationFrame and cancelAnimationFrame
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
( function() {
var x, lastTime = 0;
var vendors = ['webkit', 'moz'];
for ( x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
function isPowerOfTwo(x) {"use strict";
/*jslint bitwise:true */
return (x & (x - 1)) === 0;
}
function nextHighestPowerOfTwo(x) {"use strict";
var i;
x -= 1;
/*jslint bitwise:true */
for ( i = 1; i < 32; i <<= 1) {
x = x | x >> i;
}
/*jslint bitwise:false */
return x + 1;
}
function createCanvas(id, parent, desiredWidthInCSSPixels, desiredHeightInCSSPixels) {"use strict";
var devicePixelRatio, canvas;
canvas = document.createElement('canvas');
canvas.setAttribute("id", id);
// FIXME remove absolute positioning
canvas.style.position = 'absolute';
canvas.style.left = '0px';
canvas.style.top = '0px';
resizeCanvasElement(canvas, desiredWidthInCSSPixels, desiredHeightInCSSPixels);
// canvas is not selectable. Without this, the mouse changes to a text
// selection cursor while dragging on Safari.
canvas.onselectstart = function() {
return false;
};
parent.appendChild(canvas);
return canvas;
}
function resizeCanvasElement(canvas, desiredWidthInCSSPixels, desiredHeightInCSSPixels) {"use strict";
// http://www.khronos.org/webgl/wiki/HandlingHighDPI
// set the display size of the canvas.
canvas.style.width = desiredWidthInCSSPixels + "px";
canvas.style.height = desiredHeightInCSSPixels + "px";
// set the size of the drawingBuffer
var devicePixelRatio = window.devicePixelRatio || 1;
// FIXME disable for now, layers need to be updated first
devicePixelRatio = 1;
// FIXME crash on Mac Firefox
canvas.width = desiredWidthInCSSPixels * devicePixelRatio;
canvas.height = desiredHeightInCSSPixels * devicePixelRatio;
}
// http://stackoverflow.com/questions/646628/javascript-startswith
if ( typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function(str) {
return this.slice(0, str.length) == str;
};
}
if ( typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function(suffix) {"use strict";
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
// from High Performance JavaScript
if (!String.prototype.trim) {
String.prototype.trim = function() {
var str = this.replace(/^\s+/, ""), end = str.length - 1, ws = /\s/;
while (ws.test(str.charAt(end))) {
end -= 1;
}
return str.slice(0, end + 1);
};
}
// O'Reilly JavaScript Patterns
if ( typeof Array.isArray === "undefined") {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === "[object Array]";
};
}
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(needle) {
var i;
for ( i = 0, n = this.length; i < n; i++) {
if (this[i] === needle) {
return i;
}
}
return -1;
};
}
function addEvtListener(element, eventName, callback) {"use strict";
if ( typeof (element) === "string") {
element = document.getElementById(element);
}
if (element === null) {
return;
}
if (element.addEventListener) {
if (eventName === 'mousewheel') {
element.addEventListener('DOMMouseScroll', callback, false);
}
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
}
function removeEvtListener(element, eventName, callback) {
if ( typeof (element) == "string")
element = document.getElementById(element);
if (element == null)
return;
if (element.removeEventListener) {
if (eventName == 'mousewheel')
element.removeEventListener('DOMMouseScroll', callback, false);
element.removeEventListener(eventName, callback, false);
} else if (element.detachEvent)
element.detachEvent("on" + eventName, callback);
}
function cancelEvent(e) {
e = e ? e : window.event;
if (e.stopPropagation)
e.stopPropagation();
if (e.preventDefault)
e.preventDefault();
e.cancelBubble = true;