From a79f8442c7d4a1fd180aa2ce0251d7ed11f1c34e Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Thu, 27 Feb 2014 21:05:59 +0100 Subject: [PATCH] Add write support for gml:MultiPoint --- src/ol/format/gmlformat.js | 56 ++++++++++++++++++++++++++- test/spec/ol/format/gmlformat.test.js | 4 +- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index ff22bd19f2..6c1a3cb112 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -1102,7 +1102,8 @@ ol.format.GML.writePoint_ = 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)) { + var writeSrsName = goog.object.get(context, 'writeSrsName'); + if (goog.isDefAndNotNull(srsName) && writeSrsName !== false) { node.setAttribute('srsName', srsName); } ol.xml.pushSerializeAndPop({node: node, srsName: srsName}, @@ -1244,6 +1245,30 @@ ol.format.GML.writeMultiSurface_ = function(node, geometry, }; +/** + * @param {Node} node Node. + * @param {ol.geom.MultiPoint} geometry MultiPoint geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writeMultiPoint_ = 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); + } + var points = geometry.getPoints(); + for (var i = 0, ii = points.length; i < ii; ++i) { + ol.xml.pushSerializeAndPop({node: node, srsName: srsName}, + ol.format.GML.POINTMEMBER_SERIALIZERS_, + ol.xml.makeSimpleNodeFactory('pointMember'), [points[i]], + objectStack); + } +}; + + /** * @param {Node} node Node. * @param {ol.geom.LinearRing} ring LinearRing geometry. @@ -1279,6 +1304,23 @@ ol.format.GML.writeSurfaceMember_ = function(node, polygon, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.geom.Point} point Point geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writePointMember_ = function(node, point, objectStack) { + var context = objectStack[objectStack.length - 1]; + goog.asserts.assert(goog.isObject(context)); + var srsName = goog.object.get(context, 'srsName'); + ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ + ({node: node, srsName: srsName, writeSrsName: false}), + ol.format.GML.GEOMETRY_SERIALIZERS_, + ol.format.GML.GEOMETRY_NODE_FACTORY_, [point], []); +}; + + /** * @param {Node} node Node. * @param {ol.geom.Polygon} polygon Polygon geometry. @@ -1324,6 +1366,17 @@ ol.format.GML.SURFACEMEMBER_SERIALIZERS_ = { }; +/** + * @type {Object.>} + * @private + */ +ol.format.GML.POINTMEMBER_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'pointMember': ol.xml.makeChildAppender(ol.format.GML.writePointMember_) + } +}; + + /** * @type {Object.>} * @private @@ -1389,6 +1442,7 @@ ol.format.GML.GEOMETRY_SERIALIZERS_ = { 'http://www.opengis.net/gml': { 'Curve': ol.xml.makeChildAppender(ol.format.GML.writeCurve_), 'Point': ol.xml.makeChildAppender(ol.format.GML.writePoint_), + 'MultiPoint': ol.xml.makeChildAppender(ol.format.GML.writeMultiPoint_), 'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_), 'LinearRing': ol.xml.makeChildAppender(ol.format.GML.writeLinearRing_), 'Polygon': ol.xml.makeChildAppender(ol.format.GML.writePolygon_), diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 6d824ee1df..8a1bfdc1ee 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -254,7 +254,7 @@ describe('ol.format.GML', function() { describe('multipoint', function() { - it('can read a singular multipoint geometry', function() { + it('can read and write a singular multipoint geometry', function() { var text = '' + @@ -277,6 +277,8 @@ describe('ol.format.GML', function() { var g = readGeometry(format, text); expect(g).to.be.an(ol.geom.MultiPoint); expect(g.getCoordinates()).to.eql([[1, 2, 0], [2, 3, 0], [3, 4, 0]]); + var serialized = format.writeGeometry(g); + expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text)); }); it('can read a plural multipoint geometry', function() {