Do not add Style as a feature property
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.
This commit is contained in:
@@ -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.<ol.style.Style>}
|
||||
*/
|
||||
this.defaultStyle_ = goog.isDef(options.defaultStyle) ?
|
||||
options.defaultStyle : ol.format.KML.DEFAULT_STYLE_ARRAY_;
|
||||
|
||||
/** @type {Object.<string, (Array.<ol.style.Style>|string)>} */
|
||||
var sharedStyles = {};
|
||||
|
||||
var findStyle =
|
||||
/**
|
||||
* @param {Array.<ol.style.Style>|string|undefined} styleValue Style
|
||||
* value.
|
||||
* @return {Array.<ol.style.Style>} 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, (Array.<ol.style.Style>|string)>}
|
||||
*/
|
||||
this.sharedStyles_ = sharedStyles;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.FeatureStyleFunction}
|
||||
*/
|
||||
this.featureStyleFunction_ =
|
||||
/**
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {Array.<ol.style.Style>} Style.
|
||||
* @this {ol.Feature}
|
||||
*/
|
||||
function(resolution) {
|
||||
var style = /** @type {Array.<ol.style.Style>|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.<ol.style.Style>|undefined} style Style.
|
||||
* @param {string} styleUrl Style URL.
|
||||
* @param {Array.<ol.style.Style>} defaultStyle Default style.
|
||||
* @param {Object.<string, (Array.<ol.style.Style>|string)>} sharedStyles
|
||||
* Shared styles.
|
||||
* @return {ol.FeatureStyleFunction} Feature style function.
|
||||
* @private
|
||||
*/
|
||||
ol.format.KML.createFeatureStyleFunction_ = function(
|
||||
style, styleUrl, defaultStyle, sharedStyles) {
|
||||
var findStyle = (
|
||||
/**
|
||||
* @param {Array.<ol.style.Style>|string|undefined} styleValue Style
|
||||
* value.
|
||||
* @return {Array.<ol.style.Style>} 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.<ol.style.Style>} 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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user