-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspace.js
1610 lines (1482 loc) · 72.2 KB
/
space.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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SPACE.JS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Space(div, pop) // CONSTRUCTOR
{
this.div="#"+div; // Current div selector
this.pop=pop; // Point at popup lib
this.showBoxes=false; // Show boxes
this.showRoads=false; // Hide Roads/borders
this.showScale=true; // Show scale
this.timeFormat="Mon Day, Year"; // Default time format
this.panTime=.5; // Time to pan to position
this.overlays=[]; // Holds overlay layers
}
Space.prototype.UpdateMap=function(curTime, timeFormat, panTime) // UPDATE MAP
{
this.timeFormat=timeFormat; // Set format
this.curTime=curTime-0; // Set current time
this.DrawMapLayers(); // Redraw map
this.panTime=panTime; // Time to pan to position
}
Space.prototype.DrawMapLayers=function() // DRAW OVERLAY LAYERS
{
var i,j,o,a,vis,sty;
if (this.canvasContext) { // If a canvas up
this.canvasContext.clearRect(0,0,this.canvasWidth,this.canvasHeight); // Clear canvas
var cTime=Math.round(this.curTime); // Save rounded time
for (i=0;i<this.overlays.length;i++) { // For each overlay layer
o=this.overlays[i]; // Get ptr to layer
if (!o.start && !o.end) vis=true; // Both start/end 0, make it visible
else if (o.start) vis=false; // If only a start set, hide until it comes up
else if (o.end) vis=true; // If only an end set, show until until it comes down
else vis=false; // Both set, hide until it comes up
if (o.start && (cTime >= o.start)) vis=true; // If past start and start defined, show it
if (o.end && (cTime > o.end)) vis=false; // If past end and end defined, hide it
if (o.show && o.show.match(/g/i)) { // If only showing if timeline segment is active
vis=false; // Assume off
if ((o.start >= tln.curStart) && (o.start < tln.curEnd)) // In current time
vis=true; // Show it
}
a=((o.opacity == undefined) || (o.opacity == "")) ? 1 : o.opacity; // Use defined opacity or 1
if (o.show == "off") vis=false; // If hidded in spreadsheet, hide it
if (o.vis == "off") vis=false; // If toggled hidden, hide it
else if (o.vis == "on") vis=true; // If toggled showing, show it
if (!dtl.ShowElement(o.tag)) vis=false; // If not being shown, hide it
if (vis && (o.type == "image")) { // If a visible image
a=((o.alpha == undefined) || (o.alpha == "")) ? 1 : o.alpha; // Let alpha control opacity if defined
o.src.drawMapImage(a,this); // Draw it
}
else if (o.type == "kml") { // If a kml
a=((o.alpha == undefined) || (o.alpha == "")) ? 1 : o.alpha; // Let alpha control opacity if defined
if (!vis) a=0; // Hide if invisible
o.src.set('visible',vis); // Show/hide it
o.src.set("opacity",a); // Set opacity
}
else if (o.type == "icon") { // If an icon
sty=o.src.getStyle(); // Point at style
if (!sty) continue; // Skip if no style
sty.getImage().setOpacity(vis ? 1: 0); // Set icon opacity
sty.getText().setScale(vis ? 1 : 0); // Set text opacity
}
else if (o.type == "path") // If a path
this.DrawPath(i,this.curTime,vis) // Show it
else if (o.type == "choro") // If a choro
this.DrawChoropleth(i,this.curTime,vis) // Show it
}
}
if (this.geoRef) // If georeferencing
this.geoRef.img.drawMapImage(this.geoRef.a,this); // Show pic we're referencing
this.markerLayer.getSource().changed(); // Redraw marker layer
}
Space.prototype.InitMap=function(inActive) // INIT OPENLAYERS MAP
{
this.showBoxes=false; // Show boxes
this.showRoads=false; // Hide Roads/borders
this.showScale=true; // Hide or show scale
this.curProjection="EPSG:3857"; // Current projection
this.layers=[ // Hold layers
new ol.layer.Tile({ // Sat
visible: false, // Invisible
source: new ol.source.BingMaps({ // Bing
key: 'Avsv5vFZCgNtp6DYSf4dVOx_TG6ZREK9ivsaGcjebscLVe5EcC2078H3YmLNRncB',
imagerySet:"Aerial"
}),
projection: this.curProjection, // Default projection
title: "Satellite" // Set name
}),
new ol.layer.Tile({ // Terrain
visible: false, // Invisible
source: new ol.source.Stamen({layer: 'terrain'}), // Stamen terrain
projection: this.curProjection, // Default projection
title: "Terrain" // Set name
}),
new ol.layer.Tile({ // Watercolor
visible: false, // Invisible
source: new ol.source.Stamen({layer: 'watercolor'}), // Stamen watercolor
projection: this.curProjection, // Default projection
title: "Watercolor" // Set name
}),
new ol.layer.Tile({ // Toner
visible: false, // Invisible
source: new ol.source.Stamen({layer: 'toner-lite'}), // Stamen toner
projection: this.curProjection, // Default projection
title: "B&W" // Set name
}),
new ol.layer.Tile({ // Earth
visible: false, // Invisible
source: new ol.source.Stamen({layer: 'terrain-background'}), // Stamen plain terrain
projection: this.curProjection, // Default projection
title: "Earth" // Set name
}),
new ol.layer.Tile({ // Roadmap
visible:false, // Hide it
source: new ol.source.OSM(), // OSM
projection: this.curProjection, // Default projection
title: "Roadmap" // Set name
}),
];
this.featureSelect=new ol.interaction.Select(); // Create select interaction
this.featureSelect.setActive(false); // Turn select off
this.map=new ol.Map( { target: this.div.substr(1), // Alloc OL
loadTilesWhileInteracting: true, // Load tiles
layers:this.layers, // Layers array
interactions: inActive ? [] : ol.interaction.defaults().extend([this.featureSelect]), // Add feature select interaction
controls: inActive ? [] : ol.control.defaults({ // Controls
}).extend([ new ol.control.ScaleLine() ]), // Add scale
view: new ol.View({ // Views
center: ol.proj.transform( [-77,34],'EPSG:4326',this.curProjection),
minZoom: 2, maxZoom: 16, zoom: 3 })
});
this.SetBaseMap("Roadmap"); // Set basemap
this.CreateOverlayLayer(); // Canvas and vector layer for map overlays
this.InitPopups(); // Init popup layer
var _this=this; // Save context for callback
this.map.on('click', function(e) { // ON CLICK
var c=ol.proj.transform(e.coordinate,_this.curProjection,'EPSG:4326'); // Get center
$("#setpoint").val(Math.floor(c[0]*10000)/10000+","+Math.floor(c[1]*10000)/10000);
if (e.originalEvent.altKey) { // If alt key presssed
if (eds.curMode == "story") // Editing a story
$("#esWhere").val(_this.GetView(true)); // Set editor with view
else // Anything else
$("#esWhere").val($("#setpoint").val()); // Set editor with point
$("#setstartpos").val(_this.GetView(true)); // Set settings editor with view
}
if (e.originalEvent.shiftKey) { // If shift key presssed
var lay;
var feature=_this.map.forEachFeatureAtPixel(e.pixel, // Look through features
function(feature, layer) { // On match to location
lay=layer; // Save layer
return feature; // Return feature
});
if (feature) { // If one found
var j;
var id=feature.getId(); // Get feature id
if (lay && (lay.get("kmlId"))) { // If a KML id
var fa=lay.getSource().getFeatures(); // Point at all features
for (j=0;j<fa.length;++j) // For each one
if (fa[j].getId() == id) // A match for id
$("#setpoint").val(lay.get('kmlId')+":"+j); // Show id index
}
}
}
});
this.map.on('moveend', function(e) { // On end of move
_this.DrawMapLayers(); // Redraw maps in new extent, if moved
var o=_this.map.getView(); // Point at view
var c=ol.proj.transform(o.getCenter(),_this.curProjection,'EPSG:4326'); // Get center
var r=o.getResolution(); // Get res
r/=1440*curJson.leftRightSplit/$(_this.div).width(); // Resize to normalized screen
var pos=Math.floor(c[1]*10000)/10000+"|"+Math.floor(c[0]*10000)/10000+"|"+r+"|";
pos+=Math.floor((o.getRotation()*180/Math.PI)*1000)/-1000; // Rotation
$("#setwhere").val(Math.floor(c[0]*10000)/10000+","+Math.floor(c[1]*10000)/10000+","+Math.round(r));
_this.SendMessage("move",pos+"|scroll"); // Send that view has changed
});
}
Space.prototype.GetMapFeatureId=function(x, y) // GET FEATURE ID AT PIXEL
{
var id=this.map.forEachFeatureAtPixel([x,y], // Look through features
function(feature, layer) { // On match to location
return feature.getId(); // Return feature
});
if (id) return id; // Return feature
else return -1; // Nothing found
}
Space.prototype.GetView=function(adjust) // GET CURRENT VIEW
{
var o=this.map.getView(); // Point at view
var r=o.getResolution(); // Get res
var c=ol.proj.transform(o.getCenter(),this.curProjection,'EPSG:4326'); // Get center
if (adjust)
r/=1440*curJson.leftRightSplit/$(this.div).width(); // Resize to normalized screen
return (Math.floor(c[0]*10000)/10000+","+Math.floor(c[1]*10000)/10000+","+Math.round(r));
}
Space.prototype.UpdateMapSize=function() // UPDATE MAP SIZE TO MATCH DIV
{
if (this.map) { // If OL initted
this.map.updateSize(); // Update map
var y=$(this.div).offset().top+$(this.div).height()-30; // Y pos
$("#findPlaceDiv").css({ left:"6px",top:y+"px" }); // Move it and show
}
}
Space.prototype.SetBaseMap=function(newMap) // SET BASE MAP
{
if (this.map) // If OL initted
for (i=0;i<this.layers.length;++i) // For each layer
this.layers[i].set('visible',this.layers[i].get("title") == newMap); // Set visibility
}
Space.prototype.Goto=function(pos) // SET VIEWPOINT
{
if (!pos) return; // No where to go
var duration,manual=false;
if (pos.charAt(0) == '*') { // If hand animating
pos=pos.substr(1); // Remove *
manual=true; // Set flag
}
pos=pos.replace(/"/g,""); // Remove quotes
var v=pos.split(","); // Split up
if (v.length < 2) return; // Not enough coords
if (isNaN(v[0]) || isNaN(v[1]) || isNaN(v[0])) return; // Invalid coord
var o=this.map.getView(); // Point at view
var c=ol.proj.transform([v[0]-0,""+v[1].replace(/\*/,"")-0],'EPSG:4326',this.curProjection); // Get center
var fc=o.getCenter(); // Get from center
var fs=o.getResolution(); // Get from resolution
fs/=1440*curJson.leftRightSplit/$(this.div).width(); // Resize to normalized screen
if ((Math.abs(fc[0]-c[0]) < 2) && (Math.abs(fc[1]-c[1]) < 2) && // Center match
(Math.abs(fs-v[2]) < 3) && (Math.abs(fs-v[2]) < 3)) // Resolution match
return; // Quit
if (!v[2]) v[2]=fs; // If no res set, use current one
if (v[3]) duration=v[3]*1000; // Set duration from pos
else duration=Math.abs(this.panTime*1000); // Use global duration
v[2]*=1440*curJson.leftRightSplit/$(this.div).width(); // Resize to normalized screen
if (manual) { // If animating
clearInterval(this.gotoPosTimer); // Stop timer, if open
this.gotoStartTime=new Date().getTime(); // Start time
this.gotoPosDur=duration; // Duration
this.gotoStartPos=[fc[0],fc[1],fs]; // Start
this.gotoEndPos=[c[0],c[1],v[2]] // End
this.gotoPosTimer=setInterval(function() {mps.GoToPos() },42); // Animate map
return; // Quit
}
if (duration < 0) { // If bouncing
duration=-duration; // Make positive
var bounce=v[2]*2; // Bounce up height
o.animate({ center: c, duration: duration*2 }); // Animate pan
o.animate({ resolution: bounce , duration: duration }, { // Animate zoom in 2 parts
resolution: v[2], duration: duration }); // Second part
}
else o.animate({ // Animate to position
center: c, // Set center
resolution: v[2], // Set res
duration:duration // Set duration
});
}
Space.prototype.GoToPos=function() // ANIMATE TO A POSITION OVER TIME
{
var o=this.map.getView(); // Point at view
var pct=(new Date().getTime()-mps.gotoStartTime)/mps.gotoPosDur; // Get percentage
if (pct >= 1) { // If done
clearInterval(mps.gotoPosTimer); // Stop timer
pct=1; // Go to end
}
var start=mps.gotoStartPos; // Point at start
var end=mps.gotoEndPos; // Point at end
pct=1.0-((Math.cos(3.1414*pct)+1)/2.0); // Full cosine curve
o.setCenter([start[0]+((end[0]-start[0])*pct)-0,start[1]+((end[1]-start[1])*pct)-0]);
o.setResolution(start[2]+((end[2]-start[2])*pct)-0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMAGE OVERLAY
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Space.prototype.CreateOverlayLayer=function() // CREATE CANVAS/VECTOR OVERLAY LAYERS
{
var _this=this; // Save context for callback
this.canvasLayer=new ol.layer.Image( { // Make new image layer
source: new ol.source.ImageCanvas( { // Add canvas sourcw
canvasFunction: function(extent, res, pixelRatio, size, proj) { // Render function
if (!_this.canvasCanvas) // If no canvas yet
_this.canvasCanvas=document.createElement('canvas'); // Alloc canvas
_this.canvasWidth=size[0]; // Get width
_this.canvasHeight=size[1]; // Hgt
_this.canvasCanvas.setAttribute('width',size[0]); // Set canvas width
_this.canvasCanvas.setAttribute('height',size[1]); // Hgt
_this.canvasContext=_this.canvasCanvas.getContext('2d'); // Get context
_this.canvasExtent=extent; // Set extent
_this.canvasRes=res; // Set res
_this.DrawMapLayers(); // Make images
return _this.canvasCanvas // Return canvas reference
},
projection: this.curProjection // Projection
})
});
this.map.addLayer(this.canvasLayer); // Add image layer to map
this.markerLayer=new ol.layer.Vector( { // Make new vector layer
source: new ol.source.Vector({}), // Add source
projection: this.curProjection // Projection
})
this.map.addLayer(this.markerLayer); // Add layer to map
}
Space.prototype.MarkerLayerToTop=function() // MOVE MARKER LAYER ON TOP OF OTHER LAYERS
{
var layer=this.map.getLayers().remove(this.markerLayer); // Remove marker layer
var n=this.map.getLayers().getArray().length; // Last index
this.map.getLayers().insertAt(n,layer); // Place on top
}
Space.prototype.AddChoroplethLayer=function(col, ecol, ewid,opacity, base, where, start, end, id) // ADD CHORPLETH LAYER
{
var o={};
if (!opacity) opacity=1; // Default opacity
o.type="choro"; // Path
o.start=start; o.end=end; o.col=col; o.opacity=opacity; // Save parameters
o.ecol=ecol; o.ewid=ewid; o.where=where; o.base=base; // Save parameters
o.tag=curJson.mobs[id].tag; o.show=curJson.mobs[id].show; // Add id into tag
o.mob=id; // Save original mob id
o.styles=[]; // Alloc orginal style array
var index=this.overlays.length; // Get index
this.overlays.push(o); // Add to overlay
return index; // Return layer index
}
Space.prototype.DrawChoropleth=function(num, time, vis) // DRAW CHOROPLETH
{
var i,v,os,sty;
var fr,fb,fg,fa,er,eg,eb,ea,ew;
var o=this.overlays[num]; // Point at overlay
var features=this.overlays[o.base].src.getSource().getFeatures(); // Get KML feature array
if (!o.styles.length) { // No original styles yet
for (i=0;i<features.length;++i) { // For each feature in KML
o.styles[i]={ fr:0,fb:0,fg:255,fa:0, // Alloc style storage object
er:0,eg:0,eb:0,ea:0,ew:2 }; // Defaults
if (features[i].get("Style")) { // If an embedded style
os=features[i].get("Style")[0]; // Get embedded style
if (os.getFill()) { // If a fill
c=os.getFill().getColor(); // Get color
o.styles[i].fr=c[0]; o.styles[i].fg=c[1]; // Set colors
o.styles[i].fb=c[2]; o.styles[i].fa=c[3];
}
if (os.getStroke()) { // If a stroke
c=os.getStroke().getColor(); // Get edge color
o.styles[i].er=c[0]; o.styles[i].eg=c[1]; // Set colors
o.styles[i].eb=c[2]; o.styles[i].ea=c[3];
o.styles[i].ew=os.getStroke().getWidth(); // Get edge width
}
}
sty=new ol.style.Style( { // Alloc style
fill: new ol.style.Fill( { color:[o.styles[i].fr,o.styles[i].fg,o.styles[i].fb,o.styles[i].fa] } ), // Fill
stroke: new ol.style.Stroke( { color:[o.styles[i].er,o.styles[i].eg,o.styles[i].eb,o.styles[i].ea], width: o.styles[i].ew-0 }) // Edge
});
features[i].setStyle(sty); // Style it
}
}
var v=o.where.split(":")[1].split(","); // Get features
if (o.where.match(/from/i)) { // Must be a query
var dst=[];
v=o.where.match(/(.+) *from *(\S+) *(.+)/i); // Split into base, data, and query parts
if ((i=FindMobByID(v[2])) < 0) // Get index of data element
return; // Quit of not found
if (!(i=curJson.mobs[i].src)) // No data yet
return; // Quit of not found
dtl.Query(i,dst,v[3],"feature"); // Run query on data
v=[]; // A blank slate
for (i=1;i<dst.length;++i) // For each one found (skip header)
v.push(dst[i][0]); // Add id to list to select
}
if (o.col && (o.col.length == 4)) o.col+=o.col.substr(1,3); // Turn #555 into #55555
if (o.ecol && (o.ecol.length == 4)) o.ecol+=o.ecol.substr(1,3); // Turn #555 into #55555
for (i=0;i<features.length;++i) { // For each feature
s=o.styles[i]; // Point at style
fr=s.fr; fg=s.fg; fb=s.fb; fa=s.fa; // Get original values
er=s.er; eg=s.eg; eb=s.eb; ea=s.ea; ew=s.ew; // Get original values
if (!o.start || (time >= o.start) && (time < o.end)) { // If showing
for (j=0;j<v.length;++j) { // For each potential match
if (v[j] == i) { // A match
if (o.col) { // If a fill
fr=parseInt("0x"+o.col.substr(1,2),16); // R
fg=parseInt("0x"+o.col.substr(3,2),16); // G
fb=parseInt("0x"+o.col.substr(5,2),16); // B
fa=o.opacity; // A
}
if (o.ecol) { // If an edge
er=parseInt("0x"+o.ecol.substr(1,2),16); // R
eg=parseInt("0x"+o.ecol.substr(3,2),16); // G
eb=parseInt("0x"+o.ecol.substr(5,2),16); // B
ea=o.opacity; // A
}
if (o.ewid) // If an edge width
ew=o.ewid; // W
}
}
}
sty=new ol.style.Style( { // Alloc style
fill: new ol.style.Fill( { color:[fr,fg,fb,fa] } ), // Fill
stroke: new ol.style.Stroke( { color:[er,eg,eb,ea], width:o.ewid-0 }) // Edge
});
features[i].setStyle(sty); // Style it
}
}
Space.prototype.AddPathLayer=function(dots, col, wid, opacity, start, end, show, header, id) // ADD PATH LAYER TO MAP
{
var i,v,o={};
var index=this.overlays.length; // Get index
o.type="path"; // Path
o.start=start; o.end=end; o.show=show; o.header=header // Save start, end, show, and header
o.tag=curJson.mobs[id].id; // Add id into tag
o.mob=id; // Save original mob id
if (show && show.match(/h/i)) o.end=999999999999999999; // Infinite end is holding
this.overlays.push(o); // Add to overlay
o.src=new ol.Feature({ geometry: new ol.geom.LineString(dots)}); // Create line
o.id="Path-"+this.markerLayer.getSource().getFeatures().length; // Make path id
o.src.setId(o.id); // Set id of path
o.dots=dots; // Save dots
if (!col) col="#990000"; // Default color
if (!wid) wid=2; // Default wid
if (!opacity) opacity=1; // Default opacity
if (col.length == 4) col+col.substr(1,3); // Turn #555 into #55555
var r=parseInt("0x"+col.substr(1,2),16); // R
var g=parseInt("0x"+col.substr(3,2),16); // G
var b=parseInt("0x"+col.substr(5,2),16); // B
var sty=new ol.style.Style({ // Make style
stroke: new ol.style.Stroke({ color: [r,g,b,opacity], width: wid-0
})
});
o.src.setStyle(sty); // Set style
this.markerLayer.getSource().addFeature(o.src); // Add it
return index; // Return feature
}
Space.prototype.DrawPath=function(num, time, vis) // DRAW PATH
{
var s,e,pct,v=[],i=0,last;
var o=this.overlays[num]; // Point at overlay
if (!o.dots.length) // No dots
return; // Quit
v.push([o.dots[0][0],o.dots[0][1]]); // Add moveto dot
if (o.header) { // If a header defined
var head=this.overlays[num-1].src; // Point at header feature
var sty=head.getStyle(); // Get header style
if (sty) // If a head style
sty.getImage().setOpacity(0); // Hide
}
var animate=false; // No animation
var end=o.end; // End at last dot
if (o.show && o.show.match(/a/i)) animate=true; // Animate last dot
for (e=1;e<o.dots.length;++e) { // For each lineto dot
s=e-1; // Point at start of line
last=v.length-1; // Point at last dot
if ((time >= o.dots[last][2]) && (time < end)) { // This one's active
v.push([o.dots[e][0],o.dots[e][1]]); // Add end dot
last++; // Point at last dot
if ((time < o.dots[e][2]) && animate){ // If before end of end dot and animating
pct=(time-o.dots[s][2])/(o.dots[e][2]-o.dots[s][2]); // Get pct
v[last][0]=o.dots[s][0]+((o.dots[e][0]-o.dots[s][0])*pct); // Interpolate x
v[last][1]=o.dots[s][1]+((o.dots[e][1]-o.dots[s][1])*pct); // Interpolate y
if (head && sty && (pct < 1)) { // If a header defined
head.setGeometry(new ol.geom.Point(v[last])); // Move it
if (dtl.ShowElement(o.tag)) // If not hidden
sty.getImage().setOpacity(1); // Show it
}
}
}
}
if (!vis) v=[]; // If hidden, hide it
o.src.setGeometry(new ol.geom.LineString(v)); // Set new dots
}
Space.prototype.AddMarkerLayer=function(pos, style, id, start, end, show) // ADD MARKER LAYER TO MAP
{
var o={};
o.type="icon"; // Icon
o.start=start; o.end=end; o.show=show; // Save start, end, show
o.mob=id; // Save original mob id
if (!isNaN(id)) o.tag=curJson.mobs[id].id; // Add id into tag, if not a header
var index=this.overlays.length; // Get index
this.overlays.push(o); // Add to overlay
var v=(""+pos).split(","); // Split into parts
var c=ol.proj.transform([v[0]-0,""+v[1]-0],'EPSG:4326',this.curProjection); // Transform
o.src=new ol.Feature({ geometry: new ol.geom.Point(c) }); // Create feature at coord
o.src.setId("Mob-"+id); // Set id of mob
try { // Try
this.markerLayer.getSource().addFeature(o.src); // Add it
this.StyleMarker([index],style); // Style marker
}
catch(err) { // If error
trace("CATCH ERR ->",err); // Show it
this.overlays.pop(); // Add to overlay
return -1; // Return -1
}
return index; // Return feature
}
Space.prototype.StyleMarker=function(indices, sty) // STYLE MARKERS(s)
{
var i,image;
var w2=sty.w ? sty.w*.6667 : 8; // Set size
if (sty.f && sty.f != "none") { // If fill spec'd
if (sty.f.length == 4) sty.f+=sty.f.substr(1,3); // Turn #555 into #55555
r=parseInt("0x"+sty.f.substr(1,2),16); // R
g=parseInt("0x"+sty.f.substr(3,2),16); // G
b=parseInt("0x"+sty.f.substr(5,2),16); // B
var fill=new ol.style.Fill({ color: [r,g,b,sty.a ? sty.a : 1]}); // Create fill with alpha
}
if (sty.s) { // If stroke spec'd
if (sty.s.length == 4) sty.s+=sty.s.substr(1,3); // Turn #555 into #55555
r=parseInt("0x"+sty.s.substr(1,2),16); // R
g=parseInt("0x"+sty.s.substr(3,2),16); // G
b=parseInt("0x"+sty.s.substr(5,2),16); // B
var stroke=new ol.style.Stroke({ color: [r,g,b,sty.a ? sty.a : 1], width:1 }); // Create stroke with alpha & width
}
if (sty.w) w2=sty.w/2; // If spec'd use it
switch(sty.m.toLowerCase()) { // Route on marker style
case "square":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 4, angle: Math.PI/4
});
break;
case "star":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 5, radius2: w2/2
});
break;
case "diamond":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 4
});
break;
case "triup":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 3
});
break;
case "tridown":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 3,angle: Math.PI
});
break;
case "triright":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 3,angle: Math.PI/2
});
break;
case "trileft":
image=new ol.style.RegularShape({
radius: w2, fill: fill, stroke:stroke, points: 3,angle: -Math.PI/2
});
break;
default:
if (sty.m && sty.m.match(/\/\//)) // Must be an image file
image=new ol.style.Icon({ src: sty.m }); // Add icon
else{ // Default to dot
image=new ol.style.Circle({ // Add circle
radius: w2*3/4, fill: fill, stroke:stroke
});
}
break;
}
var tf=sty.tf, dw=1, dc="";
if (sty.tf) { // If a tf style
var v=sty.tf.split(" "); // Split into parts
tf=v[0]+" "+v[1]+" "+v[2]; // Put back size/font
dw=v[3] ? v[3].replace(/px/i,"")-0 : 1; // Get size if set
dc=v[4] ? v[4] : "#666"; // Get col if set
}
var text=new ol.style.Text( { // Text style
textAlign: "center", textBaseline: "top", // Set alignment
font: tf, // Set font
text: sty.t ? sty.t : " ", // Get label
fill: new ol.style.Fill({color: sty.tc }), // Set color
stroke: new ol.style.Stroke({color: dc, width: dw }), // Outline
offsetY: w2, // Set offset
});
if (sty.m.toLowerCase() == "ndot") { // A numbered dot
text.setOffsetY(-6); // Center
text.getFill().setColor("#fff"); // White text
}
var s=new ol.style.Style({ // Create new style
image: image, text: text // Add icon, text
});
for (i=0;i<indices.length;++i) // For each layer
this.overlays[indices[i]].src.setStyle(s); // Set style
}
Space.prototype.AddSVGLayer=function(url, geoRef, opacity, id, start, end) // ADD SVG LAYER TO MAP
{
var o={};
var _this=this; // Save context for callback
if (url && url.match(/\/\//)) // If cross-domain
url="proxy.php?url="+url; // Add proxy
var index=this.overlays.length; // Get index
o.type="svg"; // KML
o.start=start; o.end=end; // Save start, end
o.opacity=opacity; // Initial opacity
o.tag=curJson.mobs[id].id; // Add id into tag
o.show=curJson.mobs[id].show; // Add show tag
o.mob=id; // Save original mob id
var extent = [0, 0, 10, 10];
o.src=new ol.layer.Image({ source: new ol.source.ImageStatic({ // New layer
title: "LAYER-"+this.overlays.length, // Set name
projection: ol.proj.get(this.curProjection), // Set projection
imageExtent: extent, //???
url: 'https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg',
})
});
o.src.set('svgId',id) // Set id
o.src.set('visible',false) // Hide it
this.overlays.push(o); // Add to overlay
this.loadCounter++; // Add to count
this.map.addLayer(o.src); // Add to map
return index; // Return layer ID
}
Space.prototype.AddKMLLayer=function(url, opacity, id, start, end) // ADD KML LAYER TO MAP
{
var o={};
var _this=this; // Save context for callback
if (url && url.match(/\/\//)) // If cross-domain
url="proxy.php?url="+url; // Add proxy
var index=this.overlays.length; // Get index
o.type="kml"; // KML
o.start=start; o.end=end; // Save start, end
o.opacity=o.alpha=opacity; // Initial opacity
o.tag=curJson.mobs[id].id; // Add id into tag
o.show=curJson.mobs[id].show; // Add show tag
o.mob=id; // Save original mob id
if (url && url.match(/\{z\}/)) { // Tiled KML image
o.src=new ol.layer.Tile({ source: new ol.source.XYZ({ // New layer
title: "LAYER-"+this.overlays.length, // Set name
projection: ol.proj.get(this.curProjection), // Set KML projection
url: url // URL
})
});
}
if (url && url.match(/\.geojson/i)) { // Geojson image
o.src=new ol.layer.Vector({ source: new ol.source.Vector({ // New layer
title: "LAYER-"+this.overlays.length, // Set name
projection: ol.proj.get(this.curProjection), // Set projection
format: new ol.format.GeoJSON(), // Set format
url: url // URL
})
});
}
else{ // Normal KML
o.src=new ol.layer.Vector({ source: new ol.source.Vector({ // New layer
title: "LAYER-"+this.overlays.length, // Set name
projection: ol.proj.get(this.curProjection), // Set KML projection
format: new ol.format.KML({ extractStyles:true}), // KML format
url: url // URL
})
});
}
o.src.set('kmlId',id) // Set id
o.src.set('visible',false) // Hide it
this.overlays.push(o); // Add to overlay
this.loadCounter++; // Add to count
this.map.addLayer(o.src); // Add to map
return index; // Return layer ID
this.overlays[index].getSource().once("change",function(){ // WHEN KML IS LOADED
this.ShowProgress(); // Update loading progress
this.forEachFeature(function(f) { // For each feature in KML
if ((f.getGeometry().getType() == "Point") && f.get("name")){ // If a marker with a label
var sty=new ol.style.Style({ // Add style
image: new ol.style.Icon( { src: "img/marker.png" }), // Add icon
text: new ol.style.Text( { // Text style
textAlign: "left", textBaseline: "middle", // Set alignment
font: "bold 14px Arial", // Set font
text: f.get("name"), // Get label
fill: new ol.style.Fill({color: "#fff" }), // Set color
stroke: new ol.style.Stroke( { color: "#000",width: 1 }), // Set edge
offsetX: 16 // Set offset
})
});
f.setStyle(sty); // Set style to show label
}
});
});
}
Space.prototype.StyleKMLFeatures=function(num, styles) // STYLE KML FEATURE(s)
{
/*
@param {number} num The index of the KML overlay to color.
@param {array} styles An array of objects specifying the style to set any given feature to set
Each element in the array consists of a style object to set a particular feature index:
n: {string} spec's feature index,or "*" for all
a: {number} opacity (0-1)
f: {string} fill color "#rrggbb"
s: {string} stroke color "#rrggbb"
w: {number} stroke width
*/
var i,r,g,b,a;
if ((num < 0) || (num >= this.overlays.length) || (this.overlays[num].type != "kml")) // If not a valid KML
return; // Quit
var features=this.overlays[num].src.getSource().getFeatures(); // Get KML feature array
var n=styles.length; // Number of features to style
var last=n-1; // Last style possible
if (styles[0].n == "*")
n=features.length;
for (i=0;i<n;++i) { // For each feature to style
var s=styles[Math.min(i,last)]; // Point at style
if (s.f) { // If fill spec'd
if (s.f.length == 4) s.f+=s.f.substr(1,3); // Turn #555 into #55555
r=parseInt("0x"+s.f.substr(1,2),16); // R
g=parseInt("0x"+s.f.substr(3,2),16); // G
b=parseInt("0x"+s.f.substr(5,2),16); // B
var fill=new ol.style.Fill({ color: [r,g,b,s.a ? s.a : 1]}); // Create fill with alpha
}
if (s.s) { // If stroke spec'd
if (s.s.length == 4) s.s+=s.s.substr(1,3); // Turn #555 into #55555
r=parseInt("0x"+s.s.substr(1,2),16); // R
g=parseInt("0x"+s.s.substr(3,2),16); // G
b=parseInt("0x"+s.s.substr(5,2),16); // B
var stroke=new ol.style.Stroke({ color: [r,g,b,s.a ? s.a : 1], width:s.w }); // Create stroke with alpha & width
}
sty=new ol.style.Style({ fill:fill,stroke:stroke }); // Create style
if (s.n == "*") // If styling them all
features[i].setStyle(sty); // Set next feature's style
else // Justb styling on spec's bu s.n
features[s.n].setStyle(sty); // Set particular feature's style
}
}
Space.prototype.AddImageLayer=function(url, geoRef, alpha, start, end, id) // ADD MAP IMAGE TO PROJECT
{
var o={};
var index=this.overlays.length; // Get index
o.type="image"; // Image
o.start=start; o.end=end; // Save start, end
o.tag=curJson.mobs[id].id; // Add id into tag
o.mob=id; // Save original mob id
o.show=curJson.mobs[id].show; // Add show tag
o.src=new MapImage(url,geoRef,this); // Alloc mapimage obj
o.alpha=alpha; // Save alpha
this.overlays.push(o); // Add layer
this.loadCounter++; // Add to count
return index; // Return layer ID
}
function MapImage(url, geoRef, _this) // MAPIMAGE CONSTRUCTOR
{
this.img=new Image(); // Alloc image
this.img.onload=_this.ShowProgress; // Add handler to remove from count after loaded
this.img.onerror=_this.ShowProgress; // Add handler to remove from count if error
this.imgWidth; this.imgHeight; // Set size, if any
var v=geoRef.split(","); // Split into parts
this.n=v[0]-0; // Set bounds
this.s=v[1]-0;
this.e=v[2]-0;
this.w=v[3]-0;
this.full=v[5];
var ne = ol.proj.transform([this.e, this.n], 'EPSG:4326', _this.curProjection); // Project
var sw = ol.proj.transform([this.w, this.s], 'EPSG:4326', _this.curProjection);
this.north = ne[1];
this.south = sw[1];
this.east = ne[0];
this.west = sw[0];
this.centerXCoord = this.w + (Math.abs(this.e - this.w) / 2);
this.centerYCoord = this.s + (Math.abs(this.n - this.s) / 2);
this.center=ol.proj.transform([this.centerXCoord, this.centerYCoord], 'EPSG:4326', _this.curProjection); // Get center
this.rotation=v[4]*-1; // Reverse direction
this.img.src=url; // Set url
}
MapImage.prototype.drawMapImage=function(opacity, _this) // DRAW IMAGE
{
if (!this.imgWidth) {
this.imgWidth=this.img.width;
this.imgHeight=this.img.height;
this.imgWidthMeters=Math.abs(this.east - this.west);
this.imgHeightMeters=Math.abs(this.north - this.south);
}
var canvasExtentWidth = _this.canvasExtent[2] - _this.canvasExtent[0];
var canvasExtentHeight = _this.canvasExtent[3] - _this.canvasExtent[1];
var xCenterOffset = _this.canvasWidth * (this.center[0]-_this.canvasExtent[0]) / canvasExtentWidth;
var yCenterOffset = _this.canvasHeight * (_this.canvasExtent[3]-this.center[1]) / canvasExtentHeight;
var drawWidth = _this.canvasWidth * (this.imgWidthMeters / canvasExtentWidth);
var drawHeight = _this.canvasHeight * (this.imgHeightMeters / canvasExtentHeight);
if (this.full) {
xCenterOffset=_this.canvasWidth/2;
yCenterOffset=_this.canvasHeight/2
drawWidth=$(mps.div).width()*this.full/100;
if (isMobile) drawWidth*=2;
drawHeight=drawWidth/(this.imgWidth/this.imgHeight);
}
var ctx=_this.canvasContext;
if (ctx && this.img.width) {
ctx.globalAlpha = opacity;
ctx.translate(xCenterOffset,yCenterOffset);
ctx.rotate(this.rotation * (Math.PI/180));
ctx.translate(-(drawWidth / 2), -(drawHeight / 2));
ctx.drawImage(this.img, 0, 0, drawWidth, drawHeight);
ctx.translate((drawWidth / 2), (drawHeight / 2));
ctx.rotate(-(this.rotation * (Math.PI / 180)));
ctx.translate(-xCenterOffset,-yCenterOffset);
ctx.globalAlpha=1;
}
}
Space.prototype.InitPopups=function() // HANDLE POPUPS ON FEATURES
{
var _this=this; // Save context for callbacks
this.map.on('click', function(evt) { // ON MAP CLICK
var lay,id;
var feature=_this.map.forEachFeatureAtPixel(evt.pixel, // Look through features
function(feature, layer) { // On match to location
lay=layer; // Save layer
id=feature.getId();
if (id && id.match(/mob-.+h/i)) feature=null; // Skip path header
return feature; // Return feature
});
if (feature) { // If one found
var j;
if (lay && (lay.get("kmlId"))) { // If a KML id
var c=curJson.mobs[lay.get("kmlId")].click; // Get click of kml mob
if (c) pop.SendActions(c); // Send actions
var fa=lay.getSource().getFeatures(); // Point at all features
for (j=0;j<fa.length;++j) // For each one
if (fa[j].getId() == id) // A match for id
break; // Quit looking
var rx=new RegExp(lay.get('kmlId')+":"+j); // Make patt
for (j=0;j<curJson.mobs.length;++j) { // For each mob
o=curJson.mobs[j]; // Point at mob data
if (o.marker && o.marker.match(rx)) { // If a feature kml:feature match
id="Mob-"+j; // Point at mov with info
break;
}
}
}
if (!id)
return;
if (id.match(/Mob-/)) { // If a mob
var i=id.substr(4); // Strip off header
if (isNaN(i)) // Must be a path header
return; // Quit
o=curJson.mobs[i]; // Point at mob data
if (o.title) var title=o.title; // Lead with title
if (o.spaceTitle) var title=o.spaceTitle; // Space over-rides
if (o.desc) var desc=o.desc; // Lead with desc
if (o.spaceDesc) var desc=o.spaceDesc; // Space over-rides
if (o.pic) var pic=o.pic; // Lead with pic
if (o.spacePic) var title=o.spacePic; // Space over-rides
if (o.citation) { // If a cite
desc+="<div class='story-cite' style='cursor:pointer'><br><a onclick='$(\"#cite"+i+"\").fadeIn()'>";
desc+="<u>Citation</u><br><span style='display:none' id='cite"+i+"'><br>"+o.citation+"</span></div>";
}
if (o.desc) {
if (desc.match(/\(NOTIME\)/i)) // If not showing in time
desc=desc.replace(/\(NOTIME\)/i,""); // Remove tag
var ss=curJson.hideTimelineDates ? 0 : o.start; // Set start date
var ee=curJson.hideTimelineDates ? 0 : o.end; // End
_this.pop.ShowPopup(_this.div,_this.timeFormat,evt.pixel[0],evt.pixel[1],title,desc,pic,ss,ee);
}
if (o.start) // If a time defined
_this.SendMessage("time",o.start); // Send new time
if (o.click) // If a click defined
pop.SendActions(o.click); // Send actions
}
}
else // No feature found
ClearPopUps(true); // Clear any existing pop
});
this.map.on('pointermove', function(e) { // ON MOUSE MOVE
if (e.dragging) { // If dragging
ClearPopUps(true); // Clear any existing pops, but don't reset storypane
return; // Quit
}
var pixel=_this.map.getEventPixel(e.originalEvent); // Get pos
var hit=_this.map.hasFeatureAtPixel(pixel); // Anything there?
$(_this.div).css("cursor",hit ? "pointer" : ""); // Change cursor
});
}
Space.prototype.ClearLayers=function( ) // CLEAR MAP LAYERS
{
var i,o;
for (i=0;i<this.overlays.length;++i) { // For each overlay
o=this.overlays[i]; // Point at it
if (o.type == "kml") // KML
this.map.getLayers().remove(o.src); // Remove layer
}
this.markerLayer.getSource().clear(true); // Clear all markers
this.overlays=[]; // All gone
}
Space.prototype.ShowProgress=function() // SHOW RESORCE LOAD PROGRESS
{
var str="";
this.loadCounter--; // Dec
if (this.loadCounter) // If stuff to load
str=this.loadCounter+" resources to load"; // Set progress
$("#SloadProgress").text(str); // Show status
}
Space.prototype.SendMessage=function(cmd, msg) // SEND MESSAGE TO PARENT
{
var str="Space="+cmd; // Add src and window
if (msg) // If more to it
str+="|"+msg; // Add it
window.postMessage(str,"*"); // Send message to wind
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// GEO-REFERENCE
/////////////////////////////////////////////////////////////////////////////////////////////////
Space.prototype.GeoReference=function(url, where, edit) // GEO REFERENCE IMAGE
{
var n,s,e,w,r,asp;
var _this=this; // Context for callbacks
var mapWid=$(this.div).width(); // Map width
var mapHgt=$(this.div).height(); // Map height
$("#dialogDiv").remove(); // Close dialog
pop.Sound("click",curJson.muteSound); // Click sound
var str="<table>";
if (!edit) str+="<tr><td>Image URL </td><td><input id='grurl' class='ve-is' style='width:220px' type='input'></td></tr>";
str+="<tr><td>North</td><td><input id='grn' class='ve-is' style='width:80px' type='input'></td></tr>";
str+="<tr><td>South</td><td><input id='grs' class='ve-is' style='width:80px' type='input'></td></tr>";
str+="<tr><td>East</td><td><input id='gre' class='ve-is' style='width:80px' type='input'></td></tr>";
str+="<tr><td>West</td><td><input id='grw' class='ve-is' style='width:80px' type='input'></td></tr>";
str+="<tr><td>Rotation</td><td><div id='rots' style='display:inline-block;width:66%'></div> <input id='grr' class='ve-is' style='width:40px' type='input'></td></tr>";
str+="<tr><td>Combined</td><td><input id='grc' class='ve-is' style='width:220px' type='input'></td></tr>";
str+="<tr><td>Opacity</td><td><div id='gra'></div></td></tr>";
str+="<tr><td>Map base </td><td>"+MakeSelect("setbase",false,["Satellite","Terrain","Earth","Watercolor","B&W","Roadmap"],curJson.baseMap);
if (!edit) str+="<tr><td colspan=2><br></td><tr><tr><td></td><td><button id='grstart' class='ve-bs'>Geo-reference</button><td></td><tr>";
str+="</table>";
this.pop.Dialog("Geo-reference image",str, function() { // On OK
CloseGeoRef(); // Close out
},
function() { // On cancel
CloseGeoRef(); // Close out
});
$("#dialogDiv").dialog("option","position", { at: "center center", of: "#rightDiv" })
if (where) { // If a where defined
v=where.split(","); // Get parts
n=v[0]; s=v[1]; e=v[2]; w=v[3]; r=v[4]; // Set
}
$("#grn").val(n); $("#grs").val(s); $("#grr").val(r); // Init n/s/r
$("#gre").val(e); $("#grw").val(w); $("#grurl").val(url); // e/w/url
$("#gra").slider({ value:100, slide: function(e,ui) { // Init alpha slider
if (_this.geoRef) _this.geoRef.a=ui.value/100; // If georef up. set val
_this.DrawMapLayers(); // Redraw map
}});
$("#rots").slider({ slide: function(e,ui) { // Init rotation slider
$("#grr").val(ui.value/100*360); // Set text field