/
createRasterAdapter.ts
102 lines (95 loc) · 3 KB
/
createRasterAdapter.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
import { defined } from '@nextgis/utils';
import { ngwApiToAdapterOptions } from '../utils/ngwApiToAdapterOptions';
import { resourceIdFromLayerOptions } from '../utils/resourceIdFromLayerOptions';
import type { Type } from '@nextgis/utils';
import type { ResourceItem, ResourceCls } from '@nextgis/ngw-connector';
import type { MainLayerAdapter, ImageAdapterOptions } from '@nextgis/webmap';
import type {
ResourceAdapter,
NgwLayerAdapterType,
GetClassAdapterOptions,
} from '../interfaces';
export async function createRasterAdapter({
layerOptions,
webMap,
connector,
item,
}: GetClassAdapterOptions): Promise<Type<MainLayerAdapter> | undefined> {
const resourceCls = item.resource.cls;
const clsAdapterAlias: { [key in ResourceCls]?: NgwLayerAdapterType } = {
wmsserver_service: 'WMS',
tmsclient_layer: 'IMAGE',
};
let adapter =
layerOptions.adapter ||
(resourceCls && clsAdapterAlias[resourceCls]) ||
'IMAGE';
if (adapter !== undefined) {
layerOptions.adapter = adapter;
}
if (adapter === 'IMAGE') {
const layerAdapters = webMap.getLayerAdapters();
const isImageAllowed = layerAdapters ? layerAdapters.IMAGE : true;
if (!isImageAllowed) {
adapter = 'TILE';
}
}
const AdapterClass = webMap.mapAdapter.layerAdapters[
adapter
] as Type<MainLayerAdapter>;
if (AdapterClass) {
const resourceId = await resourceIdFromLayerOptions(
layerOptions,
connector,
);
return class RasterAdapter extends AdapterClass implements ResourceAdapter {
// options = {};
item?: ResourceItem = item;
resourceId = resourceId;
constructor(public map: any, _options: any) {
super(map, _options);
const opt = ngwApiToAdapterOptions({
options: layerOptions,
webMap,
baseUrl: connector.options.baseUrl || '',
});
if (opt) {
const layerAdapterOptions: ImageAdapterOptions = {
...opt,
...layerOptions.adapterOptions,
params: { resource: resourceId },
layers: opt.layers || String(resourceId),
resourceId: resourceId,
};
if (
layerOptions.adapterOptions &&
defined(layerOptions.adapterOptions.setViewDelay)
) {
layerAdapterOptions.setViewDelay =
layerOptions.adapterOptions.setViewDelay;
}
this.options = { ...this.options, ...layerAdapterOptions };
}
}
addLayer(addOptions: any) {
return super.addLayer({ ...this.options, ...addOptions });
}
async getIdentificationIds(): Promise<number[]> {
if (this.item) {
if (adapter === 'MVT') {
return [this.item.resource.id];
}
const id = this.item.resource.parent.id;
if (defined(id)) {
return [id];
}
}
return [];
}
// beforeRemove() {
// }
};
} else {
throw new Error(adapter + ' not supported yet. Only TILE');
}
}