-
Notifications
You must be signed in to change notification settings - Fork 383
/
MapInfoUtils.js
239 lines (233 loc) · 9.21 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
/**
* 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.
*/
const FeatureInfoUtils = require("./FeatureInfoUtils");
const INFO_FORMATS = FeatureInfoUtils.INFO_FORMATS;
const INFO_FORMATS_BY_MIME_TYPE = FeatureInfoUtils.INFO_FORMATS_BY_MIME_TYPE;
const pointOnSurface = require('turf-point-on-surface');
const {findIndex} = require('lodash');
const MapInfoUtils = {
/**
* specifies which info formats are currently supported
*/
// default format ↴
AVAILABLE_FORMAT: ['TEXT', 'PROPERTIES', 'HTML', 'TEMPLATE'],
VIEWERS: {},
/**
* @return a filtered version of INFO_FORMATS object.
* the returned object contains only keys that AVAILABLE_FORMAT contains.
*/
getAvailableInfoFormat() {
return Object.keys(INFO_FORMATS).filter((k) => {
return MapInfoUtils.AVAILABLE_FORMAT.indexOf(k) !== -1;
}).reduce((prev, k) => {
prev[k] = INFO_FORMATS[k];
return prev;
}, {});
},
/**
* @return the label of an inputformat given the value
*/
getLabelFromValue(value) {
return MapInfoUtils.getAvailableInfoFormatLabels().filter(info => INFO_FORMATS[info] === value)[0] || "TEXT";
},
/**
* @return like getAvailableInfoFormat but return an array filled with the keys
*/
getAvailableInfoFormatLabels() {
return Object.keys(MapInfoUtils.getAvailableInfoFormat());
},
/**
* @return like getAvailableInfoFormat but return an array filled with the values
*/
getAvailableInfoFormatValues() {
return Object.keys(MapInfoUtils.getAvailableInfoFormat()).map( label => {
return INFO_FORMATS[label];
});
},
/**
* @return {string} the default info format value
*/
getDefaultInfoFormatValue() {
return INFO_FORMATS[MapInfoUtils.AVAILABLE_FORMAT[0]];
},
/**
* @return {string} the info format value from layer, otherwise the info format in settings
*/
getDefaultInfoFormatValueFromLayer: (layer, props) =>
layer.featureInfo
&& layer.featureInfo.format
&& INFO_FORMATS[layer.featureInfo.format]
|| props.format
|| MapInfoUtils.getDefaultInfoFormatValue(),
getLayerFeatureInfoViewer(layer) {
if (layer.featureInfo
&& layer.featureInfo.viewer) {
return layer.featureInfo.viewer;
}
return {};
},
/**
* returns feature info options of layer
* @param layer {object} layer object
* @return {object} feature info options
*/
getLayerFeatureInfo(layer) {
return layer && layer.featureInfo && {...layer.featureInfo} || {};
},
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)]
},
style: [{
iconUrl: require('../components/map/openlayers/img/marker-icon.png'),
iconAnchor: [12, 41], // in leaflet there is no anchor in fraction
iconSize: [25, 41]
}]
}
];
},
getMarkerLayer(name, clickedMapPoint, styleName, otherParams, markerLabel) {
return {
type: 'vector',
visibility: true,
name: name || "GetFeatureInfo",
styleName: styleName || "marker",
label: markerLabel,
features: MapInfoUtils.clickedPointToGeoJson(clickedMapPoint),
...otherParams
};
},
/**
*
* @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
*/
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 {};
},
getValidator(format) {
const defaultValidator = {
getValidResponses: (responses) => responses,
getNoValidResponses: () => []
};
return {
getValidResponses: (responses) => {
return responses.reduce((previous, current) => {
let infoFormat;
if (current.queryParams && current.queryParams.hasOwnProperty('info_format')) {
infoFormat = current.queryParams.info_format;
}
const valid = (FeatureInfoUtils.Validator[current.format || INFO_FORMATS_BY_MIME_TYPE[infoFormat] || INFO_FORMATS_BY_MIME_TYPE[format]] || defaultValidator).getValidResponses([current]);
return [...previous, ...valid];
}, []);
},
getNoValidResponses: (responses) => {
return responses.reduce((previous, current) => {
let infoFormat;
if (current.queryParams && current.queryParams.hasOwnProperty('info_format')) {
infoFormat = current.queryParams.info_format;
}
const valid = (FeatureInfoUtils.Validator[current.format || INFO_FORMATS_BY_MIME_TYPE[infoFormat] || INFO_FORMATS_BY_MIME_TYPE[format]] || defaultValidator).getNoValidResponses([current]);
return [...previous, ...valid];
}, []);
}
};
},
getViewers() {
return {
[FeatureInfoUtils.INFO_FORMATS.PROPERTIES]: require('../components/data/identify/viewers/JSONViewer'),
[FeatureInfoUtils.INFO_FORMATS.JSON]: require('../components/data/identify/viewers/JSONViewer'),
[FeatureInfoUtils.INFO_FORMATS.HTML]: require('../components/data/identify/viewers/HTMLViewer'),
[FeatureInfoUtils.INFO_FORMATS.TEXT]: require('../components/data/identify/viewers/TextViewer')
};
},
defaultQueryableFilter(l) {
return l.visibility &&
MapInfoUtils.services[l.type] &&
(l.queryable === undefined || l.queryable) &&
l.group !== "background"
;
},
services: {
wms: require('./mapinfo/wms'),
wmts: require('./mapinfo/wmts'),
vector: require('./mapinfo/vector')
},
/**
* 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
*/
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
*/
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
*/
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;
}
};
module.exports = MapInfoUtils;