/
MapLayerTool.ts
569 lines (490 loc) · 21.2 KB
/
MapLayerTool.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tools
*/
import { Id64String } from "@itwin/core-bentley";
import { BaseMapLayerSettings, ColorDef, ModelMapLayerSettings } from "@itwin/core-common";
import { IModelApp, MapLayerSource, MapLayerSources, MapLayerSourceStatus, NotifyMessageDetails, OutputMessagePriority, Tool, WmsUtilities } from "@itwin/core-frontend";
import { parseBoolean } from "./parseBoolean";
import { parseToggle } from "./parseToggle";
/** Base class for attaching map layer tool. */
class AttachMapLayerBaseTool extends Tool {
constructor(protected _isBackground = true, protected _isBase = false) {
super();
}
protected doAttach(source?: MapLayerSource) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined || source === undefined)
return;
source.validateSource().then((validation) => {
if (validation.status === MapLayerSourceStatus.Valid || validation.status === MapLayerSourceStatus.RequireAuth) {
if (this._isBase) {
vp.displayStyle.backgroundMapBase = BaseMapLayerSettings.fromJSON({ ...source, subLayers: validation.subLayers });
vp.invalidateRenderPlan();
} else {
const settings = source.toLayerSettings(validation.subLayers);
if (settings) {
// Need to specify index in mapLayerIndex, so just use -1 to attach layer at the bottom
// Previously, this was done within attachMapLayer if index was undefined.
vp.displayStyle.attachMapLayer({ settings, mapLayerIndex: { isOverlay: !this._isBackground, index: -1 } });
}
}
if (validation.status === MapLayerSourceStatus.Valid) {
vp.invalidateRenderPlan();
const msg = IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerAttached", { sourceName: source.name });
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, msg));
} else if (validation.status === MapLayerSourceStatus.RequireAuth) {
const msg = IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerAttachedRequiresAuth", { sourceName: source.name });
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Warning, msg));
}
} else if (validation.status === MapLayerSourceStatus.IncompatibleFormat) {
const msg = IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayersIncompatibleFormat", { sourceName: source.name });
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, msg));
} else {
const msg = IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerValidationFailed", { sourceName: source.name });
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, msg));
}
}).catch((error) => {
const msg = IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerAttachError", { error, sourceName: source.name });
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, msg));
});
}
}
/** Attach a map layer from URL base class. A layer is attached for each unique model in the selection
* @beta
*/
export class AttachModelMapLayerTool extends Tool {
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
public static override toolId = "AttachModelMapLayerTool";
constructor(protected _formatId: string) { super(); }
public override async run(nameIn?: string): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (!vp)
return false;
const iModel = vp.iModel;
const elements = await iModel.elements.getProps(iModel.selectionSet.elements);
const modelIds = new Set<Id64String>();
for (const element of elements)
modelIds.add(element.model);
for (const modelId of modelIds) {
const modelProps = await iModel.models.getProps(modelId);
const modelName = modelProps[0].name ? modelProps[0].name : modelId;
const name = nameIn ? (modelIds.size > 1 ? `${nameIn}: ${modelName}` : nameIn) : modelName;
const settings = ModelMapLayerSettings.fromJSON({ name, modelId });
vp.displayStyle.attachMapLayer({ settings, mapLayerIndex: { isOverlay: false, index: -1 } });
}
return true;
}
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(args[0]);
}
public async onRestartTool() {
}
}
/** Attach a map layer from URL base class. */
class AttachMapLayerByURLBaseTool extends AttachMapLayerBaseTool {
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 4; }
constructor(protected _formatId: string) { super(); }
public override async run(url: string, name?: string, userName?: string, password?: string): Promise<boolean> {
const source = MapLayerSource.fromJSON({ url, name: (name ? name : url), formatId: this._formatId });
if (source) {
source.userName = userName;
source.password = password;
}
this.doAttach(source);
return true;
}
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(args[0], args[1], args[2], args[3]);
}
}
/** This tool attaches a WMS map layer from a given URL.
* @beta
*/
export class AttachWmsMapLayerByUrlTool extends AttachMapLayerByURLBaseTool {
public static override toolId = "AttachWmsMapLayerTool";
constructor() { super("WMS"); }
/** This method runs the tool, attaching a WMS map layer from a given URL.
* @param args contains url, name, userName, password in array order
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(WmsUtilities.getBaseUrl(args[0]), args[1], args[2], args[3]);
}
}
/** This tool attaches a WMTS map layer from a given URL.
* @beta
*/
export class AttachWmtsMapLayerByUrlTool extends AttachMapLayerByURLBaseTool {
public static override toolId = "AttachWmtsMapLayerTool";
constructor() { super("WMTS"); }
/** This method runs the tool, attaching a WMTS map layer from a given URL.
* @param args contains url, name, userName, password in array order
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(WmsUtilities.getBaseUrl(args[0]), args[1], args[2], args[3]);
}
}
/** This tool attaches an ArcGIS map layer from a given URL.
* @beta
*/
export class AttachArcGISMapLayerByUrlTool extends AttachMapLayerByURLBaseTool {
public static override toolId = "AttachArcGISMapLayerTool";
constructor() { super("ArcGIS"); }
}
/** This tool attaches an ArcGIS map layer from a given URL.
* @beta
*/
export class AttachArcGISFeatureMapLayerByUrlTool extends AttachMapLayerByURLBaseTool {
public static override toolId = "AttachArcGISFeatureMapLayerTool";
constructor() { super("ArcGISFeature"); }
}
/** This tool attaches an ArcGIS map layer from a given URL.
* @beta
*/
export class AttachOgcApiFeaturesMapLayerTool extends AttachMapLayerByURLBaseTool {
public static override toolId = "AttachOgcApiFeaturesMapLayerTool";
constructor() { super("OgcApiFeatures"); }
}
/** This tool attaches a map layer from a given tile URL.
* @beta
*/
export class AttachTileURLMapLayerByUrlTool extends AttachMapLayerByURLBaseTool {
public static override toolId = "AttachTileURLMapLayerTool";
constructor() { super("TileURL"); }
}
/** This tool add a Map Layer from a specified name (look up in MapLayerSources.json).
* @beta
*/
export class AttachMapLayerTool extends AttachMapLayerBaseTool {
public static override toolId = "AttachMapLayerTool";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, adding a map layer from a specified name in MayLayerSources.json.
* @param name the name of the map layer to add
*/
public override async run(name: string): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
MapLayerSources.create().then((sources) => {
if (sources !== undefined) {
const source = sources.findByName(name, this._isBase);
if (source !== undefined)
this.doAttach(source);
}
}).catch((_err) => { });
return true;
}
/** Executes this tool's run method with args[0] containing `name`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(args[0]);
}
}
/** This tool attaches a Overlay map layer.
* @beta
*/
export class AttachMapOverlayTool extends AttachMapLayerTool {
public static override toolId = "AttachMapOverlayTool";
constructor() {
super();
this._isBackground = false;
}
}
/** Sets map layer base tool.
* @beta
*/
export class SetMapBaseTool extends AttachMapLayerTool {
public static override toolId = "SetMapBaseTool";
constructor() {
super();
this._isBase = true;
}
}
/** Detach Map Layers Tool.
* @beta
*/
export class DetachMapLayersTool extends Tool {
public static override toolId = "DetachMapLayersTool";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 0; }
public override async parseAndRun(..._args: string[]): Promise<boolean> {
return this.run();
}
public override async run(): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
vp.displayStyle.detachMapLayerByIndex({ index: -1, isOverlay: true });
vp.displayStyle.detachMapLayerByIndex({ index: -1, isOverlay: false });
vp.invalidateRenderPlan();
return true;
}
}
function parseLayerIndex(args: string[]) {
const layerIndex = args.length > 1 ? parseInt(args[1], 10) : 0;
return isNaN(layerIndex) ? 0 : layerIndex;
}
/** This tool sets the visibility of the map layer.
* @beta
*/
export class MapLayerVisibilityTool extends Tool {
public static override toolId = "SetMapLayerVisibility";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 2; }
/** This method runs the tool, setting the visibility of a map layer.
* @param layerIndex the index of the layer to change
* @param visible a boolean that should be true if the layer should be visible
*/
public override async run(layerIndex: number, enable?: boolean): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
const mapLayer = vp.displayStyle.mapLayerAtIndex({ index: layerIndex, isOverlay: false });
if (undefined === mapLayer)
return false;
const visible = (enable === undefined) ? !mapLayer.visible : enable;
vp.displayStyle.changeMapLayerProps({ visible }, { index: layerIndex, isOverlay: false });
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `enable` and args[1] containing `layerIndex`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const enable = parseToggle(args[0]);
const layerIndex = parseLayerIndex(args);
if (typeof enable !== "string")
await this.run(layerIndex, enable);
return true;
}
}
/** This tool reorders map layers.
* @beta
*/
export class ReorderMapLayers extends Tool {
public static override toolId = "ReorderMapLayers";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 2; }
/** This method runs the tool, reordering the map layers.
* @param from a numeric value specifying the layer index that is being moved
* @param from a numeric value specifying the layer index to move that layer to
*/
public override async run(from: number, to: number): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
vp.displayStyle.moveMapLayerToIndex(isNaN(from) ? 0 : from, isNaN(to) ? vp.displayStyle.settings.mapImagery.backgroundLayers.length : to, false);
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `from` and args[1] containing `to`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const from = parseInt(args[0], 10);
const to = parseInt(args[1], 10);
await this.run(from, to);
return true;
}
}
/** This tool sets the transparency of a map layer.
* @beta
*/
export class MapLayerTransparencyTool extends Tool {
public static override toolId = "SetMapLayerTransparency";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 2; }
/** This method runs the tool, setting the transparency of a map layer.
* @param layerIndex the index of the layer to change
* @param transparency a numeric value in the range 0.0 (fully opaque) to 1.0 (fully transparent)
*/
public override async run(layerIndex: number, transparency: number): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
vp.displayStyle.changeMapLayerProps({ transparency }, { index: layerIndex, isOverlay: false });
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `transparency` and args[1] containing `layerIndex`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const transparency = parseFloat(args[0]);
const layerIndex = parseLayerIndex(args);
if (transparency >= 0 && transparency <= 1)
await this.run(layerIndex, transparency);
return true;
}
}
/** This tool sets the visibility of the map sublayer.
* @beta
*/
export class MapLayerSubLayerVisibilityTool extends Tool {
public static override toolId = "SetMapSubLayerVisibility";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 2; }
/** This method runs the tool, setting the visibility of a map sublayer.
* @param layerIndex the index of the layer to change
* @param visible a boolean that should be true if the sublayer should be visible
*/
public override async run(layerIndex: number, visible: boolean): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
vp.displayStyle.changeMapSubLayerProps({ visible }, -1, { index: layerIndex, isOverlay: false });
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `transparency` and args[1] containing `layerIndex`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const on = args[0] !== "off";
const layerIndex = parseLayerIndex(args);
await this.run(layerIndex, on);
return true;
}
}
/** This tool changes the viewport so it is zoomed to the range of a map layer.
* @beta
*/
export class MapLayerZoomTool extends Tool {
public static override toolId = "MapLayerZoom";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, changing the viewport so it is zoomed to the range of a map layer.
* @param layerIndex the index of the layer whose range to zoom to
*/
public override async run(layerIndex: number): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
vp.viewMapLayerRange({ index: layerIndex, isOverlay: false }, vp).then(() => { }).catch(() => { });
return true;
}
/** Executes this tool's run method with args[0] containing `layerIndex`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const layerIndex = parseLayerIndex(args);
await this.run(layerIndex);
return true;
}
}
/** This tool toggles whether to apply terrain heights to the map.
* @beta
*/
export class ToggleTerrainTool extends Tool {
public static override toolId = "ToggleTerrain";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, changing whether to apply terrain heights to the map.
* @param enable whether or not to enable terrain heights on the map
*/
public override async run(enable?: boolean): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
const applyTerrain = (enable === undefined) ? !vp.displayStyle.backgroundMapSettings.applyTerrain : enable;
vp.displayStyle.changeBackgroundMapProps({ applyTerrain });
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `enable`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const enable = parseToggle(args[0]);
if (typeof enable !== "string")
await this.run(enable);
return true;
}
}
/** This tool changes the color of the base map.
* @beta
*/
export class MapBaseColorTool extends Tool {
public static override toolId = "SetMapBaseColorTool";
public static override get minArgs() { return 3; }
public static override get maxArgs() { return 3; }
/** This method runs the tool, changing the color of the base map.
* @param color the color for the base map
*/
public override async run(color: ColorDef) {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
const curTransparency = vp.displayStyle.backgroundMapBase instanceof ColorDef ? vp.displayStyle.backgroundMapBase.getTransparency() : 0;
vp.displayStyle.backgroundMapBase = color.withTransparency(curTransparency);
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing a 0 to 255 red component, args[1] containing a 0 to 255 green component, and args[2] containing a 0 to 255 blue component.
* These rgb values will be used to construct the `color` parameter passed to this tool's run method.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const red = parseFloat(args[0]), green = parseFloat(args[1]), blue = parseFloat(args[2]);
return (isNaN(red) || red < 0 || red > 255 || isNaN(green) || green < 0 || green > 255 || isNaN(blue) || blue < 0 || blue > 255) ? false : this.run(ColorDef.from(red, green, blue));
}
}
/** This tool changes the transparency of the base map.
* @beta
*/
export class MapBaseTransparencyTool extends Tool {
public static override toolId = "SetMapBaseTransparencyTool";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, changing the transparency of the base map.
* @param transparency a numeric value in range 0.0 to 1.0 whether 0.0 means fully opaque and 1.0 means fully transparent
*/
public override async run(transparency: number) {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView())
return false;
vp.displayStyle.changeBaseMapTransparency(transparency);
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `transparency`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const transparency = parseFloat(args[0]);
return (isNaN(transparency) || transparency < 0 || transparency > 1) ? false : this.run(transparency);
}
}
/** This tool changes the visibility of the base map.
* @beta
*/
export class MapBaseVisibilityTool extends Tool {
public static override toolId = "SetMapBaseVisibilityTool";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, changing the visibility of the base map.
* @param visible a boolean which specifies whether or not to make the base map visible
*/
public override async run(visible: boolean) {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp || !vp.view.isSpatialView() || vp.displayStyle.backgroundMapBase instanceof ColorDef)
return false;
vp.displayStyle.backgroundMapBase = vp.displayStyle.backgroundMapBase.clone({ visible });
vp.invalidateRenderPlan();
return true;
}
/** Executes this tool's run method with args[0] containing `visible`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const visible = parseBoolean(args[0]);
return (visible !== undefined ? this.run(visible) : false);
}
}