From 3a7905f2fd74e624f6795a89f622f34c537dd1f9 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Fri, 19 Dec 2014 13:07:40 +0100 Subject: [PATCH] Support multiple feature types in GML format --- externs/olx.js | 12 ++--- src/ol/format/gml/gmlbaseformat.js | 15 ++++--- .../spec/ol/format/gml/multiple-typenames.xml | 44 +++++++++++++++++++ test/spec/ol/format/gmlformat.test.js | 43 ++++++++++++++++++ 4 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 test/spec/ol/format/gml/multiple-typenames.xml diff --git a/externs/olx.js b/externs/olx.js index 57db7a97c9..25dcf58be3 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1679,8 +1679,8 @@ olx.format.KMLOptions.prototype.defaultStyle; /** - * @typedef {{featureNS: string, - * featureType: string, + * @typedef {{featureNS: (string|undefined), + * featureType: (Array.|string|undefined), * srsName: string, * surface: (boolean|undefined), * curve: (boolean|undefined), @@ -1693,16 +1693,16 @@ olx.format.GMLOptions; /** - * Feature namespace. - * @type {string} + * Feature namespace. If not defined will be derived from GML. + * @type {string|undefined} * @api stable */ olx.format.GMLOptions.prototype.featureNS; /** - * Feature type to parse. - * @type {string} + * Feature type(s) to parse. + * @type {Array.|string|undefined} * @api stable */ olx.format.GMLOptions.prototype.featureType; diff --git a/src/ol/format/gml/gmlbaseformat.js b/src/ol/format/gml/gmlbaseformat.js index 771d526b2f..94320bfc97 100644 --- a/src/ol/format/gml/gmlbaseformat.js +++ b/src/ol/format/gml/gmlbaseformat.js @@ -45,13 +45,13 @@ ol.format.GMLBase = function(opt_options) { /** * @protected - * @type {string} + * @type {Array.|string|undefined} */ this.featureType = options.featureType; /** * @protected - * @type {string} + * @type {string|undefined} */ this.featureNS = options.featureNS; @@ -115,10 +115,13 @@ ol.format.GMLBase.prototype.readFeaturesInternal = function(node, objectStack) { } var parsers = {}; var parsersNS = {}; - parsers[featureType] = (localName == 'featureMembers') ? - ol.xml.makeArrayPusher(this.readFeatureElement, this) : - ol.xml.makeReplacer(this.readFeatureElement, this); - parsersNS[context['featureNS']] = parsers; + var featureTypes = goog.isArray(featureType) ? featureType : [featureType]; + for (var i = 0, ii = featureTypes.length; i < ii; ++i) { + parsers[featureTypes[i]] = (localName == 'featureMembers') ? + ol.xml.makeArrayPusher(this.readFeatureElement, this) : + ol.xml.makeReplacer(this.readFeatureElement, this); + } + parsersNS[goog.object.get(context, 'featureNS')] = parsers; features = ol.xml.pushParseAndPop([], parsersNS, node, objectStack); } if (!goog.isDef(features)) { diff --git a/test/spec/ol/format/gml/multiple-typenames.xml b/test/spec/ol/format/gml/multiple-typenames.xml new file mode 100644 index 0000000000..f841e5dbef --- /dev/null +++ b/test/spec/ol/format/gml/multiple-typenames.xml @@ -0,0 +1,44 @@ + + + + 3822829 + + + 3820888 + + + 296916318 + + + 37244 + + + 1641478 + + + 1244004 + + + 22259 + + + 1606103 + + + 3217145 + + + 3228576 + + + 936994 + + + 936990 + + + diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index d68aad6d27..45523a67b1 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -1107,6 +1107,49 @@ describe('ol.format.GML3', function() { }); + describe('when parsing multiple feature types', function() { + + var features; + before(function(done) { + afterLoadText('spec/ol/format/gml/multiple-typenames.xml', function(xml) { + try { + features = new ol.format.GML({ + featureNS: 'http://localhost:8080/official', + featureType: ['planet_osm_polygon', 'planet_osm_line'] + }).readFeatures(xml); + } catch (e) { + done(e); + } + done(); + }); + }); + + it('reads all features', function() { + expect(features.length).to.be(12); + }); + + }); + + describe('when parsing multiple feature types', function() { + + var features; + before(function(done) { + afterLoadText('spec/ol/format/gml/multiple-typenames.xml', function(xml) { + try { + features = new ol.format.GML().readFeatures(xml); + } catch (e) { + done(e); + } + done(); + }); + }); + + it('reads all features with autoconfigure', function() { + expect(features.length).to.be(12); + }); + + }); + });