diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 725a738e5a..d0402222cc 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -680,10 +680,14 @@ ol.format.GML.readFlatPosList_ = function(node, objectStack) { } var coords = s.split(/\s+/); // The "dimension" attribute is from the GML 3.0.1 spec. - var dim = parseInt(node.getAttribute('srsDimension') || - node.getAttribute('dimension'), 10) || - (!goog.isNull(containerDimension)) ? - parseInt(containerDimension, 10) : 2; + var dim = 2; + if (!goog.isNull(node.getAttribute('srsDimension'))) { + dim = parseInt(node.getAttribute('srsDimension'), 10); + } else if (!goog.isNull(node.getAttribute('dimension'))) { + dim = parseInt(node.getAttribute('dimension'), 10); + } else if (!goog.isNull(containerDimension)) { + dim = parseInt(containerDimension, 10); + } var x, y, z; var flatCoordinates = []; for (var i = 0, ii = coords.length; i < ii; i += dim) { @@ -997,6 +1001,27 @@ ol.format.GML.writePos_ = function(node, value, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.geom.Point} value Point geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writePosList_ = function(node, value, objectStack) { + // only 2d for simple features profile + var points = value.getCoordinates(); + var len = points.length; + var parts = new Array(len); + var point; + for (var i = 0; i < len; ++i) { + point = points[i]; + // TODO axis orientation + parts[i] = point[0] + ' ' + point[1]; + } + ol.format.XSD.writeStringTextNode(node, parts.join(' ')); +}; + + /** * @const * @param {*} value Value. @@ -1008,14 +1033,25 @@ ol.format.GML.writePos_ = function(node, value, objectStack) { ol.format.GML.POS_NODE_FACTORY_ = ol.xml.makeSimpleNodeFactory('pos'); +/** + * @const + * @param {*} value Value. + * @param {Array.<*>} objectStack Object stack. + * @param {string=} opt_nodeName Node name. + * @return {Node|undefined} Node. + * @private + */ +ol.format.GML.POSLIST_NODE_FACTORY_ = ol.xml.makeSimpleNodeFactory('posList'); + + /** * @type {Object.>} * @private */ -ol.format.GML.POS_SERIALIZERS_ = { +ol.format.GML.FLAT_COORDINATES_SERIALIZERS_ = { 'http://www.opengis.net/gml': { 'pos': ol.xml.makeChildAppender(ol.format.GML.writePos_), - 'pos_': ol.xml.makeChildAppender(ol.format.XSD.writeStringTextNode) + 'posList': ol.xml.makeChildAppender(ol.format.GML.writePosList_) } }; @@ -1033,18 +1069,39 @@ ol.format.GML.writePoint_ = function(node, geometry, objectStack) { if (goog.isDefAndNotNull(srsName)) { node.setAttribute('srsName', srsName); } - ol.xml.pushSerializeAndPop({node: node}, ol.format.GML.POS_SERIALIZERS_, + ol.xml.pushSerializeAndPop({node: node}, + ol.format.GML.FLAT_COORDINATES_SERIALIZERS_, ol.format.GML.POS_NODE_FACTORY_, [geometry], []); }; +/** + * @param {Node} node Node. + * @param {ol.geom.LineString} geometry LineString geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writeLineString_ = function(node, geometry, objectStack) { + var context = objectStack[objectStack.length - 1]; + goog.asserts.assert(goog.isObject(context)); + var srsName = goog.object.get(context, 'srsName'); + if (goog.isDefAndNotNull(srsName)) { + node.setAttribute('srsName', srsName); + } + ol.xml.pushSerializeAndPop({node: node}, + ol.format.GML.FLAT_COORDINATES_SERIALIZERS_, + ol.format.GML.POSLIST_NODE_FACTORY_, [geometry], []); +}; + + /** * @type {Object.>} * @private */ ol.format.GML.GEOMETRY_SERIALIZERS_ = { 'http://www.opengis.net/gml': { - 'Point': ol.xml.makeChildAppender(ol.format.GML.writePoint_) + 'Point': ol.xml.makeChildAppender(ol.format.GML.writePoint_), + 'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_) } }; diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index c21e4b227a..c974ef2276 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -36,7 +36,7 @@ describe('ol.format.GML', function() { describe('linestring', function() { - it('can read a linestring geometry', function() { + it('can read and write a linestring geometry', function() { var text = '' + @@ -45,6 +45,8 @@ describe('ol.format.GML', function() { var g = readGeometry(format, text); expect(g).to.be.an(ol.geom.LineString); expect(g.getCoordinates()).to.eql([[1, 2, 0], [3, 4, 0]]); + var serialized = format.writeGeometry(g); + expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text)); }); });