Skip to content

Commit

Permalink
Merge pull request openlayers#4378 from elemoine/kml-write-styles
Browse files Browse the repository at this point in the history
Add a writeStyles option to KML format
  • Loading branch information
Éric Lemoine committed Nov 6, 2015
2 parents d68991e + 5c536aa commit d29f3b9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
11 changes: 10 additions & 1 deletion externs/olx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,8 @@ olx.format.IGCOptions.prototype.altitudeMode;
/**
* @typedef {{extractStyles: (boolean|undefined),
* defaultStyle: (Array.<ol.style.Style>|undefined),
* showPointNames: (boolean|undefined)}}
* showPointNames: (boolean|undefined),
* writeStyles: (boolean|undefined)}}
* @api
*/
olx.format.KMLOptions;
Expand Down Expand Up @@ -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, string>|string|undefined),
* featureType: (Array.<string>|string|undefined),
Expand Down
20 changes: 17 additions & 3 deletions src/ol/format/kmlformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, (Array.<ol.style.Style>|string)>}
Expand Down Expand Up @@ -2169,6 +2176,7 @@ ol.format.KML.writeCoordinatesTextNode_ =
* @param {Node} node Node.
* @param {Array.<ol.Feature>} features Features.
* @param {Array.<*>} objectStack Object stack.
* @this {ol.format.KML}
* @private
*/
ol.format.KML.writeDocument_ = function(node, features, objectStack) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
};
22 changes: 22 additions & 0 deletions test/spec/ol/format/kmlformat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
'<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() {
var style = new ol.style.Style({
text: new ol.style.Text({
Expand Down

0 comments on commit d29f3b9

Please sign in to comment.