From 5c536aafc797e537895b96d0d522535e04ba1ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 4 Nov 2015 16:59:53 +0100 Subject: [PATCH] =?UTF-8?q?Add=20a=20writeStyles=20option=20to=20KML=C2=A0?= =?UTF-8?q?format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- externs/olx.js | 11 ++++++++++- src/ol/format/kmlformat.js | 20 +++++++++++++++++--- test/spec/ol/format/kmlformat.test.js | 22 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 36d2bd800e..2f3a1e9265 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1796,7 +1796,8 @@ olx.format.IGCOptions.prototype.altitudeMode; /** * @typedef {{extractStyles: (boolean|undefined), * defaultStyle: (Array.|undefined), - * showPointNames: (boolean|undefined)}} + * showPointNames: (boolean|undefined), + * writeStyles: (boolean|undefined)}} * @api */ olx.format.KMLOptions; @@ -1826,6 +1827,14 @@ olx.format.KMLOptions.prototype.showPointNames; olx.format.KMLOptions.prototype.defaultStyle; +/** + * Write styles into KML. Default is `true`. + * @type {boolean|undefined} + * @api stable + */ +olx.format.KMLOptions.prototype.writeStyles; + + /** * @typedef {{featureNS: (Object.|string|undefined), * featureType: (Array.|string|undefined), diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 7500c77da0..3e1e4c39a8 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -92,6 +92,13 @@ ol.format.KML = function(opt_options) { this.extractStyles_ = options.extractStyles !== undefined ? options.extractStyles : true; + /** + * @private + * @type {boolean} + */ + this.writeStyles_ = options.writeStyles !== undefined ? + options.writeStyles : true; + /** * @private * @type {Object.|string)>} @@ -2169,6 +2176,7 @@ ol.format.KML.writeCoordinatesTextNode_ = * @param {Node} node Node. * @param {Array.} features Features. * @param {Array.<*>} objectStack Object stack. + * @this {ol.format.KML} * @private */ ol.format.KML.writeDocument_ = function(node, features, objectStack) { @@ -2366,6 +2374,7 @@ ol.format.KML.writeBoundaryIs_ = function(node, linearRing, objectStack) { * @param {Node} node Node. * @param {ol.Feature} feature Feature. * @param {Array.<*>} objectStack Object stack. + * @this {ol.format.KML} * @private */ ol.format.KML.writePlacemark_ = function(node, feature, objectStack) { @@ -2378,14 +2387,18 @@ ol.format.KML.writePlacemark_ = function(node, feature, objectStack) { // serialize properties (properties unknown to KML are not serialized) var properties = feature.getProperties(); + var styleFunction = feature.getStyleFunction(); if (styleFunction) { // FIXME the styles returned by the style function are supposed to be // resolution-independent here var styles = styleFunction.call(feature, 0); if (styles && styles.length > 0) { - properties['Style'] = styles[0]; - var textStyle = styles[0].getText(); + var style = styles[0]; + if (this.writeStyles_) { + properties['Style'] = styles[0]; + } + var textStyle = style.getText(); if (textStyle) { properties['name'] = textStyle.getText(); } @@ -2988,6 +3001,7 @@ ol.format.KML.prototype.writeFeaturesNode = function(features, opt_options) { var orderedKeys = ol.format.KML.KML_SEQUENCE_[kml.namespaceURI]; var values = ol.xml.makeSequence(properties, orderedKeys); ol.xml.pushSerializeAndPop(context, ol.format.KML.KML_SERIALIZERS_, - ol.xml.OBJECT_PROPERTY_NODE_FACTORY, values, [opt_options], orderedKeys); + ol.xml.OBJECT_PROPERTY_NODE_FACTORY, values, [opt_options], orderedKeys, + this); return kml; }; diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index e5608800cf..007c53c840 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -1779,6 +1779,28 @@ describe('ol.format.KML', function() { expect(node).to.xmleql(ol.xml.parse(text)); }); + it('does not write styles when writeStyles option is false', function() { + format = new ol.format.KML({writeStyles: false}); + var style = new ol.style.Style({ + image: new ol.style.Icon({ + src: 'http://foo.png' + }) + }); + var feature = new ol.Feature(); + feature.setStyle([style]); + var node = format.writeFeaturesNode([feature]); + var text = + '' + + ' ' + + ' ' + + ''; + expect(node).to.xmleql(ol.xml.parse(text)); + }); + it('can write an feature\'s text style', function() { var style = new ol.style.Style({ text: new ol.style.Text({