diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index bade290338..97a4e5e9bc 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -312,6 +312,9 @@ * @property {boolean|undefined} surface Write gml:Surface instead of * gml:Polygon elements. This also affects the elements in multi-part * geometries. Default is `false“. + * @property {boolean|undefined} curve Write gml:Curve instead of + * gml:LineString 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 6e37f63e0e..ff22bd19f2 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -59,6 +59,13 @@ ol.format.GML = function(opt_options) { this.surface_ = goog.isDef(options.surface) ? options.surface : false; + /** + * @private + * @type {boolean} + */ + this.curve_ = goog.isDef(options.curve) ? + options.curve : false; + goog.base(this); }; goog.inherits(ol.format.GML, ol.format.XMLFeature); @@ -1114,7 +1121,8 @@ 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)) { + var writeSrsName = goog.object.get(context, 'writeSrsName'); + if (goog.isDefAndNotNull(srsName) && writeSrsName !== false) { node.setAttribute('srsName', srsName); } ol.xml.pushSerializeAndPop({node: node, srsName: srsName}, @@ -1192,6 +1200,26 @@ ol.format.GML.writeSurface_ = function(node, geometry, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.geom.LineString} geometry LineString geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writeCurve_ = 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.SEGMENT_SERIALIZERS_, + ol.xml.makeSimpleNodeFactory('segments'), [geometry], + objectStack); +}; + + /** * @param {Node} node Node. * @param {ol.geom.MultiPolygon} geometry MultiPolygon geometry. @@ -1268,6 +1296,23 @@ ol.format.GML.writeSurfacePatches_ = function(node, polygon, objectStack) { }; +/** + * @param {Node} node Node. + * @param {ol.geom.LineString} line LineString geometry. + * @param {Array.<*>} objectStack Node stack. + * @private + */ +ol.format.GML.writeCurveSegments_ = function(node, line, 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.CURVE_SERIALIZERS_, + ol.xml.makeSimpleNodeFactory('LineStringSegment'), [line], []); +}; + + /** * @type {Object.>} * @private @@ -1290,6 +1335,17 @@ ol.format.GML.PATCH_SERIALIZERS_ = { }; +/** + * @type {Object.>} + * @private + */ +ol.format.GML.SEGMENT_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'segments': ol.xml.makeChildAppender(ol.format.GML.writeCurveSegments_) + } +}; + + /** * @type {Object.>} * @private @@ -1313,12 +1369,25 @@ ol.format.GML.SURFACE_SERIALIZERS_ = { }; +/** + * @type {Object.>} + * @private + */ +ol.format.GML.CURVE_SERIALIZERS_ = { + 'http://www.opengis.net/gml': { + 'LineStringSegment': ol.xml.makeChildAppender( + ol.format.GML.writeLineString_) + } +}; + + /** * @type {Object.>} * @private */ 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_), 'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_), 'LinearRing': ol.xml.makeChildAppender(ol.format.GML.writeLinearRing_), @@ -1342,6 +1411,7 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack, var context = objectStack[objectStack.length - 1]; goog.asserts.assert(goog.isObject(context)); var surface = goog.object.get(context, 'surface'); + var curve = goog.object.get(context, 'curve'); goog.asserts.assertInstanceof(value, ol.geom.Geometry); var parentNode = objectStack[objectStack.length - 1].node; goog.asserts.assert(ol.xml.isNode(parentNode)); @@ -1352,6 +1422,9 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack, if (nodeName === 'Polygon' && surface === true) { nodeName = 'Surface'; } + if (nodeName === 'LineString' && curve === true) { + nodeName = 'Curve'; + } return ol.xml.createElementNS(parentNode.namespaceURI, nodeName); }; @@ -1362,7 +1435,8 @@ 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_, surface: this.surface_}; + var context = {node: geom, srsName: this.srsName_, + curve: this.curve_, 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 d48e8616b7..6d824ee1df 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -217,7 +217,7 @@ describe('ol.format.GML', function() { describe('curve', function() { - it('can read a curve geometry', function() { + it('can read and write a curve geometry', function() { var text = '' + @@ -230,6 +230,9 @@ 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]]); + format = new ol.format.GML({srsName: 'CRS:84', curve: true}); + var serialized = format.writeGeometry(g); + expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text)); }); });