-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
WFS.js
1388 lines (1297 loc) · 42.4 KB
/
WFS.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module ol/format/WFS
*/
import GML2 from './GML2.js';
import GML3 from './GML3.js';
import GML32 from './GML32.js';
import GMLBase, {GMLNS} from './GMLBase.js';
import XMLFeature from './XMLFeature.js';
import {
XML_SCHEMA_INSTANCE_URI,
createElementNS,
isDocument,
makeArrayPusher,
makeChildAppender,
makeObjectPropertySetter,
makeSimpleNodeFactory,
parse,
parseNode,
pushParseAndPop,
pushSerializeAndPop,
} from '../xml.js';
import {and as andFilterFn, bbox as bboxFilterFn} from './filter.js';
import {assert} from '../asserts.js';
import {get as getProjection} from '../proj.js';
import {
readNonNegativeIntegerString,
readPositiveInteger,
writeStringTextNode,
} from './xsd.js';
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
const FEATURE_COLLECTION_PARSERS = {
'http://www.opengis.net/gml': {
'boundedBy': makeObjectPropertySetter(
GMLBase.prototype.readExtentElement,
'bounds',
),
},
'http://www.opengis.net/wfs/2.0': {
'member': makeArrayPusher(GMLBase.prototype.readFeaturesInternal),
},
};
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
const TRANSACTION_SUMMARY_PARSERS = {
'http://www.opengis.net/wfs': {
'totalInserted': makeObjectPropertySetter(readPositiveInteger),
'totalUpdated': makeObjectPropertySetter(readPositiveInteger),
'totalDeleted': makeObjectPropertySetter(readPositiveInteger),
},
'http://www.opengis.net/wfs/2.0': {
'totalInserted': makeObjectPropertySetter(readPositiveInteger),
'totalUpdated': makeObjectPropertySetter(readPositiveInteger),
'totalDeleted': makeObjectPropertySetter(readPositiveInteger),
},
};
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
const TRANSACTION_RESPONSE_PARSERS = {
'http://www.opengis.net/wfs': {
'TransactionSummary': makeObjectPropertySetter(
readTransactionSummary,
'transactionSummary',
),
'InsertResults': makeObjectPropertySetter(readInsertResults, 'insertIds'),
},
'http://www.opengis.net/wfs/2.0': {
'TransactionSummary': makeObjectPropertySetter(
readTransactionSummary,
'transactionSummary',
),
'InsertResults': makeObjectPropertySetter(readInsertResults, 'insertIds'),
},
};
/**
* @type {Object<string, Object<string, import("../xml.js").Serializer>>}
*/
const QUERY_SERIALIZERS = {
'http://www.opengis.net/wfs': {
'PropertyName': makeChildAppender(writeStringTextNode),
},
'http://www.opengis.net/wfs/2.0': {
'PropertyName': makeChildAppender(writeStringTextNode),
},
};
/**
* @type {Object<string, Object<string, import("../xml.js").Serializer>>}
*/
const TRANSACTION_SERIALIZERS = {
'http://www.opengis.net/wfs': {
'Insert': makeChildAppender(writeFeature),
'Update': makeChildAppender(writeUpdate),
'Delete': makeChildAppender(writeDelete),
'Property': makeChildAppender(writeProperty),
'Native': makeChildAppender(writeNative),
},
'http://www.opengis.net/wfs/2.0': {
'Insert': makeChildAppender(writeFeature),
'Update': makeChildAppender(writeUpdate),
'Delete': makeChildAppender(writeDelete),
'Property': makeChildAppender(writeProperty),
'Native': makeChildAppender(writeNative),
},
};
/**
* @typedef {Object} Options
* @property {Object<string, string>|string} [featureNS] The namespace URI used for features.
* @property {Array<string>|string} [featureType] The feature type to parse. Only used for read operations.
* @property {GMLBase} [gmlFormat] The GML format to use to parse the response.
* Default is `ol/format/GML2` for WFS 1.0.0, `ol/format/GML3` for WFS 1.1.0 and `ol/format/GML32` for WFS 2.0.0.
* @property {string} [schemaLocation] Optional schemaLocation to use for serialization, this will override the default.
* @property {string} [version='1.1.0'] WFS version to use. Can be either `1.0.0`, `1.1.0` or `2.0.0`.
*/
/**
* @typedef {Object} WriteGetFeatureOptions
* @property {string} featureNS The namespace URI used for features.
* @property {string} featurePrefix The prefix for the feature namespace.
* @property {Array<string|FeatureType>} featureTypes The feature type names or FeatureType objects to
* define a unique bbox filter per feature type name (in this case, options `bbox` and `geometryName` are
* ignored.).
* @property {string} [srsName] SRS name. No srsName attribute will be set on
* geometries when this is not provided.
* @property {string} [handle] Handle.
* @property {string} [outputFormat] Output format.
* @property {number} [maxFeatures] Maximum number of features to fetch.
* @property {string} [geometryName] Geometry name to use in a BBOX filter.
* @property {Array<string>} [propertyNames] Optional list of property names to serialize.
* @property {string} [viewParams] viewParams GeoServer vendor parameter.
* @property {number} [startIndex] Start index to use for WFS paging. This is a
* WFS 2.0 feature backported to WFS 1.1.0 by some Web Feature Services.
* @property {number} [count] Number of features to retrieve when paging. This is a
* WFS 2.0 feature backported to WFS 1.1.0 by some Web Feature Services. Please note that some
* Web Feature Services have repurposed `maxfeatures` instead.
* @property {import("../extent.js").Extent} [bbox] Extent to use for the BBOX filter. The `geometryName`
* option must be set.
* @property {import("./filter/Filter.js").default} [filter] Filter condition. See
* {@link module:ol/format/filter} for more information.
* @property {string} [resultType] Indicates what response should be returned,
* e.g. `hits` only includes the `numberOfFeatures` attribute in the response and no features.
*/
/**
* @typedef {Object} FeatureType
* @property {!string} name The feature type name.
* @property {!import("../extent.js").Extent} bbox Extent to use for the BBOX filter.
* @property {!string} geometryName Geometry name to use in the BBOX filter.
*/
/**
* @typedef {Object} WriteTransactionOptions
* @property {string} featureNS The namespace URI used for features.
* @property {string} featurePrefix The prefix for the feature namespace.
* @property {string} featureType The feature type name.
* @property {string} [srsName] SRS name. No srsName attribute will be set on
* geometries when this is not provided.
* @property {string} [handle] Handle.
* @property {boolean} [hasZ] Must be set to true if the transaction is for
* a 3D layer. This will allow the Z coordinate to be included in the transaction.
* @property {Array<Object>} nativeElements Native elements. Currently not supported.
* @property {import("./GMLBase.js").Options} [gmlOptions] GML options for the WFS transaction writer.
* @property {string} [version='1.1.0'] WFS version to use for the transaction. Can be either `1.0.0`, `1.1.0` or `2.0.0`.
*/
/**
* Number of features; bounds/extent.
* @typedef {Object} FeatureCollectionMetadata
* @property {number} numberOfFeatures NumberOfFeatures.
* @property {import("../extent.js").Extent} bounds Bounds.
*/
/**
* @typedef {Object} TransactionSummary
* @property {number} totalDeleted TotalDeleted.
* @property {number} totalInserted TotalInserted.
* @property {number} totalUpdated TotalUpdated.
*/
/**
* Total deleted; total inserted; total updated; array of insert ids.
* @typedef {Object} TransactionResponse
* @property {TransactionSummary} transactionSummary Transaction summary.
* @property {Array<string>} insertIds InsertIds.
*/
/**
* @type {string}
*/
const FEATURE_PREFIX = 'feature';
/**
* @type {string}
*/
const XMLNS = 'http://www.w3.org/2000/xmlns/';
/**
* @type {Object<string, string>}
*/
const OGCNS = {
'2.0.0': 'http://www.opengis.net/ogc/1.1',
'1.1.0': 'http://www.opengis.net/ogc',
'1.0.0': 'http://www.opengis.net/ogc',
};
/**
* @type {Object<string, string>}
*/
const WFSNS = {
'2.0.0': 'http://www.opengis.net/wfs/2.0',
'1.1.0': 'http://www.opengis.net/wfs',
'1.0.0': 'http://www.opengis.net/wfs',
};
/**
* @type {Object<string, string>}
*/
const FESNS = {
'2.0.0': 'http://www.opengis.net/fes/2.0',
'1.1.0': 'http://www.opengis.net/fes',
'1.0.0': 'http://www.opengis.net/fes',
};
/**
* @type {Object<string, string>}
*/
const SCHEMA_LOCATIONS = {
'2.0.0':
'http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd',
'1.1.0':
'http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd',
'1.0.0':
'http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/wfs.xsd',
};
/**
* @type {Object<string, object>}
*/
const GML_FORMATS = {
'2.0.0': GML32,
'1.1.0': GML3,
'1.0.0': GML2,
};
/**
* @const
* @type {string}
*/
const DEFAULT_VERSION = '1.1.0';
/**
* @classdesc
* Feature format for reading and writing data in the WFS format.
* By default, supports WFS version 1.1.0. You can pass a GML format
* as option to override the default.
* Also see {@link module:ol/format/GMLBase~GMLBase} which is used by this format.
*
* @api
*/
class WFS extends XMLFeature {
/**
* @param {Options} [options] Optional configuration object.
*/
constructor(options) {
super();
options = options ? options : {};
/**
* @private
* @type {string}
*/
this.version_ = options.version ? options.version : DEFAULT_VERSION;
/**
* @private
* @type {Array<string>|string|undefined}
*/
this.featureType_ = options.featureType;
/**
* @private
* @type {Object<string, string>|string|undefined}
*/
this.featureNS_ = options.featureNS;
/**
* @private
* @type {GMLBase}
*/
this.gmlFormat_ = options.gmlFormat
? options.gmlFormat
: new GML_FORMATS[this.version_]();
/**
* @private
* @type {string}
*/
this.schemaLocation_ = options.schemaLocation
? options.schemaLocation
: SCHEMA_LOCATIONS[this.version_];
}
/**
* @return {Array<string>|string|undefined} featureType
*/
getFeatureType() {
return this.featureType_;
}
/**
* @param {Array<string>|string|undefined} featureType Feature type(s) to parse.
*/
setFeatureType(featureType) {
this.featureType_ = featureType;
}
/**
* @protected
* @param {Element} node Node.
* @param {import("./Feature.js").ReadOptions} [options] Options.
* @return {Array<import("../Feature.js").default>} Features.
* @override
*/
readFeaturesFromNode(node, options) {
/** @type {import("../xml.js").NodeStackItem} */
const context = {
node,
};
Object.assign(context, {
'featureType': this.featureType_,
'featureNS': this.featureNS_,
});
Object.assign(context, this.getReadOptions(node, options ? options : {}));
const objectStack = [context];
let featuresNS;
if (this.version_ === '2.0.0') {
featuresNS = FEATURE_COLLECTION_PARSERS;
} else {
featuresNS = this.gmlFormat_.FEATURE_COLLECTION_PARSERS;
}
let features = pushParseAndPop(
[],
featuresNS,
node,
objectStack,
this.gmlFormat_,
);
if (!features) {
features = [];
}
return features;
}
/**
* Read transaction response of the source.
*
* @param {Document|Element|Object|string} source Source.
* @return {TransactionResponse|undefined} Transaction response.
* @api
*/
readTransactionResponse(source) {
if (!source) {
return undefined;
}
if (typeof source === 'string') {
const doc = parse(source);
return this.readTransactionResponseFromDocument(doc);
}
if (isDocument(source)) {
return this.readTransactionResponseFromDocument(
/** @type {Document} */ (source),
);
}
return this.readTransactionResponseFromNode(
/** @type {Element} */ (source),
);
}
/**
* Read feature collection metadata of the source.
*
* @param {Document|Element|Object|string} source Source.
* @return {FeatureCollectionMetadata|undefined}
* FeatureCollection metadata.
* @api
*/
readFeatureCollectionMetadata(source) {
if (!source) {
return undefined;
}
if (typeof source === 'string') {
const doc = parse(source);
return this.readFeatureCollectionMetadataFromDocument(doc);
}
if (isDocument(source)) {
return this.readFeatureCollectionMetadataFromDocument(
/** @type {Document} */ (source),
);
}
return this.readFeatureCollectionMetadataFromNode(
/** @type {Element} */ (source),
);
}
/**
* @param {Document} doc Document.
* @return {FeatureCollectionMetadata|undefined}
* FeatureCollection metadata.
*/
readFeatureCollectionMetadataFromDocument(doc) {
for (let n = /** @type {Node} */ (doc.firstChild); n; n = n.nextSibling) {
if (n.nodeType == Node.ELEMENT_NODE) {
return this.readFeatureCollectionMetadataFromNode(
/** @type {Element} */ (n),
);
}
}
return undefined;
}
/**
* @param {Element} node Node.
* @return {FeatureCollectionMetadata|undefined}
* FeatureCollection metadata.
*/
readFeatureCollectionMetadataFromNode(node) {
const result = {};
const value = readNonNegativeIntegerString(
node.getAttribute('numberOfFeatures'),
);
result['numberOfFeatures'] = value;
return pushParseAndPop(
/** @type {FeatureCollectionMetadata} */ (result),
FEATURE_COLLECTION_PARSERS,
node,
[],
this.gmlFormat_,
);
}
/**
* @param {Document} doc Document.
* @return {TransactionResponse|undefined} Transaction response.
*/
readTransactionResponseFromDocument(doc) {
for (let n = /** @type {Node} */ (doc.firstChild); n; n = n.nextSibling) {
if (n.nodeType == Node.ELEMENT_NODE) {
return this.readTransactionResponseFromNode(/** @type {Element} */ (n));
}
}
return undefined;
}
/**
* @param {Element} node Node.
* @return {TransactionResponse|undefined} Transaction response.
*/
readTransactionResponseFromNode(node) {
return pushParseAndPop(
/** @type {TransactionResponse} */ ({}),
TRANSACTION_RESPONSE_PARSERS,
node,
[],
);
}
/**
* Encode format as WFS `GetFeature` and return the Node.
*
* @param {WriteGetFeatureOptions} options Options.
* @return {Node} Result.
* @api
*/
writeGetFeature(options) {
const node = createElementNS(WFSNS[this.version_], 'GetFeature');
node.setAttribute('service', 'WFS');
node.setAttribute('version', this.version_);
if (options.handle) {
node.setAttribute('handle', options.handle);
}
if (options.outputFormat) {
node.setAttribute('outputFormat', options.outputFormat);
}
if (options.maxFeatures !== undefined) {
node.setAttribute('maxFeatures', String(options.maxFeatures));
}
if (options.resultType) {
node.setAttribute('resultType', options.resultType);
}
if (options.startIndex !== undefined) {
node.setAttribute('startIndex', String(options.startIndex));
}
if (options.count !== undefined) {
node.setAttribute('count', String(options.count));
}
if (options.viewParams !== undefined) {
node.setAttribute('viewParams', options.viewParams);
}
node.setAttributeNS(
XML_SCHEMA_INSTANCE_URI,
'xsi:schemaLocation',
this.schemaLocation_,
);
/** @type {import("../xml.js").NodeStackItem} */
const context = {
node,
};
Object.assign(context, {
'version': this.version_,
'srsName': options.srsName,
'featureNS': options.featureNS ? options.featureNS : this.featureNS_,
'featurePrefix': options.featurePrefix,
'propertyNames': options.propertyNames ? options.propertyNames : [],
});
assert(
Array.isArray(options.featureTypes),
'`options.featureTypes` must be an Array',
);
if (typeof options.featureTypes[0] === 'string') {
let filter = options.filter;
if (options.bbox) {
assert(
options.geometryName,
'`options.geometryName` must also be provided when `options.bbox` is set',
);
filter = this.combineBboxAndFilter(
options.geometryName,
options.bbox,
options.srsName,
filter,
);
}
Object.assign(context, {
'geometryName': options.geometryName,
'filter': filter,
});
writeGetFeature(
node,
/** @type {!Array<string>} */ (options.featureTypes),
[context],
);
} else {
// Write one query node per element in featuresType.
options.featureTypes.forEach((/** @type {FeatureType} */ featureType) => {
const completeFilter = this.combineBboxAndFilter(
featureType.geometryName,
featureType.bbox,
options.srsName,
options.filter,
);
Object.assign(context, {
'geometryName': featureType.geometryName,
'filter': completeFilter,
});
writeGetFeature(node, [featureType.name], [context]);
});
}
return node;
}
/**
* Create a bbox filter and combine it with another optional filter.
*
* @param {!string} geometryName Geometry name to use.
* @param {!import("../extent.js").Extent} extent Extent.
* @param {string} [srsName] SRS name. No srsName attribute will be
* set on geometries when this is not provided.
* @param {import("./filter/Filter.js").default} [filter] Filter condition.
* @return {import("./filter/Filter.js").default} The filter.
*/
combineBboxAndFilter(geometryName, extent, srsName, filter) {
const bboxFilter = bboxFilterFn(geometryName, extent, srsName);
if (filter) {
// if bbox and filter are both set, combine the two into a single filter
return andFilterFn(filter, bboxFilter);
}
return bboxFilter;
}
/**
* Encode format as WFS `Transaction` and return the Node.
*
* @param {Array<import("../Feature.js").default>} inserts The features to insert.
* @param {Array<import("../Feature.js").default>} updates The features to update.
* @param {Array<import("../Feature.js").default>} deletes The features to delete.
* @param {WriteTransactionOptions} options Write options.
* @return {Node} Result.
* @api
*/
writeTransaction(inserts, updates, deletes, options) {
const objectStack = [];
const version = options.version ? options.version : this.version_;
const node = createElementNS(WFSNS[version], 'Transaction');
node.setAttribute('service', 'WFS');
node.setAttribute('version', version);
let baseObj;
/** @type {import("../xml.js").NodeStackItem} */
if (options) {
baseObj = options.gmlOptions ? options.gmlOptions : {};
if (options.handle) {
node.setAttribute('handle', options.handle);
}
}
node.setAttributeNS(
XML_SCHEMA_INSTANCE_URI,
'xsi:schemaLocation',
SCHEMA_LOCATIONS[version],
);
const request = createTransactionRequest(node, baseObj, version, options);
if (inserts) {
serializeTransactionRequest('Insert', inserts, objectStack, request);
}
if (updates) {
serializeTransactionRequest('Update', updates, objectStack, request);
}
if (deletes) {
serializeTransactionRequest('Delete', deletes, objectStack, request);
}
if (options.nativeElements) {
serializeTransactionRequest(
'Native',
options.nativeElements,
objectStack,
request,
);
}
return node;
}
/**
* @param {Document} doc Document.
* @return {import("../proj/Projection.js").default} Projection.
* @override
*/
readProjectionFromDocument(doc) {
for (let n = doc.firstChild; n; n = n.nextSibling) {
if (n.nodeType == Node.ELEMENT_NODE) {
return this.readProjectionFromNode(/** @type {Element} */ (n));
}
}
return null;
}
/**
* @param {Element} node Node.
* @return {import("../proj/Projection.js").default} Projection.
* @override
*/
readProjectionFromNode(node) {
if (node.firstElementChild && node.firstElementChild.firstElementChild) {
node = node.firstElementChild.firstElementChild;
for (let n = node.firstElementChild; n; n = n.nextElementSibling) {
if (
!(
n.childNodes.length === 0 ||
(n.childNodes.length === 1 && n.firstChild.nodeType === 3)
)
) {
const objectStack = [{}];
this.gmlFormat_.readGeometryElement(n, objectStack);
return getProjection(objectStack.pop().srsName);
}
}
}
return null;
}
}
/**
* @param {Element} node Node.
* @param {*} baseObj Base object.
* @param {string} version Version.
* @param {WriteTransactionOptions} options Options.
* @return {Object} Request object.
*/
function createTransactionRequest(node, baseObj, version, options) {
const featurePrefix = options.featurePrefix
? options.featurePrefix
: FEATURE_PREFIX;
let gmlVersion;
if (version === '1.0.0') {
gmlVersion = 2;
} else if (version === '1.1.0') {
gmlVersion = 3;
} else if (version === '2.0.0') {
gmlVersion = 3.2;
}
const obj = Object.assign(
{node},
{
version,
'featureNS': options.featureNS,
'featureType': options.featureType,
'featurePrefix': featurePrefix,
'gmlVersion': gmlVersion,
'hasZ': options.hasZ,
'srsName': options.srsName,
},
baseObj,
);
return obj;
}
/**
* @param {string} type Request type.
* @param {Array<import("../Feature.js").default>} features Features.
* @param {Array<*>} objectStack Object stack.
* @param {Element} request Transaction Request.
*/
function serializeTransactionRequest(type, features, objectStack, request) {
pushSerializeAndPop(
request,
TRANSACTION_SERIALIZERS,
makeSimpleNodeFactory(type),
features,
objectStack,
);
}
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
* @return {Object|undefined} Transaction Summary.
*/
function readTransactionSummary(node, objectStack) {
return pushParseAndPop({}, TRANSACTION_SUMMARY_PARSERS, node, objectStack);
}
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
const OGC_FID_PARSERS = {
'http://www.opengis.net/ogc': {
'FeatureId': makeArrayPusher(function (node, objectStack) {
return node.getAttribute('fid');
}),
},
'http://www.opengis.net/ogc/1.1': {
'FeatureId': makeArrayPusher(function (node, objectStack) {
return node.getAttribute('fid');
}),
},
};
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
*/
function fidParser(node, objectStack) {
parseNode(OGC_FID_PARSERS, node, objectStack);
}
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
const INSERT_RESULTS_PARSERS = {
'http://www.opengis.net/wfs': {
'Feature': fidParser,
},
'http://www.opengis.net/wfs/2.0': {
'Feature': fidParser,
},
};
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
* @return {Array<string>|undefined} Insert results.
*/
function readInsertResults(node, objectStack) {
return pushParseAndPop([], INSERT_RESULTS_PARSERS, node, objectStack);
}
/**
* @param {Element} node Node.
* @param {import("../Feature.js").default} feature Feature.
* @param {Array<*>} objectStack Node stack.
*/
function writeFeature(node, feature, objectStack) {
const context = objectStack[objectStack.length - 1];
const featureType = context['featureType'];
const featureNS = context['featureNS'];
const gmlVersion = context['gmlVersion'];
const child = createElementNS(featureNS, featureType);
node.appendChild(child);
if (gmlVersion === 2) {
GML2.prototype.writeFeatureElement(child, feature, objectStack);
} else if (gmlVersion === 3) {
GML3.prototype.writeFeatureElement(child, feature, objectStack);
} else {
GML32.prototype.writeFeatureElement(child, feature, objectStack);
}
}
/**
* @param {Node} node Node.
* @param {number|string} fid Feature identifier.
* @param {Array<*>} objectStack Node stack.
*/
function writeOgcFidFilter(node, fid, objectStack) {
const context = objectStack[objectStack.length - 1];
const version = context['version'];
const ns = OGCNS[version];
const filter = createElementNS(ns, 'Filter');
const child = createElementNS(ns, 'FeatureId');
filter.appendChild(child);
child.setAttribute('fid', /** @type {string} */ (fid));
node.appendChild(filter);
}
/**
* @param {string|undefined} featurePrefix The prefix of the feature.
* @param {string} featureType The type of the feature.
* @return {string} The value of the typeName property.
*/
function getTypeName(featurePrefix, featureType) {
featurePrefix = featurePrefix ? featurePrefix : FEATURE_PREFIX;
const prefix = featurePrefix + ':';
// The featureType already contains the prefix.
if (featureType.startsWith(prefix)) {
return featureType;
}
return prefix + featureType;
}
/**
* @param {Element} node Node.
* @param {import("../Feature.js").default} feature Feature.
* @param {Array<*>} objectStack Node stack.
*/
function writeDelete(node, feature, objectStack) {
const context = objectStack[objectStack.length - 1];
assert(feature.getId() !== undefined, 'Features must have an id set');
const featureType = context['featureType'];
const featurePrefix = context['featurePrefix'];
const featureNS = context['featureNS'];
const typeName = getTypeName(featurePrefix, featureType);
node.setAttribute('typeName', typeName);
node.setAttributeNS(XMLNS, 'xmlns:' + featurePrefix, featureNS);
const fid = feature.getId();
if (fid !== undefined) {
writeOgcFidFilter(node, fid, objectStack);
}
}
/**
* @param {Element} node Node.
* @param {import("../Feature.js").default} feature Feature.
* @param {Array<*>} objectStack Node stack.
*/
function writeUpdate(node, feature, objectStack) {
const context = objectStack[objectStack.length - 1];
assert(feature.getId() !== undefined, 'Features must have an id set');
const version = context['version'];
const featureType = context['featureType'];
const featurePrefix = context['featurePrefix'];
const featureNS = context['featureNS'];
const typeName = getTypeName(featurePrefix, featureType);
const geometryName = feature.getGeometryName();
node.setAttribute('typeName', typeName);
node.setAttributeNS(XMLNS, 'xmlns:' + featurePrefix, featureNS);
const fid = feature.getId();
if (fid !== undefined) {
const keys = feature.getKeys();
const values = [];
for (let i = 0, ii = keys.length; i < ii; i++) {
const value = feature.get(keys[i]);
if (value !== undefined) {
let name = keys[i];
if (
value &&
typeof (/** @type {?} */ (value).getSimplifiedGeometry) === 'function'
) {
name = geometryName;
}
values.push({name: name, value: value});
}
}
pushSerializeAndPop(
/** @type {import("../xml.js").NodeStackItem} */ ({
version,
'gmlVersion': context['gmlVersion'],
node,
'hasZ': context['hasZ'],
'srsName': context['srsName'],
}),
TRANSACTION_SERIALIZERS,
makeSimpleNodeFactory('Property'),
values,
objectStack,
);
writeOgcFidFilter(node, fid, objectStack);
}
}
/**
* @param {Node} node Node.
* @param {Object} pair Property name and value.
* @param {Array<*>} objectStack Node stack.
*/
function writeProperty(node, pair, objectStack) {
const context = objectStack[objectStack.length - 1];
const version = context['version'];
const ns = WFSNS[version];
const tagName = version === '2.0.0' ? 'ValueReference' : 'Name';
const name = createElementNS(ns, tagName);
const gmlVersion = context['gmlVersion'];
node.appendChild(name);
writeStringTextNode(name, pair.name);
if (pair.value !== undefined && pair.value !== null) {
const value = createElementNS(ns, 'Value');
node.appendChild(value);
if (
pair.value &&
typeof (/** @type {?} */ (pair.value).getSimplifiedGeometry) ===
'function'
) {
if (gmlVersion === 2) {
GML2.prototype.writeGeometryElement(value, pair.value, objectStack);
} else if (gmlVersion === 3) {
GML3.prototype.writeGeometryElement(value, pair.value, objectStack);
} else {
GML32.prototype.writeGeometryElement(value, pair.value, objectStack);
}
} else {
writeStringTextNode(value, pair.value);
}
}
}
/**
* @param {Element} node Node.
* @param {{vendorId: string, safeToIgnore: boolean, value: string}} nativeElement The native element.
* @param {Array<*>} objectStack Node stack.
*/
function writeNative(node, nativeElement, objectStack) {
if (nativeElement.vendorId) {
node.setAttribute('vendorId', nativeElement.vendorId);
}
if (nativeElement.safeToIgnore !== undefined) {
node.setAttribute('safeToIgnore', String(nativeElement.safeToIgnore));
}
if (nativeElement.value !== undefined) {
writeStringTextNode(node, nativeElement.value);
}
}
/**
* @type {Object<string, Object<string, import("../xml.js").Serializer>>}
*/
const GETFEATURE_SERIALIZERS = {
'http://www.opengis.net/wfs': {
'Query': makeChildAppender(writeQuery),
},
'http://www.opengis.net/wfs/2.0': {
'Query': makeChildAppender(writeQuery),
},
'http://www.opengis.net/ogc': {
'During': makeChildAppender(writeDuringFilter),
'And': makeChildAppender(writeLogicalFilter),
'Or': makeChildAppender(writeLogicalFilter),
'Not': makeChildAppender(writeNotFilter),
'BBOX': makeChildAppender(writeBboxFilter),
'Contains': makeChildAppender(writeSpatialFilter),
'Intersects': makeChildAppender(writeSpatialFilter),
'Within': makeChildAppender(writeSpatialFilter),
'DWithin': makeChildAppender(writeDWithinFilter),
'PropertyIsEqualTo': makeChildAppender(writeComparisonFilter),
'PropertyIsNotEqualTo': makeChildAppender(writeComparisonFilter),
'PropertyIsLessThan': makeChildAppender(writeComparisonFilter),
'PropertyIsLessThanOrEqualTo': makeChildAppender(writeComparisonFilter),
'PropertyIsGreaterThan': makeChildAppender(writeComparisonFilter),
'PropertyIsGreaterThanOrEqualTo': makeChildAppender(writeComparisonFilter),
'PropertyIsNull': makeChildAppender(writeIsNullFilter),
'PropertyIsBetween': makeChildAppender(writeIsBetweenFilter),
'PropertyIsLike': makeChildAppender(writeIsLikeFilter),
},
'http://www.opengis.net/fes/2.0': {
'During': makeChildAppender(writeDuringFilter),
'And': makeChildAppender(writeLogicalFilter),
'Or': makeChildAppender(writeLogicalFilter),
'Not': makeChildAppender(writeNotFilter),