Merge pull request #5745 from tamarmot/4316_goog_object_clone_cleanup

removed dependencies on goog.object.clone and fixed placemark rendering
This commit is contained in:
Andreas Hocevar
2016-08-28 12:16:45 +02:00
committed by GitHub
2 changed files with 75 additions and 42 deletions
+72 -39
View File
@@ -5,7 +5,6 @@
goog.provide('ol.format.KML'); goog.provide('ol.format.KML');
goog.require('goog.object');
goog.require('ol'); goog.require('ol');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.array'); goog.require('ol.array');
@@ -25,7 +24,6 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.math'); goog.require('ol.math');
goog.require('ol.obj');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
goog.require('ol.style.Icon'); goog.require('ol.style.Icon');
@@ -223,6 +221,13 @@ ol.format.KML.createStyleDefaults_ = function() {
src: ol.format.KML.DEFAULT_IMAGE_STYLE_SRC_ src: ol.format.KML.DEFAULT_IMAGE_STYLE_SRC_
}); });
/**
* @const
* @type {string}
* @private
*/
ol.format.KML.DEFAULT_NO_IMAGE_STYLE_ = 'NO_IMAGE';
/** /**
* @const * @const
* @type {ol.style.Stroke} * @type {ol.style.Stroke}
@@ -302,27 +307,45 @@ ol.format.KML.createNameStyleFunction_ = function(foundStyle, name) {
var textAlign = 'start'; var textAlign = 'start';
if (foundStyle.getImage()) { if (foundStyle.getImage()) {
var imageSize = foundStyle.getImage().getImageSize(); var imageSize = foundStyle.getImage().getImageSize();
if (imageSize && imageSize.length == 2) { if (imageSize === null) {
imageSize = ol.format.KML.DEFAULT_IMAGE_STYLE_SIZE_;
}
var imageScale = foundStyle.getImage().getScale();
if (isNaN(imageScale)) {
imageScale = ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_;
}
if (imageSize.length == 2) {
// Offset the label to be centered to the right of the icon, if there is // Offset the label to be centered to the right of the icon, if there is
// one. // one.
textOffset[0] = foundStyle.getImage().getScale() * imageSize[0] / 2; textOffset[0] = imageScale * imageSize[0] / 2;
textOffset[1] = -foundStyle.getImage().getScale() * imageSize[1] / 2; textOffset[1] = -imageScale * imageSize[1] / 2;
textAlign = 'left'; textAlign = 'left';
} }
} }
if (!ol.obj.isEmpty(foundStyle.getText())) { if (foundStyle.getText() !== null) {
textStyle = /** @type {ol.style.Text} */ // clone the text style, customizing it with name, alignments and offset.
(goog.object.clone(foundStyle.getText())); // Note that kml does not support many text options that OpenLayers does (rotation, textBaseline).
textStyle.setText(name); var foundText = foundStyle.getText();
textStyle.setTextAlign(textAlign); textStyle = new ol.style.Text({
textStyle.setOffsetX(textOffset[0]); text: name,
textStyle.setOffsetY(textOffset[1]); textAlign: textAlign,
offsetX: textOffset[0],
offsetY: textOffset[1],
font: foundText.getFont() || ol.format.KML.DEFAULT_TEXT_STYLE_.getFont(),
scale: foundText.getScale() || ol.format.KML.DEFAULT_TEXT_STYLE_.getScale(),
fill: foundText.getFill() || ol.format.KML.DEFAULT_TEXT_STYLE_.getFill(),
stroke: foundText.getStroke() || ol.format.KML.DEFAULT_TEXT_STROKE_STYLE_
});
} else { } else {
textStyle = new ol.style.Text({ textStyle = new ol.style.Text({
text: name, text: name,
offsetX: textOffset[0], offsetX: textOffset[0],
offsetY: textOffset[1], offsetY: textOffset[1],
textAlign: textAlign textAlign: textAlign,
font: ol.format.KML.DEFAULT_TEXT_STYLE_.getFont(),
scale: ol.format.KML.DEFAULT_TEXT_STYLE_.getScale(),
fill: ol.format.KML.DEFAULT_TEXT_STYLE_.getFill(),
stroke: ol.format.KML.DEFAULT_TEXT_STROKE_STYLE_
}); });
} }
var nameStyle = new ol.style.Style({ var nameStyle = new ol.style.Style({
@@ -514,12 +537,7 @@ ol.format.KML.readVec2_ = function(node) {
* @return {number|undefined} Scale. * @return {number|undefined} Scale.
*/ */
ol.format.KML.readScale_ = function(node) { ol.format.KML.readScale_ = function(node) {
var number = ol.format.XSD.readDecimal(node); return ol.format.XSD.readDecimal(node);
if (number !== undefined) {
return Math.sqrt(number);
} else {
return undefined;
}
}; };
@@ -558,12 +576,13 @@ ol.format.KML.IconStyleParser_ = function(node, objectStack) {
} }
var styleObject = /** @type {Object} */ (objectStack[objectStack.length - 1]); var styleObject = /** @type {Object} */ (objectStack[objectStack.length - 1]);
var IconObject = 'Icon' in object ? object['Icon'] : {}; var IconObject = 'Icon' in object ? object['Icon'] : {};
var drawIcon = (!('Icon' in object) || Object.keys(IconObject).length > 0);
var src; var src;
var href = /** @type {string|undefined} */ var href = /** @type {string|undefined} */
(IconObject['href']); (IconObject['href']);
if (href) { if (href) {
src = href; src = href;
} else { } else if (drawIcon) {
src = ol.format.KML.DEFAULT_IMAGE_STYLE_SRC_; src = ol.format.KML.DEFAULT_IMAGE_STYLE_SRC_;
} }
var anchor, anchorXUnits, anchorYUnits; var anchor, anchorXUnits, anchorYUnits;
@@ -610,27 +629,38 @@ ol.format.KML.IconStyleParser_ = function(node, objectStack) {
var scale = /** @type {number|undefined} */ var scale = /** @type {number|undefined} */
(object['scale']); (object['scale']);
if (src == ol.format.KML.DEFAULT_IMAGE_STYLE_SRC_) { if (isNaN(scale) || scale === undefined) {
size = ol.format.KML.DEFAULT_IMAGE_STYLE_SIZE_; scale = ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_;
if (scale === undefined) { } else {
scale = ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_; scale = scale * ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_;
}
} }
var imageStyle = new ol.style.Icon({ if (drawIcon) {
anchor: anchor, if (src == ol.format.KML.DEFAULT_IMAGE_STYLE_SRC_) {
anchorOrigin: ol.style.IconOrigin.BOTTOM_LEFT, size = ol.format.KML.DEFAULT_IMAGE_STYLE_SIZE_;
anchorXUnits: anchorXUnits, if (scale === undefined) {
anchorYUnits: anchorYUnits, scale = ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_;
crossOrigin: 'anonymous', // FIXME should this be configurable? }
offset: offset, }
offsetOrigin: ol.style.IconOrigin.BOTTOM_LEFT,
rotation: rotation, var imageStyle = new ol.style.Icon({
scale: scale, anchor: anchor,
size: size, anchorOrigin: ol.style.IconOrigin.BOTTOM_LEFT,
src: src anchorXUnits: anchorXUnits,
}); anchorYUnits: anchorYUnits,
styleObject['imageStyle'] = imageStyle; crossOrigin: 'anonymous', // FIXME should this be configurable?
offset: offset,
offsetOrigin: ol.style.IconOrigin.BOTTOM_LEFT,
rotation: rotation,
scale: scale,
size: size,
src: src
});
styleObject['imageStyle'] = imageStyle;
} else {
// handle the case when we explicitly want to draw no icon.
styleObject['imageStyle'] = ol.format.KML.DEFAULT_NO_IMAGE_STYLE_;
}
}; };
@@ -1079,6 +1109,9 @@ ol.format.KML.readStyle_ = function(node, objectStack) {
var imageStyle = /** @type {ol.style.Image} */ var imageStyle = /** @type {ol.style.Image} */
('imageStyle' in styleObject ? ('imageStyle' in styleObject ?
styleObject['imageStyle'] : ol.format.KML.DEFAULT_IMAGE_STYLE_); styleObject['imageStyle'] : ol.format.KML.DEFAULT_IMAGE_STYLE_);
if (imageStyle == ol.format.KML.DEFAULT_NO_IMAGE_STYLE_) {
imageStyle = undefined;
}
var textStyle = /** @type {ol.style.Text} */ var textStyle = /** @type {ol.style.Text} */
('textStyle' in styleObject ? ('textStyle' in styleObject ?
styleObject['textStyle'] : ol.format.KML.DEFAULT_TEXT_STYLE_); styleObject['textStyle'] : ol.format.KML.DEFAULT_TEXT_STYLE_);
+3 -3
View File
@@ -1394,7 +1394,7 @@ describe('ol.format.KML', function() {
expect(imageStyle.getOrigin()).to.be(null); expect(imageStyle.getOrigin()).to.be(null);
expect(imageStyle.getRotation()).to.eql(0); expect(imageStyle.getRotation()).to.eql(0);
expect(imageStyle.getSize()).to.be(null); expect(imageStyle.getSize()).to.be(null);
expect(imageStyle.getScale()).to.be(1); expect(imageStyle.getScale()).to.be(ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_);
expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_);
expect(style.getZIndex()).to.be(undefined); expect(style.getZIndex()).to.be(undefined);
}); });
@@ -1439,7 +1439,7 @@ describe('ol.format.KML', function() {
expect(imageStyle.getAnchor()).to.eql([24, 36]); expect(imageStyle.getAnchor()).to.eql([24, 36]);
expect(imageStyle.getOrigin()).to.eql([24, 108]); expect(imageStyle.getOrigin()).to.eql([24, 108]);
expect(imageStyle.getRotation()).to.eql(0); expect(imageStyle.getRotation()).to.eql(0);
expect(imageStyle.getScale()).to.eql(Math.sqrt(3)); expect(imageStyle.getScale()).to.eql(ol.format.KML.DEFAULT_IMAGE_SCALE_MULTIPLIER_ * 3.0);
expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_); expect(style.getText()).to.be(ol.format.KML.DEFAULT_TEXT_STYLE_);
expect(style.getZIndex()).to.be(undefined); expect(style.getZIndex()).to.be(undefined);
}); });
@@ -1472,7 +1472,7 @@ describe('ol.format.KML', function() {
expect(style.getStroke()).to.be(ol.format.KML.DEFAULT_STROKE_STYLE_); expect(style.getStroke()).to.be(ol.format.KML.DEFAULT_STROKE_STYLE_);
var textStyle = style.getText(); var textStyle = style.getText();
expect(textStyle).to.be.an(ol.style.Text); expect(textStyle).to.be.an(ol.style.Text);
expect(textStyle.getScale()).to.be(0.5); expect(textStyle.getScale()).to.be(0.25);
var textFillStyle = textStyle.getFill(); var textFillStyle = textStyle.getFill();
expect(textFillStyle).to.be.an(ol.style.Fill); expect(textFillStyle).to.be.an(ol.style.Fill);
expect(textFillStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]); expect(textFillStyle.getColor()).to.eql([0x78, 0x56, 0x34, 0x12 / 255]);