diff --git a/src/ol/format/gml2.js b/src/ol/format/gml2.js index b4a66c7e07..1d7334b48a 100644 --- a/src/ol/format/gml2.js +++ b/src/ol/format/gml2.js @@ -596,6 +596,33 @@ ol.format.GML2.prototype.writeLinearRing_ = function(node, geometry, objectStack * @private */ ol.format.GML2.prototype.writeMultiSurfaceOrPolygon_ = function(node, geometry, objectStack) { + var context = objectStack[objectStack.length - 1]; + var srsName = context['srsName']; + var surface = context['surface']; + if (srsName) { + node.setAttribute('srsName', srsName); + } + var polygons = geometry.getPolygons(); + ol.xml.pushSerializeAndPop({node: node, srsName: srsName, surface: surface}, + ol.format.GML2.SURFACEORPOLYGONMEMBER_SERIALIZERS_, + this.MULTIGEOMETRY_MEMBER_NODE_FACTORY_, polygons, + objectStack, undefined, this); +}; + + +/** + * @param {Node} node Node. + * @param {ol.geom.Polygon} polygon Polygon geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML2.prototype.writeSurfaceOrPolygonMember_ = function(node, polygon, objectStack) { + var child = this.GEOMETRY_NODE_FACTORY_( + polygon, objectStack); + if (child) { + node.appendChild(child); + this.writeSurfaceOrPolygon_(child, polygon, objectStack); + } }; @@ -603,6 +630,7 @@ ol.format.GML2.prototype.writeMultiSurfaceOrPolygon_ = function(node, geometry, * @param {Node} node Node. * @param {ol.Extent} extent Extent. * @param {Array.<*>} objectStack Node stack. + * @private */ ol.format.GML2.prototype.writeEnvelope = function(node, extent, objectStack) { }; @@ -705,3 +733,18 @@ ol.format.GML2.MULTIGEOMETRY_TO_MEMBER_NODENAME_ = { 'MultiPolygon': 'polygonMember', 'MultiSurface': 'surfaceMember' }; + + +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GML2.SURFACEORPOLYGONMEMBER_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'surfaceMember': ol.xml.makeChildAppender( + ol.format.GML2.prototype.writeSurfaceOrPolygonMember_), + 'polygonMember': ol.xml.makeChildAppender( + ol.format.GML2.prototype.writeSurfaceOrPolygonMember_) + } +}; diff --git a/test/spec/ol/format/gml.test.js b/test/spec/ol/format/gml.test.js index 4d0093bddd..f5a9dce525 100644 --- a/test/spec/ol/format/gml.test.js +++ b/test/spec/ol/format/gml.test.js @@ -285,6 +285,41 @@ describe('ol.format.GML2', function() { expect(node).to.xmleql(ol.xml.parse(expected)); }); + + it('can serialize a Multi Polygon', function() { + var expected = + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' 2,1.1 4.2,3 6,5.2' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' '; + + var feature = new ol.Feature({ + geometry: new ol.geom.MultiPolygon([[[[1.1, 2], [3, 4.2], [5.2, 6]]]]) + }); + feature.setId(1); + var objectStack = [{ + featureNS: featureNS, + srsName: 'EPSG:4326' + }]; + format.writeFeatureElement(node, feature, objectStack); + + expect(node).to.xmleql(ol.xml.parse(expected)); + }); }); });