forked from ngageoint/opensphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature.js
1127 lines (973 loc) · 37.6 KB
/
feature.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
goog.provide('os.feature');
goog.require('goog.reflect');
goog.require('goog.string');
goog.require('ol.Feature');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.source.Vector');
goog.require('ol.source.VectorEventType');
goog.require('os');
goog.require('os.Fields');
goog.require('os.MapContainer');
goog.require('os.command.FlyToExtent');
goog.require('os.data.RecordField');
goog.require('os.fn');
goog.require('os.geo');
goog.require('os.geom.Ellipse');
goog.require('os.im.mapping.MappingManager');
goog.require('os.map');
goog.require('os.math.Units');
goog.require('os.style.StyleField');
goog.require('os.style.StyleType');
goog.require('os.ui.filter.string');
/**
* A function used to sort features.
* @typedef {function(!ol.Feature, !ol.Feature):number}
*/
os.feature.SortFn;
/**
* Defines set of options for creating the line of bearing
* @typedef {{
* arrowLength: (number|undefined),
* arrowUnits: (string|undefined),
* bearingColumn: (string|undefined),
* bearingError: (number|undefined),
* bearingErrorColumn: (string|undefined),
* columnLength: (number|undefined),
* length: (number|undefined),
* lengthUnits: (string|undefined),
* lengthColumn: (string|undefined),
* lengthType: (string|undefined),
* lengthError: (number|undefined),
* lengthErrorUnits: (string|undefined),
* lengthErrorColumn: (string|undefined),
* showArrow: (boolean|undefined),
* showEllipse: (boolean|undefined),
* showError: (boolean|undefined)
* }}
*/
os.feature.LOBOptions;
/**
* Regular expression to match a title field on a feature.
* @type {RegExp}
* @const
*/
os.feature.TITLE_REGEX = /^(name|title)$/i;
/**
* @type {undefined|function(Array<ol.Feature>)}
*/
os.feature.flyToOverride;
/**
* @param {null|undefined|ol.Feature|Array<ol.Feature>} features
*/
os.feature.flyTo = function(features) {
if (!features) {
return;
}
if (!Array.isArray(features)) {
features = [features];
}
if (os.feature.flyToOverride) {
os.feature.flyToOverride(/** @type {Array<ol.Feature>} */ (features));
} else {
var extent = features.map(os.fn.mapFeatureToGeometry).
reduce(os.fn.reduceExtentFromGeometries, ol.extent.createEmpty());
var cmd = new os.command.FlyToExtent(extent);
os.commandStack.addCommand(cmd);
}
};
/**
* Auto detect and apply column mappings to features.
*
* @param {!Array<!ol.Feature>} features The features
* @param {number=} opt_count Optional count of features for the automap to check, defaulting to 1.
*/
os.feature.autoMap = function(features, opt_count) {
if (features && features.length > 0) {
var mm = os.im.mapping.MappingManager.getInstance();
var detectFeatures = opt_count !== undefined ? features.slice(0, opt_count) : [features[0]];
var mappings = mm.autoDetect(detectFeatures);
mappings.forEach(function(mapping) {
for (var i = 0; i < features.length; i++) {
mapping.execute(features[i]);
}
});
}
};
/**
* Simplify the geometry on a feature. Intended to reduce memory footprint and simplify geometry operations.
*
* @param {ol.Feature} feature The feature.
*/
os.feature.simplifyGeometry = function(feature) {
if (feature) {
var geom = feature.getGeometry();
if (geom) {
var flatGeom = os.geo.flattenGeometry(geom);
if (flatGeom != geom) {
feature.setGeometry(flatGeom);
}
}
}
};
/**
* Get the semi-major axis for a feature.
*
* @param {ol.Feature} feature The feature.
* @param {os.math.Units=} opt_units The desired units. Defaults to nautical miles.
* @return {number|undefined} The semi-major axis value, or undefined if not found.
*/
os.feature.getSemiMajor = function(feature, opt_units) {
var value = os.feature.getEllipseField_(feature,
os.fields.DEFAULT_SEMI_MAJ_COL_NAME,
os.Fields.SEMI_MAJOR,
os.Fields.SEMI_MAJOR_UNITS,
opt_units);
// don't return negative values, and treat 0 as undefined
return value ? Math.abs(value) : undefined;
};
/**
* Get the semi-minor axis for a feature.
*
* @param {ol.Feature} feature The feature.
* @param {os.math.Units=} opt_units The desired units. Defaults to nautical miles.
* @return {number|undefined} The semi-minor axis value, or undefined if not found.
*/
os.feature.getSemiMinor = function(feature, opt_units) {
var value = os.feature.getEllipseField_(feature,
os.fields.DEFAULT_SEMI_MIN_COL_NAME,
os.Fields.SEMI_MINOR,
os.Fields.SEMI_MINOR_UNITS,
opt_units);
// don't return negative values, and treat 0 as undefined
return value ? Math.abs(value) : undefined;
};
/**
* Get the orientation for a feature, in degrees.
*
* @param {ol.Feature} feature The feature.
* @return {number|undefined} The orientation value, or undefined if not found.
*/
os.feature.getOrientation = function(feature) {
var orientation = os.math.parseNumber(feature.get(os.Fields.ORIENTATION));
return !isNaN(orientation) ? orientation : undefined;
};
/**
* Get the radius for a feature.
*
* @param {ol.Feature} feature The feature.
* @param {os.math.Units=} opt_units The desired units. Defaults to nautical miles.
* @return {number|undefined} The radius axis value, or undefined if not found.
*/
os.feature.getRadius = function(feature, opt_units) {
var value = os.feature.getEllipseField_(feature,
os.fields.DEFAULT_RADIUS_COL_NAME,
os.Fields.RADIUS,
os.Fields.RADIUS_UNITS,
opt_units);
// don't return negative values, and treat 0 as undefined
return value ? Math.abs(value) : undefined;
};
/**
* Get the bearing for a feature, in degrees.
*
* @param {ol.Feature} feature The feature.
* @return {number|undefined} The orientation value, or undefined if not found.
*/
os.feature.getBearing = function(feature) {
var bearing = os.math.parseNumber(feature.get(os.Fields.BEARING));
return !isNaN(bearing) ? bearing : undefined;
};
/**
* Get the semi-major axis for a feature.
*
* @param {ol.Feature} feature The feature.
* @param {string|undefined} nmiField The application mapped field containing the value in nautical miles.
* @param {string|undefined} defaultField The default field.
* @param {string|undefined} defaultUnitsField The default units field.
* @param {os.math.Units=} opt_units The desired units. Defaults to nautical miles.
* @return {number|undefined} The ellipse field value, or undefined if not found.
* @private
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.getEllipseField_ = function(feature, nmiField, defaultField, defaultUnitsField, opt_units) {
var value = NaN;
var currentUnits;
var targetUnits = opt_units || os.math.Units.NAUTICAL_MILES;
if (feature) {
// try the mapped column first
if (nmiField) {
value = os.math.parseNumber(feature.values_[nmiField]);
}
if (!isNaN(value)) {
// semi-minor has been mapped to nmi
currentUnits = os.math.Units.NAUTICAL_MILES;
} else if (defaultField) {
// semi-minor has not been mapped, so try default field names
value = os.math.parseNumber(feature.values_[defaultField]);
if (defaultUnitsField) {
currentUnits = /** @type {string|undefined} */ (feature.values_[defaultUnitsField]);
}
}
if (!isNaN(value)) {
if (currentUnits && goog.object.containsValue(os.math.Units, currentUnits)) {
// units known, translate to target units
value = os.math.convertUnits(value, targetUnits, currentUnits);
} else {
// take a guess at what the units represent
value = os.geo.convertEllipseValue(value);
}
}
}
// don't return NaN
return !isNaN(value) ? value : undefined;
};
/**
* Creates an ellipse from a feature if it has the necessary data.
*
* @param {ol.Feature} feature The feature
* @param {boolean=} opt_replace If an existing ellipse should be replaced
* @return {os.geom.Ellipse|undefined} The ellipse, if one could be generated
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.createEllipse = function(feature, opt_replace) {
var ellipse;
if (!opt_replace) {
ellipse = /** @type {(os.geom.Ellipse|undefined)} */ (feature.values_[os.data.RecordField.ELLIPSE]);
if (ellipse instanceof os.geom.Ellipse) {
// ellipse already created for this feature
return ellipse;
}
}
var geom = feature ? feature.getGeometry() : null;
if (geom instanceof ol.geom.Point) {
// the feature must have a center point, and either semi-major/semi-minor/orientation OR a radius to generate an
// ellipse. no values should ever be assumed.
var center = ol.proj.toLonLat(geom.getFirstCoordinate(), os.map.PROJECTION);
if (center) {
var semiMajor = os.feature.getSemiMajor(feature, os.math.Units.METERS);
var semiMinor = os.feature.getSemiMinor(feature, os.math.Units.METERS);
var orientation = os.feature.getOrientation(feature);
var radius = os.feature.getRadius(feature, os.math.Units.METERS);
if (semiMajor && semiMinor && orientation != null) {
ellipse = new os.geom.Ellipse(center, semiMajor, semiMinor, orientation);
} else if (radius) {
ellipse = new os.geom.Ellipse(center, radius);
}
}
}
// if an ellipse couldn't be created, use the original geometry so it's still rendered on the map
feature.set(os.data.RecordField.ELLIPSE, ellipse || geom);
return ellipse;
};
/**
* Returns a column value from a feature.
* If the column is not provided or doesn't exist it will return a default value or NaN
* If the column exists but is not a number it will return a NaN
*
* @param {ol.Feature} feature The feature
* @param {string=} opt_column column on feature to use
* @param {number=} opt_default fallback value if column doesn't exist
* @return {number} some value
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.getColumnValue = function(feature, opt_column, opt_default) {
if (opt_column) {
if (feature.values_[opt_column] != null) {
var val = parseFloat(feature.values_[opt_column]);
if (val == feature.values_[opt_column]) { // this prevents against a partial conversion ie 7 != '7ate9'
return val;
}
}
return NaN; // invalid value in column
}
return opt_default ? opt_default : NaN; // column doesn't exist
};
/**
* Creates a line of bearing from a feature if it has the necessary data.
*
* @param {ol.Feature} feature The feature
* @param {boolean=} opt_replace If an existing lob should be replaced
* @param {os.feature.LOBOptions=} opt_lobOpts the options for rendering line of bearing
* @return {ol.geom.LineString|ol.geom.MultiLineString|ol.geom.Geometry|undefined} The lob, if one could be generated
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.createLineOfBearing = function(feature, opt_replace, opt_lobOpts) {
var lob;
if (!opt_replace) {
lob = /** @type {(ol.geom.LineString|undefined)} */ (feature.values_[os.data.RecordField.LINE_OF_BEARING]);
if (lob instanceof ol.geom.MultiLineString) { // lob already created for this feature
return lob;
}
}
var geom = feature ? feature.getGeometry() : null;
if (opt_lobOpts && geom instanceof ol.geom.Point) {
// var use3D = os.MapContainer.getInstance().is3DEnabled();
// the feature must have a center point and a bearing to generate a lob. no values should ever be assumed.
var center = ol.proj.toLonLat(geom.getFirstCoordinate(), os.map.PROJECTION);
var bearing = os.feature.getColumnValue(feature, opt_lobOpts.bearingColumn);
var length = opt_lobOpts.lengthType == 'column' ? // get from column unless manual
os.feature.getColumnValue(feature, opt_lobOpts.lengthColumn, 0) : 1;
if (center && bearing != null && !isNaN(bearing) && length) {
// sanitize
bearing = bearing % 360;
bearing += bearing <= 0 ? 360 : 0;
var coords = [];
if (center.length < 3) {
center[2] = 0;
}
var multiplier = opt_lobOpts.lengthType == 'column' ? opt_lobOpts.columnLength : opt_lobOpts.length;
multiplier = multiplier || os.style.DEFAULT_LOB_LENGTH;
var lengthUnits = opt_lobOpts.lengthUnits || os.style.DEFAULT_UNITS;
var convertedLength = os.math.convertUnits(length, os.style.DEFAULT_UNITS, lengthUnits);
var effectiveBearing = length < 0 ? (bearing + 180) % 360 : bearing; // flip it if the length will be negative
var effectiveLength = Math.min(Math.abs(convertedLength * multiplier), os.geo.MAX_LINE_LENGTH);
// now do some calcs
var end = osasm.geodesicDirect(center, effectiveBearing, effectiveLength);
end[2] = center[2];
var inverse = osasm.geodesicInverse(center, end);
var endBearing = inverse.finalBearing;
// create the line and split it across the date line so it renders correctly on a 2D map
lob = new ol.geom.LineString([center, end], ol.geom.GeometryLayout.XYZM);
lob = os.geo.splitOnDateLine(lob);
if (lob instanceof ol.geom.LineString) {
coords.push(lob.getCoordinates());
}
if (opt_lobOpts.showArrow) {
var arrowUnits = opt_lobOpts.arrowUnits || os.style.DEFAULT_UNITS;
var arrowLength = opt_lobOpts.arrowLength || os.style.DEFAULT_ARROW_SIZE;
var convertedArrowLength = os.math.convertUnits(arrowLength, os.style.DEFAULT_UNITS, arrowUnits);
var effectiveArrowLength = Math.min(convertedArrowLength, os.geo.MAX_LINE_LENGTH);
var right = osasm.geodesicDirect(end, endBearing + 180 - 45, effectiveArrowLength);
right.push(center[2]);
var rightArm = new ol.geom.LineString([end, right], ol.geom.GeometryLayout.XYZM);
rightArm = os.geo.splitOnDateLine(rightArm);
if (rightArm instanceof ol.geom.LineString) {
coords.push(rightArm.getCoordinates());
}
var left = osasm.geodesicDirect(end, endBearing + 180 + 45, effectiveArrowLength);
left.push(center[2]);
var leftArm = new ol.geom.LineString([end, left], ol.geom.GeometryLayout.XYZM);
leftArm = os.geo.splitOnDateLine(leftArm);
if (leftArm instanceof ol.geom.LineString) {
coords.push(leftArm.getCoordinates());
}
}
// lob must be a ol.geom.MultiLineString
if (coords.length > 0) {
lob = new ol.geom.MultiLineString(coords, ol.geom.GeometryLayout.XYZM);
lob.set(os.geom.GeometryField.NORMALIZED, true);
lob.osTransform();
}
var plusArc = null;
var minusArc = null;
if (opt_lobOpts.showError) { // draw error arcs
var lengthErrorUnits = opt_lobOpts.lengthErrorUnits || os.style.DEFAULT_UNITS;
var lengthError = Math.abs(os.feature.getColumnValue(feature, opt_lobOpts.lengthErrorColumn));
var lengthErrorMultiplier = opt_lobOpts.lengthError !== undefined ?
opt_lobOpts.lengthError : os.style.DEFAULT_LOB_LENGTH_ERROR;
var bearingError = Math.abs(os.feature.getColumnValue(feature, opt_lobOpts.bearingErrorColumn));
var bearingErrorMultiplier = opt_lobOpts.bearingError !== undefined ?
opt_lobOpts.bearingError : os.style.DEFAULT_LOB_BEARING_ERROR;
if (bearingError === null || isNaN(bearingError)) {
bearingError = 1;
}
if (lengthError === null || isNaN(lengthError)) {
lengthError = 1;
}
var cLengthError = os.math.convertUnits(lengthError, os.style.DEFAULT_UNITS, lengthErrorUnits) *
lengthErrorMultiplier;
if (bearingError > 0 && bearingErrorMultiplier > 0) {
var plusPts = os.geo.interpolateArc(center, effectiveLength + cLengthError,
Math.min(bearingError * bearingErrorMultiplier * 2, 360), bearing);
plusArc = new ol.geom.LineString(plusPts, ol.geom.GeometryLayout.XYZM);
plusArc = os.geo.splitOnDateLine(plusArc);
plusArc.set(os.geom.GeometryField.NORMALIZED, true);
plusArc.osTransform();
if (lengthError > 0 && lengthErrorMultiplier > 0) { // only draw one arc if it is zero
var pts = os.geo.interpolateArc(center, effectiveLength - cLengthError,
Math.min(bearingError * bearingErrorMultiplier * 2, 360), bearing);
minusArc = new ol.geom.LineString(pts, ol.geom.GeometryLayout.XYZM);
minusArc = os.geo.splitOnDateLine(minusArc);
minusArc.set(os.geom.GeometryField.NORMALIZED, true);
minusArc.osTransform();
}
} else if (lengthError > 0 && lengthErrorMultiplier > 0) { // no bearing error perpendicular line instead of arc
var uLineCenter = osasm.geodesicDirect(end, endBearing + 180, -cLengthError);
var uLineRight = osasm.geodesicDirect(uLineCenter, endBearing + 90, -cLengthError);
uLineRight.push(center[2]);
var uLineLeft = osasm.geodesicDirect(uLineCenter, endBearing - 90, -cLengthError);
uLineLeft.push(center[2]);
plusArc = new ol.geom.LineString([uLineLeft, uLineRight], ol.geom.GeometryLayout.XYZM);
plusArc = os.geo.splitOnDateLine(plusArc);
plusArc.set(os.geom.GeometryField.NORMALIZED, true);
plusArc.osTransform();
var bLineCenter = osasm.geodesicDirect(end, endBearing + 180, cLengthError);
var bLineRight = osasm.geodesicDirect(bLineCenter, endBearing + 90, cLengthError);
bLineRight.push(center[2]);
var bLineLeft = osasm.geodesicDirect(bLineCenter, endBearing - 90, cLengthError);
bLineLeft.push(center[2]);
minusArc = new ol.geom.LineString([bLineLeft, bLineRight], ol.geom.GeometryLayout.XYZM);
minusArc = os.geo.splitOnDateLine(minusArc);
minusArc.set(os.geom.GeometryField.NORMALIZED, true);
minusArc.osTransform();
}
}
os.interpolate.interpolateGeom(lob);
feature.set(os.data.RecordField.LINE_OF_BEARING_ERROR_HIGH, plusArc);
feature.set(os.data.RecordField.LINE_OF_BEARING_ERROR_LOW, minusArc);
}
if (opt_lobOpts.showEllipse) { // TODO remove this if we ever allow independent styles
os.feature.createEllipse(feature);
} else {
feature.set(os.data.RecordField.ELLIPSE, null);
}
}
// if a lob couldn't be created, use the original geometry so it's still rendered on the map
feature.set(os.data.RecordField.LINE_OF_BEARING, lob || geom);
return lob;
};
/**
* Set the altitude component on a feature if it has a point geometry.
*
* @param {ol.Feature} feature The feature
* @param {string=} opt_field The altitude field
*/
os.feature.setAltitude = function(feature, opt_field) {
var field = opt_field || os.Fields.ALT;
var geom = feature.getGeometry();
if (geom instanceof ol.geom.Point) {
var coords = geom.getFlatCoordinates();
if (coords.length < 3 || coords[2] === 0) {
var altitude = Number(feature.get(field) || 0);
if (isNaN(altitude)) {
altitude = 0;
}
coords[2] = altitude;
geom.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, coords);
}
}
};
/**
* Automatically populate coordinate fields on a feature. Requires a point geometry to set fields.
*
* @param {ol.Feature} feature The feature
* @param {boolean=} opt_replace If existing values should be replaced
* @param {ol.geom.Geometry=} opt_geometry Alternate geometry to populate the fields
* @param {boolean=} opt_silent
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.populateCoordFields = function(feature, opt_replace, opt_geometry, opt_silent) {
var changed = false;
var geom = opt_geometry || feature.getGeometry();
if (geom instanceof ol.geom.Point) {
var coords = geom.getFlatCoordinates();
if (coords.length > 1) {
coords = ol.proj.toLonLat(coords, os.map.PROJECTION);
if (opt_replace || feature.values_[os.Fields.MGRS] == undefined) {
feature.values_[os.Fields.MGRS] = osasm.toMGRS(coords);
changed = true;
}
if (opt_replace || feature.values_[os.Fields.LAT] == undefined) {
feature.values_[os.Fields.LAT] = coords[1];
changed = true;
}
if (opt_replace || feature.values_[os.Fields.LON] == undefined) {
feature.values_[os.Fields.LON] = coords[0];
changed = true;
}
if (opt_replace || feature.values_[os.Fields.ALT] == undefined) {
if (coords[2]) {
feature.values_[os.Fields.ALT] = coords[2];
changed = true;
}
}
if (opt_replace || feature.values_[os.Fields.LAT_DMS] == undefined) {
feature.values_[os.Fields.LAT_DMS] = os.geo.toSexagesimal(coords[1], false);
changed = true;
}
if (opt_replace || feature.values_[os.Fields.LON_DMS] == undefined) {
feature.values_[os.Fields.LON_DMS] = os.geo.toSexagesimal(coords[0], true);
changed = true;
}
if (opt_replace || feature.values_[os.Fields.LAT_DDM] == undefined) {
feature.values_[os.Fields.LAT_DDM] = os.geo.toDegreesDecimalMinutes(coords[1], false);
changed = true;
}
if (opt_replace || feature.values_[os.Fields.LON_DDM] == undefined) {
feature.values_[os.Fields.LON_DDM] = os.geo.toDegreesDecimalMinutes(coords[0], true);
changed = true;
}
}
}
// fire a change event if anything was updated, so the UI can update
if (!opt_silent && changed) {
feature.changed();
}
};
/**
* Gets a field value from an {@link ol.Feature}.
*
* @param {ol.Feature} item
* @param {string} field
* @return {*} The value
*/
os.feature.getField = function(item, field) {
return item ? item.get(field) : undefined;
};
/**
* Get the title from a feature, matching a property (case insensitive) called 'name' or 'title'.
*
* @param {ol.Feature} feature The feature.
* @return {string|undefined} The feature title, or undefined if not found.
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.getTitle = function(feature) {
var title;
if (feature) {
for (var key in feature.values_) {
if (os.feature.TITLE_REGEX.test(key)) {
var value = feature.values_[key];
if (value && typeof value === 'string') {
title = value;
break;
}
}
}
}
return title;
};
/**
* Get the property name for the {@link ol.Feature#values_} property. This is necessary for compiled code because
* `values_` will be renamed by the Closure compiler.
* @type {string}
* @private
* @const
*/
os.feature.VALUES_FIELD_ = goog.reflect.objectProperty('values_', new ol.Feature());
/**
* Create a filter function expression to get a value from a feature.
*
* @param {string} itemVar The feature variable name.
* @param {string} field The field to get.
* @return {string} The get expression.
*/
os.feature.filterFnGetter = function(itemVar, field) {
// create the string: itemVar.values_["column_name"]
// make the field safe for use as an object property name, to prevent injection attacks
return itemVar + '.' + os.feature.VALUES_FIELD_ + '[' + os.ui.filter.string.quoteString(field) + ']';
};
/**
* If a field is internal to the application and should be skipped by user-facing features.
*
* @param {string} field The metadata field
* @return {boolean}
*/
os.feature.isInternalField = function(field) {
return os.data.RecordField.REGEXP.test(field) || os.style.StyleType.REGEXP.test(field) ||
os.style.StyleField.REGEXP.test(field);
};
/**
* Get the layer id of a feature
*
* @param {ol.Feature} feature The feature
* @return {string|undefined}
*/
os.feature.getLayerId = function(feature) {
var sourceId = undefined;
if (feature) {
sourceId = /** @type {string|undefined} */ (feature.get(os.data.RecordField.SOURCE_ID));
}
return sourceId;
};
/**
* Get the layer containing a feature. This accounts for features that may be rendered in a
* {@link os.layer.AnimationOverlay}, which uses an OL layer/source with hit detection disabled. In that case, find
* the original layer from the map.
*
* @param {ol.Feature} feature The feature
* @return {ol.layer.Layer}
*/
os.feature.getLayer = function(feature) {
var layer = null;
if (feature) {
var sourceId = os.feature.getLayerId(feature);
if (sourceId) {
// look up the layer via the id
layer = os.MapContainer.getInstance().getLayer(sourceId);
}
}
return layer;
};
/**
* Get the source containing a feature. This accounts for features that may be rendered in a
* {@link os.layer.AnimationOverlay}, which uses an OL layer/source with hit detection disabled. In that case, find
* the original source from the data manager.
*
* @param {ol.Feature} feature The feature
* @param {ol.layer.Layer=} opt_layer The layer containing the feature
* @return {os.source.Vector} The source, if it can be found
*/
os.feature.getSource = function(feature, opt_layer) {
var source = null;
if (opt_layer != null) {
// layer was provided - make sure the source is an OS source
var layerSource = opt_layer.getSource();
if (os.instanceOf(layerSource, os.source.Vector.NAME)) {
source = /** @type {!os.source.Vector} */ (layerSource);
}
}
if (source == null && feature != null) {
// no layer or not an OS source, so check if the feature has the source id
var sourceId = os.feature.getLayerId(feature);
if (sourceId) {
// have the source id - check if it's in the data manager
source = os.osDataManager.getSource(sourceId);
}
}
return source;
};
/**
* Get the color of a feature.
*
* @param {ol.Feature} feature The feature.
* @param {os.source.ISource=} opt_source The source containing the feature, or null to ignore source color.
* @param {*=} opt_default The default color.
* @return {*} The color.
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.getColor = function(feature, opt_source, opt_default) {
if (feature) {
var color = /** @type {string|undefined} */ (feature.values_[os.data.RecordField.COLOR]);
if (!color) {
// check the layer config to see if it's replacing feature styles
var layerConfig = os.style.getLayerConfig(feature);
if (layerConfig && layerConfig[os.style.StyleField.REPLACE_STYLE]) {
color = /** @type {string|undefined} */ (os.style.getConfigColor(layerConfig, false));
}
}
if (!color) {
var featureConfig = /** @type {Array<Object>|Object|undefined} */ (feature.values_[os.style.StyleType.FEATURE]);
if (featureConfig) {
if (Array.isArray(featureConfig)) {
for (var i = 0; !color && i < featureConfig.length; i++) {
color = os.style.getConfigColor(featureConfig[i]) || undefined;
}
} else {
color = os.style.getConfigColor(featureConfig) || undefined;
}
}
}
if (color) {
return color;
} else if (opt_source) {
return opt_source.getColor();
} else if (opt_source !== null) {
var source = os.feature.getSource(feature);
if (source) {
return source.getColor();
}
}
}
return opt_default !== undefined ? opt_default : os.style.DEFAULT_LAYER_COLOR;
};
/**
* Gets the shape name for a feature.
*
* @param {!ol.Feature} feature The feature
* @param {os.source.Vector=} opt_source The source containing the feature
* @param {boolean=} opt_preferSource If the source shape should be preferred over the feature shape.
* @return {string|undefined}
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.getShapeName = function(feature, opt_source, opt_preferSource) {
var shapeName = /** @type {string|undefined} */ (feature.values_[os.style.StyleField.SHAPE]);
if (!shapeName || opt_preferSource) {
// get the source shape if the feature didn't define its own shape, or the source shape is preferred
var source = opt_source || os.feature.getSource(feature);
if (source && os.instanceOf(source, os.source.Vector.NAME)) {
var sourceShape = source.getGeometryShape();
if (opt_preferSource || sourceShape !== os.style.ShapeType.DEFAULT) {
shapeName = sourceShape;
}
}
}
return shapeName;
};
/**
* Gets the center shape name for a feature.
*
* @param {!ol.Feature} feature The feature
* @param {os.source.Vector=} opt_source The source containing the feature
* @param {boolean=} opt_preferSource If the source shape should be preferred over the feature shape.
* @return {string|undefined}
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.getCenterShapeName = function(feature, opt_source, opt_preferSource) {
var shapeName = /** @type {string|undefined} */ (feature.values_[os.style.StyleField.CENTER_SHAPE]);
if (!shapeName || opt_preferSource) {
// get the source shape if the feature didn't define its own shape, or the source shape is preferred
var source = opt_source || os.feature.getSource(feature);
if (source && os.instanceOf(source, os.source.Vector.NAME)) {
var sourceShape = source.getCenterGeometryShape();
if (opt_preferSource || sourceShape !== os.style.ShapeType.DEFAULT) {
shapeName = sourceShape;
}
}
}
return shapeName;
};
/**
* Hides the label for a feature.
*
* @param {ol.Feature} feature The feature
* @return {boolean} If the label was hidden, or false if it was hidden already.
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.hideLabel = function(feature) {
if (feature && feature.values_[os.style.StyleField.SHOW_LABELS] !== false) {
feature.values_[os.style.StyleField.SHOW_LABELS] = false;
return true;
}
return false;
};
/**
* Shows the label for a feature.
*
* @param {ol.Feature} feature The feature
* @return {boolean} If the label was hidden, or false if it was hidden already.
*
* @suppress {accessControls} To allow direct access to feature metadata.
*/
os.feature.showLabel = function(feature) {
if (feature && feature.values_[os.style.StyleField.SHOW_LABELS] !== true) {
feature.values_[os.style.StyleField.SHOW_LABELS] = true;
return true;
}
return false;
};
/**
* Updates the feature (typically after style changes)
*
* @param {ol.Feature} feature The feature
* @param {ol.source.Source=} opt_source The source containing the feature
*/
os.feature.update = function(feature, opt_source) {
if (!opt_source) {
opt_source = os.feature.getSource(feature);
}
if (!opt_source && os.MapContainer.getInstance().containsFeature(feature)) {
opt_source = os.MapContainer.getInstance().getLayer(os.MapContainer.DRAW_ID).getSource();
}
var id = feature.getId();
if (id && opt_source && /** @type {ol.source.Vector} */ (opt_source).getFeatureById(id)) {
// for 3D synchronizer
opt_source.dispatchEvent(new ol.source.Vector.Event(ol.source.VectorEventType.CHANGEFEATURE, feature));
// for 2D
opt_source.changed();
}
};
/**
* Remove features from application
*
* @param {!string} sourceId
* @param {Array<ol.Feature>} features
*/
os.feature.removeFeatures = function(sourceId, features) {
var source = os.osDataManager.getSource(sourceId);
if (source && features) {
source.removeFeatures(features);
}
};
/**
* Copy a feature, saving its current style as a local feature style.
*
* @param {!ol.Feature} feature The feature to copy
* @param {Object=} opt_layerConfig The feature's layer config
* @return {!ol.Feature}
*/
os.feature.copyFeature = function(feature, opt_layerConfig) {
var clone = feature.clone();
clone.setId(ol.getUid(clone));
// copy the feature's current style to a new config and set it on the cloned feature
// base config priority: feature config > layer config > default config
var baseConfig = /** @type {Object|undefined} */ (feature.get(os.style.StyleType.FEATURE)) || opt_layerConfig ||
os.style.DEFAULT_VECTOR_CONFIG;
var featureConfig = os.style.createFeatureConfig(feature, baseConfig, opt_layerConfig);
clone.set(os.style.StyleType.FEATURE, featureConfig);
var shapeName = os.feature.getShapeName(feature);
if (shapeName) {
// default shape is point for vector layers, icon for KML layers. check the config to see if it should be an icon.
if (shapeName == os.style.ShapeType.DEFAULT) {
shapeName = os.style.isIconConfig(featureConfig) ? os.style.ShapeType.ICON : os.style.ShapeType.POINT;
}
// places doesn't support selected styles
shapeName = shapeName.replace(/^Selected /, '');
clone.set(os.style.StyleField.SHAPE, shapeName);
}
var centerShapeName = os.feature.getCenterShapeName(feature);
if (centerShapeName) {
// default shape is point for vector layers, icon for KML layers. check the config to see if it should be an icon.
if (centerShapeName == os.style.ShapeType.DEFAULT) {
centerShapeName = os.style.isIconConfig(featureConfig) ? os.style.ShapeType.ICON : os.style.ShapeType.POINT;
}
// places doesn't support selected styles
centerShapeName = centerShapeName.replace(/^Selected /, '');
clone.set(os.style.StyleField.CENTER_SHAPE, centerShapeName);
}
return clone;
};
/**
* Sets an opacity multiplier on every feature in a set.
*
* @param {Array<!ol.Feature>} features
* @param {number} opacity
* @param {os.source.Vector=} opt_source
* @suppress {accessControls}
*/
os.feature.updateFeaturesFadeStyle = function(features, opacity, opt_source) {
var source = opt_source || os.feature.getSource(features[0]);
if (source) {
for (var i = 0, n = features.length; i < n; i++) {
var feature = features[i];
// don't fade dynamic features because they are always displayed
if (feature && !(feature instanceof os.feature.DynamicFeature)) {
// we need to make a copy/clone of the style to mess with the config
var layerConfig = os.style.getLayerConfig(feature, source);
var baseConfig = /** @type {Object|undefined} */
(feature.values_[os.style.StyleType.FEATURE]) ||
layerConfig ||
os.style.DEFAULT_VECTOR_CONFIG;
os.style.setConfigOpacityColor(baseConfig, opacity);
var style = os.style.createFeatureStyle(feature, baseConfig, layerConfig);
feature.setStyle(style);
}
}
}
};
/**
* Test if a feature's source id matches the provided source id.
*