From aa4d798dc75ac7646bda3f7d314ebda1e231fb04 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Thu, 20 Feb 2014 20:30:04 +0100 Subject: [PATCH] Add gml:MultiPoint parsing --- src/ol/format/gmlformat.js | 57 +++++++++++++++++++++++++++ test/spec/ol/format/gmlformat.test.js | 30 ++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 3da9e476a3..610bc1476f 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -9,6 +9,7 @@ goog.require('ol.extent'); goog.require('ol.format.XML'); goog.require('ol.geom.GeometryCollection'); goog.require('ol.geom.LineString'); +goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon'); goog.require('ol.xml'); @@ -92,6 +93,38 @@ ol.format.GML.readPoint_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + * @return {ol.geom.MultiPoint|undefined} Point. + */ +ol.format.GML.readMultiPoint_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'MultiPoint'); + var coordinates = ol.xml.pushParseAndPop( + /** @type {Array.>} */ ([]), + ol.format.GML.MULTIPOINT_PARSERS_, node, objectStack); + if (goog.isDefAndNotNull(coordinates)) { + return new ol.geom.MultiPoint(coordinates); + } else { + return undefined; + } +}; + + +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + */ +ol.format.GML.pointMemberParser_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'pointMember'); + ol.xml.parse(ol.format.GML.POINTMEMBER_PARSERS_, node, objectStack); +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -443,6 +476,7 @@ ol.format.GML.readFlatPosList_ = function(node, objectStack) { ol.format.GML.GEOMETRY_PARSERS_ = ol.xml.makeParsersNS( ol.format.GML.NAMESPACE_URIS_, { 'Point': ol.xml.makeArrayPusher(ol.format.GML.readPoint_), + 'MultiPoint': ol.xml.makeArrayPusher(ol.format.GML.readMultiPoint_), 'LineString': ol.xml.makeArrayPusher(ol.format.GML.readLineString_), 'LinearRing' : ol.xml.makeArrayPusher(ol.format.GML.readLinearRing_), 'Polygon': ol.xml.makeArrayPusher(ol.format.GML.readPolygon_), @@ -476,6 +510,29 @@ ol.format.GML.FLAT_LINEAR_RINGS_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML.MULTIPOINT_PARSERS_ = ol.xml.makeParsersNS( + ol.format.GML.NAMESPACE_URIS_, { + 'pointMember': ol.xml.makeArrayPusher(ol.format.GML.pointMemberParser_) + }); + + +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML.POINTMEMBER_PARSERS_ = ol.xml.makeParsersNS( + ol.format.GML.NAMESPACE_URIS_, { + 'Point': ol.xml.makeArrayPusher( + ol.format.GML.readFlatCoordinatesFromNode_) + }); + + /** * @const * @type {Object.>} diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 4698b4e009..4d5d5c2fcf 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -153,6 +153,35 @@ describe('ol.format.GML', function() { }); + describe('multipoint', function() { + + it('can read a singular multipoint geometry', function() { + var text = + '' + + ' ' + + ' ' + + ' 1 2' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 2 3' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 3 4' + + ' ' + + ' ' + + ''; + var g = format.readGeometry(text); + expect(g).to.be.an(ol.geom.MultiPoint); + expect(g.getCoordinates()).to.eql([[1, 2, 0], [2, 3, 0], [3, 4, 0]]); + }); + + }); + }); }); @@ -160,5 +189,6 @@ describe('ol.format.GML', function() { goog.require('ol.format.GML'); goog.require('ol.geom.LineString'); +goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon');