diff --git a/src/ol/format/gml2.js b/src/ol/format/gml2.js index 32c878ea86..b4a66c7e07 100644 --- a/src/ol/format/gml2.js +++ b/src/ol/format/gml2.js @@ -488,6 +488,17 @@ ol.format.GML2.prototype.getCoords_ = function(point, opt_srsName) { * @private */ ol.format.GML2.prototype.writeMultiCurveOrLineString_ = function(node, geometry, objectStack) { + var context = objectStack[objectStack.length - 1]; + var srsName = context['srsName']; + var curve = context['curve']; + if (srsName) { + node.setAttribute('srsName', srsName); + } + var lines = geometry.getLineStrings(); + ol.xml.pushSerializeAndPop({node: node, srsName: srsName, curve: curve}, + ol.format.GML2.LINESTRINGORCURVEMEMBER_SERIALIZERS_, + this.MULTIGEOMETRY_MEMBER_NODE_FACTORY_, lines, + objectStack, undefined, this); }; @@ -552,6 +563,11 @@ ol.format.GML2.prototype.writePointMember_ = function(node, point, objectStack) * @private */ ol.format.GML2.prototype.writeLineStringOrCurveMember_ = function(node, line, objectStack) { + var child = this.GEOMETRY_NODE_FACTORY_(line, objectStack); + if (child) { + node.appendChild(child); + this.writeCurveOrLineString_(child, line, objectStack); + } }; @@ -648,3 +664,44 @@ ol.format.GML2.POINTMEMBER_SERIALIZERS_ = { ol.format.GML2.prototype.writePointMember_) } }; + + +/** + * @type {Object.>} + * @private + */ +ol.format.GML2.LINESTRINGORCURVEMEMBER_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'lineStringMember': ol.xml.makeChildAppender( + ol.format.GML2.prototype.writeLineStringOrCurveMember_), + 'curveMember': ol.xml.makeChildAppender( + ol.format.GML2.prototype.writeLineStringOrCurveMember_) + } +}; + + +/** + * @const + * @param {*} value Value. + * @param {Array.<*>} objectStack Object stack. + * @param {string=} opt_nodeName Node name. + * @return {Node|undefined} Node. + * @private + */ +ol.format.GML2.prototype.MULTIGEOMETRY_MEMBER_NODE_FACTORY_ = function(value, objectStack, opt_nodeName) { + var parentNode = objectStack[objectStack.length - 1].node; + return ol.xml.createElementNS('http://www.opengis.net/gml', + ol.format.GML2.MULTIGEOMETRY_TO_MEMBER_NODENAME_[parentNode.nodeName]); +}; + +/** + * @const + * @type {Object.} + * @private + */ +ol.format.GML2.MULTIGEOMETRY_TO_MEMBER_NODENAME_ = { + 'MultiLineString': 'lineStringMember', + 'MultiCurve': 'curveMember', + 'MultiPolygon': 'polygonMember', + 'MultiSurface': 'surfaceMember' +}; diff --git a/test/spec/ol/format/gml.test.js b/test/spec/ol/format/gml.test.js index f04615499f..4d0093bddd 100644 --- a/test/spec/ol/format/gml.test.js +++ b/test/spec/ol/format/gml.test.js @@ -254,6 +254,37 @@ describe('ol.format.GML2', function() { expect(node).to.xmleql(ol.xml.parse(expected)); }); + + it('can serialize a Multi Line String', function() { + var expected = + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 2,1.1 4.2,3' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' '; + + var feature = new ol.Feature({ + geometry: new ol.geom.MultiLineString([[[1.1, 2], [3, 4.2]]]) + }); + feature.setId(1); + var objectStack = [{ + featureNS: featureNS, + srsName: 'EPSG:4326' + }]; + format.writeFeatureElement(node, feature, objectStack); + + expect(node).to.xmleql(ol.xml.parse(expected)); + }); }); });