From 85fe1bf737b3517c25001308ba84154c558cc6f7 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Thu, 20 Feb 2014 13:09:10 +0100 Subject: [PATCH] Add parsing for gml:Curve --- src/ol/format/gmlformat.js | 88 ++++++++++++++++++++++++++- test/spec/ol/format/gmlformat.test.js | 19 ++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 78848b537e..0dd50c8226 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -132,6 +132,26 @@ ol.format.GML.patchesParser_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + * @return {Array.} flat coordinates. + */ +ol.format.GML.segmentsParser_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'segments'); + var result = ol.xml.pushParseAndPop( + /** @type {Array.} */ ([null]), + ol.format.GML.SEGMENTS_PARSERS_, node, objectStack); + if (!goog.isDef(result)) { + return null; + } else { + return result; + } +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -152,6 +172,26 @@ ol.format.GML.polygonPatchParser_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + * @return {Array.} flat coordinates. + */ +ol.format.GML.lineStringSegmentParser_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'LineStringSegment'); + var result = ol.xml.pushParseAndPop( + /** @type {Array.} */ ([null]), + ol.format.GML.GEOMETRY_FLAT_COORDINATES_PARSERS_, node, objectStack); + if (!goog.isDef(result)) { + return null; + } else { + return result; + } +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -292,6 +332,28 @@ ol.format.GML.readSurface_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + * @return {ol.geom.LineString|undefined} LineString. + */ +ol.format.GML.readCurve_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'Curve'); + var flatCoordinates = ol.xml.pushParseAndPop( + /** @type {Array.} */ ([null]), + ol.format.GML.CURVE_PARSERS_, node, objectStack); + if (goog.isDefAndNotNull(flatCoordinates)) { + var lineString = new ol.geom.LineString(null); + lineString.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates); + return lineString; + } else { + return undefined; + } +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -360,7 +422,8 @@ ol.format.GML.GEOMETRY_PARSERS_ = ol.xml.makeParsersNS( 'LineString': ol.xml.makeArrayPusher(ol.format.GML.readLineString_), 'LinearRing' : ol.xml.makeArrayPusher(ol.format.GML.readLinearRing_), 'Polygon': ol.xml.makeArrayPusher(ol.format.GML.readPolygon_), - 'Surface': ol.xml.makeArrayPusher(ol.format.GML.readSurface_) + 'Surface': ol.xml.makeArrayPusher(ol.format.GML.readSurface_), + 'Curve': ol.xml.makeArrayPusher(ol.format.GML.readCurve_) }); @@ -399,6 +462,17 @@ ol.format.GML.SURFACE_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML.CURVE_PARSERS_ = ol.xml.makeParsersNS( + ol.format.GML.NAMESPACE_URIS_, { + 'segments': ol.xml.makeReplacer(ol.format.GML.segmentsParser_) + }); + + /** * @const * @type {Object.>} @@ -410,6 +484,18 @@ ol.format.GML.PATCHES_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML.SEGMENTS_PARSERS_ = ol.xml.makeParsersNS( + ol.format.GML.NAMESPACE_URIS_, { + 'LineStringSegment': ol.xml.makeReplacer( + ol.format.GML.lineStringSegmentParser_) + }); + + /** * @const * @type {Object.>} diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 8f19474913..7f7a5ce622 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -104,6 +104,25 @@ describe('ol.format.GML', function() { }); + describe('curve', function() { + + it('can read a curve geometry', function() { + var text = + '' + + ' ' + + ' ' + + ' 1 2 3 4' + + ' ' + + ' ' + + ''; + var g = format.readGeometry(text); + expect(g).to.be.an(ol.geom.LineString); + expect(g.getCoordinates()).to.eql([[1, 2, 0], [3, 4, 0]]); + }); + + }); + }); });