From e479bd142f698e062c59c297fd1dad0503509aa8 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Fri, 21 Feb 2014 14:01:24 +0100 Subject: [PATCH] Add gml:MultiCurve support --- src/ol/format/gmlformat.js | 62 +++++++++++++++++++++++++++ test/spec/ol/format/gmlformat.test.js | 54 +++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 4dc15deff2..dfde2a18c7 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -137,6 +137,28 @@ ol.format.GML.readMultiLineString_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + * @return {ol.geom.MultiLineString|undefined} MultiLineString. + */ +ol.format.GML.readMultiCurve_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'MultiCurve'); + var lineStrings = ol.xml.pushParseAndPop( + /** @type {Array.} */ ([]), + ol.format.GML.MULTICURVE_PARSERS_, node, objectStack); + if (goog.isDefAndNotNull(lineStrings)) { + var multiLineString = new ol.geom.MultiLineString(null); + multiLineString.setLineStrings(lineStrings); + return multiLineString; + } else { + return undefined; + } +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -185,6 +207,19 @@ ol.format.GML.lineStringMemberParser_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + */ +ol.format.GML.curveMemberParser_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'curveMember' || + node.localName == 'curveMembers'); + ol.xml.parse(ol.format.GML.CURVEMEMBER_PARSERS_, node, objectStack); +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -558,6 +593,7 @@ ol.format.GML.GEOMETRY_PARSERS_ = ol.xml.makeParsersNS( 'MultiPolygon': ol.xml.makeArrayPusher(ol.format.GML.readMultiPolygon_), 'Surface': ol.xml.makeArrayPusher(ol.format.GML.readSurface_), 'Curve': ol.xml.makeArrayPusher(ol.format.GML.readCurve_), + 'MultiCurve': ol.xml.makeArrayPusher(ol.format.GML.readMultiCurve_), 'Envelope': ol.xml.makeArrayPusher(ol.format.GML.readEnvelope_) }); @@ -612,6 +648,20 @@ ol.format.GML.MULTILINESTRING_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML.MULTICURVE_PARSERS_ = ol.xml.makeParsersNS( + ol.format.GML.NAMESPACE_URIS_, { + 'curveMember': ol.xml.makeArrayPusher( + ol.format.GML.curveMemberParser_), + 'curveMembers': ol.xml.makeArrayPusher( + ol.format.GML.curveMemberParser_) + }); + + /** * @const * @type {Object.>} @@ -650,6 +700,18 @@ ol.format.GML.LINESTRINGMEMBER_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML.CURVEMEMBER_PARSERS_ = ol.xml.makeParsersNS( + ol.format.GML.NAMESPACE_URIS_, { + 'LineString': ol.xml.makeArrayPusher(ol.format.GML.readLineString_), + 'Curve': ol.xml.makeArrayPusher(ol.format.GML.readCurve_) + }); + + /** * @const * @type {Object.>} diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 8a83c79f71..8a9889535f 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -333,6 +333,60 @@ describe('ol.format.GML', function() { }); + describe('multicurve', function() { + + it('can read a singular multicurve-linestring geometry', function() { + var text = + '' + + ' ' + + ' ' + + ' 1 2 2 3' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 3 4 4 5' + + ' ' + + ' ' + + ''; + var g = format.readGeometry(text); + expect(g).to.be.an(ol.geom.MultiLineString); + expect(g.getCoordinates()).to.eql( + [[[1, 2, 0], [2, 3, 0]], [[3, 4, 0], [4, 5, 0]]]); + }); + + it('can read a singular multicurve-curve geometry', function() { + var text = + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 1 2 2 3' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 3 4 4 5' + + ' ' + + ' ' + + ' ' + + ' ' + + ''; + var g = format.readGeometry(text); + expect(g).to.be.an(ol.geom.MultiLineString); + expect(g.getCoordinates()).to.eql( + [[[1, 2, 0], [2, 3, 0]], [[3, 4, 0], [4, 5, 0]]]); + }); + + }); + }); });