From fcf9649ef96db21276a50c8fa89aa4fc916e2c76 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 5 Apr 2017 09:46:33 +0200 Subject: [PATCH] Add ol.format.filter.during --- src/ol/format/filter.js | 15 ++++++++++ src/ol/format/filter/during.js | 33 ++++++++++++++++++++++ src/ol/format/wfs.js | 49 +++++++++++++++++++++++++++++++++ test/spec/ol/format/wfs.test.js | 31 +++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 src/ol/format/filter/during.js diff --git a/src/ol/format/filter.js b/src/ol/format/filter.js index 08bf1d6af1..6d67dc6cb8 100644 --- a/src/ol/format/filter.js +++ b/src/ol/format/filter.js @@ -2,6 +2,7 @@ goog.provide('ol.format.filter'); goog.require('ol.format.filter.And'); goog.require('ol.format.filter.Bbox'); +goog.require('ol.format.filter.During'); goog.require('ol.format.filter.EqualTo'); goog.require('ol.format.filter.GreaterThan'); goog.require('ol.format.filter.GreaterThanOrEqualTo'); @@ -230,3 +231,17 @@ ol.format.filter.like = function(propertyName, pattern, return new ol.format.filter.IsLike(propertyName, pattern, opt_wildCard, opt_singleChar, opt_escapeChar, opt_matchCase); }; + + +/** + * Create a `` temporal operator. + * + * @param {!string} propertyName Name of the context property to compare. + * @param {!string} begin The begin date in ISO-8601 format. + * @param {!string} end The end date in ISO-8601 format. + * @returns {!ol.format.filter.During} `` operator. + * @api + */ +ol.format.filter.during = function(propertyName, begin, end) { + return new ol.format.filter.During(propertyName, begin, end); +}; diff --git a/src/ol/format/filter/during.js b/src/ol/format/filter/during.js new file mode 100644 index 0000000000..81c70c889f --- /dev/null +++ b/src/ol/format/filter/during.js @@ -0,0 +1,33 @@ +goog.provide('ol.format.filter.During'); + +goog.require('ol'); +goog.require('ol.format.filter.Comparison'); + + +/** + * @classdesc + * Represents a `` comparison operator. + * + * @constructor + * @param {!string} propertyName Name of the context property to compare. + * @param {!string} begin The begin date in ISO-8601 format. + * @param {!string} end The end date in ISO-8601 format. + * @extends {ol.format.filter.Comparison} + * @api + */ +ol.format.filter.During = function(propertyName, begin, end) { + ol.format.filter.Comparison.call(this, 'During', propertyName); + + /** + * @public + * @type {!string} + */ + this.begin = begin; + + /** + * @public + * @type {!string} + */ + this.end = end; +}; +ol.inherits(ol.format.filter.During, ol.format.filter.Comparison); diff --git a/src/ol/format/wfs.js b/src/ol/format/wfs.js index 9f3780dab0..098021b943 100644 --- a/src/ol/format/wfs.js +++ b/src/ol/format/wfs.js @@ -90,6 +90,13 @@ ol.format.WFS.OGCNS = 'http://www.opengis.net/ogc'; ol.format.WFS.WFSNS = 'http://www.opengis.net/wfs'; +/** + * @const + * @type {string} + */ +ol.format.WFS.FESNS = 'http://www.opengis.net/fes'; + + /** * @const * @type {Object.} @@ -646,6 +653,32 @@ ol.format.WFS.writeWithinFilter_ = function(node, filter, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.format.filter.During} filter Filter. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.WFS.writeDuringFilter_ = function(node, filter, objectStack) { + + var valueReference = ol.xml.createElementNS(ol.format.WFS.FESNS, 'ValueReference'); + ol.format.XSD.writeStringTextNode(valueReference, filter.propertyName); + node.appendChild(valueReference); + + var timePeriod = ol.xml.createElementNS(ol.format.GMLBase.GMLNS, 'TimePeriod'); + + node.appendChild(timePeriod); + + var begin = ol.xml.createElementNS(ol.format.GMLBase.GMLNS, 'begin'); + timePeriod.appendChild(begin); + ol.format.WFS.writeTimeInstant_(begin, filter.begin); + + var end = ol.xml.createElementNS(ol.format.GMLBase.GMLNS, 'end'); + timePeriod.appendChild(end); + ol.format.WFS.writeTimeInstant_(end, filter.end); +}; + + /** * @param {Node} node Node. * @param {ol.format.filter.LogicalNary} filter Filter. @@ -777,6 +810,21 @@ ol.format.WFS.writeOgcLiteral_ = function(node, value) { }; +/** + * @param {Node} node Node. + * @param {string} time PropertyName value. + * @private + */ +ol.format.WFS.writeTimeInstant_ = function(node, time) { + var timeInstant = ol.xml.createElementNS(ol.format.GMLBase.GMLNS, 'TimeInstant'); + node.appendChild(timeInstant); + + var timePosition = ol.xml.createElementNS(ol.format.GMLBase.GMLNS, 'timePosition'); + timeInstant.appendChild(timePosition); + ol.format.XSD.writeStringTextNode(timePosition, time); +}; + + /** * @type {Object.>} * @private @@ -786,6 +834,7 @@ ol.format.WFS.GETFEATURE_SERIALIZERS_ = { 'Query': ol.xml.makeChildAppender(ol.format.WFS.writeQuery_) }, 'http://www.opengis.net/ogc': { + 'During': ol.xml.makeChildAppender(ol.format.WFS.writeDuringFilter_), 'And': ol.xml.makeChildAppender(ol.format.WFS.writeLogicalFilter_), 'Or': ol.xml.makeChildAppender(ol.format.WFS.writeLogicalFilter_), 'Not': ol.xml.makeChildAppender(ol.format.WFS.writeNotFilter_), diff --git a/test/spec/ol/format/wfs.test.js b/test/spec/ol/format/wfs.test.js index aa0ae06a30..55d3ad751a 100644 --- a/test/spec/ol/format/wfs.test.js +++ b/test/spec/ol/format/wfs.test.js @@ -587,6 +587,37 @@ describe('ol.format.WFS', function() { expect(serialized.firstElementChild).to.xmleql(ol.xml.parse(text)); }); + it('creates During property filter', function() { + var text = + '' + + ' ' + + ' ' + + ' date_prop' + + ' ' + + ' ' + + ' ' + + ' 2010-01-20T00:00:00Z' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 2012-12-31T00:00:00Z' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ''; + + var serialized = new ol.format.WFS().writeGetFeature({ + srsName: 'EPSG:4326', + featureTypes: ['states'], + filter: ol.format.filter.during('date_prop', '2010-01-20T00:00:00Z', '2012-12-31T00:00:00Z') + }); + expect(serialized.firstElementChild).to.xmleql(ol.xml.parse(text)); + }); + }); describe('when writing out a Transaction request', function() {