Add optional context property to OpenLayers.Rule, so rules can now be evaluated against diffent contexts than feature.attributes. This changeset also renames Rule.Logical.children to Rule.Logical.rules, to make it more consistent with OL.Style. r=crschmidt (closes #1331)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@6116 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2008-02-08 16:56:48 +00:00
parent bb26a2601d
commit 3581276835
8 changed files with 1622 additions and 1568 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,101 +1,141 @@
/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. /* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */ * for the full text of the license. */
/** /**
* @requires OpenLayers/Util.js * @requires OpenLayers/Util.js
* @requires OpenLayers/Style.js * @requires OpenLayers/Style.js
*/ */
/** /**
* Class: OpenLayers.Rule * Class: OpenLayers.Rule
* This class represents a OGC Rule, as being used for rule-based SLD styling. * This class represents a OGC Rule, as being used for rule-based SLD styling.
*/ */
OpenLayers.Rule = OpenLayers.Class({ OpenLayers.Rule = OpenLayers.Class({
/** /**
* APIProperty: name * APIProperty: name
* {String} name of this rule * {String} name of this rule
*/ */
name: 'default', name: 'default',
/** /**
* Property: elseFilter * Property: context
* {Boolean} Determines whether this rule is only to be applied only if * {Object} An optional object with properties that the rule and its
* no other rules match (ElseFilter according to the SLD specification). * symbolizers' property values should be evaluatad against. If no
* Default is false. For instances of OpenLayers.Rule, if elseFilter is * context is specified, feature.attributes will be used
* false, the rule will always apply. For subclasses, the else property is */
* ignored. context: null,
*/
elseFilter: false, /**
* Property: elseFilter
/** * {Boolean} Determines whether this rule is only to be applied only if
* Property: symbolizer * no other rules match (ElseFilter according to the SLD specification).
* {Object} Hash of styles for this rule. Contains hashes of feature * Default is false. For instances of OpenLayers.Rule, if elseFilter is
* styles. Keys are one or more of ["Point", "Line", "Polygon"] * false, the rule will always apply. For subclasses, the else property is
*/ * ignored.
symbolizer: null, */
elseFilter: false,
/**
* APIProperty: minScaleDenominator /**
* {Number} or {String} minimum scale at which to draw the feature. * Property: symbolizer
* In the case of a String, this can be a combination of text and * {Object} Hash of styles for this rule. Contains hashes of feature
* propertyNames in the form "literal ${propertyName}" * styles. Keys are one or more of ["Point", "Line", "Polygon"]
*/ */
minScaleDenominator: null, symbolizer: null,
/** /**
* APIProperty: maxScaleDenominator * APIProperty: minScaleDenominator
* {Number} or {String} maximum scale at which to draw the feature. * {Number} or {String} minimum scale at which to draw the feature.
* In the case of a String, this can be a combination of text and * In the case of a String, this can be a combination of text and
* propertyNames in the form "literal ${propertyName}" * propertyNames in the form "literal ${propertyName}"
*/ */
maxScaleDenominator: null, minScaleDenominator: null,
/** /**
* Constructor: OpenLayers.Rule * APIProperty: maxScaleDenominator
* Creates a Rule. * {Number} or {String} maximum scale at which to draw the feature.
* * In the case of a String, this can be a combination of text and
* Parameters: * propertyNames in the form "literal ${propertyName}"
* options - {Object} An optional object with properties to set on the */
* rule maxScaleDenominator: null,
*
* Returns: /**
* {<OpenLayers.Rule>} * Constructor: OpenLayers.Rule
*/ * Creates a Rule.
initialize: function(options) { *
this.symbolizer = {}; * Parameters:
* options - {Object} An optional object with properties to set on the
OpenLayers.Util.extend(this, options); * rule
}, *
* Returns:
/** * {<OpenLayers.Rule>}
* APIMethod: destroy */
* nullify references to prevent circular references and memory leaks initialize: function(options) {
*/ this.symbolizer = {};
destroy: function() {
for (var i in this.symbolizer) { OpenLayers.Util.extend(this, options);
this.symbolizer[i] = null; },
}
this.symbolizer = null; /**
}, * APIMethod: destroy
* nullify references to prevent circular references and memory leaks
/** */
* APIMethod: evaluate destroy: function() {
* evaluates this rule for a specific feature for (var i in this.symbolizer) {
* this.symbolizer[i] = null;
* Parameters: }
* feature - {<OpenLayers.Feature>} feature to apply the rule to. this.symbolizer = null;
* },
* Returns:
* {boolean} true if the rule applies, false if it does not. /**
* This rule is the default rule and always returns true. * APIMethod: evaluate
*/ * evaluates this rule for a specific feature
evaluate: function(feature) { *
// Default rule always applies. Subclasses will want to override this. * Parameters:
return true; * feature - {<OpenLayers.Feature>} feature to apply the rule to.
}, *
* Returns:
CLASS_NAME: "OpenLayers.Rule" * {boolean} true if the rule applies, false if it does not.
* This rule is the default rule and always returns true.
*/
evaluate: function(feature) {
var context = this.getContext(feature);
var applies = true;
if (this.minScaleDenominator || this.maxScaleDenominator) {
var scale = feature.layer.map.getScale();
}
// check if within minScale/maxScale bounds
if (this.minScaleDenominator) {
applies = scale >= OpenLayers.Style.createLiteral(
this.minScaleDenominator, context);
}
if (applies && this.maxScaleDenominator) {
applies = scale < OpenLayers.Style.createLiteral(
this.maxScaleDenominator, context);
}
return applies;
},
/**
* Method: getContext
* Gets the context for evaluating this rule
*
* Paramters:
* feature - {<OpenLayers.Feature>} feature to take the context from if
* none is specified.
*/
getContext: function(feature) {
var context = this.context;
if (!context) {
context = feature.attributes || feature.data;
}
return context;
},
CLASS_NAME: "OpenLayers.Rule"
}); });

View File

@@ -1,204 +1,206 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */ * full text of the license. */
/** /**
* @requires OpenLayers/Rule.js * @requires OpenLayers/Rule.js
*/ */
/** /**
* Class: OpenLayers.Rule.Comparison * Class: OpenLayers.Rule.Comparison
* This class represents the comparison rules, as being used for rule-based * This class represents the comparison rules, as being used for rule-based
* SLD styling * SLD styling
* *
* Inherits from * Inherits from
* - <OpenLayers.Rule> * - <OpenLayers.Rule>
*/ */
OpenLayers.Rule.Comparison = OpenLayers.Class(OpenLayers.Rule, { OpenLayers.Rule.Comparison = OpenLayers.Class(OpenLayers.Rule, {
/** /**
* APIProperty: type * APIProperty: type
* {String} type: type of the comparison. This is one of * {String} type: type of the comparison. This is one of
* - OpenLayers.Rule.Comparison.EQUAL_TO = "=="; * - OpenLayers.Rule.Comparison.EQUAL_TO = "==";
* - OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!="; * - OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
* - OpenLayers.Rule.Comparison.LESS_THAN = "<"; * - OpenLayers.Rule.Comparison.LESS_THAN = "<";
* - OpenLayers.Rule.Comparison.GREATER_THAN = ">"; * - OpenLayers.Rule.Comparison.GREATER_THAN = ">";
* - OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<="; * - OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
* - OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; * - OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
* - OpenLayers.Rule.Comparison.BETWEEN = ".."; * - OpenLayers.Rule.Comparison.BETWEEN = "..";
* - OpenLayers.Rule.Comparison.LIKE = "~"; * - OpenLayers.Rule.Comparison.LIKE = "~";
*/ */
type: null, type: null,
/** /**
* APIProperty: property * APIProperty: property
* {String} * {String}
* name of the feature attribute to compare * name of the context property to compare
*/ */
property: null, property: null,
/** /**
* APIProperty: value * APIProperty: value
* {Number} or {String} * {Number} or {String}
* comparison value for binary comparisons. In the case of a String, this * comparison value for binary comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form * can be a combination of text and propertyNames in the form
* "literal ${propertyName}" * "literal ${propertyName}"
*/ */
value: null, value: null,
/** /**
* APIProperty: lowerBoundary * APIProperty: lowerBoundary
* {Number} or {String} * {Number} or {String}
* lower boundary for between comparisons. In the case of a String, this * lower boundary for between comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form * can be a combination of text and propertyNames in the form
* "literal ${propertyName}" * "literal ${propertyName}"
*/ */
lowerBoundary: null, lowerBoundary: null,
/** /**
* APIProperty: upperBoundary * APIProperty: upperBoundary
* {Number} or {String} * {Number} or {String}
* upper boundary for between comparisons. In the case of a String, this * upper boundary for between comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form * can be a combination of text and propertyNames in the form
* "literal ${propertyName}" * "literal ${propertyName}"
*/ */
upperBoundary: null, upperBoundary: null,
/** /**
* Constructor: OpenLayers.Rule.Comparison * Constructor: OpenLayers.Rule.Comparison
* Creates a comparison rule. * Creates a comparison rule.
* *
* Parameters: * Parameters:
* params - {Object} Hash of parameters for this rule: * params - {Object} Hash of parameters for this rule:
* - * -
* - value: * - value:
* options - {Object} An optional object with properties to set on the * options - {Object} An optional object with properties to set on the
* rule * rule
* *
* Returns: * Returns:
* {<OpenLayers.Rule.Comparison>} * {<OpenLayers.Rule.Comparison>}
*/ */
initialize: function(options) { initialize: function(options) {
OpenLayers.Rule.prototype.initialize.apply(this, [options]); OpenLayers.Rule.prototype.initialize.apply(this, [options]);
}, },
/** /**
* APIMethod: evaluate * APIMethod: evaluate
* evaluates this rule for a specific feature * evaluates this rule for a specific context
* *
* Parameters: * Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to. * context - {Object} context to apply the rule to.
* *
* Returns: * Returns:
* {boolean} true if the rule applies, false if it does not * {boolean} true if the rule applies, false if it does not
*/ */
evaluate: function(feature) { evaluate: function(feature) {
var attributes = feature.attributes || feature.data; if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) {
switch(this.type) { return false;
case OpenLayers.Rule.Comparison.EQUAL_TO: }
case OpenLayers.Rule.Comparison.LESS_THAN: var context = this.getContext(feature);
case OpenLayers.Rule.Comparison.GREATER_THAN: switch(this.type) {
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO: case OpenLayers.Rule.Comparison.EQUAL_TO:
case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO: case OpenLayers.Rule.Comparison.LESS_THAN:
return this.binaryCompare(feature, this.property, this.value); case OpenLayers.Rule.Comparison.GREATER_THAN:
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO:
case OpenLayers.Rule.Comparison.BETWEEN: case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO:
var result = return this.binaryCompare(context, this.property, this.value);
attributes[this.property] > this.lowerBoundary;
result = result && case OpenLayers.Rule.Comparison.BETWEEN:
attributes[this.property] < this.upperBoundary; var result =
return result; context[this.property] > this.lowerBoundary;
case OpenLayers.Rule.Comparison.LIKE: result = result &&
var regexp = new RegExp(this.value, context[this.property] < this.upperBoundary;
"gi"); return result;
return regexp.test(attributes[this.property]); case OpenLayers.Rule.Comparison.LIKE:
} var regexp = new RegExp(this.value,
}, "gi");
return regexp.test(context[this.property]);
/** }
* APIMethod: value2regex },
* Converts the value of this rule into a regular expression string,
* according to the wildcard characters specified. This method has to /**
* be called after instantiation of this class, if the value is not a * APIMethod: value2regex
* regular expression already. * Converts the value of this rule into a regular expression string,
* * according to the wildcard characters specified. This method has to
* Parameters: * be called after instantiation of this class, if the value is not a
* wildCard - {<Char>} wildcard character in the above value, default * regular expression already.
* is "*" *
* singleChar - {<Char>) single-character wildcard in the above value * Parameters:
* default is "." * wildCard - {<Char>} wildcard character in the above value, default
* escape - {<Char>) escape character in the above value, default is * is "*"
* "!" * singleChar - {<Char>) single-character wildcard in the above value
* * default is "."
* Returns: * escape - {<Char>) escape character in the above value, default is
* {String} regular expression string * "!"
*/ *
value2regex: function(wildCard, singleChar, escapeChar) { * Returns:
if (wildCard == ".") { * {String} regular expression string
var msg = "'.' is an unsupported wildCard character for "+ */
"OpenLayers.Rule.Comparison"; value2regex: function(wildCard, singleChar, escapeChar) {
OpenLayers.Console.error(msg); if (wildCard == ".") {
return null; var msg = "'.' is an unsupported wildCard character for "+
} "OpenLayers.Rule.Comparison";
OpenLayers.Console.error(msg);
// set UMN MapServer defaults for unspecified parameters return null;
wildCard = wildCard ? wildCard : "*"; }
singleChar = singleChar ? singleChar : ".";
escapeChar = escapeChar ? escapeChar : "!"; // set UMN MapServer defaults for unspecified parameters
wildCard = wildCard ? wildCard : "*";
this.value = this.value.replace( singleChar = singleChar ? singleChar : ".";
new RegExp("\\"+escapeChar, "g"), "\\"); escapeChar = escapeChar ? escapeChar : "!";
this.value = this.value.replace(
new RegExp("\\"+singleChar, "g"), "."); this.value = this.value.replace(
this.value = this.value.replace( new RegExp("\\"+escapeChar, "g"), "\\");
new RegExp("\\"+wildCard, "g"), ".*"); this.value = this.value.replace(
this.value = this.value.replace( new RegExp("\\"+singleChar, "g"), ".");
new RegExp("\\\\.\\*", "g"), "\\"+wildCard); this.value = this.value.replace(
this.value = this.value.replace( new RegExp("\\"+wildCard, "g"), ".*");
new RegExp("\\\\\\.", "g"), "\\"+singleChar); this.value = this.value.replace(
new RegExp("\\\\.\\*", "g"), "\\"+wildCard);
return this.value; this.value = this.value.replace(
}, new RegExp("\\\\\\.", "g"), "\\"+singleChar);
/** return this.value;
* Function: binaryCompare },
* Compares a feature property to a rule value
* /**
* Parameters: * Function: binaryCompare
* feature - {<OpenLayers.Feature>} * Compares a feature property to a rule value
* property - {String} or {Number} *
* value - {String} or {Number}, same as property * Parameters:
* * context - {Object}
* Returns: * property - {String} or {Number}
* {boolean} * value - {String} or {Number}, same as property
*/ *
binaryCompare: function(feature, property, value) { * Returns:
var attributes = feature.attributes || feature.data; * {boolean}
switch (this.type) { */
case OpenLayers.Rule.Comparison.EQUAL_TO: binaryCompare: function(context, property, value) {
return attributes[property] == value; switch (this.type) {
case OpenLayers.Rule.Comparison.NOT_EQUAL_TO: case OpenLayers.Rule.Comparison.EQUAL_TO:
return attributes[property] != value; return context[property] == value;
case OpenLayers.Rule.Comparison.LESS_THAN: case OpenLayers.Rule.Comparison.NOT_EQUAL_TO:
return attributes[property] < value; return context[property] != value;
case OpenLayers.Rule.Comparison.GREATER_THAN: case OpenLayers.Rule.Comparison.LESS_THAN:
return attributes[property] > value; return context[property] < value;
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO: case OpenLayers.Rule.Comparison.GREATER_THAN:
return attributes[property] <= value; return context[property] > value;
case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO: case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO:
return attributes[property] >= value; return context[property] <= value;
} case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO:
}, return context[property] >= value;
}
CLASS_NAME: "OpenLayers.Rule.Comparison" },
});
CLASS_NAME: "OpenLayers.Rule.Comparison"
});
OpenLayers.Rule.Comparison.EQUAL_TO = "==";
OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
OpenLayers.Rule.Comparison.LESS_THAN = "<"; OpenLayers.Rule.Comparison.EQUAL_TO = "==";
OpenLayers.Rule.Comparison.GREATER_THAN = ">"; OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<="; OpenLayers.Rule.Comparison.LESS_THAN = "<";
OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; OpenLayers.Rule.Comparison.GREATER_THAN = ">";
OpenLayers.Rule.Comparison.BETWEEN = ".."; OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
OpenLayers.Rule.Comparison.LIKE = "~"; OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
OpenLayers.Rule.Comparison.BETWEEN = "..";
OpenLayers.Rule.Comparison.LIKE = "~";

View File

@@ -1,66 +1,69 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */ * full text of the license. */
/** /**
* @requires OpenLayers/Rule.js * @requires OpenLayers/Rule.js
*/ */
/** /**
* Class: OpenLayers.Rule.FeatureId * Class: OpenLayers.Rule.FeatureId
* This class represents a ogc:FeatureId Rule, as being used for rule-based SLD * This class represents a ogc:FeatureId Rule, as being used for rule-based SLD
* styling * styling
* *
* Inherits from * Inherits from
* - <OpenLayers.Rule> * - <OpenLayers.Rule>
*/ */
OpenLayers.Rule.FeatureId = OpenLayers.Class(OpenLayers.Rule, { OpenLayers.Rule.FeatureId = OpenLayers.Class(OpenLayers.Rule, {
/** /**
* APIProperty: fids * APIProperty: fids
* {Array(<String>)} Feature Ids to evaluate this rule against. To be passed * {Array(<String>)} Feature Ids to evaluate this rule against. To be passed
* To be passed inside the params object. * To be passed inside the params object.
*/ */
fids: null, fids: null,
/** /**
* Constructor: OpenLayers.Rule.FeatureId * Constructor: OpenLayers.Rule.FeatureId
* Creates an ogc:FeatureId rule. * Creates an ogc:FeatureId rule.
* *
* Parameters: * Parameters:
* options - {Object} An optional object with properties to set on the * options - {Object} An optional object with properties to set on the
* rule * rule
* *
* Returns: * Returns:
* {<OpenLayers.Rule.FeatureId>} * {<OpenLayers.Rule.FeatureId>}
*/ */
initialize: function(options) { initialize: function(options) {
this.fids = []; this.fids = [];
OpenLayers.Rule.prototype.initialize.apply(this, [options]); OpenLayers.Rule.prototype.initialize.apply(this, [options]);
}, },
/** /**
* APIMethod: evaluate * APIMethod: evaluate
* evaluates this rule for a specific feature * evaluates this rule for a specific feature
* *
* Parameters: * Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to. * feature - {<OpenLayers.Feature>} feature to apply the rule to.
* For vector features, the check is run against the fid, * For vector features, the check is run against the fid,
* for plain features against the id. * for plain features against the id.
* *
* Returns: * Returns:
* {boolean} true if the rule applies, false if it does not * {boolean} true if the rule applies, false if it does not
*/ */
evaluate: function(feature) { evaluate: function(feature) {
for (var i=0; i<this.fids.length; i++) { if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) {
var fid = feature.fid || feature.id; return false;
if (fid == this.fids[i]) { }
return true; for (var i=0; i<this.fids.length; i++) {
} var fid = feature.fid || feature.id;
} if (fid == this.fids[i]) {
return false; return true;
}, }
}
CLASS_NAME: "OpenLayers.Rule.FeatureId" return false;
}); },
CLASS_NAME: "OpenLayers.Rule.FeatureId"
});

View File

@@ -1,101 +1,104 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */ * full text of the license. */
/** /**
* @requires OpenLayers/Rule.js * @requires OpenLayers/Rule.js
*/ */
/** /**
* Class: OpenLayers.Rule.Logical * Class: OpenLayers.Rule.Logical
* This class represents ogc:And, ogc:Or and ogc:Not rules. * This class represents ogc:And, ogc:Or and ogc:Not rules.
* *
* Inherits from * Inherits from
* - <OpenLayers.Rule> * - <OpenLayers.Rule>
*/ */
OpenLayers.Rule.Logical = OpenLayers.Class(OpenLayers.Rule, { OpenLayers.Rule.Logical = OpenLayers.Class(OpenLayers.Rule, {
/** /**
* APIProperty: children * APIProperty: children
* {Array(<OpenLayers.Rule>)} child rules for this rule * {Array(<OpenLayers.Rule>)} child rules for this rule
*/ */
children: null, rules: null,
/** /**
* APIProperty: type * APIProperty: type
* {String} type of logical operator. Available types are: * {String} type of logical operator. Available types are:
* - OpenLayers.Rule.Locical.AND = "&&"; * - OpenLayers.Rule.Locical.AND = "&&";
* - OpenLayers.Rule.Logical.OR = "||"; * - OpenLayers.Rule.Logical.OR = "||";
* - OpenLayers.Rule.Logical.NOT = "!"; * - OpenLayers.Rule.Logical.NOT = "!";
*/ */
type: null, type: null,
/** /**
* Constructor: OpenLayers.Rule.Logical * Constructor: OpenLayers.Rule.Logical
* Creates a logical rule (And, Or, Not). * Creates a logical rule (And, Or, Not).
* *
* Parameters: * Parameters:
* options - {Object} An optional object with properties to set on the * options - {Object} An optional object with properties to set on the
* rule * rule
* *
* Returns: * Returns:
* {<OpenLayers.Rule.Logical>} * {<OpenLayers.Rule.Logical>}
*/ */
initialize: function(options) { initialize: function(options) {
this.children = []; this.rules = [];
OpenLayers.Rule.prototype.initialize.apply(this, [options]); OpenLayers.Rule.prototype.initialize.apply(this, [options]);
}, },
/** /**
* APIMethod: destroy * APIMethod: destroy
* nullify references to prevent circular references and memory leaks * nullify references to prevent circular references and memory leaks
*/ */
destroy: function() { destroy: function() {
for (var i=0; i<this.children.length; i++) { for (var i=0; i<this.rules.length; i++) {
this.children[i].destroy(); this.rules[i].destroy();
} }
this.children = null; this.rules = null;
OpenLayers.Rule.prototype.destroy.apply(this, arguments); OpenLayers.Rule.prototype.destroy.apply(this, arguments);
}, },
/** /**
* APIMethod: evaluate * APIMethod: evaluate
* evaluates this rule for a specific feature * evaluates this rule for a specific feature
* *
* Parameters: * Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to. * feature - {<OpenLayers.Feature>} feature to apply the rule to.
* *
* Returns: * Returns:
* {boolean} true if the rule applies, false if it does not * {boolean} true if the rule applies, false if it does not
*/ */
evaluate: function(feature) { evaluate: function(feature) {
switch(this.type) { if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) {
case OpenLayers.Rule.Logical.AND: return false;
for (var i=0; i<this.children.length; i++) { }
if (this.children[i].evaluate(feature) == false) { switch(this.type) {
return false; case OpenLayers.Rule.Logical.AND:
} for (var i=0; i<this.rules.length; i++) {
} if (this.rules[i].evaluate(feature) == false) {
return true; return false;
}
case OpenLayers.Rule.Logical.OR: }
for (var i=0; i<this.children.length; i++) { return true;
if (this.children[i].evaluate(feature) == true) {
return true; case OpenLayers.Rule.Logical.OR:
} for (var i=0; i<this.rules.length; i++) {
} if (this.rules[i].evaluate(feature) == true) {
return false; return true;
}
case OpenLayers.Rule.Logical.NOT: }
return (!this.children[0].evaluate(feature)); return false;
}
}, case OpenLayers.Rule.Logical.NOT:
return (!this.rules[0].evaluate(feature));
CLASS_NAME: "OpenLayers.Rule.Logical" }
}); },
CLASS_NAME: "OpenLayers.Rule.Logical"
OpenLayers.Rule.Logical.AND = "&&"; });
OpenLayers.Rule.Logical.OR = "||";
OpenLayers.Rule.Logical.NOT = "!";
OpenLayers.Rule.Logical.AND = "&&";
OpenLayers.Rule.Logical.OR = "||";
OpenLayers.Rule.Logical.NOT = "!";

View File

@@ -1,326 +1,317 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */ * full text of the license. */
/** /**
* @requires OpenLayers/Util.js * @requires OpenLayers/Util.js
* @requires OpenLayers/Feature/Vector.js * @requires OpenLayers/Feature/Vector.js
*/ */
/** /**
* Class: OpenLayers.Style * Class: OpenLayers.Style
* This class represents a UserStyle obtained * This class represents a UserStyle obtained
* from a SLD, containing styling rules. * from a SLD, containing styling rules.
*/ */
OpenLayers.Style = OpenLayers.Class({ OpenLayers.Style = OpenLayers.Class({
/** /**
* APIProperty: name * APIProperty: name
* {String} * {String}
*/ */
name: null, name: null,
/** /**
* APIProperty: layerName * APIProperty: layerName
* {<String>} name of the layer that this style belongs to, usually * {<String>} name of the layer that this style belongs to, usually
* according to the NamedLayer attribute of an SLD document. * according to the NamedLayer attribute of an SLD document.
*/ */
layerName: null, layerName: null,
/** /**
* APIProperty: isDefault * APIProperty: isDefault
* {Boolean} * {Boolean}
*/ */
isDefault: false, isDefault: false,
/** /**
* Property: rules * Property: rules
* Array({<OpenLayers.Rule>}) * Array({<OpenLayers.Rule>})
*/ */
rules: null, rules: null,
/** /**
* Property: defaultStyle * Property: defaultStyle
* {Object} hash of style properties to use as default for merging * {Object} hash of style properties to use as default for merging
* rule-based style symbolizers onto. If no rules are defined, createStyle * rule-based style symbolizers onto. If no rules are defined, createStyle
* will return this style. * will return this style.
*/ */
defaultStyle: null, defaultStyle: null,
/** /**
* Property: propertyStyles * Property: propertyStyles
* {Hash of Boolean} cache of style properties that need to be parsed for * {Hash of Boolean} cache of style properties that need to be parsed for
* propertyNames. Property names are keys, values won't be used. * propertyNames. Property names are keys, values won't be used.
*/ */
propertyStyles: null, propertyStyles: null,
/** /**
* Constructor: OpenLayers.Style * Constructor: OpenLayers.Style
* Creates a UserStyle. * Creates a UserStyle.
* *
* Parameters: * Parameters:
* style - {Object} Optional hash of style properties that will be * style - {Object} Optional hash of style properties that will be
* used as default style for this style object. This style * used as default style for this style object. This style
* applies if no rules are specified. Symbolizers defined in * applies if no rules are specified. Symbolizers defined in
* rules will extend this default style. * rules will extend this default style.
* options - {Object} An optional object with properties to set on the * options - {Object} An optional object with properties to set on the
* userStyle * userStyle
* *
* Return: * Return:
* {<OpenLayers.Style>} * {<OpenLayers.Style>}
*/ */
initialize: function(style, options) { initialize: function(style, options) {
this.rules = []; this.rules = [];
// use the default style from OpenLayers.Feature.Vector if no style // use the default style from OpenLayers.Feature.Vector if no style
// was given in the constructor // was given in the constructor
this.setDefaultStyle(style || this.setDefaultStyle(style ||
OpenLayers.Feature.Vector.style["default"]); OpenLayers.Feature.Vector.style["default"]);
OpenLayers.Util.extend(this, options); OpenLayers.Util.extend(this, options);
}, },
/** /**
* APIMethod: destroy * APIMethod: destroy
* nullify references to prevent circular references and memory leaks * nullify references to prevent circular references and memory leaks
*/ */
destroy: function() { destroy: function() {
for (var i=0; i<this.rules.length; i++) { for (var i=0; i<this.rules.length; i++) {
this.rules[i].destroy(); this.rules[i].destroy();
this.rules[i] = null; this.rules[i] = null;
} }
this.rules = null; this.rules = null;
this.defaultStyle = null; this.defaultStyle = null;
}, },
/** /**
* APIMethod: createStyle * APIMethod: createStyle
* creates a style by applying all feature-dependent rules to the base * creates a style by applying all feature-dependent rules to the base
* style. * style.
* *
* Parameters: * Parameters:
* feature - {<OpenLayers.Feature>} feature to evaluate rules for * feature - {<OpenLayers.Feature>} feature to evaluate rules for
* baseStyle - {Object} hash of styles feature styles to extend * baseStyle - {Object} hash of styles feature styles to extend
* *
* Returns: * Returns:
* {<OpenLayers.Feature.Vector.style>} hash of feature styles * {<OpenLayers.Feature.Vector.style>} hash of feature styles
*/ */
createStyle: function(feature) { createStyle: function(feature) {
var style = OpenLayers.Util.extend({}, this.defaultStyle); var style = OpenLayers.Util.extend({}, this.defaultStyle);
var rules = this.rules; var rules = this.rules;
var rule; var rule, context;
var elseRules = []; var elseRules = [];
var appliedRules = false; var appliedRules = false;
for(var i=0; i<rules.length; i++) { for(var i=0; i<rules.length; i++) {
rule = rules[i]; rule = rules[i];
// does the rule apply? context = rule.context;
var applies = rule.evaluate(feature); if (!context) {
context = feature.attributes || feature.data;
if (rule.minScaleDenominator || rule.maxScaleDenominator) { }
var scale = feature.layer.map.getScale(); // does the rule apply?
} var applies = rule.evaluate(feature);
// check if within minScale/maxScale bounds if(applies) {
if (rule.minScaleDenominator) { if(rule instanceof OpenLayers.Rule && rule.elseFilter) {
applies = scale >= OpenLayers.Style.createLiteral( elseRules.push(rule);
rule.minScaleDenominator, feature); } else {
} appliedRules = true;
if (applies && rule.maxScaleDenominator) { this.applySymbolizer(rule, style, feature, context);
applies = scale < OpenLayers.Style.createLiteral( }
rule.maxScaleDenominator, feature); }
} }
if(applies) { // if no other rules apply, apply the rules with else filters
if(rule instanceof OpenLayers.Rule && rule.elseFilter) { if(appliedRules == false && elseRules.length > 0) {
elseRules.push(rule); appliedRules = true;
} else { for(var i=0; i<elseRules.length; i++) {
appliedRules = true; this.applySymbolizer(elseRules[i], style, feature, context);
this.applySymbolizer(rule, style, feature); }
} }
}
} // don't display if there were rules but none applied
if(rules.length > 0 && appliedRules == false) {
// if no other rules apply, apply the rules with else filters style.display = "none";
if(appliedRules == false && elseRules.length > 0) { } else {
appliedRules = true; style.display = "";
for(var i=0; i<elseRules.length; i++) { }
this.applySymbolizer(elseRules[i], style, feature);
} return style;
} },
// calculate literals for all styles in the propertyStyles cache /**
this.createLiterals(style, feature); * Method: applySymbolizer
*
// don't display if there were rules but none applied * Parameters:
if(rules.length > 0 && appliedRules == false) { * rule - {OpenLayers.Rule}
style.display = "none"; * style - {Object}
} else { * feature - {<OpenLayer.Feature.Vector>}
style.display = ""; * context - {Object}
} *
* Returns:
return style; * {Object} A style with new symbolizer applied.
}, */
applySymbolizer: function(rule, style, feature, context) {
/** var symbolizerPrefix = feature.geometry ?
* Method: applySymbolizer this.getSymbolizerPrefix(feature.geometry) :
* OpenLayers.Style.SYMBOLIZER_PREFIXES[0];
* Parameters:
* rule - {OpenLayers.Rule} var symbolizer = rule.symbolizer[symbolizerPrefix];
* style - {Object}
* feature - {<OpenLayer.Feature.Vector>} // merge the style with the current style
* return this.createLiterals(
* Returns: OpenLayers.Util.extend(style, symbolizer), context);
* {Object} A style with new symbolizer applied. },
*/
applySymbolizer: function(rule, style, feature) { /**
var symbolizerPrefix = feature.geometry ? * Method: createLiterals
this.getSymbolizerPrefix(feature.geometry) : * creates literals for all style properties that have an entry in
OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; * <this.propertyStyles>.
*
// merge the style with the current style * Parameters:
var symbolizer = rule.symbolizer[symbolizerPrefix]; * style - {Object} style to create literals for. Will be modified
return OpenLayers.Util.extend(style, symbolizer); * inline.
}, * context - {Object} context to take property values from. Defaults to
* feature.attributes (or feature.data, if attributes are not
/** * available)
* Method: createLiterals *
* creates literals for all style properties that have an entry in * Returns;
* <this.propertyStyles>. * {Object} the modified style
* */
* Parameters: createLiterals: function(style, context) {
* style - {Object} style to create literals for. Will be modified for (var i in this.propertyStyles) {
* inline. style[i] = OpenLayers.Style.createLiteral(style[i], context);
* feature - {<OpenLayers.Feature.Vector>} feature to take properties from }
* return style;
* Returns; },
* {Object} the modified style
*/ /**
createLiterals: function(style, feature) { * Method: findPropertyStyles
for (var i in this.propertyStyles) { * Looks into all rules for this style and the defaultStyle to collect
style[i] = OpenLayers.Style.createLiteral(style[i], feature); * all the style hash property names containing ${...} strings that have
} * to be replaced using the createLiteral method before returning them.
return style; *
}, * Returns:
* {Object} hash of property names that need createLiteral parsing. The
/** * name of the property is the key, and the value is true;
* Method: findPropertyStyles */
* Looks into all rules for this style and the defaultStyle to collect findPropertyStyles: function() {
* all the style hash property names containing ${...} strings that have var propertyStyles = {};
* to be replaced using the createLiteral method before returning them.
* // check the default style
* Returns: var style = this.defaultStyle;
* {Object} hash of property names that need createLiteral parsing. The for (var i in style) {
* name of the property is the key, and the value is true; if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) {
*/ propertyStyles[i] = true;
findPropertyStyles: function() { }
var propertyStyles = {}; }
// check the default style // walk through all rules to check for properties in their symbolizer
var style = this.defaultStyle; var rules = this.rules;
for (var i in style) { var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES;
if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) { for (var i in rules) {
propertyStyles[i] = true; for (var s=0; s<prefixes.length; s++) {
} style = rules[i].symbolizer[prefixes[s]];
} for (var j in style) {
if (typeof style[j] == "string" &&
// walk through all rules to check for properties in their symbolizer style[j].match(/\$\{\w+\}/)) {
var rules = this.rules; propertyStyles[j] = true;
var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES; }
for (var i in rules) { }
for (var s=0; s<prefixes.length; s++) { }
style = rules[i].symbolizer[prefixes[s]]; }
for (var j in style) { return propertyStyles;
if (typeof style[j] == "string" && },
style[j].match(/\$\{\w+\}/)) {
propertyStyles[j] = true; /**
} * APIMethod: addRules
} * Adds rules to this style.
} *
} * Parameters:
return propertyStyles; * rules - {Array(<OpenLayers.Rule>)}
}, */
addRules: function(rules) {
/** this.rules = this.rules.concat(rules);
* APIMethod: addRules this.propertyStyles = this.findPropertyStyles();
* Adds rules to this style. },
*
* Parameters: /**
* rules - {Array(<OpenLayers.Rule>)} * APIMethod: setDefaultStyle
*/ * Sets the default style for this style object.
addRules: function(rules) { *
this.rules = this.rules.concat(rules); * Parameters:
this.propertyStyles = this.findPropertyStyles(); * style - {Object} Hash of style properties
}, */
setDefaultStyle: function(style) {
/** this.defaultStyle = style;
* APIMethod: setDefaultStyle this.propertyStyles = this.findPropertyStyles();
* Sets the default style for this style object. },
*
* Parameters: /**
* style - {Object} Hash of style properties * Method: getSymbolizerPrefix
*/ * Returns the correct symbolizer prefix according to the
setDefaultStyle: function(style) { * geometry type of the passed geometry
this.defaultStyle = style; *
this.propertyStyles = this.findPropertyStyles(); * Parameters:
}, * geometry {<OpenLayers.Geometry>}
*
/** * Returns:
* Method: getSymbolizerPrefix * {String} key of the according symbolizer
* Returns the correct symbolizer prefix according to the */
* geometry type of the passed geometry getSymbolizerPrefix: function(geometry) {
* var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES;
* Parameters: for (var i=0; i<prefixes.length; i++) {
* geometry {<OpenLayers.Geometry>} if (geometry.CLASS_NAME.indexOf(prefixes[i]) != -1) {
* return prefixes[i];
* Returns: }
* {String} key of the according symbolizer }
*/ },
getSymbolizerPrefix: function(geometry) {
var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES; CLASS_NAME: "OpenLayers.Style"
for (var i=0; i<prefixes.length; i++) { });
if (geometry.CLASS_NAME.indexOf(prefixes[i]) != -1) {
return prefixes[i];
} /**
} * Function: createLiteral
}, * converts a style value holding a combination of PropertyName and Literal
* into a Literal, taking the property values from the passed features.
CLASS_NAME: "OpenLayers.Style" *
}); * Parameters:
* value {String} value to parse. If this string contains a construct like
* "foo ${bar}", then "foo " will be taken as literal, and "${bar}"
/** * will be replaced by the value of the "bar" attribute of the passed
* Function: createLiteral * feature.
* converts a style value holding a combination of PropertyName and Literal * context {Object} context to take attribute values from
* into a Literal, taking the property values from the passed features. *
* * Returns:
* Parameters: * {String} the parsed value. In the example of the value parameter above, the
* value {String} value to parse. If this string contains a construct like * result would be "foo valueOfBar", assuming that the passed feature has an
* "foo ${bar}", then "foo " will be taken as literal, and "${bar}" * attribute named "bar" with the value "valueOfBar".
* will be replaced by the value of the "bar" attribute of the passed */
* feature. OpenLayers.Style.createLiteral = function(value, context) {
* feature {<OpenLayers.Feature>} feature to take attribute values from if (typeof value == "string" && value.indexOf("${") != -1) {
* value = OpenLayers.String.format(value, context)
* Returns: value = isNaN(value) ? value : parseFloat(value);
* {String} the parsed value. In the example of the value parameter above, the }
* result would be "foo valueOfBar", assuming that the passed feature has an return value;
* attribute named "bar" with the value "valueOfBar". }
*/
OpenLayers.Style.createLiteral = function(value, feature) { /**
if (typeof value == "string" && value.indexOf("${") != -1) { * Constant: OpenLayers.Style.SYMBOLIZER_PREFIXES
var attributes = feature.attributes || feature.data; * {Array} prefixes of the sld symbolizers. These are the
value = OpenLayers.String.format(value, attributes) * same as the main geometry types
value = isNaN(value) ? value : parseFloat(value); */
} OpenLayers.Style.SYMBOLIZER_PREFIXES = ['Point', 'Line', 'Polygon'];
return value;
}
/**
* Constant: OpenLayers.Style.SYMBOLIZER_PREFIXES
* {Array} prefixes of the sld symbolizers. These are the
* same as the main geometry types
*/
OpenLayers.Style.SYMBOLIZER_PREFIXES = ['Point', 'Line', 'Polygon'];

View File

@@ -1,41 +1,41 @@
<html> <html>
<head> <head>
<script src="../../lib/OpenLayers.js"></script> <script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript"> <script type="text/javascript">
function test_Logical_constructor(t) { function test_Logical_constructor(t) {
t.plan(3); t.plan(3);
var options = {'foo': 'bar'}; var options = {'foo': 'bar'};
var rule = new OpenLayers.Rule.Logical(options); var rule = new OpenLayers.Rule.Logical(options);
t.ok(rule instanceof OpenLayers.Rule.Logical, t.ok(rule instanceof OpenLayers.Rule.Logical,
"new OpenLayers.Rule.Logical returns object" ); "new OpenLayers.Rule.Logical returns object" );
t.eq(rule.foo, "bar", "constructor sets options correctly"); t.eq(rule.foo, "bar", "constructor sets options correctly");
t.eq(typeof rule.evaluate, "function", "rule has an evaluate function"); t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");
} }
function test_Logical_destroy(t) { function test_Logical_destroy(t) {
t.plan(1); t.plan(1);
var rule = new OpenLayers.Rule.Logical(); var rule = new OpenLayers.Rule.Logical();
rule.destroy(); rule.destroy();
t.eq(rule.children, null, "children array nulled properly"); t.eq(rule.rules, null, "rules array nulled properly");
} }
function test_Logical_evaluate(t) { function test_Logical_evaluate(t) {
t.plan(1); t.plan(1);
var rule = new OpenLayers.Rule.Logical({ var rule = new OpenLayers.Rule.Logical({
type: OpenLayers.Rule.Logical.NOT}); type: OpenLayers.Rule.Logical.NOT});
rule.children.push(new OpenLayers.Rule()); rule.rules.push(new OpenLayers.Rule());
var feature = new OpenLayers.Feature.Vector(); var feature = new OpenLayers.Feature.Vector();
t.eq(rule.evaluate(feature), false, t.eq(rule.evaluate(feature), false,
"feature evaluates to false correctly."); "feature evaluates to false correctly.");
} }
</script> </script>
</head> </head>
<body> <body>
</body> </body>
</html> </html>

View File

@@ -1,131 +1,146 @@
<html> <html>
<head> <head>
<script src="../lib/OpenLayers.js"></script> <script src="../lib/OpenLayers.js"></script>
<script type="text/javascript"> <script type="text/javascript">
function test_Style_constructor(t) { function test_Style_constructor(t) {
t.plan(3); t.plan(3);
var options = {'foo': 'bar'}; var options = {'foo': 'bar'};
var style = new OpenLayers.Style(null, options); var style = new OpenLayers.Style(null, options);
t.ok(style instanceof OpenLayers.Style, t.ok(style instanceof OpenLayers.Style,
"new OpenLayers.Style returns object" ); "new OpenLayers.Style returns object" );
t.eq(style.foo, "bar", "constructor sets options correctly"); t.eq(style.foo, "bar", "constructor sets options correctly");
t.eq(typeof style.createStyle, "function", "style has a createStyle function"); t.eq(typeof style.createStyle, "function", "style has a createStyle function");
} }
function test_Style_create(t) { function test_Style_create(t) {
t.plan(10); t.plan(10);
var map = new OpenLayers.Map("map"); var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector("layer"); var layer = new OpenLayers.Layer.Vector("layer");
var baseStyle = OpenLayers.Util.extend( var baseStyle = OpenLayers.Util.extend(
OpenLayers.Feature.Vector.style["default"], OpenLayers.Feature.Vector.style["default"],
{externalGraphic: "bar${foo}.png"}); {externalGraphic: "bar${foo}.png"});
var style = new OpenLayers.Style(baseStyle); var style = new OpenLayers.Style(baseStyle);
var rule1 = new OpenLayers.Rule.FeatureId({ var rule1 = new OpenLayers.Rule.FeatureId({
fids: ["1"], fids: ["1"],
symbolizer: {"Point": {fillColor: "green"}}, symbolizer: {"Point": {fillColor: "green"}},
maxScaleDenominator: 500000}); maxScaleDenominator: 500000});
var rule2 = new OpenLayers.Rule.FeatureId({ var rule2 = new OpenLayers.Rule.FeatureId({
fids: ["1"], fids: ["1"],
symbolizer: {"Point": {fillColor: "yellow"}}, symbolizer: {"Point": {fillColor: "yellow"}},
minScaleDenominator: 500000, minScaleDenominator: 500000,
maxScaleDenominator: 1000000}); maxScaleDenominator: 1000000});
var rule3 = new OpenLayers.Rule.FeatureId({ var rule3 = new OpenLayers.Rule.FeatureId({
fids: ["1"], fids: ["1"],
symbolizer: {"Point": {fillColor: "red"}}, symbolizer: {"Point": {fillColor: "red"}},
minScaleDenominator: 1000000, minScaleDenominator: 1000000,
maxScaleDenominator: 2500000}); maxScaleDenominator: 2500000});
style.addRules([rule1, rule2, rule3]); style.addRules([rule1, rule2, rule3]);
var feature = new OpenLayers.Feature.Vector( var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(3,5), new OpenLayers.Geometry.Point(3,5),
{"foo": "bar"}, {"foo": "bar"},
style); style);
feature.fid = "1"; feature.fid = "1";
// for this fid, the above rule should apply // for this fid, the above rule should apply
layer.addFeatures([feature]); layer.addFeatures([feature]);
map.addLayer(layer); map.addLayer(layer);
map.setBaseLayer(layer); map.setBaseLayer(layer);
map.setCenter(new OpenLayers.LonLat(3,5), 10); map.setCenter(new OpenLayers.LonLat(3,5), 10);
// at this scale, the feature should be green // at this scale, the feature should be green
var createdStyle = style.createStyle(feature); var createdStyle = style.createStyle(feature);
t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style correctly."); t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style correctly.");
t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale());
t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule applied correctly."); t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule applied correctly.");
map.setCenter(new OpenLayers.LonLat(3,5), 9); map.setCenter(new OpenLayers.LonLat(3,5), 9);
// at this scale, the feature should be red // at this scale, the feature should be red
createdStyle = style.createStyle(feature); createdStyle = style.createStyle(feature);
t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale());
t.eq(createdStyle.fillColor, "yellow", "Point symbolizer from rule applied correctly."); t.eq(createdStyle.fillColor, "yellow", "Point symbolizer from rule applied correctly.");
map.setCenter(new OpenLayers.LonLat(3,5), 8); map.setCenter(new OpenLayers.LonLat(3,5), 8);
// at this scale, the feature should be yellow // at this scale, the feature should be yellow
createdStyle = style.createStyle(feature); createdStyle = style.createStyle(feature);
t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale());
t.eq(createdStyle.fillColor, "red", "Point symbolizer from rule applied correctly."); t.eq(createdStyle.fillColor, "red", "Point symbolizer from rule applied correctly.");
map.setCenter(new OpenLayers.LonLat(3,5), 7); map.setCenter(new OpenLayers.LonLat(3,5), 7);
// at this scale, the feature should be invisible // at this scale, the feature should be invisible
createdStyle = style.createStyle(feature); createdStyle = style.createStyle(feature);
t.eq(createdStyle.display, "none", "Feature is invisible at scale "+map.getScale()); t.eq(createdStyle.display, "none", "Feature is invisible at scale "+map.getScale());
t.eq(createdStyle.fillColor, baseStyle.fillColor, "Point symbolizer from base style applied correctly."); t.eq(createdStyle.fillColor, baseStyle.fillColor, "Point symbolizer from base style applied correctly.");
feature.fid = "2"; feature.fid = "2";
// now the rule should not apply // now the rule should not apply
createdStyle = style.createStyle(feature); createdStyle = style.createStyle(feature);
t.eq(createdStyle.fillColor, baseStyle.fillColor, "Correct style for rule that does not apply to fid=\"2\"."); t.eq(createdStyle.fillColor, baseStyle.fillColor, "Correct style for rule that does not apply to fid=\"2\".");
} }
function test_Style_createStyle(t) { function test_Style_createStyle(t) {
t.plan(2); t.plan(2);
var style = new OpenLayers.Style(); var style = new OpenLayers.Style();
var rule = new OpenLayers.Rule({ var rule = new OpenLayers.Rule({
id: Math.random() id: Math.random()
}); });
var elseRule = new OpenLayers.Rule({ var elseRule = new OpenLayers.Rule({
id: Math.random(), id: Math.random(),
elseFilter: true elseFilter: true
}); });
style.addRules([rule, elseRule]); style.addRules([rule, elseRule]);
// test that applySymbolizer is only called with rule // test that applySymbolizer is only called with rule
style.applySymbolizer = function(r) { style.applySymbolizer = function(r) {
t.eq(r.id, rule.id, "(plain) applySymbolizer called with correct rule"); t.eq(r.id, rule.id, "(plain) applySymbolizer called with correct rule");
} }
style.createStyle(new OpenLayers.Feature.Vector()); style.createStyle(new OpenLayers.Feature.Vector());
rule.evaluate = function() {return false;}; rule.evaluate = function() {return false;};
style.applySymbolizer = function(r) { style.applySymbolizer = function(r) {
t.eq(r.id, elseRule.id, "(else) applySymbolizer called with correct rule"); t.eq(r.id, elseRule.id, "(else) applySymbolizer called with correct rule");
} }
style.createStyle(new OpenLayers.Feature.Vector()); style.createStyle(new OpenLayers.Feature.Vector());
}
} function test_Style_context(t) {
t.plan(1);
function test_Style_destroy(t) { var context = {
t.plan(1); foo: "bar",
size: 10};
var style = new OpenLayers.Style(); var rule = new OpenLayers.Rule.Comparison({
style.destroy(); type: OpenLayers.Rule.Comparison.LESS_THAN,
t.eq(style.rules, null, "rules array nulled properly"); context: context,
} property: "size",
value: 11,
</script> symbolizer: {"Point": {externalGraphic: "${foo}.png"}}});
</head> var style = new OpenLayers.Style();
<body> style.addRules([rule]);
<div id="map" style="width:500px;height:500px"></div> var styleHash = style.createStyle(new OpenLayers.Feature.Vector());
</body> t.eq(styleHash.externalGraphic, "bar.png", "correctly evaluated rule against a custom context");
</html> }
function test_Style_destroy(t) {
t.plan(1);
var style = new OpenLayers.Style();
style.destroy();
t.eq(style.rules, null, "rules array nulled properly");
}
</script>
</head>
<body>
<div id="map" style="width:500px;height:500px"></div>
</body>
</html>