/
MapLayerSources.ts
292 lines (249 loc) · 11.1 KB
/
MapLayerSources.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module MapLayers
*/
import { compareStrings } from "@itwin/core-bentley";
import {
BackgroundMapProvider, BackgroundMapType, BaseMapLayerSettings, DeprecatedBackgroundMapProps, ImageMapLayerSettings, MapSubLayerProps,
} from "@itwin/core-common";
import { Point2d } from "@itwin/core-geometry";
import { IModelApp } from "../../IModelApp";
import { IModelConnection } from "../../IModelConnection";
import { request, RequestBasicCredentials } from "../../request/Request";
import { ArcGisUtilities, MapCartoRectangle, MapLayerSourceValidation } from "../internal";
/** Values for return codes from [[MapLayerSource.validateSource]]
* @public
*/
export enum MapLayerSourceStatus {
/** Layer source is valid */
Valid,
/** Authorization has failed when accessing this layer source. */
InvalidCredentials,
/** Provided format id could not be resolved in [[MapLayerFormatRegistry]] */
InvalidFormat,
/** The tiling schema of the source is not supported */
InvalidTileTree,
/** Could not not connect to remote server using the provided URL.*/
InvalidUrl,
/** Authorization is required to access this map layer source. */
RequireAuth,
/** Map-layer coordinate system is not supported */
InvalidCoordinateSystem,
/** Format is not compatible with the URL provided.
*/
IncompatibleFormat,
}
/** JSON representation of a map layer source.
* @see [ImageryMapLayerFormatId]($common)
* @public
*/
export interface MapLayerSourceProps {
/** Identifies the map layers source. Defaults to 'WMS'. */
formatId?: string;
/** Name */
name: string;
/** URL of the source's endpoint. */
url: string;
/** True to indicate background is transparent. Defaults to 'true'. */
transparentBackground?: boolean;
/** Indicate if this source definition should be used as a base map. Defaults to false. */
baseMap?: boolean;
/** List of query parameters that will get appended to the source.
* @beta
*/
queryParams?: { [key: string]: string };
}
/** A source for map layers. These may be catalogued for convenient use by users or applications.
* @public
*/
export class MapLayerSource {
public formatId: string;
public name: string;
public url: string;
public baseMap = false;
public transparentBackground?: boolean;
public userName?: string;
public password?: string;
/** List of query parameters that will get appended to the source URL that should be be persisted part of the JSON representation.
* @beta
*/
public savedQueryParams?: { [key: string]: string };
/** List of query parameters that will get appended to the source URL that should *not* be be persisted part of the JSON representation.
* @beta
*/
public unsavedQueryParams?: { [key: string]: string };
private constructor(formatId = "WMS", name: string, url: string, baseMap = false, transparentBackground = true, savedQueryParams?: { [key: string]: string}) {
this.formatId = formatId;
this.name = name;
this.url = url;
this.baseMap = baseMap;
this.transparentBackground = transparentBackground;
this.savedQueryParams = savedQueryParams;
}
public static fromJSON(json: MapLayerSourceProps): MapLayerSource | undefined {
if (json === undefined)
return undefined;
return new MapLayerSource(json.formatId, json.name, json.url, json.baseMap, json.transparentBackground, json.queryParams);
}
public async validateSource(ignoreCache?: boolean): Promise<MapLayerSourceValidation> {
return IModelApp.mapLayerFormatRegistry.validateSource({source: this, ignoreCache});
}
/** @internal*/
public static fromBackgroundMapProps(props: DeprecatedBackgroundMapProps) {
const provider = BackgroundMapProvider.fromBackgroundMapProps(props);
const layerSettings = BaseMapLayerSettings.fromProvider(provider);
if (undefined !== layerSettings) {
const source = MapLayerSource.fromJSON(layerSettings);
if (source) {
source.baseMap = true;
return source;
}
}
return undefined;
}
public toJSON(): Omit<MapLayerSourceProps, "formatId"> & {formatId: string} {
return { url: this.url, name: this.name, formatId: this.formatId, transparentBackground: this.transparentBackground, queryParams: this.savedQueryParams };
}
public toLayerSettings(subLayers?: MapSubLayerProps[]): ImageMapLayerSettings | undefined {
// When MapLayerSetting is created from a MapLayerSource, sub-layers and credentials need to be set separately.
const layerSettings = ImageMapLayerSettings.fromJSON({ ...this, subLayers });
if (this.userName !== undefined || this.password !== undefined) {
layerSettings?.setCredentials(this.userName, this.password);
}
if (this.savedQueryParams) {
layerSettings.savedQueryParams = {...this.savedQueryParams};
}
if (this.unsavedQueryParams) {
layerSettings.unsavedQueryParams = {...this.unsavedQueryParams};
}
return layerSettings;
}
private getCredentials(): RequestBasicCredentials | undefined {
return this.userName && this.password ? { user: this.userName, password: this.password } : undefined;
}
/** Collect all query parameters
* @beta
*/
public collectQueryParams() {
let queryParams: {[key: string]: string} = {};
if (this.savedQueryParams)
queryParams = {...this.savedQueryParams};
if (this.unsavedQueryParams)
queryParams = {...queryParams, ...this.unsavedQueryParams};
return queryParams;
}
}
/** A collection of [[MapLayerSource]] objects.
* @beta
*/
export class MapLayerSources {
private static _instance?: MapLayerSources;
private constructor(private _sources: MapLayerSource[]) { }
public static getInstance() { return MapLayerSources._instance; }
public findByName(name: string, baseMap: boolean = false): MapLayerSource | undefined {
const nameTest = name.toLowerCase();
for (const source of this._sources)
if (source.baseMap === baseMap && source.name.toLowerCase().indexOf(nameTest) !== -1)
return source;
return undefined;
}
public get layers(): MapLayerSource[] {
const layers = new Array<MapLayerSource>();
this._sources.forEach((source) => {
if (!source.baseMap)
layers.push(source);
});
return layers;
}
public get allSource() { return this._sources; }
public get bases(): MapLayerSource[] {
const layers = new Array<MapLayerSource>();
this._sources.forEach((source) => {
if (source.baseMap)
layers.push(source);
});
return layers;
}
private static getBingMapLayerSource(): MapLayerSource[] {
const mapLayerSources: MapLayerSource[] = [];
mapLayerSources.push(MapLayerSource.fromBackgroundMapProps({ providerName: "BingProvider", providerData: { mapType: BackgroundMapType.Street } })!);
mapLayerSources.push(MapLayerSource.fromBackgroundMapProps({ providerName: "BingProvider", providerData: { mapType: BackgroundMapType.Aerial } })!);
mapLayerSources.push(MapLayerSource.fromBackgroundMapProps({ providerName: "BingProvider", providerData: { mapType: BackgroundMapType.Hybrid } })!);
return mapLayerSources;
}
private static getMapBoxLayerSource(): MapLayerSource[] {
const mapLayerSources: MapLayerSource[] = [];
mapLayerSources.push(MapLayerSource.fromBackgroundMapProps({ providerName: "MapBoxProvider", providerData: { mapType: BackgroundMapType.Street } })!);
mapLayerSources.push(MapLayerSource.fromBackgroundMapProps({ providerName: "MapBoxProvider", providerData: { mapType: BackgroundMapType.Aerial } })!);
mapLayerSources.push(MapLayerSource.fromBackgroundMapProps({ providerName: "MapBoxProvider", providerData: { mapType: BackgroundMapType.Hybrid } })!);
return mapLayerSources;
}
public static async create(iModel?: IModelConnection, queryForPublicSources = false, addMapBoxSources = false): Promise<MapLayerSources> {
if (!queryForPublicSources && MapLayerSources._instance)
return MapLayerSources._instance;
if (!iModel)
iModel = IModelApp.viewManager.selectedView ? IModelApp.viewManager.selectedView.iModel : undefined;
let sourceRange = MapCartoRectangle.createMaximum();
if (iModel) {
const projectCenter = iModel.projectExtents.localXYZToWorld(.5, .5, .5)!;
const cartoCenter = iModel.spatialToCartographicFromEcef(projectCenter);
const globeRange = MapCartoRectangle.createMaximum();
const nearDelta = Point2d.create(globeRange.xLength() / 100, globeRange.yLength() / 100);
sourceRange = MapCartoRectangle.fromRadians(cartoCenter.longitude - nearDelta.x, cartoCenter.latitude - nearDelta.y, cartoCenter.longitude + nearDelta.x, cartoCenter.latitude + nearDelta.y);
}
const sources = new Array<MapLayerSource>();
const urlSet = new Set<string>();
const addSource = ((source: MapLayerSource) => {
if (!urlSet.has(source.url)) {
sources.push(source);
urlSet.add(source.url);
}
});
this.getBingMapLayerSource().forEach((source) => {
addSource(source);
});
if (addMapBoxSources) {
this.getMapBoxLayerSource().forEach((source) => {
addSource(source);
});
}
if (queryForPublicSources) {
const sourcesJson = await request(`${IModelApp.publicPath}assets/MapLayerSources.json`, "json");
for (const sourceJson of sourcesJson) {
const source = MapLayerSource.fromJSON(sourceJson);
if (source)
addSource(source);
}
(await ArcGisUtilities.getSourcesFromQuery(sourceRange)).forEach((queriedSource) => addSource(queriedSource));
}
sources.sort((a: MapLayerSource, b: MapLayerSource) => compareStrings(a.name.toLowerCase(), b.name.toLowerCase()));
const mapLayerSources = new MapLayerSources(sources);
MapLayerSources._instance = mapLayerSources;
return mapLayerSources;
}
public static async addSourceToMapLayerSources(mapLayerSource?: MapLayerSource): Promise<MapLayerSources | undefined> {
if (!MapLayerSources._instance || !mapLayerSource) {
return undefined;
}
MapLayerSources._instance._sources = MapLayerSources._instance._sources.filter((source) => {
return !(source.name === mapLayerSource.name || source.url === mapLayerSource.url);
});
MapLayerSources._instance._sources.push(mapLayerSource);
MapLayerSources._instance._sources.sort((a: MapLayerSource, b: MapLayerSource) => compareStrings(a.name.toLowerCase(), b.name.toLowerCase()));
return MapLayerSources._instance;
}
public static removeLayerByName(name: string): boolean {
if (!MapLayerSources._instance) {
return false;
}
// For now we only rely on the name
const lengthBeforeRemove = MapLayerSources._instance._sources.length;
MapLayerSources._instance._sources = MapLayerSources._instance._sources.filter((source) => {
return (source.name !== name);
});
return (lengthBeforeRemove !== MapLayerSources._instance._sources.length);
}
}