From ae1221d81121b9082642281bacebe06879edbd1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 22 Jun 2015 10:11:46 +0200 Subject: [PATCH 1/3] Do not add Style as a feature property MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes the KML format not add a Style property on features, as this may cause problems when serializing to another format such as JSON. --- src/ol/format/kmlformat.js | 129 +++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 54 deletions(-) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 52cdfad2bc..4ce11818e0 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -77,34 +77,13 @@ ol.format.KML = function(opt_options) { */ this.defaultDataProjection = ol.proj.get('EPSG:4326'); - var defaultStyle = goog.isDef(options.defaultStyle) ? + /** + * @private + * @type {Array.} + */ + this.defaultStyle_ = goog.isDef(options.defaultStyle) ? options.defaultStyle : ol.format.KML.DEFAULT_STYLE_ARRAY_; - /** @type {Object.|string)>} */ - var sharedStyles = {}; - - var findStyle = - /** - * @param {Array.|string|undefined} styleValue Style - * value. - * @return {Array.} Style. - */ - function(styleValue) { - if (goog.isArray(styleValue)) { - return styleValue; - } else if (goog.isString(styleValue)) { - // KML files in the wild occasionally forget the leading `#` on styleUrls - // defined in the same document. Add a leading `#` if it enables to find - // a style. - if (!(styleValue in sharedStyles) && ('#' + styleValue in sharedStyles)) { - styleValue = '#' + styleValue; - } - return findStyle(sharedStyles[styleValue]); - } else { - return defaultStyle; - } - }; - /** * @private * @type {boolean} @@ -116,30 +95,7 @@ ol.format.KML = function(opt_options) { * @private * @type {Object.|string)>} */ - this.sharedStyles_ = sharedStyles; - - /** - * @private - * @type {ol.FeatureStyleFunction} - */ - this.featureStyleFunction_ = - /** - * @param {number} resolution Resolution. - * @return {Array.} Style. - * @this {ol.Feature} - */ - function(resolution) { - var style = /** @type {Array.|undefined} */ - (this.get('Style')); - if (goog.isDef(style)) { - return style; - } - var styleUrl = /** @type {string|undefined} */ (this.get('styleUrl')); - if (goog.isDef(styleUrl)) { - return findStyle(styleUrl); - } - return defaultStyle; - }; + this.sharedStyles_ = {}; }; goog.inherits(ol.format.KML, ol.format.XMLFeature); @@ -322,6 +278,57 @@ ol.format.KML.ICON_ANCHOR_UNITS_MAP_ = { }; +/** + * @param {Array.|undefined} style Style. + * @param {string} styleUrl Style URL. + * @param {Array.} defaultStyle Default style. + * @param {Object.|string)>} sharedStyles + * Shared styles. + * @return {ol.FeatureStyleFunction} Feature style function. + * @private + */ +ol.format.KML.createFeatureStyleFunction_ = function( + style, styleUrl, defaultStyle, sharedStyles) { + var findStyle = ( + /** + * @param {Array.|string|undefined} styleValue Style + * value. + * @return {Array.} Style. + */ + function(styleValue) { + if (goog.isArray(styleValue)) { + return styleValue; + } else if (goog.isString(styleValue)) { + // KML files in the wild occasionally forget the leading `#` on + // styleUrls defined in the same document. Add a leading `#` if it + // enables to find a style. + if (!(styleValue in sharedStyles) && + ('#' + styleValue in sharedStyles)) { + styleValue = '#' + styleValue; + } + return findStyle(sharedStyles[styleValue]); + } else { + return defaultStyle; + } + }); + return ( + /** + * @param {number} resolution Resolution. + * @return {Array.} Style. + * @this {ol.Feature} + */ + function(resolution) { + if (goog.isDef(style)) { + return style; + } + if (goog.isDef(styleUrl)) { + return findStyle(styleUrl); + } + return defaultStyle; + }); +}; + + /** * @param {Node} node Node. * @private @@ -1668,13 +1675,27 @@ ol.format.KML.prototype.readPlacemark_ = function(node, objectStack) { feature.setId(id); } var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]); - if (goog.isDefAndNotNull(object.geometry)) { - ol.format.Feature.transformWithOptions(object.geometry, false, options); + + var geometry = goog.object.get(object, 'geometry'); + if (goog.isDefAndNotNull(geometry)) { + ol.format.Feature.transformWithOptions(geometry, false, options); } - feature.setProperties(object); + feature.setGeometry(geometry); + goog.object.remove(object, 'geometry'); + if (this.extractStyles_) { - feature.setStyle(this.featureStyleFunction_); + var style = goog.object.get(object, 'Style'); + var styleUrl = goog.object.get(object, 'styleUrl'); + var styleFunction = ol.format.KML.createFeatureStyleFunction_( + style, styleUrl, this.defaultStyle_, this.sharedStyles_); + feature.setStyle(styleFunction); } + goog.object.remove(object, 'Style'); + // we do not remove the styleUrl property from the object, so it + // gets stored on feature when setProperties is called + + feature.setProperties(object); + return feature; }; From b66a1148734b1f9fcdfefcc8535ace122aaf49aa Mon Sep 17 00:00:00 2001 From: Julien Enselme Date: Mon, 13 Jul 2015 12:00:44 +0200 Subject: [PATCH 2/3] Add test to check that Style is not added a feature property --- test/spec/ol/format/kml/style.kml | 37 +++++++++++++++++++++++++++ test/spec/ol/format/kmlformat.test.js | 26 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 test/spec/ol/format/kml/style.kml diff --git a/test/spec/ol/format/kml/style.kml b/test/spec/ol/format/kml/style.kml new file mode 100644 index 0000000000..fb118786f1 --- /dev/null +++ b/test/spec/ol/format/kml/style.kml @@ -0,0 +1,37 @@ + + + Drawing + + + + 7.282103855735575,47.252335388740185 7.286372432423239,47.23794977574282 7.353199702540651,47.23711986131712 7.287223923345992,47.26352553438413 + + + + + + + + 7.227134983319887,47.25587491183515 7.23801833800725,47.242582453518516 7.260803078150167,47.2619584739595 7.239912047946697,47.265971706882716 7.227134983319887,47.25587491183515 + + + + + + \ No newline at end of file diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index adaaef5c18..f1262746d7 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -2474,6 +2474,31 @@ describe('ol.format.KML', function() { }); + describe('#JSONExport', function() { + + var features; + before(function(done) { + afterLoadText('spec/ol/format/kml/style.kml', function(xml) { + try { + features = format.readFeatures(xml); + } catch (e) { + done(e); + } + done(); + }); + }); + + it('feature must not have a properties property', function() { + var geojsonFormat = new ol.format.GeoJSON(); + features.forEach(function(feature) { + var geojsonFeature = geojsonFormat.writeFeatureObject(feature); + expect(geojsonFeature.properties).to.be(null); + JSON.stringify(geojsonFeature); + }); + }); + + }); + describe('#readName', function() { it('returns undefined if there is no name', function() { @@ -2578,6 +2603,7 @@ describe('ol.format.KML', function() { goog.require('goog.array'); goog.require('goog.dom.xml'); goog.require('ol.Feature'); +goog.require('ol.format.GeoJSON'); goog.require('ol.format.KML'); goog.require('ol.geom.GeometryCollection'); goog.require('ol.geom.GeometryLayout'); From b0584acaa56f4288c3d20fc4bf169313698d96f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Fri, 7 Aug 2015 09:19:09 +0200 Subject: [PATCH 3/3] Add ol.format.KML.findStyle_ function This commit adds an `ol.format.KML.findStyle_` function to prevent creating a function for every call to `ol.format.KML.createFeatureStyleFunction_`, as suggested by @ahocevar in #3900. --- src/ol/format/kmlformat.js | 50 ++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 4ce11818e0..6378660828 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -289,28 +289,6 @@ ol.format.KML.ICON_ANCHOR_UNITS_MAP_ = { */ ol.format.KML.createFeatureStyleFunction_ = function( style, styleUrl, defaultStyle, sharedStyles) { - var findStyle = ( - /** - * @param {Array.|string|undefined} styleValue Style - * value. - * @return {Array.} Style. - */ - function(styleValue) { - if (goog.isArray(styleValue)) { - return styleValue; - } else if (goog.isString(styleValue)) { - // KML files in the wild occasionally forget the leading `#` on - // styleUrls defined in the same document. Add a leading `#` if it - // enables to find a style. - if (!(styleValue in sharedStyles) && - ('#' + styleValue in sharedStyles)) { - styleValue = '#' + styleValue; - } - return findStyle(sharedStyles[styleValue]); - } else { - return defaultStyle; - } - }); return ( /** * @param {number} resolution Resolution. @@ -322,13 +300,39 @@ ol.format.KML.createFeatureStyleFunction_ = function( return style; } if (goog.isDef(styleUrl)) { - return findStyle(styleUrl); + return ol.format.KML.findStyle_(styleUrl, defaultStyle, sharedStyles); } return defaultStyle; }); }; +/** + * @param {Array.|string|undefined} styleValue Style value. + * @param {Array.} defaultStyle Default style. + * @param {Object.|string)>} sharedStyles + * Shared styles. + * @return {Array.} Style. + * @private + */ +ol.format.KML.findStyle_ = function(styleValue, defaultStyle, sharedStyles) { + if (goog.isArray(styleValue)) { + return styleValue; + } else if (goog.isString(styleValue)) { + // KML files in the wild occasionally forget the leading `#` on styleUrls + // defined in the same document. Add a leading `#` if it enables to find + // a style. + if (!(styleValue in sharedStyles) && ('#' + styleValue in sharedStyles)) { + styleValue = '#' + styleValue; + } + return ol.format.KML.findStyle_( + sharedStyles[styleValue], defaultStyle, sharedStyles); + } else { + return defaultStyle; + } +}; + + /** * @param {Node} node Node. * @private