-
Notifications
You must be signed in to change notification settings - Fork 88
/
azimut.js
132 lines (120 loc) · 5.1 KB
/
azimut.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
// The MIT License (MIT)
//
// Copyright (c) 2016-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
import ngeoDrawController from 'ngeo/draw/Controller';
import ngeoMiscFilters from 'ngeo/misc/filters';
import ngeoGeometryType from 'ngeo/GeometryType';
import ngeoInteractionMeasureAzimut, {getAzimut} from 'ngeo/interaction/MeasureAzimut';
import {listen} from 'ol/events';
import olFeature from 'ol/Feature';
import {fromCircle} from 'ol/geom/Polygon';
import olStyleStyle from 'ol/style/Style';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoMeasureazimut', [ngeoDrawController.name, ngeoMiscFilters.name]);
/**
* @param {angular.ICompileService} $compile Angular compile service.
* @param {angular.gettext.gettextCatalog} gettextCatalog Gettext catalog.
* @param {angular.IFilterService} $filter Angular filter
* @param {import('ngeo/options').ngeoMeasurePrecision} ngeoMeasurePrecision The precision.
* @param {import('ngeo/options').ngeoMeasureDecimals} ngeoMeasureDecimals The decimals.
* @returns {angular.IDirective} The directive specs.
* @ngInject
* @ngdoc directive
* @ngname ngeoDrawpoint
*/
function measureAzimutComponent(
$compile,
gettextCatalog,
$filter,
ngeoMeasurePrecision,
ngeoMeasureDecimals
) {
return {
restrict: 'A',
require: '^^ngeoDrawfeature',
/**
* @param {angular.IScope} $scope Scope.
* @param {JQuery} element Element.
* @param {angular.IAttributes} attrs Attributes.
* @param {angular.IController} [drawFeatureCtrl] Controller.
*/
link: ($scope, element, attrs, drawFeatureCtrl) => {
if (!drawFeatureCtrl) {
throw new Error('Missing drawFeatureCtrl');
}
const helpMsg = gettextCatalog.getString('Click to start drawing circle');
const contMsg = gettextCatalog.getString('Click to finish');
/** @type {import('ngeo/interaction/Measure').MeasureOptions} */
const options = {
style: new olStyleStyle(),
startMsg: $compile(`<div translate>${helpMsg}</div>`)($scope)[0],
continueMsg: $compile(`<div translate>${contMsg}</div>`)($scope)[0],
};
options.precision = ngeoMeasurePrecision;
options.decimals = ngeoMeasureDecimals;
const measureAzimut = new ngeoInteractionMeasureAzimut(
$filter('ngeoUnitPrefix'),
$filter('number'),
options
);
if (drawFeatureCtrl.uid) {
measureAzimut.set('ngeo-interaction-draw-uid', `${drawFeatureCtrl.uid}-azimut`);
}
drawFeatureCtrl.registerInteraction(measureAzimut);
drawFeatureCtrl.measureAzimut = measureAzimut;
listen(
measureAzimut,
'measureend',
/**
* @type {import('ol/events').ListenerFunction}
*/
(event) => {
const myEvent = /** @type {import('ngeo/interaction/Measure').MeasureEvent} */ (event);
// In the case of azimut measure interaction, the feature's
// geometry is actually a collection (line + circle)
// For our purpose here, we only need the circle, which gets
// transformed into a polygon with 64 sides.
const geometry =
/** @type {import('ol/geom/GeometryCollection').default} */
(myEvent.detail.feature.getGeometry());
const circle = /** @type {import('ol/geom/Circle').default} */ (geometry.getGeometries()[1]);
const polygon = fromCircle(
circle,
Number.parseInt(attrs.$$element.attr('ngeo-measureazimut-nbpoints') || 64)
);
myEvent.detail.feature = new olFeature(polygon);
const azimut = getAzimut(
/** @type {import('ol/geom/LineString').default} */ (geometry.getGeometries()[0])
);
myEvent.detail.feature.set('azimut', azimut);
drawFeatureCtrl.handleDrawEnd(ngeoGeometryType.CIRCLE, event);
},
drawFeatureCtrl
);
listen(measureAzimut, 'change:active', drawFeatureCtrl.handleActiveChange, drawFeatureCtrl);
},
};
}
myModule.directive('ngeoMeasureazimut', measureAzimutComponent);
export default myModule;