/
RoutingFeatureComponent.js
449 lines (404 loc) · 12.7 KB
/
RoutingFeatureComponent.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
// The MIT License (MIT)
//
// Copyright (c) 2018-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 ngeoRoutingNominatimService from 'ngeo/routing/NominatimService.js';
import ngeoRoutingNominatimInputComponent from 'ngeo/routing/NominatimInputComponent.js';
import * as olProj from 'ol/proj.js';
import olFeature from 'ol/Feature.js';
import olCollection from 'ol/Collection.js';
import olSourceVector from 'ol/source/Vector.js';
import olLayerVector from 'ol/layer/Vector.js';
import olStyleStyle from 'ol/style/Style.js';
import olStyleText from 'ol/style/Text.js';
import olStyleFill from 'ol/style/Fill.js';
import olStyleStroke from 'ol/style/Stroke.js';
import olGeomPoint from 'ol/geom/Point.js';
import olInteractionModify from 'ol/interaction/Modify.js';
import olInteractionDraw from 'ol/interaction/Draw.js';
import 'ngeo/sass/font.scss';
/**
* @type {angular.IModule}
* @hidden
*/
const module = angular.module('ngeoRoutingFeatureComponent', [
ngeoRoutingNominatimService.name,
ngeoRoutingNominatimInputComponent.name,
]);
module.run(
/**
* @ngInject
* @param {angular.ITemplateCacheService} $templateCache
*/
($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('ngeo/routing/routingfeature', require('./routingfeature.html'));
}
);
module.value(
'ngeoRoutingFeatureTemplateUrl',
/**
* @param {angular.IAttributes} $attrs Attributes.
* @return {string} Template URL.
*/
($attrs) => {
const templateUrl = $attrs.ngeoRoutingFeatureTemplateUrl;
return templateUrl !== undefined ? templateUrl : 'ngeo/routing/routingfeature';
}
);
/**
* @param {angular.IAttributes} $attrs Attributes.
* @param {function(angular.IAttributes): string} ngeoRoutingFeatureTemplateUrl Template function.
* @return {string} Template URL.
* @ngInject
* @private
* @hidden
*/
function ngeoRoutingFeatureTemplateUrl($attrs, ngeoRoutingFeatureTemplateUrl) {
return ngeoRoutingFeatureTemplateUrl($attrs);
}
/**
* @param {angular.IScope} $scope Angular scope.
* @param {angular.ITimeoutService} $timeout Angular timeout service.
* @param {angular.IQService} $q Angular q service
* @param {import("ngeo/routing/NominatimService.js").NominatimService} ngeoNominatimService service for
* Nominatim
* @constructor
* @hidden
* @ngInject
* @ngdoc controller
* @ngname NgeoRoutingFeatureController
*/
export class Controller {
/**
* @param {angular.IScope} $scope
* @param {angular.ITimeoutService} $timeout
* @param {angular.IQService} $q
* @param {import("ngeo/routing/NominatimService.js").NominatimService} ngeoNominatimService
*/
constructor($scope, $timeout, $q, ngeoNominatimService) {
/**
* @type {angular.IScope}
* @private
*/
this.scope_ = $scope;
/**
* @type {angular.ITimeoutService}
* @private
*/
this.timeout_ = $timeout;
/**
* @type {angular.IQService}
* @private
*/
this.$q_ = $q;
/**
* @type {import("ngeo/routing/NominatimService.js").NominatimService}
* @private
*/
this.ngeoNominatimService_ = ngeoNominatimService;
/**
* @type {?import("ol/Map.js").default}
* @private
*/
this.map = null;
/**
* @type {?olFeature<import("ol/geom/Geometry.js").default>}
*/
this.feature = null;
/**
* @type {string}
*/
this.featureLabel = '';
/**
* @type {string}
*/
this.fillColor = '';
/**
* @type {string}
*/
this.strokeColor = '';
/**
* @type {?function(olFeature<import("ol/geom/Geometry.js").default>): void}
*/
this.onChange = null;
/**
* @type {import("ol/Collection.js").default<olFeature<import("ol/geom/Geometry.js").default>>}
* @private
*/
this.vectorFeatures_ = new olCollection();
/**
* @type {import("ol/source/Vector.js").default<import("ol/geom/Geometry.js").default>}
* @private
*/
this.vectorSource_ = new olSourceVector({
features: this.vectorFeatures_,
});
/**
* @type {import("ol/layer/Vector.js").default}
* @private
*/
this.vectorLayer_ = new olLayerVector({
source: this.vectorSource_,
style: (feature, resolution) => {
return [
new olStyleStyle({
text: new olStyleText({
fill: new olStyleFill({
color: this.fillColor || '#000000',
}),
font: '900 30px "Font Awesome 5 Free"',
offsetY: -15,
stroke: new olStyleStroke({
width: 3,
color: this.strokeColor || '#000000',
}),
text: '\uf041',
}),
}),
];
},
});
/**
* Interaction for moving start and end.
* @type {import("ol/interaction/Modify.js").default}
* @private
*/
this.modifyFeature_ = new olInteractionModify({
features: this.vectorFeatures_,
});
/**
* @type {?import("ol/interaction/Draw.js").default}
* @private
*/
this.draw_ = null;
/**
* @param {import('ngeo/routing/NominatimService').NominatimSearchResult} selected Selected result.
*/
this.onSelect = this.onSelect_.bind(this);
/**
* @type {string}
*/
this.errorMessage = '';
}
$onInit() {
if (!this.map) {
return;
}
this.map.addLayer(this.vectorLayer_);
// setup modify interaction
this.modifyFeature_.setActive(true);
this.map.addInteraction(this.modifyFeature_);
this.modifyFeature_.on('modifyend', (event) => {
const feature = event.features.getArray()[0];
this.vectorSource_.clear();
this.snapFeature_(feature);
});
this.scope_.$watch(
() => this.feature,
(newVal, oldVal) => {
if (newVal) {
this.onFeatureChange_();
}
if (newVal === null) {
this.vectorSource_.clear();
this.featureLabel = '';
}
}
);
}
/**
* Cleanup, mostly relevant for vias.
*/
$onDestroy() {
if (!this.map) {
return;
}
this.map.removeLayer(this.vectorLayer_);
this.modifyFeature_.setActive(false);
this.map.removeInteraction(this.modifyFeature_);
}
/**
*/
set() {
if (!this.map) {
return;
}
if (this.draw_) {
this.map.removeInteraction(this.draw_);
}
this.draw_ = new olInteractionDraw({
features: this.vectorFeatures_,
type: 'Point',
});
this.draw_.on('drawstart', () => {
if (this.feature) {
this.vectorSource_.removeFeature(this.feature);
}
});
this.draw_.on('drawend', (event) => {
if (this.draw_ && this.map) {
this.map.removeInteraction(this.draw_);
}
this.snapFeature_(event.feature);
this.modifyFeature_.setActive(true);
});
this.modifyFeature_.setActive(false);
this.map.addInteraction(this.draw_);
}
/**
* @param {import("ol/coordinate.js").Coordinate} coordinate LonLat coordinate.
* @param {string} label Feature name/label.
* @private
*/
setFeature_(coordinate, label) {
if (!this.map) {
return;
}
const transformedCoordinate = olProj.fromLonLat(coordinate, this.map.getView().getProjection());
if (label === '') {
label = transformedCoordinate.join('/');
}
this.feature = new olFeature({
geometry: new olGeomPoint(transformedCoordinate),
name: label,
});
}
onFeatureChange_() {
if (!this.feature) {
return;
}
// Update label
this.featureLabel = /** @type {string} */ (this.feature.get('name') || '');
// Update vector source
this.vectorSource_.clear();
this.vectorSource_.addFeature(this.feature);
// Notify others
if (this.onChange) {
this.timeout_(() => {
if (this.feature && this.onChange) {
this.onChange(this.feature);
}
});
}
}
/**
* @param {import('ngeo/routing/NominatimService').NominatimSearchResult} selected Selected result.
* @private
*/
onSelect_(selected) {
const coordinate = selected.coordinate.map(parseFloat);
const label = selected.label;
this.setFeature_(coordinate, label);
const newCoordinates = /** @type {import("ol/geom/Point.js").default} */ (this.feature.getGeometry()).getCoordinates();
this.map.getView().setCenter(newCoordinates);
}
/**
* Snaps a feature to the street network using the getNearest
* function of the routing service. Replaces the feature.
* @param {olFeature<import("ol/geom/Geometry.js").default>} feature Feature to snap
* @private
*/
snapFeature_(feature) {
const coord = this.getLonLatFromPoint_(feature);
if (!coord) {
return;
}
const config = {};
/**
* @param {angular.IHttpResponse<import('./NominatimService').NominatimSearchResponseResult>} resp
*/
const onSuccess = (resp) => {
const lon = parseFloat(resp.data.lon);
const lat = parseFloat(resp.data.lat);
const coordinate = [lon, lat];
const label = resp.data.display_name;
this.setFeature_(coordinate, label);
};
/**
* @param {angular.IHttpResponse<import('./NominatimService').NominatimSearchResponseResult>} resp
*/
const onError = (resp) => {
this.errorMessage = 'Error: nominatim server not responding.';
console.log(resp);
};
this.$q_.when(this.ngeoNominatimService_.reverse(coord, config)).then(onSuccess, onError);
}
/**
* Converts feature point into LonLat coordinate.
* @param {olFeature<import("ol/geom/Geometry.js").default>} point Feature point to convert
* @return {?import("ol/coordinate.js").Coordinate} LonLat coordinate
* @private
*/
getLonLatFromPoint_(point) {
if (!this.map) {
return null;
}
const geometry = point.getGeometry();
if (!(geometry instanceof olGeomPoint)) {
throw new Error('Wrong time values type');
}
const coords = geometry.getCoordinates();
const projection = this.map.getView().getProjection();
return olProj.toLonLat(coords, projection);
}
}
/**
* Provides a text input and draw interaction to allow a user to create and modify a ol.Feature
* (point geometry).
*
* The text input is provided by {@link import("ngeo/nominatimInputComponent.js").default} and includes
* Nominatim search.
*
* Example:
*
* <ngeo-routing-feature
* ngeo-routing-feature-map="ctrl.map"
* ngeo-routing-feature-feature="ctrl.feature"
* ngeo-routing-feature-fill-color="#6BE62E"
* ngeo-routing-feature-stroke-color="#4CB01E"
* ngeo-routing-feature-on-change="ctrl.handleChange">
*
* Is used in in the partial of {@link import("ngeo/routingComponent.js").default}.
*
* See the [../examples/routing.html](../examples/routing.html) example for a usage sample.
*
* @htmlAttribute {import("ol/Map.js").default} ngeo-routing-feature-map The map.
* @htmlAttribute {olFeature<import("ol/geom/Geometry.js").default>} ngeo-routing-feature-feature The feature.
* @htmlAttribute {string} ngeo-routing-feature-fill-color The marker fill color.
* @htmlAttribute {string} ngeo-routing-feature-stroke-color The marker stroke color.
* @htmlAttribute {function(olFeature<import("ol/geom/Geometry.js").default>)} ngeo-routing-feature-on-change Event fired when
* feature changes.
* @ngdoc directive
* @ngname ngeoRoutingFeature
*/
const routingFeatureComponent = {
controller: Controller,
bindings: {
'map': '<ngeoRoutingFeatureMap',
'feature': '=ngeoRoutingFeatureFeature',
'fillColor': '<?ngeoRoutingFeatureFillColor',
'strokeColor': '<?ngeoRoutingFeatureStrokeColor',
'onChange': '=?ngeoRoutingFeatureOnChange',
},
templateUrl: ngeoRoutingFeatureTemplateUrl,
};
module.component('ngeoRoutingFeature', routingFeatureComponent);
export default module;