-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widget.js
1545 lines (1456 loc) · 66.4 KB
/
Widget.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
///////////////////////////////////////////////////////////////////////////
// Copyright © 2014 Esri. All Rights Reserved.
//
// Licensed under the Apache License Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////
//
// 05/04/2017 - added functionality to display an interactive polyline total when drawing two or more segments.
// 03/30/2017 - tested with WAB 2.4. Fixed bug with editing measures.
// 08/19/2016 - tested with WAB 2.1 and added functionality to display bearing.
// 02/16/2016 - updated logic to use the abbrevation on measurement labels.
// 02/12/2016 - added checkbox to display summary measurement labels. Fixed problem that was leaving a 0.0 measurement when finishing a line.
// 12/16/2015 - tested with WAB 1.3 and added nautical miles to the distance pick list.
// 10/06/2015 - added editing capability to some geometries. CLICK graphic to start and stop editing, CTRL-CLICK to delete a measure graphic.
// 09/30/2015 - updated to remove unnecessary logic for IE - NIM094815: The mouse-drag event now works correctly using IE10 and IE11.
// 09/29/2015 - added logic to use better area and length calculations using the API geometryEngine
// to work with geodesic and planar projections
// 09/09/2015 - upgrade to new release of WAB 1.2
// - fixed problem with settings that was corrupting the WAB app
// 04/06/2015 - upgrade to new release of WAB 1.1
// - use new JSAPI scaleUtils to get map units
// - added logic for Internet Explorer to measure all geometry types
// - updated offset for mobile on certain geometry types
// 03/04/2015 - stacked area measurements
// added logic for calculating map units
// added ability to change measure text size and color
// 02/24/2015 - added logic for using on iPad, iPhone and Android devices
// added logic for stopping popups
// 02/18/2015 - removed the last measure from the freehand polygon tool
// 02/17/2015 - fix problem with perimeter measurement
// 02/17/2015 - Initial release created from the draw widget.
define([
'dojo/_base/declare',
'dijit/_WidgetsInTemplateMixin',
'jimu/BaseWidget',
'esri/layers/GraphicsLayer',
'esri/toolbars/edit',
'esri/graphic',
'esri/geometry/Extent',
'esri/geometry/Point',
'esri/symbols/SimpleMarkerSymbol',
'esri/geometry/Polyline',
'esri/symbols/SimpleLineSymbol',
'esri/geometry/Polygon',
'esri/symbols/SimpleFillSymbol',
'esri/symbols/TextSymbol',
'esri/symbols/Font',
'esri/SpatialReference',
'esri/units',
"esri/geometry/scaleUtils",
'esri/geometry/webMercatorUtils',
'esri/geometry/geodesicUtils',
'esri/geometry/geometryEngine',
'dojo/_base/lang',
'dojo/on',
'dojo/_base/html',
'dojo/_base/Color',
'dojo/_base/query',
'dojo/_base/array',
'dojo/_base/event',
'dojo/touch',
'dojo/has',
'dijit/form/Select',
'dijit/form/NumberSpinner',
'jimu/dijit/ViewStack',
'jimu/dijit/SymbolChooser',
'jimu/dijit/DrawBox',
'jimu/utils',
'dojo/sniff'
],
function(declare, _WidgetsInTemplateMixin, BaseWidget, GraphicsLayer, Edit, Graphic, Extent, Point,
SimpleMarkerSymbol, Polyline, SimpleLineSymbol, Polygon, SimpleFillSymbol,
TextSymbol, Font, SpatialReference, esriUnits, scaleUtils, webMercatorUtils, geodesicUtils, geometryEngine, lang, on, html,
Color, Query, array, event, touch, has, Select, NumberSpinner, ViewStack, SymbolChooser,
DrawBox, jimuUtils, sniff) { /*jshint unused: false*/
return declare([BaseWidget, _WidgetsInTemplateMixin], {
name: 'Measure',
baseClass: 'jimu-widget-measure',
measureClickHandler: null,
measureMoveHandler: null,
measureMouseDownHandler: null,
measureMouseDragHandler: null,
measureType: "",
measureGraphic: null,
measureGraphic2: null,
measurePolylineGraphic: null,
measureGraphicReturn: null,
measureGraphicsLayer: null,
measureSegment: 1,
useGeodesic: true,
editingEnabled: false,
editToolbar: null,
editGraphicID: "",
editGraphic1: null,
editGraphic2: null,
measureEditVertex: null,
isEditable: false,
measureBearing: null,
measureReturnBearing: null,
postMixInProperties: function() {
this.inherited(arguments);
this._resetUnitsArrays();
},
postCreate: function() {
this.inherited(arguments);
//jimuUtils.combineRadioCheckBoxWithLabel(this.showMeasure, this.showMeasureLabel);
this.drawBox.setMap(this.map);
this.viewStack = new ViewStack({
viewType: 'dom',
views: [this.pointSection, this.lineSection, this.polygonSection]
});
html.place(this.viewStack.domNode, this.settingContent);
this.textSymChooser.inputText.value = "Sample Text";
this.textSymChooser.textPreview.innerHTML = "Sample Text";
// override default font size
this.textSymChooser.textFontSize.value = "12";
this.textSymChooser.textFontSize.textbox.value = "12";
this._initUnitSelect();
this._bindEvents();
this.measureGraphicsLayer = new GraphicsLayer();
this.measureGraphicsLayer.name = "Search Buffer Results";
this.map.addLayer(this.measureGraphicsLayer);
this.drawBox.drawLayer = this.measureGraphicsLayer;
this.editToolbar = new Edit(this.map);
this.own(on(this.editToolbar, 'vertex-move-start', lang.hitch(this, this._editVertexStart)));
this.own(on(this.editToolbar, 'vertex-move-stop', lang.hitch(this, this._editVertexStop)));
this.own(on(this.editToolbar, 'vertex-delete', lang.hitch(this, this._vertexDelete)));
this.own(on(this.editToolbar, 'vertex-move', lang.hitch(this, this._editVertex)));
this.own(on(this.measureGraphicsLayer, "click", lang.hitch(this, function(evt) {
event.stop(evt);
//delete feature if ctrl key is depressed
if (evt.ctrlKey === true || evt.metaKey === true) {
var id = evt.graphic.attributes.id;
for (var i = this.measureGraphicsLayer.graphics.length-1; i >= 0; i--) {
var g = this.measureGraphicsLayer.graphics[i];
if (g.attributes.id === id) {
this.measureGraphicsLayer.remove(g);
}
}
this.editToolbar.deactivate();
this.editingEnabled = false;
} else {
//if (evt.shiftKey === true) { //edit feature if shift key is depressed
// see if graphic is editable. lines, polylines and polygons are editable.
if (evt.graphic.attributes.editable) {
if (this.editingEnabled === false) {
this.editingEnabled = true;
this.editToolbar.activate(Edit.EDIT_VERTICES, evt.graphic);
if (evt.graphic.geometry.type === 'polyline') {
this.viewStack.switchView(this.lineSection);
} else {
this.viewStack.switchView(this.polygonSection);
}
} else {
this.editToolbar.deactivate();
this.editingEnabled = false;
}
}
}
})));
var wkid = this.map.spatialReference.wkid;
if (wkid === 102100 || wkid === 3857 || wkid === 102113 || wkid === 4326) {
this.useGeodesic = true;
} else {
this.useGeodesic = false;
}
},
_resetUnitsArrays: function() {
this.defaultDistanceUnits = [];
this.defaultAreaUnits = [];
this.configDistanceUnits = [];
this.configAreaUnits = [];
this.distanceUnits = [];
this.areaUnits = [];
},
_bindEvents: function() {
//bind DrawBox
this.own(on(this.drawBox, 'IconSelected', lang.hitch(this, this._onIconSelected)));
this.own(on(this.drawBox, 'DrawEnd', lang.hitch(this, this._onDrawEnd)));
this.own(on(this.drawBox, 'Clear', lang.hitch(this, this._clear)));
//bind symbol change events
this.own(on(this.pointSymChooser, 'change', lang.hitch(this, function() {
this._setDrawDefaultSymbols();
})));
this.own(on(this.lineSymChooser, 'change', lang.hitch(this, function() {
this._setDrawDefaultSymbols();
})));
this.own(on(this.fillSymChooser, 'change', lang.hitch(this, function() {
this._setDrawDefaultSymbols();
})));
this.own(on(this.textSymChooser, 'change', lang.hitch(this, function(symbol) {
this._setDrawDefaultSymbols();
})));
this.own(on(this.helpImage, 'click', lang.hitch(this, function() {
var win = window.open("/Widget/Rolmaat/help/nl/index.html", "_blank");
win.focus();
})));
},
_onIconSelected: function(target, geotype, commontype) {
this.measureBearing = "";
this.measureReturnBearing = "";
// create a unique ID that will be used to associate the drawn graphic and text measurement graphics
var d = new Date();
this.editGraphicID = "gra_" + d.getTime();
if (geotype === 'LINE' || geotype === 'POLYLINE' || geotype === 'POLYGON') {
this.isEditable = true;
} else {
this.isEditable = false;
}
// if the user was editing, let's turn editing off
if (this.editingEnabled) {
this.editToolbar.deactivate();
this.editingEnabled = false;
}
this.measureType = geotype;
this._setDrawDefaultSymbols();
if (commontype === 'point') {
this.viewStack.switchView(this.pointSection);
} else if (commontype === 'polyline') {
this.viewStack.switchView(this.lineSection);
if (geotype === 'POLYLINE') {
this.measureClickHandler = on(this.map, "click", lang.hitch(this, this._measureClick));
} else {
if (has('ipad') || has('iphone') || has('android')) {
this.measureMouseDownHandler = on(document, touch.press, lang.hitch(this, this._measureMouseDown));
} else {
this.measureMouseDownHandler = on(this.map, "mouse-down", lang.hitch(this, this._measureMouseDown));
}
}
} else if (commontype === 'polygon') {
this.viewStack.switchView(this.polygonSection);
if (geotype === 'POLYGON') {
this.measureClickHandler = on(this.map, "click", lang.hitch(this, this._measureClick));
} else {
if (has('ipad') || has('iphone') || has('android')) {
this.measureMouseDownHandler = on(document, touch.press, lang.hitch(this, this._measureMouseDown));
} else {
this.measureMouseDownHandler = on(this.map, "mouse-down", lang.hitch(this, this._measureMouseDown));
}
}
} else if (commontype === 'text') {
this.viewStack.switchView(this.textSection);
}
this._setMeasureVisibility();
},
_onDrawEnd: function(graphic, geotype, commontype) {
// var dog = new BarkBark; // used to break on purpose for debugging...
// set graphic attributes
var att = {id: this.editGraphicID, editable: this.isEditable};
graphic.setAttributes(att);
var geometry = graphic.geometry;
var firstPoint = null;
var lastPoint = null;
var l; // number of segments
var pl = Polygon(geometry);
if (this.measureType === 'FREEHAND_POLYGON') {
// with this measure type we want to remove the last graphic
// as it completes the freehand polygon to the original start
// and makes the interactive measure incomplete
this.measureGraphicsLayer.remove(this.measureGraphic);
}
if (has('ipad') || has('iphone') || has('android')) {
// measuring on mobile devices doesn't add the final segment measurements.
if (this.measureType === 'POLYLINE') {
firstPoint = null;
lastPoint = null;
pl = Polyline(geometry);
l = pl.paths[0].length;
firstPoint = pl.getPoint(0, l - 2);
lastPoint = pl.getPoint(0, l - 1);
if (l > 1) {
var segLength = this._calculateSegmentLength(firstPoint, lastPoint);
var angle = this._calculateAngle(firstPoint, lastPoint);
var midPT = this._calculateMidPoint(firstPoint, lastPoint);
this._addClickMeasure(midPT, angle, segLength);
}
}
if (this.measureType === 'POLYGON') {
firstPoint = null;
lastPoint = null;
pl = Polygon(geometry);
l = pl.rings[0].length;
firstPoint = pl.getPoint(0, l - 2);
lastPoint = pl.getPoint(0, l - 1);
if (l > 1) {
var segLength = this._calculateSegmentLength(firstPoint, lastPoint);
var angle = this._calculateAngle(firstPoint, lastPoint);
var midPT = this._calculateMidPoint(firstPoint, lastPoint);
this._addClickMeasure(midPT, angle, segLength);
}
// do this again to get the second to last measurement
var fp = pl.getPoint(0, l - 3);
var lp = pl.getPoint(0, l - 2);
if (l > 1) {
var segLength = this._calculateSegmentLength(fp, lp);
var angle = this._calculateAngle(fp, lp);
var mp = this._calculateMidPoint(fp, lp);
this._addClickMeasure(mp, angle, segLength);
}
}
}
if (this.measurePolylineGraphic) {
this.measureGraphicsLayer.remove(this.measurePolylineGraphic);
}
this.measureGraphic = null;
this.measureGraphic2 = null;
this.measurePolylineGraphic = null;
this.measureGraphicReturn = null;
this.measureSegment = 1;
if (geometry.type === 'extent') {
var a = geometry;
var polygon = new Polygon(a.spatialReference);
var r = [
[a.xmin, a.ymin],
[a.xmin, a.ymax],
[a.xmax, a.ymax],
[a.xmax, a.ymin],
[a.xmin, a.ymin]
];
polygon.addRing(r);
geometry = polygon;
commontype = 'polygon';
}
if (commontype === 'polyline') {
if (this.showTotals.checked){
if (this.measureType !== 'LINE' && this.measureType !== 'FREEHAND_POLYLINE') {
this._addLineMeasure(geometry);
}
}
if (geotype === 'POLYLINE') {
if (this.measureMoveHandler) {
this.measureMoveHandler.remove();
}
} else {
this.measureMouseDragHandler.remove();
}
} else if (commontype === 'polygon') {
if (this.showTotals.checked){
this._addPolygonMeasure(geometry);
}
if (geotype === 'POLYGON') {
if (this.measureMoveHandler) {
this.measureMoveHandler.remove();
}
} else {
this.measureMouseDragHandler.remove();
}
}
},
_reorderGraphics: function() {
// text for measurements needs to be on type all others
var graArray = [];
// make 2 pass through the graphics in the measure array move text to the top.
// second pass adds text graphics
for (var i = 0; i < this.drawBox.drawLayer.graphics.length; i++) {
var gra = this.drawBox.drawLayer.graphics[i];
if (gra.symbol.type === 'textsymbol') {
graArray.push(gra);
}
}
// first pass adds non text graphics
for (var i = 0; i < this.drawBox.drawLayer.graphics.length; i++) {
var gra = this.drawBox.drawLayer.graphics[i];
if (gra.symbol.type !== 'textsymbol') {
graArray.push(gra);
}
}
this.drawBox.drawLayer.graphics = graArray;
this.drawBox.drawLayer.redraw();
},
_measureClick: function(e) {
// start the measure of the graphic being drawn
// console.log("Map measure clicked");
this.measureClickHandler.remove();
if (has('ipad') || has('iphone') || has('android')) {
this.measureMoveHandler = on(this.map, "click", lang.hitch(this, this._measureClickSegment));
} else {
this.measureMoveHandler = on(this.map, "mouse-move", lang.hitch(this, this._measureMove));
}
},
_measureMove: function(e) {
// get the graphic being drawn
var gra = this.drawBox.drawToolBar._graphic;
// the geometry may be null as drawing is beginning
if (gra) {
// get the last point in the drawn graphic
var firstPoint = null;
var lastPoint = null;
var l; // number of segments
if (gra.geometry.type === 'polyline') {
var pl = Polyline(gra.geometry);
l = pl.paths[0].length;
lastPoint = pl.getPoint(0, l - 1);
} else {
// must be a polygon
var poly = Polygon(gra.geometry);
l = poly.rings[0].length;
firstPoint = poly.getPoint(0, 0);
lastPoint = poly.getPoint(0, l - 1);
}
var segLength = this._calculateSegmentLength(lastPoint, e.mapPoint);
var angle = this._calculateAngle(lastPoint, e.mapPoint);
var midPT = this._calculateMidPoint(lastPoint, e.mapPoint);
this.measureBearing = this._getBearing(lastPoint, e.mapPoint);
if (l > this.measureSegment) {
// this means that we have started a new segment and need to leave the last measure in place
//var att = {id: this.editGraphicID, index: this.measureSegment};
//var mg = new Graphic(this.measureGraphic.geometry, this.measureGraphic.symbol, att, null);
//this.measureGraphicsLayer.add(mg);
this.measureGraphic = null;
this.measureSegment = l;
}
if (angle) {
this._addSegmentMeasure(midPT, angle, segLength);
}
// lets get the return length to the beginning point
if (l > 1 && gra.geometry.type === 'polygon') {
var mp = this._calculateMidPoint(firstPoint, e.mapPoint);
var sl = this._calculateSegmentLength(firstPoint, e.mapPoint);
var angle = this._calculateAngle(firstPoint, e.mapPoint);
this.measureReturnBearing = this._getBearing(e.mapPoint, firstPoint);
this._addReturnMeasure(mp, angle, sl);
}
// display an interactive measurement of the polyline when there are more than 2 segments
if (l > 1 && gra.geometry.type === 'polyline') {
var path = [];
for (var i = 0; i < gra.geometry.paths[0].length; i++) {
path.push(gra.geometry.paths[0][i]);
}
path.push([e.mapPoint.x, e.mapPoint.y]);
var currentPL = new Polyline(this.map.spatialReference);
currentPL.addPath(path);
var polyLength = this._calculatePolylineLength(currentPL);
this._addPolylineTotalMeasure(polyLength, e.mapPoint);
}
//console.log("Map measure moved - length: " + segLength);
}
},
_editVertexStart: function(e) {
console.log("Started Vertex Edit");
// this is a good place to determine which measure dimension graphics we are going to edit
// depending on which vertex is chosen.
var gra = e.graphic;
this.editGraphicID = e.graphic.attributes.id;
var geomtype = gra.geometry.type;
var pl;
var l = 0;
if (geomtype === 'polyline') {
pl = new Polyline(gra.geometry);
l = pl.paths[0].length;
} else {
pl = new Polygon(gra.geometry);
l = pl.rings[0].length;
}
this._setDrawDefaultSymbols();
var isGhostVertex = e.vertexinfo.isGhost;
var pointIndex = e.vertexinfo.pointIndex;
if (isGhostVertex) {
// this is a ghost vertex that means we are splitting the line. We need to add a new dimension graphic
// and update the other dimension graphics sequence numbers
// update the index numbers for the other dimension graphics
var g;
for (var i = 0; i < gra._graphicsLayer.graphics.length; i++) {
g = gra._graphicsLayer.graphics[i];
var att = g.attributes;
if (att.id === gra.attributes.id && att.index > pointIndex && att.index < 999) {
g.attributes.index += 1;
}
}
// we are moving a vertex in the middle of the polyline and measuring 2 segments
this.editGraphic1 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, pointIndex);
// new graphic for second dimension
var a = {id: this.editGraphic1.attributes.id, index: this.editGraphic1.attributes.index + 1};
this.editGraphic2 = new Graphic(this.editGraphic1.geometry, this.editGraphic1.symbol, a, null);
this.measureGraphicsLayer.add(this.editGraphic2);
} else {
// not a ghost vertex. if the vertex is the first or last we will only be calculating one measure
// otherwise we will be calculating distances for two segments.
// find the dimension graphic that will be changed
if (pointIndex === 0) {
if (geomtype === 'polyline') {
this.editGraphic1 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, pointIndex + 1);
this.editGraphic2 = null;
} else {
// get the first dimension and the last dimension for a polygon
this.editGraphic1 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, pointIndex + 1);
this.editGraphic2 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, l - 1);
}
} else if (pointIndex === l - 1) {
this.editGraphic1 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, pointIndex);
this.editGraphic2 = null;
} else {
// we are moving a vertex in the middle of the polyline and measuring 2 segments
this.editGraphic1 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, pointIndex);
this.editGraphic2 = this._findDimensionGraphic(gra._graphicsLayer, gra.attributes.id, pointIndex + 1);
}
}
// make the vertex information available globally
this.measureEditVertex = e;
// as per usual, we have to add logic to make this work with Internet Explorer...
if (has('ipad') || has('iphone') || has('android')) {
this.measureMouseDragHandler = on(document, touch.move, lang.hitch(this, this._editVertex));
} else if (has("ie") === 9 || has("ie") === 10 || has("trident")) {
this.measureMoveHandler = on(this.map, "mouse-drag", lang.hitch(this, this._editVertex));
//console.log("Using mouse-drag for IE");
} else {
this.measureMoveHandler = on(this.map, "mouse-drag", lang.hitch(this, this._editVertex));
//this.measureMoveHandler = on(this.map, "mouse-drag", lang.hitch(this, this._editVertex));
}
},
_findDimensionGraphic: function(graphicsLayer, id, index) {
var gra = null;
for (var i = 0; i < graphicsLayer.graphics.length; i++) {
var g = graphicsLayer.graphics[i];
var att = g.attributes;
if (att.id === id && index === att.index) {
gra = g;
}
}
return gra;
},
_editVertexStop: function(e) {
console.log("Stopped Vertex Edit");
if (this.measureMoveHandler) {
this.measureMoveHandler.remove();
}
// remove the summary text graphics
var id = e.graphic.attributes.id;
for (var i = this.measureGraphicsLayer.graphics.length-1; i >= 0; i--) {
var g = this.measureGraphicsLayer.graphics[i];
if (g.attributes.id === id) {
// remove area and length summary graphics. we are going to recalculate and re-add them
if (g.attributes.index === 999) {
this.measureGraphicsLayer.remove(g);
}
}
}
if (this.showTotals.checked){
if (e.graphic.geometry.type === 'polyline') {
this._addLineMeasure(e.graphic.geometry);
} else {
this._addPolygonMeasure(e.graphic.geometry);
}
}
this.measureGraphic = null;
this.measureGraphic2 = null;
this.measureGraphicReturn = null;
this.measureSegment = 1;
},
_vertexMove: function(e) {
console.log("Moving Vertex...");
// get the graphic being drawn
var gra = this.measureEditVertex.graphic;
var vi = this.measureEditVertex.vertexinfo;
var transform = e.transform;
if (!this.measureMoveHandler) {
this.measureMoveHandler = on(this.map, "mouse-drag", lang.hitch(this, this._editVertex));
}
},
_editVertex: function(e) {
console.log("Editing Vertex...");
// get the graphic being drawn
var gra = this.measureEditVertex.graphic;
var vi = this.measureEditVertex.vertexinfo;
var transform = e.transform;
// calculate the point movement
var pt = this.map.toScreen(vi.graphic.geometry);
var ePoint = this.map.toMap(pt.offset(transform.dx, transform.dy));
//console.log("Transform dx: " + transform.dx + ", dy: " + transform.dy)
// the geometry may be null as drawing is beginning
if (gra) {
// get the last point in the drawn graphic
var stationaryPoint1 = null;
var stationaryPoint2 = null;
var pl;
var l;
if (gra.geometry.type === 'polyline') {
pl = Polyline(gra.geometry);
l = pl.paths[0].length;
} else {
// must be a polygon
pl = Polygon(gra.geometry);
l = pl.rings[0].length;
}
if (vi.isGhost) {
stationaryPoint1 = pl.getPoint(0, vi.pointIndex -1);
stationaryPoint2 = pl.getPoint(0, vi.pointIndex);
} else {
if (vi.pointIndex === 0) {
stationaryPoint1 = pl.getPoint(0, vi.pointIndex + 1);
if (gra.geometry.type === 'polygon') {
stationaryPoint2 = pl.getPoint(0, l - 2);
}
} else if (vi.pointIndex === l -1) {
stationaryPoint1 = pl.getPoint(0, vi.pointIndex -1);
} else {
// we are moving a vertex in the middle of the polyline and measuring 2 segments
stationaryPoint1 = pl.getPoint(0, vi.pointIndex -1);
stationaryPoint2 = pl.getPoint(0, vi.pointIndex +1);
}
}
// calculate the first segment dimension
var segLength = this._calculateSegmentLength(stationaryPoint1, ePoint);
var angle = this._calculateAngle(stationaryPoint1, ePoint);
var midPT = this._calculateMidPoint(stationaryPoint1, ePoint);
this.measureBearing = this._getBearing(stationaryPoint1, ePoint);
this.measureGraphic = this.editGraphic1;
this._addSegmentMeasure(midPT, angle, segLength);
// if not moving an end vertex, calculate the second segment dimension
if (stationaryPoint2) {
segLength = this._calculateSegmentLength(stationaryPoint2, ePoint);
angle = this._calculateAngle(stationaryPoint2, ePoint);
midPT = this._calculateMidPoint(stationaryPoint2, ePoint);
this.measureReturnBearing = this._getBearing(ePoint, stationaryPoint2);
this.measureGraphic2 = this.editGraphic2;
this._addSegmentMeasure2(midPT, angle, segLength);
}
}
},
_vertexDelete: function(e) {
// routine to delete the selected vertex
var gra = e.graphic;
var vi = e.vertexinfo;
var a = 0;
// remove text graphics
for (var i = this.measureGraphicsLayer.graphics.length-1; i >= 0; i--) {
var g = this.measureGraphicsLayer.graphics[i];
if (g.attributes.id === this.editGraphicID && g.geometry.type === 'point') {
this.measureGraphicsLayer.remove(g);
}
}
// add new measures to the line
var pl;
var l;
if (gra.geometry.type === 'polyline') {
pl = Polyline(gra.geometry);
l = pl.paths[0].length;
} else {
// must be a polygon
pl = Polygon(gra.geometry);
l = pl.rings[0].length;
}
for (i = 1; i < l; i++) {
var pt1 = pl.getPoint(0, i -1);
var pt2 = pl.getPoint(0, i);
// calculate the first segment dimension
var segLength = this._calculateSegmentLength(pt1, pt2);
var angle = this._calculateAngle(pt1, pt2);
var midPT = this._calculateMidPoint(pt1, pt2);
this.measureBearing = this._getBearing(pt1, pt2);
this.measureGraphic = null;
this.measureSegment = i;
this._addSegmentMeasure(midPT, angle, segLength);
}
if (this.showTotals.checked){
if (gra.geometry.type === 'polyline') {
this._addLineMeasure(pl);
} else {
this._addPolygonMeasure(pl);
}
}
},
_measureClickSegment: function(e) {
// get the graphic being drawn
var gra = this.drawBox.drawToolBar._graphic;
// the geometry may be null as drawing is beginning
if (gra) {
// get the last point in the drawn graphic
var firstPoint = null;
var lastPoint = null;
var l; // number of segments
if (gra.geometry.type === 'polyline') {
var pl = Polyline(gra.geometry);
l = pl.paths[0].length;
firstPoint = pl.getPoint(0, l - 2);
lastPoint = pl.getPoint(0, l - 1);
} else {
// must be a polygon
var poly = Polygon(gra.geometry);
l = poly.rings[0].length;
firstPoint = poly.getPoint(0, l - 2);
lastPoint = poly.getPoint(0, l - 1);
}
if (l > 1) {
var segLength = this._calculateSegmentLength(firstPoint, lastPoint);
var angle = this._calculateAngle(firstPoint, lastPoint);
var midPT = this._calculateMidPoint(firstPoint, lastPoint);
this._addClickMeasure(midPT, angle, segLength);
}
}
},
_measureMouseDown: function(e) {
// ts - start the measure of the graphic being drawn
//console.log("Map measure mouse down");
this.measureMouseDownHandler.remove();
if (has('ipad') || has('iphone') || has('android')) {
this.measureMouseDragHandler = on(document, touch.move, lang.hitch(this, this._measureDrag));
} else {
this.measureMouseDragHandler = on(this.map, "mouse-drag", lang.hitch(this, this._measureDrag));
}
},
_measureDrag: function(e) {
// get the graphic being drawn
var gra = this.drawBox.drawToolBar._graphic;
// the geometry may be null as drawing is beginning
if (gra) {
// get the last point in the drawn graphic
var firstPoint = null;
var lastPoint = null;
var l; // number of segments
if (this.measureType === 'LINE') {
var pl = Polyline(gra.geometry);
firstPoint = pl.getPoint(0, 0);
var segLength = this._calculateSegmentLength(firstPoint, e.mapPoint);
var angle = this._calculateAngle(firstPoint, e.mapPoint);
var midPT = this._calculateMidPoint(firstPoint, e.mapPoint);
this._addSegmentMeasure(midPT, angle, segLength);
}
if (this.measureType === 'FREEHAND_POLYLINE') {
var pl = Polyline(gra.geometry);
var segLength = this._calculatePolylineLength(pl);
if (has('ipad') || has('iphone') || has('android')) {
// if mobile we want to offset the text so it can be seen.
var pt = new Point(e.mapPoint.x, e.mapPoint.y, this.map.spatialReference);
pt.y += this._calculateDistanceFromPixels(20);
this._addSegmentMeasure(pt, 0, segLength);
} else {
this._addSegmentMeasure(e.mapPoint, 0, segLength);
}
}
if (this.measureType === 'TRIANGLE') {
var pl = Polygon(gra.geometry);
firstPoint = pl.getPoint(0, 1);
lastPoint = pl.getPoint(0, 2);
if (firstPoint) { // this is for IE. It can't resolve the point when it begins drawing.
var segLength = this._calculateSegmentLength(firstPoint, lastPoint);
var angle = this._calculateAngle(firstPoint, lastPoint);
var midPT = this._calculateMidPoint(firstPoint, lastPoint);
this._addSegmentMeasure(midPT, angle, segLength);
}
}
if (this.measureType === 'EXTENT') {
var ext = gra._extent;
firstPoint = Point(ext.xmin, ext.ymax);
lastPoint = Point(ext.xmax, ext.ymax);
var segLength = this._calculateSegmentLength(firstPoint, lastPoint);
var angle = this._calculateAngle(firstPoint, lastPoint);
var midPT = this._calculateMidPoint(firstPoint, lastPoint);
this._addSegmentMeasure(midPT, angle, 'w: ' + segLength);
fPoint = Point(ext.xmin, ext.ymin);
lPoint = Point(ext.xmin, ext.ymax);
var segLength = this._calculateSegmentLength(fPoint, lPoint);
var angle = this._calculateAngle(fPoint, lPoint);
var midPT = this._calculateMidPoint(fPoint, lPoint);
this._addSegmentMeasure2(midPT, angle, 'h: ' + segLength);
}
if (this.measureType === 'CIRCLE') {
var ext = gra._extent;
var segLength = this._calculateSegmentLength(ext.getCenter(), e.mapPoint);
if (has('ipad') || has('iphone') || has('android')) {
// if mobile we want to offset the text so it can be seen.
var pt = new Point(e.mapPoint.x, e.mapPoint.y, this.map.spatialReference);
pt.y += this._calculateDistanceFromPixels(20);
this._addSegmentMeasure(pt, 0, 'r=' + segLength);
} else {
this._addSegmentMeasure(e.mapPoint, 0, 'r=' + segLength);
}
}
if (this.measureType === 'ELLIPSE') {
var ext = gra._extent;
firstPoint = Point(ext.xmin, ext.ymax);
lastPoint = Point(ext.xmax, ext.ymax);
var segLength = this._calculateSegmentLength(firstPoint, lastPoint);
var angle = this._calculateAngle(firstPoint, lastPoint);
var midPT = this._calculateMidPoint(firstPoint, lastPoint);
this._addSegmentMeasure(midPT, angle, 'w: ' + segLength);
var fPoint = Point(ext.xmin, ext.ymin);
var lPoint = Point(ext.xmin, ext.ymax);
var segLength = this._calculateSegmentLength(fPoint, lPoint);
var angle = this._calculateAngle(fPoint, lPoint);
var midPT = this._calculateMidPoint(fPoint, lPoint);
this._addSegmentMeasure2(midPT, angle, 'h: ' + segLength);
}
if (this.measureType == 'FREEHAND_POLYGON') {
var pl = new Polyline(gra.geometry.spatialReference);
var points = gra.geometry.rings[0];
points = points.slice(0, points.length - 1);
pl.addPath(points);
var segLength = this._calculatePolylineLength(pl);
if (has('ipad') || has('iphone') || has('android')) {
// if mobile we want to offset the text so it can be seen.
var pt = new Point(e.mapPoint.x, e.mapPoint.y, this.map.spatialReference);
pt.y += this._calculateDistanceFromPixels(20);
this._addSegmentMeasure(pt, 0, segLength);
} else {
this._addSegmentMeasure(e.mapPoint, 0, segLength);
}
}
}
},
_calculateSegmentLength: function(pt1, pt2) {
var pl = new Polyline(this.map.spatialReference);
pl.addPath([pt1, pt2]);
// we want the last point being drawn
var unit = this.distanceUnitSelect.value;
var gsUnit = this._getUnitByEsriUnit(unit);
var geoLength = 0;
if (this.useGeodesic) {
geoLength = geometryEngine.geodesicLength(pl, gsUnit);
} else {
geoLength = geometryEngine.planarLength(pl, gsUnit);
}
var abbr = this._getDistanceUnitInfo(unit).abbr;
var localeLength = jimuUtils.localizeNumber(geoLength.toFixed(1));
var length = localeLength + abbr;
this.textSymChooser.inputText.value = length;
this.textSymChooser.textPreview.innerHTML = length;
return length;
},
_calculatePolylineLength: function(pl) {
// we want the last point being drawn
var unit = this.distanceUnitSelect.value;
var gsUnit = this._getUnitByEsriUnit(unit);
var geoLength = 0;
if (this.useGeodesic) {
geoLength = geometryEngine.geodesicLength(pl, gsUnit);
} else {
geoLength = geometryEngine.planarLength(pl, gsUnit);
}
var abbr = this._getDistanceUnitInfo(unit).abbr;
// this is a placeholder for logic. Need to add logic for actual map units either meters or feet
var localeLength = jimuUtils.localizeNumber(geoLength.toFixed(1));
var length = localeLength + abbr;
this.textSymChooser.inputText.value = length;
return length;
},
_calculateAngle: function(pt1, pt2) {
// some basic trig to calculate the angle for the text to be placed
var y = pt2.y - pt1.y;
var x = pt2.x - pt1.x;
var r = y / x;
var angle = Math.atan(r) * 180 / Math.PI * -1;
return angle;
},
_calculateMidPoint: function(pt1, pt2) {
var midX = (pt1.x + pt2.x) / 2;
var midY = (pt1.y + pt2.y) / 2;
var midPoint = new Point(midX, midY, this.map.spatialReference);
return midPoint;
},
_calculateMidPointWithOffset: function(pt1, pt2) {
// THIS DOESN'T WORK YET... NEED TO GET BETTER FORMULA FOR CALCULATING OFFSET
var midX = (pt1.x + pt2.x) / 2;
var midY = (pt1.y + pt2.y) / 2;
// offset the point from the line to do smooth tracking of the measurement
var offset = this._calculateDistanceFromPixels(10); // convert 10 pixels into map units
var dx = pt1.x - pt2.x;
var slope = dx / (pt1.y - pt2.y);
var x = (Math.sqrt(Math.abs(Math.pow(offset, 2) - Math.pow(dx, 2))) / slope) + midX;
var y = (slope * (x - midX)) + midY;
var midPoint = new Point(x, y, this.map.spatialReference);
return midPoint;
},
_calculateDistanceFromPixels: function(pixels) {
var screenPoint = this.map.toScreen(this.map.extent.getCenter());
var upperLeftScreenPoint = new Point(screenPoint.x - pixels, screenPoint.y - pixels);
var lowerRightScreenPoint = new Point(screenPoint.x + pixels, screenPoint.y + pixels);
var upperLeftMapPoint = this.map.toMap(upperLeftScreenPoint);
var lowerRightMapPoint = this.map.toMap(lowerRightScreenPoint);
var ext = new Extent(upperLeftMapPoint.x, upperLeftMapPoint.y, lowerRightMapPoint.x, lowerRightMapPoint.y, this.map.spatialReference);
return ext.getWidth();
},
_addSegmentMeasure: function(pt, angle, length) {
var a = Font.STYLE_ITALIC;
var b = Font.VARIANT_NORMAL;
var c = Font.WEIGHT_BOLD;
var symbolFont = new Font(this.textSymChooser.textFontSize.value + "px", a, b, c, "Courier");
var fontColor = this.textSymChooser.textColor.color;
if (this.showBearing.checked){
length += " " + this.measureBearing;
}
var textSymbol = new TextSymbol(length, symbolFont, fontColor);
var xOff = 0;
var yOff = 0;
if (angle >= 0 && angle < 45) {
xOff = 5;
yOff = 10;
} else if (angle > 45) {
xOff = 10;
yOff = 5;
} else if (angle > -45 && angle < 0) {
xOff = 5;
yOff = 13;
} else {
xOff = -10;
yOff = 5;
}
textSymbol.setOffset(xOff, yOff);
textSymbol.setAngle(angle);
// console.log("angle: " + angle);
if (this.measureGraphic === null || this.measureGraphic._graphicsLayer === null) {
var att = {id: this.editGraphicID, index: this.measureSegment};
this.measureGraphic = new Graphic(pt, textSymbol, att, null);
this.measureGraphicsLayer.add(this.measureGraphic);
} else {
this.measureGraphic.setGeometry(pt);
this.measureGraphic.setSymbol(textSymbol);
//console.log("Moving Segment Measure...");
}
},
_addPolylineTotalMeasure: function(length, pt) {
var a = Font.STYLE_ITALIC;
var b = Font.VARIANT_NORMAL;
var c = Font.WEIGHT_BOLD;
var symbolFont = new Font(this.textSymChooser.textFontSize.value + "px", a, b, c, "Courier");
var fontColor = this.textSymChooser.textColor.color;
var textSymbol = new TextSymbol(length, symbolFont, fontColor);
textSymbol.setOffset(60, 10);
textSymbol.setAlign(Font.ALIGN_START);
if (this.measurePolylineGraphic === null || this.measurePolylineGraphic._graphicsLayer === null) {
this.measurePolylineGraphic = new Graphic(pt, textSymbol, null, null);
this.measureGraphicsLayer.add(this.measurePolylineGraphic);
} else {
this.measurePolylineGraphic.setGeometry(pt);
this.measurePolylineGraphic.setSymbol(textSymbol);
}
},
_addClickMeasure: function(pt, angle, length) {
var a = Font.STYLE_ITALIC;
var b = Font.VARIANT_NORMAL;
var c = Font.WEIGHT_BOLD;
var symbolFont = new Font(this.textSymChooser.textFontSize.value + "px", a, b, c, "Courier");
var fontColor = this.textSymChooser.textColor.color;
var textSymbol = new TextSymbol(length, symbolFont, fontColor);
var xOff = 0;
var yOff = 0;
if (angle >= 0 && angle < 45) {
xOff = 5;
yOff = 10;
} else if (angle > 45) {
xOff = 10;