-
Notifications
You must be signed in to change notification settings - Fork 405
/
MapInfoUtils.js
445 lines (422 loc) · 15.7 KB
/
MapInfoUtils.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
/**
* Copyright 2015-2016, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import { INFO_FORMATS, INFO_FORMATS_BY_MIME_TYPE, JSON_MIME_TYPE, GEOJSON_MIME_TYPE, validator } from './FeatureInfoUtils';
import pointOnSurface from 'turf-point-on-surface';
import { findIndex } from 'lodash';
import iconUrl from '../components/map/openlayers/img/marker-icon.png';
import JSONViewer from '../components/data/identify/viewers/JSONViewer';
import HTMLViewer from '../components/data/identify/viewers/HTMLViewer';
import TextViewer from '../components/data/identify/viewers/TextViewer';
import wfs from './mapinfo/wfs';
import wms from './mapinfo/wms';
import wmts from './mapinfo/wmts';
import vector from './mapinfo/vector';
import threeDTiles from './mapinfo/threeDTiles';
import model from './mapinfo/model';
import arcgis from './mapinfo/arcgis';
let MapInfoUtils;
/**
* Map of info modes which are used to display feature info data (identify tools).
* To be distinguished with INFO_FORMATS which is the map of mime types used in client server communication.
* These are strictly a representation of the various ways that info data is visualized.
* Has an N <=> N relationship with INFO_FORMATS.
*/
const INFO_VIEW_MODES = {
TEXT: "TEXT",
PROPERTIES: "PROPERTIES",
HTML: "HTML",
TEMPLATE: "TEMPLATE"
};
/**
* @returns {object} Map of views which are used to display feature info data (identify tools).
*/
export const getInfoViewModes = () => {
return {...INFO_VIEW_MODES};
};
/**
* Given an `infoFormat` mime-type passed, it returns the default view mode (e.g. `PROPERTIES`, `HTML`, `TEXT`) for the format selected.
*
* @param {string} infoFormat the info format mime type.
* @returns {string} the info view mode that is used for that info format.
*/
export const getDefaultInfoViewMode = (infoFormat) => {
let infoView;
switch (infoFormat) {
case INFO_FORMATS.TEXT:
infoView = INFO_VIEW_MODES.TEXT;
break;
case INFO_FORMATS.HTML:
infoView = INFO_VIEW_MODES.HTML;
break;
case INFO_FORMATS.JSON:
infoView = INFO_VIEW_MODES.PROPERTIES;
break;
case INFO_FORMATS.GEOJSON:
infoView = INFO_VIEW_MODES.PROPERTIES;
break;
default:
// TODO: re-assess leaving default null value, this way tests work but caller is burdened with fallback.
infoView;
}
return infoView;
};
/**
* Given a infoViewMode (e.g. "HTML", "PROPERTIES", "TEMPLATE"), returns the mime-type to use for the request for the given layer.
*
* @param {string} infoView the info view mode.
* @param {array} layerInfoFormatCfg the layer supported GFI mime types.
* @returns {string} the info format mime type.
*/
export const getInfoFormatByInfoView = (infoView, layerInfoFormatCfg) => {
let infoFormat;
switch (infoView) {
case INFO_VIEW_MODES.TEXT:
infoFormat = INFO_FORMATS.TEXT;
break;
case INFO_VIEW_MODES.HTML:
infoFormat = INFO_FORMATS.HTML;
break;
case INFO_VIEW_MODES.PROPERTIES:
case INFO_VIEW_MODES.TEMPLATE:
infoFormat = layerInfoFormatCfg?.includes(GEOJSON_MIME_TYPE) ? INFO_FORMATS.GEOJSON : INFO_FORMATS.JSON;
break;
default:
// TODO: re-assess leaving default null value, this way tests work but caller is burdened with fallback.
infoFormat;
}
return infoFormat;
};
/**
* specifies which info formats are currently supported
*/
// default format ↴
export const SUPPORTED_FORMATS = ['TEXT', 'HTML', 'JSON', 'GEOJSON'];
export const EMPTY_RESOURCE_VALUE = 'NODATA';
/**
* @return a filtered version of INFO_FORMATS object.
* the returned object contains only keys that SUPPORTED_FORMATS contains.
*/
export const getAvailableInfoFormat = () => {
return Object.keys(INFO_FORMATS).filter((k) => {
return MapInfoUtils.SUPPORTED_FORMATS.indexOf(k) !== -1;
}).reduce((prev, k) => {
prev[k] = INFO_FORMATS[k];
return prev;
}, {});
};
/**
* @return the label of an inputformat given the value
*/
export const getLabelFromValue = (value) => {
return MapInfoUtils.getAvailableInfoFormatLabels().filter(info => INFO_FORMATS[info] === value)[0] || "TEXT";
};
/**
* @return like getAvailableInfoFormat but return an array filled with the keys
*/
export const getAvailableInfoFormatLabels = () => {
return Object.keys(MapInfoUtils.getAvailableInfoFormat());
};
/**
* @return like getAvailableInfoFormat but return an array filled with the values
*/
export const getAvailableInfoFormatValues = () => {
return Object.keys(MapInfoUtils.getAvailableInfoFormat()).map( label => {
return INFO_FORMATS[label];
});
};
/**
* @return {string} the default info format value
*/
export const getDefaultInfoFormatValue = () => {
return INFO_FORMATS[MapInfoUtils.SUPPORTED_FORMATS[0]];
};
/**
* @param {object} param object map of params for a getFeatureInfo request.
* @return {boolean} Check if param.info_format of param.outputFormat is set as json / geojson mime type.
*/
export const isDataFormat = (param) => {
return param?.info_format === JSON_MIME_TYPE
|| param?.outputFormat === JSON_MIME_TYPE
|| param?.info_format === GEOJSON_MIME_TYPE
|| param?.outputFormat === GEOJSON_MIME_TYPE;
};
/**
* returns feature info options of layer
* @param layer {object} layer object
* @return {object} feature info options
*/
export const getLayerFeatureInfo = (layer) => {
return layer && layer.featureInfo && {...layer.featureInfo} || {};
};
/**
* Extracts the proper mime time to use for the layer, given the passed props that determine the preferred type. This
* helps to convert, for instance, the mime-type set as default for the map (e.g. `application/json`) into the effective
* mime type requested by the server (e.g. `application/geo+json`)
* @param {object} layer the layer
* @param props.format the preferred format, corresponding to the global settings information sheet field. it can be a mime type like `application/json`.
* @return {string} the info format value from layer, otherwise the info format in settings
*/
export const getDefaultInfoFormatValueFromLayer = (layer, props) => {
const featInfoFormat = getLayerFeatureInfo(layer)?.format;
if (featInfoFormat) {
// When the user explicitly configures the format from the layer settings => feature info page, return directly from definition map.
// Check if featInfoFormat is an actual view, otherwise retrieve infoFormat directly.
return Object.values(getInfoViewModes()).includes(featInfoFormat)
? getInfoFormatByInfoView(featInfoFormat, layer.infoFormats)
: getAvailableInfoFormat()[featInfoFormat];
}
if (props.format) {
if (props.format === JSON_MIME_TYPE && layer.infoFormats && layer.infoFormats.includes(GEOJSON_MIME_TYPE)) {
// When global settings is configured for PROPERTIES (json), layer settings are not used and the layer.info_format configuration supports geo+json
// then override global settings and set param.info_format to geo+json mime type explicitly.
return GEOJSON_MIME_TYPE;
}
// otherwise, preserve and obey the global configration for getFeatureInfo mime type.
return props.format;
}
// if global configration somehow fails provide a last fallback.
return MapInfoUtils.getDefaultInfoFormatValue();
};
/**
* @param {object} layer a layer object
* @returns {object} the viewer configured for the layer. If viewer is not configured, it returns an empty object.
*/
export const getLayerFeatureInfoViewer = (layer) => {
if (layer.featureInfo
&& layer.featureInfo.viewer) {
return layer.featureInfo.viewer;
}
return {};
};
export const clickedPointToGeoJson = (clickedPoint) => {
if (!clickedPoint) {
return [];
}
if (clickedPoint.type === "Feature") {
let features = [pointOnSurface(clickedPoint)];
if (clickedPoint && clickedPoint.geometry && clickedPoint.geometry.type !== "Point") {
features.push(clickedPoint);
}
return features;
}
if (clickedPoint.lng === undefined || clickedPoint.lat === undefined) {
return clickedPoint.features || [];
}
return [
...(clickedPoint.features || []), // highlight features
{
id: "get-feature-info-point",
type: "Feature",
geometry: {
type: 'Point',
coordinates: [
parseFloat(clickedPoint.lng),
parseFloat(clickedPoint.lat),
...(clickedPoint.height !== undefined
? [parseFloat(clickedPoint.height)]
: [])
]
},
properties: {
id: 'get-feature-info-point'
},
style: [{
iconUrl,
iconAnchor: [12, 41], // in leaflet there is no anchor in fraction
iconSize: [25, 41],
leaderLine: clickedPoint.height !== undefined
}]
}
];
};
export const getMarkerLayer = (name, clickedMapPoint, styleName, otherParams, markerLabel) => {
return {
type: 'vector',
visibility: true,
queryable: false,
name: name || "GetFeatureInfo",
styleName: styleName || "marker",
label: markerLabel,
features: MapInfoUtils.clickedPointToGeoJson(clickedMapPoint),
...otherParams
};
};
/**
* Creates GFI request and metadata for specific layer.
* @param {object} layer the layer object
* @param {object} options the options for the request
* @param {string} options.format the format to use
* @param {string} options.map the map object, with projection and
* @param {object} options.point
*/
export const buildIdentifyRequest = (layer, options) => {
if (MapInfoUtils.services[layer.type]) {
let infoFormat = MapInfoUtils.getDefaultInfoFormatValueFromLayer(layer, options);
let viewer = MapInfoUtils.getLayerFeatureInfoViewer(layer);
const featureInfo = MapInfoUtils.getLayerFeatureInfo(layer);
return MapInfoUtils.services[layer.type].buildRequest(layer, options, infoFormat, viewer, featureInfo);
}
return {};
};
/**
* Returns an Observable that emits the response when ready.
* @param {object} layer the layer
* @param {string} baseURL the URL for the request
* @param {object} params for the request
*/
export const getIdentifyFlow = (layer, baseURL, params) => {
if (MapInfoUtils.services[layer.type] && MapInfoUtils.services[layer.type].getIdentifyFlow) {
return MapInfoUtils.services[layer.type].getIdentifyFlow(layer, baseURL, params);
}
return null;
};
const deduceInfoFormat = (response) => {
let infoFormat;
// Handle WMS, WMTS
if (response.queryParams && response.queryParams.hasOwnProperty('info_format')) {
infoFormat = response.queryParams.info_format;
}
// handle WFS
if (response.queryParams && response.queryParams.hasOwnProperty('outputFormat')) {
infoFormat = response.queryParams.outputFormat;
}
return infoFormat;
};
const determineValidatorFormat = (response, format) => {
if (response.format) return response.format;
const infoFormat = deduceInfoFormat(response);
return INFO_FORMATS_BY_MIME_TYPE[infoFormat] || INFO_FORMATS_BY_MIME_TYPE[format];
};
const determineValidator = (response, format) => {
const validatorFormat = determineValidatorFormat(response, format);
return validator(validatorFormat);
};
export const getValidator = (format) => {
return {
getValidResponses: (responses) => {
return responses.filter((current) => {
if (current) {
return determineValidator(current, format).isValidResponse(current);
}
return false;
});
},
getNoValidResponses: (responses) => {
return responses.filter((current) => {
if (current) {
return !determineValidator(current, format).isValidResponse(current);
}
return false;
});
}
};
};
export const getViewers = () => {
return {
[INFO_VIEW_MODES.TEMPLATE]: JSONViewer,
[INFO_VIEW_MODES.PROPERTIES]: JSONViewer,
[INFO_VIEW_MODES.HTML]: HTMLViewer,
[INFO_VIEW_MODES.TEXT]: TextViewer
};
};
/**
* @param {string} infoFormat the info format key corresponding to a specific mime type in INFO_FORMATS OR a custom viewer key set by the user.
* @param {object} viewers a map of {infoFormat: viewerType} (see MapInfoUtils.getViewers).
* @returns {jsx} the associated viewer component.
*/
export const getDefaultViewer = function(infoFormat, viewers = getViewers()) {
let isInfoKey = getAvailableInfoFormatLabels()?.includes(infoFormat);
let isInfoValue = getAvailableInfoFormatValues()?.includes(infoFormat);
if (isInfoKey) {
return viewers[getDefaultInfoViewMode(getAvailableInfoFormat()[infoFormat])];
}
if (isInfoValue) {
return viewers[getDefaultInfoViewMode(infoFormat)];
}
return viewers[infoFormat];
};
export const defaultQueryableFilter = (l) => {
return l.visibility &&
MapInfoUtils.services[l.type] &&
(l.queryable === undefined || l.queryable) &&
l.group !== "background" && l?.featureInfo?.format !== 'HIDDEN'
;
};
export const services = {
'wfs': wfs,
'wms': wms,
'wmts': wmts,
'vector': vector,
'3dtiles': threeDTiles,
'model': model,
'arcgis': arcgis
};
/**
* To get the custom viewer with the given type
* This way you can extend the featureinfo with your custom viewers in external projects.
* @param type {string} the string the component was registered with
* @return {object} the registered component
*/
export const getViewer = (type) => {
return !!MapInfoUtils.VIEWERS[type] ? MapInfoUtils.VIEWERS[type] : null;
};
/**
* To register a custom viewer
* This way you can extend the featureinfo with your custom viewers in external projects.
* @param type {string} the string you want to register the component with
* @param viewer {object} the component to register
*/
export const setViewer = (type, viewer) => {
MapInfoUtils.VIEWERS[type] = viewer;
};
/**
* returns new options filtered by include and exclude options
* @param layer {object} layer object
* @param includeOptions {array} options to include
* @param excludeParams {array} options to exclude
* @return {object} new filtered options
*/
export const filterRequestParams = (layer, includeOptions, excludeParams) => {
let includeOpt = includeOptions || [];
let excludeList = excludeParams || [];
let options = Object.keys(layer).reduce((op, next) => {
if (next !== "params" && includeOpt.indexOf(next) !== -1) {
op[next] = layer[next];
} else if (next === "params" && excludeList.length > 0) {
let params = layer[next];
Object.keys(params).forEach((n) => {
if (findIndex(excludeList, (el) => {return el === n; }) === -1) {
op[n] = params[n];
}
}, {});
}
return op;
}, {});
return options;
};
let rowViewers = {};
export const registerRowViewer = (name, options) => {
rowViewers[name] = options;
};
export const getRowViewer = (name) => {
return rowViewers[name];
};
MapInfoUtils = {
SUPPORTED_FORMATS,
getAvailableInfoFormatLabels,
getAvailableInfoFormat,
getDefaultInfoFormatValue,
clickedPointToGeoJson,
services,
getDefaultInfoFormatValueFromLayer,
getLayerFeatureInfoViewer,
getLayerFeatureInfo,
VIEWERS: {},
registerRowViewer,
getRowViewer
};