diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 5663a6b486..bade290338 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -309,6 +309,9 @@ * @property {string} featureNS Feature namespace. * @property {string} featureType Feature type to parse. * @property {string} srsName srsName to use when writing geometries. + * @property {boolean|undefined} surface Write gml:Surface instead of + * gml:Polygon elements. This also affects the elements in multi-part + * geometries. Default is `false“. */ /** diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 27bac21dd6..6e37f63e0e 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -52,6 +52,13 @@ ol.format.GML = function(opt_options) { */ this.srsName_ = options.srsName; + /** + * @private + * @type {boolean} + */ + this.surface_ = goog.isDef(options.surface) ? + options.surface : false; + goog.base(this); }; goog.inherits(ol.format.GML, ol.format.XMLFeature); @@ -1165,6 +1172,26 @@ ol.format.GML.writePolygon_ = function(node, geometry, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.geom.Polygon} geometry Polygon geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writeSurface_ = 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, srsName: srsName}, + ol.format.GML.PATCH_SERIALIZERS_, + ol.xml.makeSimpleNodeFactory('patches'), [geometry], + objectStack); +}; + + /** * @param {Node} node Node. * @param {ol.geom.MultiPolygon} geometry MultiPolygon geometry. @@ -1224,6 +1251,23 @@ ol.format.GML.writeSurfaceMember_ = function(node, polygon, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.geom.Polygon} polygon Polygon geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writeSurfacePatches_ = function(node, polygon, 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.SURFACE_SERIALIZERS_, + ol.xml.makeSimpleNodeFactory('PolygonPatch'), [polygon], []); +}; + + /** * @type {Object.>} * @private @@ -1235,6 +1279,17 @@ ol.format.GML.SURFACEMEMBER_SERIALIZERS_ = { }; +/** + * @type {Object.>} + * @private + */ +ol.format.GML.PATCH_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'patches': ol.xml.makeChildAppender(ol.format.GML.writeSurfacePatches_) + } +}; + + /** * @type {Object.>} * @private @@ -1247,6 +1302,17 @@ ol.format.GML.RING_SERIALIZERS_ = { }; +/** + * @type {Object.>} + * @private + */ +ol.format.GML.SURFACE_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'PolygonPatch': ol.xml.makeChildAppender(ol.format.GML.writePolygon_) + } +}; + + /** * @type {Object.>} * @private @@ -1257,6 +1323,7 @@ ol.format.GML.GEOMETRY_SERIALIZERS_ = { 'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_), 'LinearRing': ol.xml.makeChildAppender(ol.format.GML.writeLinearRing_), 'Polygon': ol.xml.makeChildAppender(ol.format.GML.writePolygon_), + 'Surface': ol.xml.makeChildAppender(ol.format.GML.writeSurface_), 'MultiSurface': ol.xml.makeChildAppender(ol.format.GML.writeMultiSurface_) } }; @@ -1272,6 +1339,9 @@ ol.format.GML.GEOMETRY_SERIALIZERS_ = { */ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack, opt_nodeName) { + var context = objectStack[objectStack.length - 1]; + goog.asserts.assert(goog.isObject(context)); + var surface = goog.object.get(context, 'surface'); goog.asserts.assertInstanceof(value, ol.geom.Geometry); var parentNode = objectStack[objectStack.length - 1].node; goog.asserts.assert(ol.xml.isNode(parentNode)); @@ -1279,6 +1349,9 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack, if (nodeName === 'MultiPolygon') { nodeName = 'MultiSurface'; } + if (nodeName === 'Polygon' && surface === true) { + nodeName = 'Surface'; + } return ol.xml.createElementNS(parentNode.namespaceURI, nodeName); }; @@ -1289,7 +1362,7 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack, */ ol.format.GML.prototype.writeGeometryNode = function(geometry) { var geom = ol.xml.createElementNS('http://www.opengis.net/gml', 'geom'); - var context = {node: geom, srsName: this.srsName_}; + var context = {node: geom, srsName: this.srsName_, surface: this.surface_}; ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ (context), ol.format.GML.GEOMETRY_SERIALIZERS_, ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], []); diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index afc377fc5b..d48e8616b7 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -179,12 +179,12 @@ describe('ol.format.GML', function() { describe('surface', function() { - it('can read a surface geometry', function() { + it('can read and write a surface geometry', function() { var text = '' + ' ' + - ' ' + + ' ' + ' ' + ' ' + ' 1 2 3 2 3 4 1 2' + @@ -208,6 +208,9 @@ describe('ol.format.GML', function() { expect(g.getCoordinates()).to.eql([[[1, 2, 0], [3, 2, 0], [3, 4, 0], [1, 2, 0]], [[2, 3, 0], [2, 5, 0], [4, 5, 0], [2, 3, 0]], [[3, 4, 0], [3, 6, 0], [5, 6, 0], [3, 4, 0]]]); + format = new ol.format.GML({srsName: 'CRS:84', surface: true}); + var serialized = format.writeGeometry(g); + expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text)); }); }); @@ -573,7 +576,7 @@ describe('ol.format.GML', function() { ' ' + ' ' + ' ' + - ' ' + + ' ' + ' ' + ' ' + ' 1 2 3 2 3 4 1 2' + @@ -596,7 +599,7 @@ describe('ol.format.GML', function() { ' ' + ' ' + ' ' + - ' ' + + ' ' + ' ' + ' ' + ' 1 2 3 2 3 4 1 2' +