From 33cacab11cec45db0754beede5172a8245cb9e78 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 26 Aug 2013 15:27:45 -0600 Subject: [PATCH] Optional zIndex for stroke symbolizers --- src/objectliterals.jsdoc | 1 + src/ol/style/lineliteral.js | 15 ++++--- src/ol/style/strokesymbolizer.js | 46 ++++++++++++++++++--- test/spec/ol/style/lineliteral.test.js | 7 ++++ test/spec/ol/style/strokesymbolizer.test.js | 10 +++++ 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index f0ed7ae810..d61cb12f3f 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -728,6 +728,7 @@ * color code. * @property {number|ol.expr.Expression|undefined} opacity Stroke opacity (0-1). * @property {number|ol.expr.Expression|undefined} width Stroke width in pixels. + * @property {number|ol.expr.Expression|undefined} zIndex Stack order. */ /** diff --git a/src/ol/style/lineliteral.js b/src/ol/style/lineliteral.js index 9b1282ffcf..6ba2759d8e 100644 --- a/src/ol/style/lineliteral.js +++ b/src/ol/style/lineliteral.js @@ -7,7 +7,8 @@ goog.require('ol.style.Literal'); /** * @typedef {{color: (string), * opacity: (number), - * width: (number)}} + * width: (number), + * zIndex: (number|undefined)}} */ ol.style.LineLiteralOptions; @@ -36,6 +37,9 @@ ol.style.LineLiteral = function(options) { /** @type {number} */ this.width = options.width; + /** @type {number|undefined} */ + this.zIndex = options.zIndex; + }; goog.inherits(ol.style.LineLiteral, ol.style.Literal); @@ -43,8 +47,9 @@ goog.inherits(ol.style.LineLiteral, ol.style.Literal); /** * @inheritDoc */ -ol.style.LineLiteral.prototype.equals = function(lineLiteral) { - return this.color == lineLiteral.color && - this.opacity == lineLiteral.opacity && - this.width == lineLiteral.width; +ol.style.LineLiteral.prototype.equals = function(other) { + return this.color == other.color && + this.opacity == other.opacity && + this.width == other.width && + this.zIndex == other.zIndex; }; diff --git a/src/ol/style/strokesymbolizer.js b/src/ol/style/strokesymbolizer.js index 339bae811b..6997f63e94 100644 --- a/src/ol/style/strokesymbolizer.js +++ b/src/ol/style/strokesymbolizer.js @@ -49,6 +49,15 @@ ol.style.Stroke = function(opt_options) { (options.width instanceof ol.expr.Expression) ? options.width : new ol.expr.Literal(options.width); + /** + * @type {ol.expr.Expression} + * @private + */ + this.zIndex_ = !goog.isDefAndNotNull(options.zIndex) ? + null : + (options.zIndex instanceof ol.expr.Expression) ? + options.zIndex : new ol.expr.Literal(options.zIndex); + }; goog.inherits(ol.style.Stroke, ol.style.Symbolizer); @@ -79,20 +88,28 @@ ol.style.Stroke.prototype.createLiteral = function(featureOrType) { this.width_, feature)); goog.asserts.assert(!isNaN(width), 'width must be a number'); + var zIndex; + if (!goog.isNull(this.zIndex_)) { + zIndex = Number(ol.expr.evaluateFeature(this.zIndex_, feature)); + goog.asserts.assert(!isNaN(zIndex), 'zIndex must be a number'); + } + var literal = null; if (type === ol.geom.GeometryType.LINESTRING || type === ol.geom.GeometryType.MULTILINESTRING) { literal = new ol.style.LineLiteral({ color: color, opacity: opacity, - width: width + width: width, + zIndex: zIndex }); } else if (type === ol.geom.GeometryType.POLYGON || type === ol.geom.GeometryType.MULTIPOLYGON) { literal = new ol.style.PolygonLiteral({ strokeColor: color, strokeOpacity: opacity, - strokeWidth: width + strokeWidth: width, + zIndex: zIndex }); } @@ -127,6 +144,15 @@ ol.style.Stroke.prototype.getWidth = function() { }; +/** + * Get the stroke zIndex. + * @return {ol.expr.Expression} Stroke zIndex. + */ +ol.style.Stroke.prototype.getZIndex = function() { + return this.zIndex_; +}; + + /** * Set the stroke color. * @param {ol.expr.Expression} color Stroke color. @@ -158,9 +184,19 @@ ol.style.Stroke.prototype.setWidth = function(width) { /** - * @typedef {{color: (string), - * opacity: (number), - * width: (number)}} + * Set the stroke zIndex. + * @param {ol.expr.Expression} zIndex Stroke zIndex. + */ +ol.style.Stroke.prototype.setZIndex = function(zIndex) { + goog.asserts.assertInstanceof(zIndex, ol.expr.Expression); + this.zIndex_ = zIndex; +}; + + +/** + * @typedef {{strokeColor: (string), + * strokeOpacity: (number), + * strokeWidth: (number)}} */ ol.style.StrokeDefaults = { color: '#696969', diff --git a/test/spec/ol/style/lineliteral.test.js b/test/spec/ol/style/lineliteral.test.js index 94be24dc43..19349064bc 100644 --- a/test/spec/ol/style/lineliteral.test.js +++ b/test/spec/ol/style/lineliteral.test.js @@ -30,10 +30,17 @@ describe('ol.style.LineLiteral', function() { color: '#BADA55', opacity: 0.5 }); + var differentZIndex = new ol.style.LineLiteral({ + width: 3, + color: '#BADA55', + opacity: 1, + zIndex: 3 + }); expect(literal.equals(equalLiteral)).to.be(true); expect(literal.equals(differentColor)).to.be(false); expect(literal.equals(differentWidth)).to.be(false); expect(literal.equals(differentOpacity)).to.be(false); + expect(literal.equals(differentZIndex)).to.be(false); }); }); diff --git a/test/spec/ol/style/strokesymbolizer.test.js b/test/spec/ol/style/strokesymbolizer.test.js index 1ea72c8e93..4d28fdfc69 100644 --- a/test/spec/ol/style/strokesymbolizer.test.js +++ b/test/spec/ol/style/strokesymbolizer.test.js @@ -20,6 +20,15 @@ describe('ol.style.Stroke', function() { expect(symbolizer).to.be.a(ol.style.Stroke); }); + it('accepts zIndex', function() { + var symbolizer = new ol.style.Stroke({ + opacity: ol.expr.parse('value / 100'), + width: ol.expr.parse('widthAttr'), + zIndex: 5 + }); + expect(symbolizer).to.be.a(ol.style.Stroke); + }); + }); describe('#createLiteral()', function() { @@ -40,6 +49,7 @@ describe('ol.style.Stroke', function() { expect(literal).to.be.a(ol.style.LineLiteral); expect(literal.opacity).to.be(42 / 100); expect(literal.width).to.be(1.5); + expect(literal.zIndex).to.be(undefined); }); it('applies the default values', function() {