Add a writeStyles option to KML format
This commit is contained in:
@@ -1796,7 +1796,8 @@ olx.format.IGCOptions.prototype.altitudeMode;
|
|||||||
/**
|
/**
|
||||||
* @typedef {{extractStyles: (boolean|undefined),
|
* @typedef {{extractStyles: (boolean|undefined),
|
||||||
* defaultStyle: (Array.<ol.style.Style>|undefined),
|
* defaultStyle: (Array.<ol.style.Style>|undefined),
|
||||||
* showPointNames: (boolean|undefined)}}
|
* showPointNames: (boolean|undefined),
|
||||||
|
* writeStyles: (boolean|undefined)}}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
olx.format.KMLOptions;
|
olx.format.KMLOptions;
|
||||||
@@ -1826,6 +1827,14 @@ olx.format.KMLOptions.prototype.showPointNames;
|
|||||||
olx.format.KMLOptions.prototype.defaultStyle;
|
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, string>|string|undefined),
|
* @typedef {{featureNS: (Object.<string, string>|string|undefined),
|
||||||
* featureType: (Array.<string>|string|undefined),
|
* featureType: (Array.<string>|string|undefined),
|
||||||
|
|||||||
@@ -92,6 +92,13 @@ ol.format.KML = function(opt_options) {
|
|||||||
this.extractStyles_ = options.extractStyles !== undefined ?
|
this.extractStyles_ = options.extractStyles !== undefined ?
|
||||||
options.extractStyles : true;
|
options.extractStyles : true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
this.writeStyles_ = options.writeStyles !== undefined ?
|
||||||
|
options.writeStyles : true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {Object.<string, (Array.<ol.style.Style>|string)>}
|
* @type {Object.<string, (Array.<ol.style.Style>|string)>}
|
||||||
@@ -2169,6 +2176,7 @@ ol.format.KML.writeCoordinatesTextNode_ =
|
|||||||
* @param {Node} node Node.
|
* @param {Node} node Node.
|
||||||
* @param {Array.<ol.Feature>} features Features.
|
* @param {Array.<ol.Feature>} features Features.
|
||||||
* @param {Array.<*>} objectStack Object stack.
|
* @param {Array.<*>} objectStack Object stack.
|
||||||
|
* @this {ol.format.KML}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.format.KML.writeDocument_ = function(node, features, objectStack) {
|
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 {Node} node Node.
|
||||||
* @param {ol.Feature} feature Feature.
|
* @param {ol.Feature} feature Feature.
|
||||||
* @param {Array.<*>} objectStack Object stack.
|
* @param {Array.<*>} objectStack Object stack.
|
||||||
|
* @this {ol.format.KML}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.format.KML.writePlacemark_ = function(node, feature, objectStack) {
|
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)
|
// serialize properties (properties unknown to KML are not serialized)
|
||||||
var properties = feature.getProperties();
|
var properties = feature.getProperties();
|
||||||
|
|
||||||
var styleFunction = feature.getStyleFunction();
|
var styleFunction = feature.getStyleFunction();
|
||||||
if (styleFunction) {
|
if (styleFunction) {
|
||||||
// FIXME the styles returned by the style function are supposed to be
|
// FIXME the styles returned by the style function are supposed to be
|
||||||
// resolution-independent here
|
// resolution-independent here
|
||||||
var styles = styleFunction.call(feature, 0);
|
var styles = styleFunction.call(feature, 0);
|
||||||
if (styles && styles.length > 0) {
|
if (styles && styles.length > 0) {
|
||||||
|
var style = styles[0];
|
||||||
|
if (this.writeStyles_) {
|
||||||
properties['Style'] = styles[0];
|
properties['Style'] = styles[0];
|
||||||
var textStyle = styles[0].getText();
|
}
|
||||||
|
var textStyle = style.getText();
|
||||||
if (textStyle) {
|
if (textStyle) {
|
||||||
properties['name'] = textStyle.getText();
|
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 orderedKeys = ol.format.KML.KML_SEQUENCE_[kml.namespaceURI];
|
||||||
var values = ol.xml.makeSequence(properties, orderedKeys);
|
var values = ol.xml.makeSequence(properties, orderedKeys);
|
||||||
ol.xml.pushSerializeAndPop(context, ol.format.KML.KML_SERIALIZERS_,
|
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;
|
return kml;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1779,6 +1779,28 @@ describe('ol.format.KML', function() {
|
|||||||
expect(node).to.xmleql(ol.xml.parse(text));
|
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 =
|
||||||
|
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
|
||||||
|
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
|
||||||
|
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
|
||||||
|
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
|
||||||
|
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
|
||||||
|
' <Placemark>' +
|
||||||
|
' </Placemark>' +
|
||||||
|
'</kml>';
|
||||||
|
expect(node).to.xmleql(ol.xml.parse(text));
|
||||||
|
});
|
||||||
|
|
||||||
it('can write an feature\'s text style', function() {
|
it('can write an feature\'s text style', function() {
|
||||||
var style = new ol.style.Style({
|
var style = new ol.style.Style({
|
||||||
text: new ol.style.Text({
|
text: new ol.style.Text({
|
||||||
|
|||||||
Reference in New Issue
Block a user