-
Notifications
You must be signed in to change notification settings - Fork 88
/
WfsPermalink.js
376 lines (342 loc) · 12.5 KB
/
WfsPermalink.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
// The MIT License (MIT)
//
// Copyright (c) 2016-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
import {extend as extendExtent, createEmpty as createEmptyExtent} from 'ol/extent.js';
import Feature from 'ol/Feature.js';
import {equalTo, and, or} from 'ol/format/filter.js';
import olFormatWFS from 'ol/format/WFS.js';
/**
* @typedef {import('ngeo/query/MapQuerent.js').QueryResult} QueryResult
*/
/**
* Results for a query source.
* @typedef {Object} QueryResultSource
* @property {Array<Feature<import("ol/geom/Geometry.js").default>>} features The matching features for this source.
* @property {number|string} id Identifier (can be not unique).
* @property {string} label Label.
* @property {number} [limit] The maximum number of features that can be returned for a query with this
* source.
* @property {boolean} pending Is the request for this source still ongoing?
* @property {boolean} [tooManyResults] If the last query for this source would return more features than the
* configured limit.
* @property {number} [totalFeatureCount] If `tooManyResults` is `true`, this contains the total number of
* features.
* @property {string} [identifierAttributeField] The field to use to identify the feature.
* @property {string[]} [mergeComposants] The original datasources merged in merged result.
* @property {string[]} [requestPartners] The datasources of the request (which are summed in totalFeatureCount).
*/
/**
* @typedef {Object} WfsPermalinkData
* @property {string} wfsType
* @property {WfsPermalinkFilterGroup[]} filterGroups
* @property {boolean} showFeatures
*/
/**
* @typedef {Object} WfsPermalinkFilter
* @property {string} property
* @property {string[]|string} condition
*/
/**
* @typedef {Object} WfsPermalinkFilterGroup
* @property {WfsPermalinkFilter[]} filters
*/
/**
* WFS permalink service that can be used to load features with a WFS
* GetFeature request given query parameters.
*
* Resulting features are then highlighted and the map is zoomed to the nearest map extent.
*
* Configuration:
*
* ngeoWfsPermalinkOptions:
* wfsTypes:
* - featureType: <featureType>
* label: <attribute used as label>
* - ...
*
* Parameters:
*
* - ``wfs_layer`` tells what layer will be queried
* - ``wfs_showFeatures`` (boolean) tells if the features should be
* highlighted and listed (when true) or if the map should only be
* recentered on the features (when false). Default is true.
* - other parameters will be considered as WFS attribute/values filters and
* must be of the form:
* ``wfs_<layer attribute name>=<a comma-separated list of values>``
*
* Example:
* http://example.com?wfs_layer=parcels&wfs_city=Oslo&wfs_number=12,34,56
* will load parcels #12, 34 and 56 of the city of Oslo.
*
* It is possible to define several groups of filtering parameters by:
*
* - adding a ``wfs_ngroups`` parameter telling how many groups are defined
* - prefixing all filtering parameters by the number of each group,
* starting at 0. For instance ``wfs_0_<layer attribute name>``
*
* Example:
* http://example.com?wfs_layer=parcels&wfs_ngroups=2&wfs_0_city=Oslo&wfs_0_number=12,34,56&wfs_1_city=Paris&wfs_1_number=78,90
* will load parcels #12, 34 and 56 of the city of Oslo as well as
* parcels #78 and 90 of the city of Paris.
*
* @constructor
* @param {angular.IHttpService} $http Angular $http service.
* @param {import('ngeo/options.js').ngeoPermalinkOgcserverUrl} ngeoPermalinkOgcserverUrl URL to the WFS server
* @param {QueryResult} ngeoQueryResult The ngeo query result service.
* @param {import('ngeo/options.js').ngeoWfsPermalinkOptions} ngeoWfsPermalinkOptions The options to
* configure the ngeo wfs permalink service with.
* @ngdoc service
* @ngname ngeoWfsPermalink
* @ngInject
*/
export function WfsPermalinkService(
$http,
ngeoPermalinkOgcserverUrl,
ngeoQueryResult,
ngeoWfsPermalinkOptions
) {
const options = ngeoWfsPermalinkOptions;
/**
* @type {import('ngeo/options.js').ngeoPermalinkOgcserverUrl}
*/
this.url_ = ngeoPermalinkOgcserverUrl;
/**
* @type {number}
*/
this.maxFeatures_ = options.maxFeatures !== undefined ? options.maxFeatures : 50;
/**
* @type {Object<string, import('ngeo/options.js').WfsType>}
*/
this.wfsTypes_ = {};
console.assert(Array.isArray(options.wfsTypes), 'wfsTypes is not correctly set');
options.wfsTypes.forEach((wfsType) => {
this.wfsTypes_[wfsType.featureType] = wfsType;
});
/**
* @type {string}
*/
this.defaultFeatureNS_ = options.defaultFeatureNS;
/**
* @type {string}
*/
this.defaultFeaturePrefix_ = options.defaultFeaturePrefix;
/**
* @type {number|undefined}
*/
this.pointRecenterZoom_ = options.pointRecenterZoom;
/**
* @type {angular.IHttpService}
*/
this.$http_ = $http;
/**
* @type {QueryResult}
*/
this.result_ = ngeoQueryResult;
}
/**
* Clear the results.
*/
WfsPermalinkService.prototype.clear = function () {
this.clearResult_();
};
/**
* Build a WFS GetFeature request for the given query parameter data, send the
* request and add the received features to {@link QueryResult}.
*
* @param {WfsPermalinkData} queryData Query data for the WFS request.
* @param {import("ol/Map.js").default} map The ol3 map object to get the current projection from.
* @param {number} [zoomLevel] The level to zoom on when recentering on features.
*/
WfsPermalinkService.prototype.issue = function (queryData, map, zoomLevel = undefined) {
console.assert(
this.url_,
'url is not set. to use the wfs permalink service, ' + 'set the value `ngeoWfsPermalinkOptions`'
);
this.clearResult_();
const typeName = queryData.wfsType;
if (!this.wfsTypes_.hasOwnProperty(typeName)) {
return;
}
const wfsType = this.wfsTypes_[typeName];
const filters = this.createFilters_(queryData.filterGroups);
if (filters === null) {
return;
}
this.issueRequest_(wfsType, filters, map, queryData.showFeatures, zoomLevel);
};
/**
* @param {import('ngeo/options.js').WfsType} wfsType Type.
* @param {import("ol/format/filter/Filter.js").default} filter Filter.
* @param {import("ol/Map.js").default} map The ol3 map object to get the current projection from.
* @param {boolean} showFeatures Show features or only zoom to feature extent?
* @param {number} [zoomLevel] The level to zoom on when recentering on features.
*/
WfsPermalinkService.prototype.issueRequest_ = function (
wfsType,
filter,
map,
showFeatures,
zoomLevel = undefined
) {
const wfsFormat = new olFormatWFS();
const featureRequestXml = wfsFormat.writeGetFeature({
srsName: map.getView().getProjection().getCode(),
featureNS: wfsType.featureNS !== undefined ? wfsType.featureNS : this.defaultFeatureNS_,
featurePrefix: wfsType.featurePrefix !== undefined ? wfsType.featurePrefix : this.defaultFeaturePrefix_,
featureTypes: [wfsType.featureType],
outputFormat: 'GML3',
filter: filter,
maxFeatures: this.maxFeatures_,
});
const featureRequest = new XMLSerializer().serializeToString(featureRequestXml);
const config = {
headers: {'Content-Type': 'text/xml; charset=UTF-8'},
};
this.$http_.post(this.url_, featureRequest, config).then((response) => {
const features = wfsFormat.readFeatures(response.data);
if (features.length == 0) {
return;
}
// zoom to features
const size = map.getSize();
if (size !== undefined) {
const maxZoom = zoomLevel === undefined ? this.pointRecenterZoom_ : zoomLevel;
const padding = [10, 10, 10, 10];
map.getView().fit(this.getExtent_(features), {size, maxZoom, padding});
}
// then show if requested
if (showFeatures) {
const resultSource = /** @type {QueryResultSource} */ ({
'features': features,
'id': wfsType.featureType,
'identifierAttributeField': wfsType.label,
'label': wfsType.featureType,
'pending': false,
});
this.result_.sources.push(resultSource);
this.result_.total = features.length;
}
});
};
/**
* @param {import('ol/Feature.js').FeatureLike[]} features Features.
* @return {import('ol/extent.js').Extent} The extent of all features.
*/
WfsPermalinkService.prototype.getExtent_ = function (features) {
return /** @type {import('ol/extent.js').Extent} */ (
/** @type {any[]} */ (features).reduce((extent, feature) => {
if (feature instanceof Feature) {
const geometry = feature.getGeometry();
if (geometry) {
return extendExtent(extent, geometry.getExtent());
}
return extent;
}
}, createEmptyExtent())
);
};
/**
* Create OGC filters for the filter groups extracted from the query params.
*
* @param {WfsPermalinkFilterGroup[]} filterGroups Filter groups.
* @return {?import("ol/format/filter/Filter.js").default} OGC filters.
*/
WfsPermalinkService.prototype.createFilters_ = function (filterGroups) {
if (filterGroups.length == 0) {
return null;
}
/**
* The function
* @param {WfsPermalinkFilterGroup} filterGroup The filter
* @return {import("ol/format/filter/Filter.js").default} The return
*/
const createFiltersForGroup = function (filterGroup) {
const filters = filterGroup.filters.map((filterDef) => {
const condition = filterDef.condition;
if (Array.isArray(condition)) {
return WfsPermalinkService.or_(condition.map((cond) => equalTo(filterDef.property, cond)));
} else {
return equalTo(filterDef.property, condition);
}
});
return WfsPermalinkService.and_(filters);
};
const filters = filterGroups.map(createFiltersForGroup);
if (filters) {
return WfsPermalinkService.or_(filters);
}
return null;
};
/**
* Join a list of filters with `and(...)`.
*
* @param {Array<import("ol/format/filter/Filter.js").default>} filters The filters to join.
* @return {import("ol/format/filter/Filter.js").default} The joined filters.
*/
WfsPermalinkService.and_ = function (filters) {
return WfsPermalinkService.joinFilters_(filters, and);
};
/**
* Join a list of filters with `or(...)`.
*
* @param {Array<import("ol/format/filter/Filter.js").default>} filters The filters to join.
* @return {import("ol/format/filter/Filter.js").default} The joined filters.
*/
WfsPermalinkService.or_ = function (filters) {
return WfsPermalinkService.joinFilters_(filters, or);
};
/**
* Join a list of filters with a given join function.
*
* @param {Array<import("ol/format/filter/Filter.js").default>} filters The filters to join.
* @param {function(import("ol/format/filter/Filter.js").default, import("ol/format/filter/Filter.js").default): import("ol/format/filter/Filter.js").default} joinFn
* The function to join two filters.
* @return {import("ol/format/filter/Filter.js").default} The joined filters.
*/
WfsPermalinkService.joinFilters_ = function (filters, joinFn) {
return filters.reduce((combinedFilters, currentFilter) => {
if (combinedFilters === null) {
return currentFilter;
} else {
console.assert(currentFilter !== null);
return joinFn(combinedFilters, currentFilter);
}
});
};
/**
* Clear every features for all result sources and reset the total counter
* as well.
*/
WfsPermalinkService.prototype.clearResult_ = function () {
this.result_.total = 0;
this.result_.sources.forEach((source) => {
source.features.length = 0;
});
};
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoWfsPermalink', [
// FIXME add dependencies
]);
myModule.service('ngeoWfsPermalink', WfsPermalinkService);
export default myModule;