Skip to content

Commit

Permalink
Optional polygon stroke or fill
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Mar 8, 2013
1 parent f93bf2a commit 77355ca
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 28 deletions.
95 changes: 68 additions & 27 deletions src/ol/style/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ goog.require('ol.style.SymbolizerLiteral');


/**
* @typedef {{fillStyle: (string),
* strokeStyle: (string),
* strokeWidth: (number),
* @typedef {{fillStyle: (string|undefined),
* strokeStyle: (string|undefined),
* strokeWidth: (number|undefined),
* opacity: (number)}}
*/
ol.style.PolygonLiteralOptions;
Expand All @@ -25,17 +25,30 @@ ol.style.PolygonLiteralOptions;
ol.style.PolygonLiteral = function(config) {
goog.base(this);

goog.asserts.assertString(config.fillStyle, 'fillStyle must be a string');
/** @type {string} */
/** @type {string|undefined} */
this.fillStyle = config.fillStyle;
if (goog.isDef(config.fillStyle)) {
goog.asserts.assertString(config.fillStyle, 'fillStyle must be a string');
}

goog.asserts.assertString(config.strokeStyle, 'strokeStyle must be a string');
/** @type {string} */
/** @type {string|undefined} */
this.strokeStyle = config.strokeStyle;
if (goog.isDef(this.strokeStyle)) {
goog.asserts.assertString(
this.strokeStyle, 'strokeStyle must be a string');
}

goog.asserts.assertNumber(config.strokeWidth, 'strokeWidth must be a number');
/** @type {number} */
/** @type {number|undefined} */
this.strokeWidth = config.strokeWidth;
if (goog.isDef(this.strokeWidth)) {
goog.asserts.assertNumber(
this.strokeWidth, 'strokeWidth must be a number');
}

goog.asserts.assert(
goog.isDef(this.fillStyle) ||
(goog.isDef(this.strokeStyle) && goog.isDef(this.strokeWidth)),
'Either fillStyle or strokeStyle and strokeWidth must be set');

goog.asserts.assertNumber(config.opacity, 'opacity must be a number');
/** @type {number} */
Expand Down Expand Up @@ -69,28 +82,45 @@ ol.style.Polygon = function(options) {
* @type {ol.Expression}
* @private
*/
this.fillStyle_ = !goog.isDef(options.fillStyle) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.fillStyle) :
this.fillStyle_ = !goog.isDefAndNotNull(options.fillStyle) ?
null :
(options.fillStyle instanceof ol.Expression) ?
options.fillStyle : new ol.ExpressionLiteral(options.fillStyle);

// stroke handling - if any stroke property is supplied, use defaults
var strokeStyle = null,
strokeWidth = null;

if (goog.isDefAndNotNull(options.strokeStyle) ||
goog.isDefAndNotNull(options.strokeWidth)) {

strokeStyle = !goog.isDefAndNotNull(options.strokeStyle) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeStyle) :
(options.strokeStyle instanceof ol.Expression) ?
options.strokeStyle : new ol.ExpressionLiteral(options.strokeStyle);

strokeWidth = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
}

/**
* @type {ol.Expression}
* @private
*/
this.strokeStyle_ = !goog.isDef(options.strokeStyle) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeStyle) :
(options.strokeStyle instanceof ol.Expression) ?
options.strokeStyle : new ol.ExpressionLiteral(options.strokeStyle);
this.strokeStyle_ = strokeStyle;

/**
* @type {ol.Expression}
* @private
*/
this.strokeWidth_ = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
this.strokeWidth_ = strokeWidth;

// one of stroke or fill can be null, both null is user error
goog.asserts.assert(!goog.isNull(this.fillStyle_) ||
!(goog.isNull(this.strokeStyle_) && goog.isNull(this.strokeWidth_)),
'Stroke or fill properties must be provided');

/**
* @type {ol.Expression}
Expand All @@ -116,14 +146,25 @@ ol.style.Polygon.prototype.createLiteral = function(opt_feature) {
attrs = feature.getAttributes();
}

var fillStyle = this.fillStyle_.evaluate(feature, attrs);
goog.asserts.assertString(fillStyle, 'fillStyle must be a string');

var strokeStyle = this.strokeStyle_.evaluate(feature, attrs);
goog.asserts.assertString(strokeStyle, 'strokeStyle must be a string');

var strokeWidth = this.strokeWidth_.evaluate(feature, attrs);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
var fillStyle = goog.isNull(this.fillStyle_) ?
undefined :
/** @type {string} */ (this.fillStyle_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(fillStyle) || goog.isString(fillStyle));

var strokeStyle = goog.isNull(this.strokeStyle_) ?
undefined :
/** @type {string} */ (this.strokeStyle_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(strokeStyle) || goog.isString(strokeStyle));

var strokeWidth = goog.isNull(this.strokeWidth_) ?
undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));

goog.asserts.assert(
goog.isDef(fillStyle) ||
(goog.isDef(strokeStyle) && goog.isDef(strokeWidth)),
'either fill style or strokeStyle and strokeWidth must be defined');

var opacity = this.opacity_.evaluate(feature, attrs);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
Expand Down
15 changes: 14 additions & 1 deletion test/spec/ol/style/polygon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ol.style.Polygon', function() {
it('accepts expressions', function() {
var symbolizer = new ol.style.Polygon({
opacity: new ol.Expression('value / 100'),
fillStyle: ol.Expression('fillAttr')
fillStyle: new ol.Expression('fillAttr')
});
expect(symbolizer).toBeA(ol.style.Polygon);
});
Expand All @@ -70,6 +70,19 @@ describe('ol.style.Polygon', function() {
expect(literal).toBeA(ol.style.PolygonLiteral);
expect(literal.opacity).toBe(42 / 100);
expect(literal.fillStyle).toBe('#ff0000');
expect(literal.strokeStyle).toBeUndefined();
});

it('applies default strokeWidth if only strokeStyle is given', function() {
var symbolizer = new ol.style.Polygon({
strokeStyle: '#ff0000'
});

var literal = symbolizer.createLiteral();
expect(literal).toBeA(ol.style.PolygonLiteral);
expect(literal.strokeStyle).toBe('#ff0000');
expect(literal.strokeWidth).toBe(1.5);
expect(literal.fillStyle).toBeUndefined();
});

});
Expand Down

0 comments on commit 77355ca

Please sign in to comment.