From 9150fa2a600055daf7357d28611cfbc501263cdd Mon Sep 17 00:00:00 2001 From: cpsTinK Date: Tue, 2 Aug 2016 16:20:09 +0400 Subject: [PATCH] Added Intersects and Within filters into ol.format.wfs --- src/ol/format/ogc/filter.js | 108 ++++++++++++++++++++++++++ src/ol/format/wfsformat.js | 40 ++++++++++ test/spec/ol/format/wfsformat.test.js | 76 ++++++++++++++++++ 3 files changed, 224 insertions(+) diff --git a/src/ol/format/ogc/filter.js b/src/ol/format/ogc/filter.js index e341b47027..a3d74842f0 100644 --- a/src/ol/format/ogc/filter.js +++ b/src/ol/format/ogc/filter.js @@ -6,6 +6,8 @@ goog.provide('ol.format.ogc.filter.And'); goog.provide('ol.format.ogc.filter.Or'); goog.provide('ol.format.ogc.filter.Not'); goog.provide('ol.format.ogc.filter.Bbox'); +goog.provide('ol.format.ogc.filter.Intersects'); +goog.provide('ol.format.ogc.filter.Within'); goog.provide('ol.format.ogc.filter.Comparison'); goog.provide('ol.format.ogc.filter.ComparisonBinary'); goog.provide('ol.format.ogc.filter.EqualTo'); @@ -72,6 +74,36 @@ ol.format.ogc.filter.bbox = function(geometryName, extent, opt_srsName) { return new ol.format.ogc.filter.Bbox(geometryName, extent, opt_srsName); }; +/** + * Create a `` operator to test whether a geometry-valued property + * intersects a fixed bounding geometry + * + * @param {!string} geometryName Geometry name to use. + * @param {!ol.geom.Geometry} geometry Geometry. + * @param {string=} opt_srsName SRS name. No srsName attribute will be + * set on geometries when this is not provided. + * @returns {!ol.format.ogc.filter.Intersects} `` operator. + * @api + */ +ol.format.ogc.filter.intersects = function(geometryName, geometry, opt_srsName) { + return new ol.format.ogc.filter.Intersects(geometryName, geometry, opt_srsName); +}; + +/** + * Create a `` operator to test whether a geometry-valued property + * intersects a fixed bounding geometry + * + * @param {!string} geometryName Geometry name to use. + * @param {!ol.geom.Geometry} geometry Geometry. + * @param {string=} opt_srsName SRS name. No srsName attribute will be + * set on geometries when this is not provided. + * @returns {!ol.format.ogc.filter.Within} `` operator. + * @api + */ +ol.format.ogc.filter.within = function(geometryName, geometry, opt_srsName) { + return new ol.format.ogc.filter.Within(geometryName, geometry, opt_srsName); +}; + /** * Creates a `` comparison operator. @@ -376,6 +408,82 @@ ol.format.ogc.filter.Bbox = function(geometryName, extent, opt_srsName) { ol.inherits(ol.format.ogc.filter.Bbox, ol.format.ogc.filter.Filter); +/** + * @classdesc + * Represents a `` operator to test whether a geometry-valued property + * intersects a fixed bounding geometry + * + * @constructor + * @param {!string} geometryName Geometry name to use. + * @param {!ol.geom.Geometry} geometry Geometry. + * @param {string=} opt_srsName SRS name. No srsName attribute will be + * set on geometries when this is not provided. + * @extends {ol.format.ogc.filter.Filter} + * @api + */ +ol.format.ogc.filter.Intersects = function(geometryName, geometry, opt_srsName) { + + ol.format.ogc.filter.Filter.call(this, 'Intersects'); + + /** + * @public + * @type {!string} + */ + this.geometryName = geometryName || 'the_geom'; + + /** + * @public + * @type {ol.geom.Geometry} + */ + this.geometry = geometry; + + /** + * @public + * @type {string|undefined} + */ + this.srsName = opt_srsName; +}; +ol.inherits(ol.format.ogc.filter.Intersects, ol.format.ogc.filter.Filter); + + +/** + * @classdesc + * Represents a `` operator to test whether a geometry-valued property + * within a fixed bounding geometry + * + * @constructor + * @param {!string} geometryName Geometry name to use. + * @param {!ol.geom.Geometry} geometry Geometry. + * @param {string=} opt_srsName SRS name. No srsName attribute will be + * set on geometries when this is not provided. + * @extends {ol.format.ogc.filter.Filter} + * @api + */ +ol.format.ogc.filter.Within = function(geometryName, geometry, opt_srsName) { + + ol.format.ogc.filter.Filter.call(this, 'Within'); + + /** + * @public + * @type {!string} + */ + this.geometryName = geometryName || 'the_geom'; + + /** + * @public + * @type {ol.geom.Geometry} + */ + this.geometry = geometry; + + /** + * @public + * @type {string|undefined} + */ + this.srsName = opt_srsName; +}; +ol.inherits(ol.format.ogc.filter.Within, ol.format.ogc.filter.Filter); + + // Property comparison filters diff --git a/src/ol/format/wfsformat.js b/src/ol/format/wfsformat.js index a9809d9134..16b4db2e37 100644 --- a/src/ol/format/wfsformat.js +++ b/src/ol/format/wfsformat.js @@ -582,6 +582,44 @@ ol.format.WFS.writeBboxFilter_ = function(node, filter, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.format.ogc.filter.Filter} filter Filter. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.WFS.writeIntersectsFilter_ = function(node, filter, objectStack) { + goog.asserts.assertInstanceof(filter, ol.format.ogc.filter.Intersects, + 'must be intersects filter'); + + var context = objectStack[objectStack.length - 1]; + goog.asserts.assert(goog.isObject(context), 'context should be an Object'); + context['srsName'] = filter.srsName; + + ol.format.WFS.writeOgcPropertyName_(node, filter.geometryName); + ol.format.GML3.prototype.writeGeometryElement(node, filter.geometry, objectStack); +}; + + +/** + * @param {Node} node Node. + * @param {ol.format.ogc.filter.Filter} filter Filter. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.WFS.writeWithinFilter_ = function(node, filter, objectStack) { + goog.asserts.assertInstanceof(filter, ol.format.ogc.filter.Within, + 'must be within filter'); + + var context = objectStack[objectStack.length - 1]; + goog.asserts.assert(goog.isObject(context), 'context should be an Object'); + context['srsName'] = filter.srsName; + + ol.format.WFS.writeOgcPropertyName_(node, filter.geometryName); + ol.format.GML3.prototype.writeGeometryElement(node, filter.geometry, objectStack); +}; + + /** * @param {Node} node Node. * @param {ol.format.ogc.filter.Filter} filter Filter. @@ -736,6 +774,8 @@ ol.format.WFS.GETFEATURE_SERIALIZERS_ = { 'Or': ol.xml.makeChildAppender(ol.format.WFS.writeLogicalFilter_), 'Not': ol.xml.makeChildAppender(ol.format.WFS.writeNotFilter_), 'BBOX': ol.xml.makeChildAppender(ol.format.WFS.writeBboxFilter_), + 'Intersects': ol.xml.makeChildAppender(ol.format.WFS.writeIntersectsFilter_), + 'Within': ol.xml.makeChildAppender(ol.format.WFS.writeWithinFilter_), 'PropertyIsEqualTo': ol.xml.makeChildAppender(ol.format.WFS.writeComparisonFilter_), 'PropertyIsNotEqualTo': ol.xml.makeChildAppender(ol.format.WFS.writeComparisonFilter_), 'PropertyIsLessThan': ol.xml.makeChildAppender(ol.format.WFS.writeComparisonFilter_), diff --git a/test/spec/ol/format/wfsformat.test.js b/test/spec/ol/format/wfsformat.test.js index 3460f7a046..713f18345b 100644 --- a/test/spec/ol/format/wfsformat.test.js +++ b/test/spec/ol/format/wfsformat.test.js @@ -504,6 +504,82 @@ describe('ol.format.WFS', function() { expect(serialized.firstElementChild).to.xmleql(ol.xml.parse(text)); }); + it('creates an AND intersects filter', function() { + var text = + '' + + ' ' + + ' ' + + ' the_geom' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 10 20 10 25 15 25 15 20 10 20' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ''; + var f = ol.format.ogc.filter; + var serialized = new ol.format.WFS().writeGetFeature({ + srsName: 'EPSG:4326', + featureTypes: ['area'], + filter: f.intersects( + 'the_geom', + new ol.geom.Polygon([[ + [10, 20], + [10, 25], + [15, 25], + [15, 20], + [10, 20] + ]]) + ) + }); + expect(serialized.firstElementChild).to.xmleql(ol.xml.parse(text)); + }); + + it('creates an AND within filter', function() { + var text = + '' + + ' ' + + ' ' + + ' the_geom' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 10 20 10 25 15 25 15 20 10 20' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ''; + var f = ol.format.ogc.filter; + var serialized = new ol.format.WFS().writeGetFeature({ + srsName: 'EPSG:4326', + featureTypes: ['area'], + filter: f.within( + 'the_geom', + new ol.geom.Polygon([[ + [10, 20], + [10, 25], + [15, 25], + [15, 20], + [10, 20] + ]]) + ) + }); + expect(serialized.firstElementChild).to.xmleql(ol.xml.parse(text)); + }); + }); describe('when writing out a Transaction request', function() {