diff --git a/src/ol/format/GMLBase.js b/src/ol/format/GMLBase.js index 2c90cd2413..5ac3c3788e 100644 --- a/src/ol/format/GMLBase.js +++ b/src/ol/format/GMLBase.js @@ -150,7 +150,11 @@ class GMLBase extends XMLFeature { objectStack, this ); - } else if (localName == 'featureMembers' || localName == 'featureMember') { + } else if ( + localName == 'featureMembers' || + localName == 'featureMember' || + localName == 'member' + ) { const context = objectStack[0]; let featureType = context['featureType']; let featureNS = context['featureNS']; @@ -214,7 +218,7 @@ class GMLBase extends XMLFeature { } parsersNS[featureNS[p]] = parsers; } - if (localName == 'featureMember') { + if (localName == 'featureMember' || localName == 'member') { features = pushParseAndPop(undefined, parsersNS, node, objectStack); } else { features = pushParseAndPop([], parsersNS, node, objectStack); diff --git a/src/ol/format/WFS.js b/src/ol/format/WFS.js index 3438edfe44..8d842d9509 100644 --- a/src/ol/format/WFS.js +++ b/src/ol/format/WFS.js @@ -3,6 +3,7 @@ */ 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 { @@ -39,6 +40,9 @@ const FEATURE_COLLECTION_PARSERS = { 'bounds' ), }, + 'http://www.opengis.net/wfs/2.0': { + 'member': makeArrayPusher(GMLBase.prototype.readFeaturesInternal), + }, }; /** @@ -51,6 +55,11 @@ const TRANSACTION_SUMMARY_PARSERS = { 'totalUpdated': makeObjectPropertySetter(readNonNegativeInteger), 'totalDeleted': makeObjectPropertySetter(readNonNegativeInteger), }, + 'http://www.opengis.net/wfs/2.0': { + 'totalInserted': makeObjectPropertySetter(readNonNegativeInteger), + 'totalUpdated': makeObjectPropertySetter(readNonNegativeInteger), + 'totalDeleted': makeObjectPropertySetter(readNonNegativeInteger), + }, }; /** @@ -65,6 +74,13 @@ const TRANSACTION_RESPONSE_PARSERS = { ), 'InsertResults': makeObjectPropertySetter(readInsertResults, 'insertIds'), }, + 'http://www.opengis.net/wfs/2.0': { + 'TransactionSummary': makeObjectPropertySetter( + readTransactionSummary, + 'transactionSummary' + ), + 'InsertResults': makeObjectPropertySetter(readInsertResults, 'insertIds'), + }, }; /** @@ -74,6 +90,9 @@ const QUERY_SERIALIZERS = { 'http://www.opengis.net/wfs': { 'PropertyName': makeChildAppender(writeStringTextNode), }, + 'http://www.opengis.net/wfs/2.0': { + 'PropertyName': makeChildAppender(writeStringTextNode), + }, }; /** @@ -87,14 +106,23 @@ const TRANSACTION_SERIALIZERS = { '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} [featureNS] The namespace URI used for features. * @property {Array|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/GML3`. + * @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`. */ /** @@ -134,7 +162,7 @@ const TRANSACTION_SERIALIZERS = { * a 3D layer. This will allow the Z coordinate to be included in the transaction. * @property {Array} 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` or `1.1.0`. + * @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`. */ /** @@ -164,30 +192,53 @@ const FEATURE_PREFIX = 'feature'; const XMLNS = 'http://www.w3.org/2000/xmlns/'; /** - * @type {string} + * @type {Object} */ -const OGCNS = 'http://www.opengis.net/ogc'; +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 {string} + * @type {Object} */ -const WFSNS = 'http://www.opengis.net/wfs'; +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 {string} + * @type {Object} */ -const FESNS = 'http://www.opengis.net/fes'; +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} */ 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} + */ +const GML_FORMATS = { + '2.0.0': GML32, + '1.1.0': GML3, + '1.0.0': GML2, +}; + /** * @const * @type {string} @@ -198,7 +249,7 @@ 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 if you want to read a WFS that contains GML2 (WFS 1.0.0). + * as option to override the default. * Also see {@link module:ol/format/GMLBase~GMLBase} which is used by this format. * * @api @@ -212,6 +263,12 @@ class WFS extends XMLFeature { const options = opt_options ? opt_options : {}; + /** + * @private + * @type {string} + */ + this.version_ = options.version ? options.version : DEFAULT_VERSION; + /** * @private * @type {Array|string|undefined} @@ -228,7 +285,9 @@ class WFS extends XMLFeature { * @private * @type {GMLBase} */ - this.gmlFormat_ = options.gmlFormat ? options.gmlFormat : new GML3(); + this.gmlFormat_ = options.gmlFormat + ? options.gmlFormat + : new GML_FORMATS[this.version_](); /** * @private @@ -236,7 +295,7 @@ class WFS extends XMLFeature { */ this.schemaLocation_ = options.schemaLocation ? options.schemaLocation - : SCHEMA_LOCATIONS[DEFAULT_VERSION]; + : SCHEMA_LOCATIONS[this.version_]; } /** @@ -262,7 +321,7 @@ class WFS extends XMLFeature { readFeaturesFromNode(node, opt_options) { /** @type {import("../xml.js").NodeStackItem} */ const context = { - node: node, + node, }; assign(context, { 'featureType': this.featureType_, @@ -271,12 +330,15 @@ class WFS extends XMLFeature { assign(context, this.getReadOptions(node, opt_options ? opt_options : {})); const objectStack = [context]; - this.gmlFormat_.FEATURE_COLLECTION_PARSERS[GMLNS][ - 'featureMember' - ] = makeArrayPusher(GMLBase.prototype.readFeaturesInternal); + let featuresNS; + if (this.version_ === '2.0.0') { + featuresNS = FEATURE_COLLECTION_PARSERS; + } else { + featuresNS = this.gmlFormat_.FEATURE_COLLECTION_PARSERS; + } let features = pushParseAndPop( [], - this.gmlFormat_.FEATURE_COLLECTION_PARSERS, + featuresNS, node, objectStack, this.gmlFormat_ @@ -406,9 +468,9 @@ class WFS extends XMLFeature { * @api */ writeGetFeature(options) { - const node = createElementNS(WFSNS, 'GetFeature'); + const node = createElementNS(WFSNS[this.version_], 'GetFeature'); node.setAttribute('service', 'WFS'); - node.setAttribute('version', '1.1.0'); + node.setAttribute('version', this.version_); let filter; if (options) { if (options.handle) { @@ -455,9 +517,10 @@ class WFS extends XMLFeature { ); /** @type {import("../xml.js").NodeStackItem} */ const context = { - node: node, + node, }; assign(context, { + 'version': this.version_, 'srsName': options.srsName, 'featureNS': options.featureNS ? options.featureNS : this.featureNS_, 'featurePrefix': options.featurePrefix, @@ -487,101 +550,41 @@ class WFS extends XMLFeature { */ writeTransaction(inserts, updates, deletes, options) { const objectStack = []; - const node = createElementNS(WFSNS, 'Transaction'); - const version = options.version ? options.version : DEFAULT_VERSION; - const gmlVersion = version === '1.0.0' ? 2 : 3; + 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} */ - let obj; if (options) { baseObj = options.gmlOptions ? options.gmlOptions : {}; if (options.handle) { node.setAttribute('handle', options.handle); } } - const schemaLocation = SCHEMA_LOCATIONS[version]; node.setAttributeNS( XML_SCHEMA_INSTANCE_URI, 'xsi:schemaLocation', - schemaLocation + SCHEMA_LOCATIONS[version] ); - const featurePrefix = options.featurePrefix - ? options.featurePrefix - : FEATURE_PREFIX; + + const request = createTransactionRequest(node, baseObj, version, options); if (inserts) { - obj = assign( - {node: node}, - { - 'featureNS': options.featureNS, - 'featureType': options.featureType, - 'featurePrefix': featurePrefix, - 'gmlVersion': gmlVersion, - 'hasZ': options.hasZ, - 'srsName': options.srsName, - } - ); - assign(obj, baseObj); - pushSerializeAndPop( - obj, - TRANSACTION_SERIALIZERS, - makeSimpleNodeFactory('Insert'), - inserts, - objectStack - ); + serializeTransactionRequest('Insert', inserts, objectStack, request); } if (updates) { - obj = assign( - {node: node}, - { - 'featureNS': options.featureNS, - 'featureType': options.featureType, - 'featurePrefix': featurePrefix, - 'gmlVersion': gmlVersion, - 'hasZ': options.hasZ, - 'srsName': options.srsName, - } - ); - assign(obj, baseObj); - pushSerializeAndPop( - obj, - TRANSACTION_SERIALIZERS, - makeSimpleNodeFactory('Update'), - updates, - objectStack - ); + serializeTransactionRequest('Update', updates, objectStack, request); } if (deletes) { - pushSerializeAndPop( - { - node: node, - 'featureNS': options.featureNS, - 'featureType': options.featureType, - 'featurePrefix': featurePrefix, - 'gmlVersion': gmlVersion, - 'srsName': options.srsName, - }, - TRANSACTION_SERIALIZERS, - makeSimpleNodeFactory('Delete'), - deletes, - objectStack - ); + serializeTransactionRequest('Delete', deletes, objectStack, request); } if (options.nativeElements) { - pushSerializeAndPop( - { - node: node, - 'featureNS': options.featureNS, - 'featureType': options.featureType, - 'featurePrefix': featurePrefix, - 'gmlVersion': gmlVersion, - 'srsName': options.srsName, - }, - TRANSACTION_SERIALIZERS, - makeSimpleNodeFactory('Native'), + serializeTransactionRequest( + 'Native', options.nativeElements, - objectStack + objectStack, + request ); } return node; @@ -625,6 +628,57 @@ class WFS extends XMLFeature { } } +/** + * @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 = 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} 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. @@ -644,6 +698,11 @@ const OGC_FID_PARSERS = { return node.getAttribute('fid'); }), }, + 'http://www.opengis.net/ogc/1.1': { + 'FeatureId': makeArrayPusher(function (node, objectStack) { + return node.getAttribute('fid'); + }), + }, }; /** @@ -662,6 +721,9 @@ const INSERT_RESULTS_PARSERS = { 'http://www.opengis.net/wfs': { 'Feature': fidParser, }, + 'http://www.opengis.net/wfs/2.0': { + 'Feature': fidParser, + }, }; /** @@ -687,8 +749,10 @@ function writeFeature(node, feature, objectStack) { node.appendChild(child); if (gmlVersion === 2) { GML2.prototype.writeFeatureElement(child, feature, objectStack); - } else { + } else if (gmlVersion === 3) { GML3.prototype.writeFeatureElement(child, feature, objectStack); + } else { + GML32.prototype.writeFeatureElement(child, feature, objectStack); } } @@ -698,8 +762,11 @@ function writeFeature(node, feature, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeOgcFidFilter(node, fid, objectStack) { - const filter = createElementNS(OGCNS, 'Filter'); - const child = createElementNS(OGCNS, 'FeatureId'); + 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); @@ -749,6 +816,7 @@ function writeDelete(node, feature, objectStack) { function writeUpdate(node, feature, objectStack) { const context = objectStack[objectStack.length - 1]; assert(feature.getId() !== undefined, 27); // Features must have an id set + const version = context['version']; const featureType = context['featureType']; const featurePrefix = context['featurePrefix']; const featureNS = context['featureNS']; @@ -775,8 +843,9 @@ function writeUpdate(node, feature, objectStack) { } pushSerializeAndPop( /** @type {import("../xml.js").NodeStackItem} */ ({ + version, 'gmlVersion': context['gmlVersion'], - node: node, + node, 'hasZ': context['hasZ'], 'srsName': context['srsName'], }), @@ -795,13 +864,15 @@ function writeUpdate(node, feature, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeProperty(node, pair, objectStack) { - const name = createElementNS(WFSNS, 'Name'); const context = objectStack[objectStack.length - 1]; + const version = context['version']; + const ns = WFSNS[version]; + const name = createElementNS(ns, 'Name'); const gmlVersion = context['gmlVersion']; node.appendChild(name); writeStringTextNode(name, pair.name); if (pair.value !== undefined && pair.value !== null) { - const value = createElementNS(WFSNS, 'Value'); + const value = createElementNS(ns, 'Value'); node.appendChild(value); if ( pair.value && @@ -810,8 +881,10 @@ function writeProperty(node, pair, objectStack) { ) { if (gmlVersion === 2) { GML2.prototype.writeGeometryElement(value, pair.value, objectStack); - } else { + } else if (gmlVersion === 3) { GML3.prototype.writeGeometryElement(value, pair.value, objectStack); + } else { + GML32.prototype.writeGeometryElement(value, pair.value, objectStack); } } else { writeStringTextNode(value, pair.value); @@ -843,6 +916,9 @@ 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), @@ -862,6 +938,27 @@ const GETFEATURE_SERIALIZERS = { '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), + 'BBOX': makeChildAppender(writeBboxFilter), + 'Contains': makeChildAppender(writeContainsFilter), + 'Disjoint': makeChildAppender(writeDisjointFilter), + 'Intersects': makeChildAppender(writeIntersectsFilter), + 'ResourceId': makeChildAppender(writeResourceIdFilter), + 'Within': makeChildAppender(writeWithinFilter), + '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), + }, }; /** @@ -871,6 +968,7 @@ const GETFEATURE_SERIALIZERS = { */ function writeQuery(node, featureType, objectStack) { const context = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const version = context['version']; const featurePrefix = context['featurePrefix']; const featureNS = context['featureNS']; const propertyNames = context['propertyNames']; @@ -882,7 +980,13 @@ function writeQuery(node, featureType, objectStack) { } else { typeName = featureType; } - node.setAttribute('typeName', typeName); + let typeNameAttr; + if (version === '2.0.0') { + typeNameAttr = 'typeNames'; + } else { + typeNameAttr = 'typeName'; + } + node.setAttribute(typeNameAttr, typeName); if (srsName) { node.setAttribute('srsName', srsName); } @@ -903,7 +1007,7 @@ function writeQuery(node, featureType, objectStack) { ); const filter = context['filter']; if (filter) { - const child = createElementNS(OGCNS, 'Filter'); + const child = createElementNS(getFilterNS(version), 'Filter'); node.appendChild(child); writeFilterCondition(child, filter, objectStack); } @@ -915,8 +1019,10 @@ function writeQuery(node, featureType, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeFilterCondition(node, filter, objectStack) { + const context = /** @type {Object} */ (objectStack[objectStack.length - 1]); /** @type {import("../xml.js").NodeStackItem} */ - const item = {node: node}; + const item = {node}; + assign(item, {context}); pushSerializeAndPop( item, GETFEATURE_SERIALIZERS, @@ -932,11 +1038,14 @@ function writeFilterCondition(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeBboxFilter(node, filter, objectStack) { - const context = objectStack[objectStack.length - 1]; - context['srsName'] = filter.srsName; + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + parent['srsName'] = filter.srsName; + const format = GML_FORMATS[version]; - writeOgcPropertyName(node, filter.geometryName); - GML3.prototype.writeGeometryElement(node, filter.extent, objectStack); + writePropertyName(version, node, filter.geometryName); + format.prototype.writeGeometryElement(node, filter.extent, objectStack); } /** @@ -945,11 +1054,14 @@ function writeBboxFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeContainsFilter(node, filter, objectStack) { - const context = objectStack[objectStack.length - 1]; - context['srsName'] = filter.srsName; + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + parent['srsName'] = filter.srsName; + const format = GML_FORMATS[version]; - writeOgcPropertyName(node, filter.geometryName); - GML3.prototype.writeGeometryElement(node, filter.geometry, objectStack); + writePropertyName(version, node, filter.geometryName); + format.prototype.writeGeometryElement(node, filter.geometry, objectStack); } /** @@ -958,11 +1070,39 @@ function writeContainsFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeIntersectsFilter(node, filter, objectStack) { - const context = objectStack[objectStack.length - 1]; - context['srsName'] = filter.srsName; + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + parent['srsName'] = filter.srsName; + const format = GML_FORMATS[version]; - writeOgcPropertyName(node, filter.geometryName); - GML3.prototype.writeGeometryElement(node, filter.geometry, objectStack); + writePropertyName(version, node, filter.geometryName); + format.prototype.writeGeometryElement(node, filter.geometry, objectStack); +} + +/** + * @param {Node} node Node. + * @param {import("./filter/Disjoint.js").default} filter Filter. + * @param {Array<*>} objectStack Node stack. + */ +function writeDisjointFilter(node, filter, objectStack) { + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + parent['srsName'] = filter.srsName; + const format = GML_FORMATS[version]; + + writePropertyName(version, node, filter.geometryName); + format.prototype.writeGeometryElement(node, filter.geometry, objectStack); +} + +/** + * @param {Element} node Element. + * @param {import("./filter/ResourceId.js").default} filter Filter. + * @param {Array<*>} objectStack Node stack. + */ +function writeResourceIdFilter(node, filter, objectStack) { + node.setAttribute('rid', /** @type {string} */ (filter.rid)); } /** @@ -971,11 +1111,14 @@ function writeIntersectsFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeWithinFilter(node, filter, objectStack) { - const context = objectStack[objectStack.length - 1]; - context['srsName'] = filter.srsName; + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + parent['srsName'] = filter.srsName; + const format = GML_FORMATS[version]; - writeOgcPropertyName(node, filter.geometryName); - GML3.prototype.writeGeometryElement(node, filter.geometry, objectStack); + writePropertyName(version, node, filter.geometryName); + format.prototype.writeGeometryElement(node, filter.geometry, objectStack); } /** @@ -984,7 +1127,11 @@ function writeWithinFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeDuringFilter(node, filter, objectStack) { - const valueReference = createElementNS(FESNS, 'ValueReference'); + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + const ns = FESNS[version]; + const valueReference = createElementNS(ns, 'ValueReference'); writeStringTextNode(valueReference, filter.propertyName); node.appendChild(valueReference); @@ -1007,8 +1154,11 @@ function writeDuringFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeLogicalFilter(node, filter, objectStack) { + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; /** @type {import("../xml.js").NodeStackItem} */ - const item = {node: node}; + const item = {node}; + assign(item, {context}); const conditions = filter.conditions; for (let i = 0, ii = conditions.length; i < ii; ++i) { const condition = conditions[i]; @@ -1028,8 +1178,11 @@ function writeLogicalFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeNotFilter(node, filter, objectStack) { + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; /** @type {import("../xml.js").NodeStackItem} */ - const item = {node: node}; + const item = {node}; + assign(item, {context}); const condition = filter.condition; pushSerializeAndPop( item, @@ -1046,11 +1199,15 @@ function writeNotFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeComparisonFilter(node, filter, objectStack) { + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + const ns = OGCNS[context['version']]; if (filter.matchCase !== undefined) { node.setAttribute('matchCase', filter.matchCase.toString()); } - writeOgcPropertyName(node, filter.propertyName); - writeOgcLiteral(node, '' + filter.expression); + writePropertyName(version, node, filter.propertyName); + writeOgcLiteral(ns, node, '' + filter.expression); } /** @@ -1059,7 +1216,10 @@ function writeComparisonFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeIsNullFilter(node, filter, objectStack) { - writeOgcPropertyName(node, filter.propertyName); + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + writePropertyName(version, node, filter.propertyName); } /** @@ -1068,15 +1228,20 @@ function writeIsNullFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeIsBetweenFilter(node, filter, objectStack) { - writeOgcPropertyName(node, filter.propertyName); + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + const ns = OGCNS[context['version']]; - const lowerBoundary = createElementNS(OGCNS, 'LowerBoundary'); + writePropertyName(version, node, filter.propertyName); + + const lowerBoundary = createElementNS(ns, 'LowerBoundary'); node.appendChild(lowerBoundary); - writeOgcLiteral(lowerBoundary, '' + filter.lowerBoundary); + writeOgcLiteral(ns, lowerBoundary, '' + filter.lowerBoundary); - const upperBoundary = createElementNS(OGCNS, 'UpperBoundary'); + const upperBoundary = createElementNS(ns, 'UpperBoundary'); node.appendChild(upperBoundary); - writeOgcLiteral(upperBoundary, '' + filter.upperBoundary); + writeOgcLiteral(ns, upperBoundary, '' + filter.upperBoundary); } /** @@ -1085,41 +1250,61 @@ function writeIsBetweenFilter(node, filter, objectStack) { * @param {Array<*>} objectStack Node stack. */ function writeIsLikeFilter(node, filter, objectStack) { + const parent = /** @type {Object} */ (objectStack[objectStack.length - 1]); + const context = parent['context']; + const version = context['version']; + const ns = OGCNS[version]; node.setAttribute('wildCard', filter.wildCard); node.setAttribute('singleChar', filter.singleChar); node.setAttribute('escapeChar', filter.escapeChar); if (filter.matchCase !== undefined) { node.setAttribute('matchCase', filter.matchCase.toString()); } - writeOgcPropertyName(node, filter.propertyName); - writeOgcLiteral(node, '' + filter.pattern); + writePropertyName(version, node, filter.propertyName); + writeOgcLiteral(ns, node, '' + filter.pattern); } /** + * @param {string} ns Namespace. * @param {string} tagName Tag name. * @param {Node} node Node. * @param {string} value Value. */ -function writeOgcExpression(tagName, node, value) { - const property = createElementNS(OGCNS, tagName); +function writeOgcExpression(ns, tagName, node, value) { + const property = createElementNS(ns, tagName); writeStringTextNode(property, value); node.appendChild(property); } /** + * @param {string} version Version. * @param {Node} node Node. * @param {string} value PropertyName value. */ -function writeOgcPropertyName(node, value) { - writeOgcExpression('PropertyName', node, value); +function writePropertyName(version, node, value) { + if (version === '2.0.0') { + writeFesValueReference(FESNS[version], node, value); + } else { + writeOgcExpression(OGCNS[version], 'PropertyName', node, value); + } } /** + * @param {string} ns Namespace. * @param {Node} node Node. * @param {string} value PropertyName value. */ -function writeOgcLiteral(node, value) { - writeOgcExpression('Literal', node, value); +function writeFesValueReference(ns, node, value) { + writeOgcExpression(ns, 'ValueReference', node, value); +} + +/** + * @param {string} ns Namespace. + * @param {Node} node Node. + * @param {string} value PropertyName value. + */ +function writeOgcLiteral(ns, node, value) { + writeOgcExpression(ns, 'Literal', node, value); } /** @@ -1139,12 +1324,20 @@ function writeTimeInstant(node, time) { * Encode filter as WFS `Filter` and return the Node. * * @param {import("./filter/Filter.js").default} filter Filter. + * @param {string} version Version. * @return {Node} Result. * @api */ -export function writeFilter(filter) { - const child = createElementNS(OGCNS, 'Filter'); - writeFilterCondition(child, filter, []); +export function writeFilter(filter, version) { + const child = createElementNS(OGCNS[version], 'Filter'); + const context = { + node: child, + }; + assign(context, { + 'version': version, + 'filter': filter, + }); + writeFilterCondition(child, filter, [context]); return child; } @@ -1169,4 +1362,14 @@ function writeGetFeature(node, featureTypes, objectStack) { ); } +function getFilterNS(version) { + let ns; + if (version === '2.0.0') { + ns = FESNS[version]; + } else { + ns = OGCNS[version]; + } + return ns; +} + export default WFS; diff --git a/src/ol/format/filter.js b/src/ol/format/filter.js index c4d2c9615d..05c8be7904 100644 --- a/src/ol/format/filter.js +++ b/src/ol/format/filter.js @@ -4,6 +4,7 @@ import And from './filter/And.js'; import Bbox from './filter/Bbox.js'; import Contains from './filter/Contains.js'; +import Disjoint from './filter/Disjoint.js'; import During from './filter/During.js'; import EqualTo from './filter/EqualTo.js'; import GreaterThan from './filter/GreaterThan.js'; @@ -17,6 +18,7 @@ import LessThanOrEqualTo from './filter/LessThanOrEqualTo.js'; import Not from './filter/Not.js'; import NotEqualTo from './filter/NotEqualTo.js'; import Or from './filter/Or.js'; +import ResourceId from './filter/ResourceId.js'; import Within from './filter/Within.js'; /** @@ -99,6 +101,21 @@ export function intersects(geometryName, geometry, opt_srsName) { return new Intersects(geometryName, geometry, opt_srsName); } +/** + * Create a `` operator to test whether a geometry-valued property + * is disjoint to a given geometry. + * + * @param {!string} geometryName Geometry name to use. + * @param {!import("../geom/Geometry.js").default} geometry Geometry. + * @param {string=} opt_srsName SRS name. No srsName attribute will be + * set on geometries when this is not provided. + * @returns {!Disjoint} `` operator. + * @api + */ +export function disjoint(geometryName, geometry, opt_srsName) { + return new Disjoint(geometryName, geometry, opt_srsName); +} + /** * Create a `` operator to test whether a geometry-valued property * is within a given geometry. @@ -260,3 +277,7 @@ export function like( export function during(propertyName, begin, end) { return new During(propertyName, begin, end); } + +export function resourceId(rid) { + return new ResourceId(rid); +} diff --git a/src/ol/format/filter/Disjoint.js b/src/ol/format/filter/Disjoint.js new file mode 100644 index 0000000000..a3601c55a2 --- /dev/null +++ b/src/ol/format/filter/Disjoint.js @@ -0,0 +1,24 @@ +/** + * @module ol/format/filter/Disjoint + */ +import Spatial from './Spatial.js'; + +/** + * @classdesc + * Represents a `` operator to test whether a geometry-valued property + * is disjoint to a given geometry. + * @api + */ +class Disjoint extends Spatial { + /** + * @param {!string} geometryName Geometry name to use. + * @param {!import("../../geom/Geometry.js").default} geometry Geometry. + * @param {string=} opt_srsName SRS name. No srsName attribute will be + * set on geometries when this is not provided. + */ + constructor(geometryName, geometry, opt_srsName) { + super('Disjoint', geometryName, geometry, opt_srsName); + } +} + +export default Disjoint; diff --git a/src/ol/format/filter/ResourceId.js b/src/ol/format/filter/ResourceId.js new file mode 100644 index 0000000000..d68a6bcf75 --- /dev/null +++ b/src/ol/format/filter/ResourceId.js @@ -0,0 +1,25 @@ +/** + * @module ol/format/filter/ResourceId + */ +import Filter from './Filter.js'; + +/** + * @classdesc + * + * @abstract + */ +class ResourceId extends Filter { + /** + * @param {!string} rid Resource ID. + */ + constructor(rid) { + super('ResourceId'); + + /** + * @type {!string} + */ + this.rid = rid; + } +} + +export default ResourceId; diff --git a/test/spec/ol/format/wfs.test.js b/test/spec/ol/format/wfs.test.js index 94fe9abe9e..d3c407f18c 100644 --- a/test/spec/ol/format/wfs.test.js +++ b/test/spec/ol/format/wfs.test.js @@ -1,5 +1,6 @@ import Feature from '../../../../src/ol/Feature.js'; import GML2 from '../../../../src/ol/format/GML2.js'; +import GML32 from '../../../../src/ol/format/GML32.js'; import LineString from '../../../../src/ol/geom/LineString.js'; import MultiLineString from '../../../../src/ol/geom/MultiLineString.js'; import MultiPoint from '../../../../src/ol/geom/MultiPoint.js'; @@ -16,6 +17,7 @@ import { bbox as bboxFilter, between as betweenFilter, contains as containsFilter, + disjoint as disjointFilter, during as duringFilter, equalTo as equalToFilter, greaterThan as greaterThanFilter, @@ -27,6 +29,7 @@ import { like as likeFilter, not as notFilter, or as orFilter, + resourceId as resourceIdFilter, within as withinFilter, } from '../../../../src/ol/format/filter.js'; import {parse} from '../../../../src/ol/xml.js'; @@ -1376,9 +1379,221 @@ describe('ol.format.WFS', function () { andFilter( likeFilter('name', 'Mississippi*'), equalToFilter('waterway', 'riverbank') - ) + ), + '1.1.0' ); expect(serialized).to.xmleql(parse(text)); }); }); + + describe('WFS 2.0.0', function () { + before(function (done) { + proj4.defs( + 'http://www.opengis.net/def/crs/EPSG/0/26713', + '+proj=utm +zone=13 +ellps=clrk66 +datum=NAD27 +units=m +no_defs' + ); + proj4.defs( + 'urn:ogc:def:crs:EPSG::26713', + '+proj=utm +zone=13 +ellps=clrk66 +datum=NAD27 +units=m +no_defs' + ); + register(proj4); + done(); + }); + + after(function () { + delete proj4.defs['http://www.opengis.net/def/crs/EPSG/0/26713']; + delete proj4.defs['urn:ogc:def:crs:EPSG::26713']; + clearAllProjections(); + addCommon(); + }); + + it('can writeGetFeature query with simple resourceId filter', function () { + const getFeatureXml = ` + + + + + + + + + + `.trim(); + const wfs = new WFS({ + version: '2.0.0', + }); + const filter = resourceIdFilter('bugsites.3'); + const serialized = wfs.writeGetFeature({ + featureNS: 'http://www.openplans.org/spearfish', + featureTypes: ['bugsites'], + featurePrefix: 'sf', + filter, + }); + expect(serialized).to.xmleql(parse(getFeatureXml)); + }); + + it('can writeGetFeature query with negated disjoint spatial filter', function () { + const geometryXml = ` + + + + + 590431 4915204 590430 + 4915205 590429 4915204 590430 + 4915203 590431 4915204 + + + + `.trim(); + const getFeatureXml = ` + + + + + + + + sf:the_geom + ${geometryXml} + + + + + + `.trim(); + const wfs = new WFS({ + version: '2.0.0', + }); + const geometryNode = parse(geometryXml); + const geometry = new GML32().readGeometryElement(geometryNode, [{}]); + const filter = notFilter(disjointFilter('sf:the_geom', geometry)); + const serialized = wfs.writeGetFeature({ + featureNS: 'http://www.openplans.org/spearfish', + featureTypes: ['bugsites'], + featurePrefix: 'sf', + filter, + }); + expect(serialized).to.xmleql(parse(getFeatureXml)); + }); + + it('can parse basic GetFeature response', function () { + const response = ` + + + + + + + 590529 4914625 + + + 3 + Beetle site + + + + `.trim(); + const wfs = new WFS({ + version: '2.0.0', + }); + const features = wfs.readFeatures(parse(response)); + expect(features.length).to.be(1); + expect(features[0]).to.be.an(Feature); + }); + + describe('when writing out a Transaction request', function () { + it('creates a handle', function () { + const text = + ''; + const serialized = new WFS({ + version: '2.0.0', + }).writeTransaction(null, null, null, { + handle: 'handle_t', + }); + expect(serialized).to.xmleql(parse(text)); + }); + }); + + describe('when writing out a Transaction request', function () { + it('creates the correct srsName', function () { + const text = ` + + + + + + + + -5178372.1885436 1992365.7775042 -4434792.7774889 1601008.1927386 -4043435.1927233 2148908.8114105 + + + + + xyz + + + + `.trim(); + const format = new WFS({ + version: '2.0.0', + }); + const insertFeature = new Feature({ + the_geom: new MultiLineString([ + [ + [-5178372.1885436, 1992365.7775042], + [-4434792.7774889, 1601008.1927386], + [-4043435.1927233, 2148908.8114105], + ], + ]), + TYPE: 'xyz', + }); + insertFeature.setGeometryName('the_geom'); + const inserts = [insertFeature]; + const serialized = format.writeTransaction(inserts, null, null, { + featureNS: 'http://foo', + featureType: 'FAULTS', + featurePrefix: 'feature', + gmlOptions: {multiCurve: true, srsName: 'EPSG:900913'}, + }); + expect(serialized).to.xmleql(parse(text)); + }); + }); + }); });