Uniformly support evaluating symbolizers without features

In combination with a (not yet implemented) `Symbolizer#isLiteral` method, calling `Symbolizer#evaluate` without a feature is the fast track for rendering batches of like-styled features.  This change also simplifies the handling of undefined symbolizer literal properties (where stroke or fill properties are optional).
This commit is contained in:
Tim Schaub
2013-06-21 10:42:11 -06:00
parent cab983f826
commit 8e8b26805f
6 changed files with 76 additions and 77 deletions

View File

@@ -16,12 +16,18 @@ goog.require('ol.geom.GeometryType');
* function scope. The feature itself will be used as the `this` argument.
*
* @param {ol.expression.Expression} expr The expression.
* @param {ol.Feature} feature The feature.
* @param {ol.Feature=} opt_feature The feature.
* @return {*} The result of the expression.
*/
ol.expression.evaluateFeature = function(expr, feature) {
return expr.evaluate(
feature.getAttributes(), ol.expression.lib, feature);
ol.expression.evaluateFeature = function(expr, opt_feature) {
var result;
if (goog.isDef(opt_feature)) {
result = expr.evaluate(
opt_feature.getAttributes(), ol.expression.lib, opt_feature);
} else {
result = expr.evaluate();
}
return result;
};

View File

@@ -3,6 +3,7 @@ goog.provide('ol.style.IconLiteral');
goog.provide('ol.style.IconType');
goog.require('goog.asserts');
goog.require('ol.expression');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Point');
@@ -118,28 +119,28 @@ ol.style.Icon = function(options) {
* @inheritDoc
* @return {ol.style.IconLiteral} Literal shape symbolizer.
*/
ol.style.Icon.prototype.createLiteral = function(feature) {
var attrs = feature && feature.getAttributes();
ol.style.Icon.prototype.createLiteral = function(opt_feature) {
var url = /** @type {string} */ (this.url_.evaluate(attrs, null, feature));
goog.asserts.assert(goog.isString(url) && url != '#', 'url must be a string');
var url = ol.expression.evaluateFeature(this.url_, opt_feature);
goog.asserts.assertString(url, 'url must be a string');
goog.asserts.assert(url != '#', 'url must not be "#"');
var width = /** @type {number|undefined} */ (goog.isNull(this.width_) ?
undefined : this.width_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(width) || goog.isNumber(width),
'width must be undefined or a number');
var width;
if (!goog.isNull(this.width_)) {
width = ol.expression.evaluateFeature(this.width_, opt_feature);
goog.asserts.assertNumber(width, 'width must be a number');
}
var height = /** @type {number|undefined} */ (goog.isNull(this.height_) ?
undefined : this.height_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(height) || goog.isNumber(height),
'height must be undefined or a number');
var height;
if (!goog.isNull(this.height_)) {
height = ol.expression.evaluateFeature(this.height_, opt_feature);
goog.asserts.assertNumber(height, 'height must be a number');
}
var opacity = /** {@type {number} */ (this.opacity_.evaluate(attrs, null,
feature));
var opacity = ol.expression.evaluateFeature(this.opacity_, opt_feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
var rotation =
/** {@type {number} */ (this.rotation_.evaluate(attrs, null, feature));
var rotation = ol.expression.evaluateFeature(this.rotation_, opt_feature);
goog.asserts.assertNumber(rotation, 'rotation must be a number');
return new ol.style.IconLiteral({

View File

@@ -2,6 +2,7 @@ goog.provide('ol.style.Line');
goog.provide('ol.style.LineLiteral');
goog.require('goog.asserts');
goog.require('ol.expression');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Symbolizer');
@@ -98,19 +99,16 @@ goog.inherits(ol.style.Line, ol.style.Symbolizer);
* @return {ol.style.LineLiteral} Literal line symbolizer.
*/
ol.style.Line.prototype.createLiteral = function(opt_feature) {
var attrs,
feature = opt_feature;
if (goog.isDef(feature)) {
attrs = feature.getAttributes();
}
var strokeColor = this.strokeColor_.evaluate(attrs, null, feature);
var strokeColor = ol.expression.evaluateFeature(
this.strokeColor_, opt_feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
var strokeWidth = this.strokeWidth_.evaluate(attrs, null, feature);
var strokeWidth = ol.expression.evaluateFeature(
this.strokeWidth_, opt_feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
var opacity = this.opacity_.evaluate(attrs, null, feature);
var opacity = ol.expression.evaluateFeature(this.opacity_, opt_feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.LineLiteral({

View File

@@ -2,6 +2,7 @@ goog.provide('ol.style.Polygon');
goog.provide('ol.style.PolygonLiteral');
goog.require('goog.asserts');
goog.require('ol.expression');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Symbolizer');
@@ -149,33 +150,31 @@ goog.inherits(ol.style.Polygon, ol.style.Symbolizer);
* @return {ol.style.PolygonLiteral} Literal shape symbolizer.
*/
ol.style.Polygon.prototype.createLiteral = function(opt_feature) {
var attrs,
feature = opt_feature;
if (goog.isDef(feature)) {
attrs = feature.getAttributes();
var fillColor;
if (!goog.isNull(this.fillColor_)) {
fillColor = ol.expression.evaluateFeature(this.fillColor_, opt_feature);
goog.asserts.assertString(fillColor, 'fillColor must be a string');
}
var fillColor = goog.isNull(this.fillColor_) ?
undefined :
/** @type {string} */ (this.fillColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(fillColor) || goog.isString(fillColor));
var strokeColor;
if (!goog.isNull(this.strokeColor_)) {
strokeColor = ol.expression.evaluateFeature(this.strokeColor_, opt_feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
}
var strokeColor = goog.isNull(this.strokeColor_) ?
undefined :
/** @type {string} */ (this.strokeColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeColor) || goog.isString(strokeColor));
var strokeWidth = goog.isNull(this.strokeWidth_) ?
undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));
var strokeWidth;
if (!goog.isNull(this.strokeWidth_)) {
strokeWidth = ol.expression.evaluateFeature(this.strokeWidth_, opt_feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
}
goog.asserts.assert(
goog.isDef(fillColor) ||
(goog.isDef(strokeColor) && goog.isDef(strokeWidth)),
'either fill style or strokeColor and strokeWidth must be defined');
'either fillColor or strokeColor and strokeWidth must be defined');
var opacity = this.opacity_.evaluate(attrs, null, feature);
var opacity = ol.expression.evaluateFeature(this.opacity_, opt_feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.PolygonLiteral({

View File

@@ -3,6 +3,7 @@ goog.provide('ol.style.ShapeLiteral');
goog.provide('ol.style.ShapeType');
goog.require('goog.asserts');
goog.require('ol.expression');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Point');
@@ -184,36 +185,34 @@ ol.style.Shape = function(options) {
* @return {ol.style.ShapeLiteral} Literal shape symbolizer.
*/
ol.style.Shape.prototype.createLiteral = function(opt_feature) {
var attrs,
feature = opt_feature;
if (goog.isDef(feature)) {
attrs = feature.getAttributes();
}
var size = this.size_.evaluate(attrs, null, feature);
var size = ol.expression.evaluateFeature(this.size_, opt_feature);
goog.asserts.assertNumber(size, 'size must be a number');
var fillColor = goog.isNull(this.fillColor_) ?
undefined :
/** @type {string} */ (this.fillColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(fillColor) || goog.isString(fillColor));
var fillColor;
if (!goog.isNull(this.fillColor_)) {
fillColor = ol.expression.evaluateFeature(this.fillColor_, opt_feature);
goog.asserts.assertString(fillColor, 'fillColor must be a string');
}
var strokeColor = goog.isNull(this.strokeColor_) ?
undefined :
/** @type {string} */ (this.strokeColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeColor) || goog.isString(strokeColor));
var strokeColor;
if (!goog.isNull(this.strokeColor_)) {
strokeColor = ol.expression.evaluateFeature(this.strokeColor_, opt_feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
}
var strokeWidth = goog.isNull(this.strokeWidth_) ?
undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));
var strokeWidth;
if (!goog.isNull(this.strokeWidth_)) {
strokeWidth = ol.expression.evaluateFeature(this.strokeWidth_, opt_feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
}
goog.asserts.assert(
goog.isDef(fillColor) ||
(goog.isDef(strokeColor) && goog.isDef(strokeWidth)),
'either fill style or strokeColor and strokeWidth must be defined');
'either fillColor or strokeColor and strokeWidth must be defined');
var opacity = this.opacity_.evaluate(attrs, null, feature);
var opacity = ol.expression.evaluateFeature(this.opacity_, opt_feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.ShapeLiteral({

View File

@@ -2,6 +2,7 @@ goog.provide('ol.style.Text');
goog.provide('ol.style.TextLiteral');
goog.require('goog.asserts');
goog.require('ol.expression');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Symbolizer');
@@ -121,25 +122,20 @@ goog.inherits(ol.style.Text, ol.style.Symbolizer);
* @return {ol.style.TextLiteral} Literal text symbolizer.
*/
ol.style.Text.prototype.createLiteral = function(opt_feature) {
var attrs,
feature = opt_feature;
if (goog.isDef(feature)) {
attrs = feature.getAttributes();
}
var color = this.color_.evaluate(attrs, null, feature);
var color = ol.expression.evaluateFeature(this.color_, opt_feature);
goog.asserts.assertString(color, 'color must be a string');
var fontFamily = this.fontFamily_.evaluate(attrs, null, feature);
var fontFamily = ol.expression.evaluateFeature(this.fontFamily_, opt_feature);
goog.asserts.assertString(fontFamily, 'fontFamily must be a string');
var fontSize = this.fontSize_.evaluate(attrs, null, feature);
var fontSize = ol.expression.evaluateFeature(this.fontSize_, opt_feature);
goog.asserts.assertNumber(fontSize, 'fontSize must be a number');
var text = this.text_.evaluate(attrs, null, feature);
var text = ol.expression.evaluateFeature(this.text_, opt_feature);
goog.asserts.assertString(text, 'text must be a string');
var opacity = this.opacity_.evaluate(attrs, null, feature);
var opacity = ol.expression.evaluateFeature(this.opacity_, opt_feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.TextLiteral({