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.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */
/**
* @requires OpenLayers/Util.js
* @requires OpenLayers/Style.js
*/
/**
* Class: OpenLayers.Rule
* This class represents a OGC Rule, as being used for rule-based SLD styling.
*/
OpenLayers.Rule = OpenLayers.Class({
/**
* APIProperty: name
* {String} name of this rule
*/
name: 'default',
/**
* Property: elseFilter
* {Boolean} Determines whether this rule is only to be applied only if
* no other rules match (ElseFilter according to the SLD specification).
* Default is false. For instances of OpenLayers.Rule, if elseFilter is
* false, the rule will always apply. For subclasses, the else property is
* ignored.
*/
elseFilter: false,
/**
* Property: symbolizer
* {Object} Hash of styles for this rule. Contains hashes of feature
* styles. Keys are one or more of ["Point", "Line", "Polygon"]
*/
symbolizer: null,
/**
* APIProperty: minScaleDenominator
* {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
* propertyNames in the form "literal ${propertyName}"
*/
minScaleDenominator: null,
/**
* APIProperty: maxScaleDenominator
* {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
* propertyNames in the form "literal ${propertyName}"
*/
maxScaleDenominator: null,
/**
* Constructor: OpenLayers.Rule
* Creates a Rule.
*
* Parameters:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule>}
*/
initialize: function(options) {
this.symbolizer = {};
OpenLayers.Util.extend(this, options);
},
/**
* APIMethod: destroy
* nullify references to prevent circular references and memory leaks
*/
destroy: function() {
for (var i in this.symbolizer) {
this.symbolizer[i] = null;
}
this.symbolizer = null;
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
*
* Returns:
* {boolean} true if the rule applies, false if it does not.
* This rule is the default rule and always returns true.
*/
evaluate: function(feature) {
// Default rule always applies. Subclasses will want to override this.
return true;
},
CLASS_NAME: "OpenLayers.Rule"
/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */
/**
* @requires OpenLayers/Util.js
* @requires OpenLayers/Style.js
*/
/**
* Class: OpenLayers.Rule
* This class represents a OGC Rule, as being used for rule-based SLD styling.
*/
OpenLayers.Rule = OpenLayers.Class({
/**
* APIProperty: name
* {String} name of this rule
*/
name: 'default',
/**
* Property: context
* {Object} An optional object with properties that the rule and its
* symbolizers' property values should be evaluatad against. If no
* context is specified, feature.attributes will be used
*/
context: null,
/**
* Property: elseFilter
* {Boolean} Determines whether this rule is only to be applied only if
* no other rules match (ElseFilter according to the SLD specification).
* Default is false. For instances of OpenLayers.Rule, if elseFilter is
* false, the rule will always apply. For subclasses, the else property is
* ignored.
*/
elseFilter: false,
/**
* Property: symbolizer
* {Object} Hash of styles for this rule. Contains hashes of feature
* styles. Keys are one or more of ["Point", "Line", "Polygon"]
*/
symbolizer: null,
/**
* APIProperty: minScaleDenominator
* {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
* propertyNames in the form "literal ${propertyName}"
*/
minScaleDenominator: null,
/**
* APIProperty: maxScaleDenominator
* {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
* propertyNames in the form "literal ${propertyName}"
*/
maxScaleDenominator: null,
/**
* Constructor: OpenLayers.Rule
* Creates a Rule.
*
* Parameters:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule>}
*/
initialize: function(options) {
this.symbolizer = {};
OpenLayers.Util.extend(this, options);
},
/**
* APIMethod: destroy
* nullify references to prevent circular references and memory leaks
*/
destroy: function() {
for (var i in this.symbolizer) {
this.symbolizer[i] = null;
}
this.symbolizer = null;
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
*
* Returns:
* {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
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Rule.js
*/
/**
* Class: OpenLayers.Rule.Comparison
* This class represents the comparison rules, as being used for rule-based
* SLD styling
*
* Inherits from
* - <OpenLayers.Rule>
*/
OpenLayers.Rule.Comparison = OpenLayers.Class(OpenLayers.Rule, {
/**
* APIProperty: type
* {String} type: type of the comparison. This is one of
* - OpenLayers.Rule.Comparison.EQUAL_TO = "==";
* - OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
* - OpenLayers.Rule.Comparison.LESS_THAN = "<";
* - OpenLayers.Rule.Comparison.GREATER_THAN = ">";
* - OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
* - OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
* - OpenLayers.Rule.Comparison.BETWEEN = "..";
* - OpenLayers.Rule.Comparison.LIKE = "~";
*/
type: null,
/**
* APIProperty: property
* {String}
* name of the feature attribute to compare
*/
property: null,
/**
* APIProperty: value
* {Number} or {String}
* comparison value for binary comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form
* "literal ${propertyName}"
*/
value: null,
/**
* APIProperty: lowerBoundary
* {Number} or {String}
* lower boundary for between comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form
* "literal ${propertyName}"
*/
lowerBoundary: null,
/**
* APIProperty: upperBoundary
* {Number} or {String}
* upper boundary for between comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form
* "literal ${propertyName}"
*/
upperBoundary: null,
/**
* Constructor: OpenLayers.Rule.Comparison
* Creates a comparison rule.
*
* Parameters:
* params - {Object} Hash of parameters for this rule:
* -
* - value:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule.Comparison>}
*/
initialize: function(options) {
OpenLayers.Rule.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
*
* Returns:
* {boolean} true if the rule applies, false if it does not
*/
evaluate: function(feature) {
var attributes = feature.attributes || feature.data;
switch(this.type) {
case OpenLayers.Rule.Comparison.EQUAL_TO:
case OpenLayers.Rule.Comparison.LESS_THAN:
case OpenLayers.Rule.Comparison.GREATER_THAN:
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO:
case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO:
return this.binaryCompare(feature, this.property, this.value);
case OpenLayers.Rule.Comparison.BETWEEN:
var result =
attributes[this.property] > this.lowerBoundary;
result = result &&
attributes[this.property] < this.upperBoundary;
return result;
case OpenLayers.Rule.Comparison.LIKE:
var regexp = new RegExp(this.value,
"gi");
return regexp.test(attributes[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
* regular expression already.
*
* Parameters:
* wildCard - {<Char>} wildcard character in the above value, default
* is "*"
* singleChar - {<Char>) single-character wildcard in the above value
* default is "."
* escape - {<Char>) escape character in the above value, default is
* "!"
*
* Returns:
* {String} regular expression string
*/
value2regex: function(wildCard, singleChar, escapeChar) {
if (wildCard == ".") {
var msg = "'.' is an unsupported wildCard character for "+
"OpenLayers.Rule.Comparison";
OpenLayers.Console.error(msg);
return null;
}
// set UMN MapServer defaults for unspecified parameters
wildCard = wildCard ? wildCard : "*";
singleChar = singleChar ? singleChar : ".";
escapeChar = escapeChar ? escapeChar : "!";
this.value = this.value.replace(
new RegExp("\\"+escapeChar, "g"), "\\");
this.value = this.value.replace(
new RegExp("\\"+singleChar, "g"), ".");
this.value = this.value.replace(
new RegExp("\\"+wildCard, "g"), ".*");
this.value = this.value.replace(
new RegExp("\\\\.\\*", "g"), "\\"+wildCard);
this.value = this.value.replace(
new RegExp("\\\\\\.", "g"), "\\"+singleChar);
return this.value;
},
/**
* Function: binaryCompare
* Compares a feature property to a rule value
*
* Parameters:
* feature - {<OpenLayers.Feature>}
* property - {String} or {Number}
* value - {String} or {Number}, same as property
*
* Returns:
* {boolean}
*/
binaryCompare: function(feature, property, value) {
var attributes = feature.attributes || feature.data;
switch (this.type) {
case OpenLayers.Rule.Comparison.EQUAL_TO:
return attributes[property] == value;
case OpenLayers.Rule.Comparison.NOT_EQUAL_TO:
return attributes[property] != value;
case OpenLayers.Rule.Comparison.LESS_THAN:
return attributes[property] < value;
case OpenLayers.Rule.Comparison.GREATER_THAN:
return attributes[property] > value;
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO:
return attributes[property] <= value;
case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO:
return attributes[property] >= value;
}
},
CLASS_NAME: "OpenLayers.Rule.Comparison"
});
OpenLayers.Rule.Comparison.EQUAL_TO = "==";
OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
OpenLayers.Rule.Comparison.LESS_THAN = "<";
OpenLayers.Rule.Comparison.GREATER_THAN = ">";
OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
OpenLayers.Rule.Comparison.BETWEEN = "..";
OpenLayers.Rule.Comparison.LIKE = "~";
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Rule.js
*/
/**
* Class: OpenLayers.Rule.Comparison
* This class represents the comparison rules, as being used for rule-based
* SLD styling
*
* Inherits from
* - <OpenLayers.Rule>
*/
OpenLayers.Rule.Comparison = OpenLayers.Class(OpenLayers.Rule, {
/**
* APIProperty: type
* {String} type: type of the comparison. This is one of
* - OpenLayers.Rule.Comparison.EQUAL_TO = "==";
* - OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
* - OpenLayers.Rule.Comparison.LESS_THAN = "<";
* - OpenLayers.Rule.Comparison.GREATER_THAN = ">";
* - OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
* - OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
* - OpenLayers.Rule.Comparison.BETWEEN = "..";
* - OpenLayers.Rule.Comparison.LIKE = "~";
*/
type: null,
/**
* APIProperty: property
* {String}
* name of the context property to compare
*/
property: null,
/**
* APIProperty: value
* {Number} or {String}
* comparison value for binary comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form
* "literal ${propertyName}"
*/
value: null,
/**
* APIProperty: lowerBoundary
* {Number} or {String}
* lower boundary for between comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form
* "literal ${propertyName}"
*/
lowerBoundary: null,
/**
* APIProperty: upperBoundary
* {Number} or {String}
* upper boundary for between comparisons. In the case of a String, this
* can be a combination of text and propertyNames in the form
* "literal ${propertyName}"
*/
upperBoundary: null,
/**
* Constructor: OpenLayers.Rule.Comparison
* Creates a comparison rule.
*
* Parameters:
* params - {Object} Hash of parameters for this rule:
* -
* - value:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule.Comparison>}
*/
initialize: function(options) {
OpenLayers.Rule.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific context
*
* Parameters:
* context - {Object} context to apply the rule to.
*
* Returns:
* {boolean} true if the rule applies, false if it does not
*/
evaluate: function(feature) {
if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) {
return false;
}
var context = this.getContext(feature);
switch(this.type) {
case OpenLayers.Rule.Comparison.EQUAL_TO:
case OpenLayers.Rule.Comparison.LESS_THAN:
case OpenLayers.Rule.Comparison.GREATER_THAN:
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO:
case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO:
return this.binaryCompare(context, this.property, this.value);
case OpenLayers.Rule.Comparison.BETWEEN:
var result =
context[this.property] > this.lowerBoundary;
result = result &&
context[this.property] < this.upperBoundary;
return result;
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
* regular expression already.
*
* Parameters:
* wildCard - {<Char>} wildcard character in the above value, default
* is "*"
* singleChar - {<Char>) single-character wildcard in the above value
* default is "."
* escape - {<Char>) escape character in the above value, default is
* "!"
*
* Returns:
* {String} regular expression string
*/
value2regex: function(wildCard, singleChar, escapeChar) {
if (wildCard == ".") {
var msg = "'.' is an unsupported wildCard character for "+
"OpenLayers.Rule.Comparison";
OpenLayers.Console.error(msg);
return null;
}
// set UMN MapServer defaults for unspecified parameters
wildCard = wildCard ? wildCard : "*";
singleChar = singleChar ? singleChar : ".";
escapeChar = escapeChar ? escapeChar : "!";
this.value = this.value.replace(
new RegExp("\\"+escapeChar, "g"), "\\");
this.value = this.value.replace(
new RegExp("\\"+singleChar, "g"), ".");
this.value = this.value.replace(
new RegExp("\\"+wildCard, "g"), ".*");
this.value = this.value.replace(
new RegExp("\\\\.\\*", "g"), "\\"+wildCard);
this.value = this.value.replace(
new RegExp("\\\\\\.", "g"), "\\"+singleChar);
return this.value;
},
/**
* Function: binaryCompare
* Compares a feature property to a rule value
*
* Parameters:
* context - {Object}
* property - {String} or {Number}
* value - {String} or {Number}, same as property
*
* Returns:
* {boolean}
*/
binaryCompare: function(context, property, value) {
switch (this.type) {
case OpenLayers.Rule.Comparison.EQUAL_TO:
return context[property] == value;
case OpenLayers.Rule.Comparison.NOT_EQUAL_TO:
return context[property] != value;
case OpenLayers.Rule.Comparison.LESS_THAN:
return context[property] < value;
case OpenLayers.Rule.Comparison.GREATER_THAN:
return context[property] > value;
case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO:
return context[property] <= value;
case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO:
return context[property] >= value;
}
},
CLASS_NAME: "OpenLayers.Rule.Comparison"
});
OpenLayers.Rule.Comparison.EQUAL_TO = "==";
OpenLayers.Rule.Comparison.NOT_EQUAL_TO = "!=";
OpenLayers.Rule.Comparison.LESS_THAN = "<";
OpenLayers.Rule.Comparison.GREATER_THAN = ">";
OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
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
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Rule.js
*/
/**
* Class: OpenLayers.Rule.FeatureId
* This class represents a ogc:FeatureId Rule, as being used for rule-based SLD
* styling
*
* Inherits from
* - <OpenLayers.Rule>
*/
OpenLayers.Rule.FeatureId = OpenLayers.Class(OpenLayers.Rule, {
/**
* APIProperty: fids
* {Array(<String>)} Feature Ids to evaluate this rule against. To be passed
* To be passed inside the params object.
*/
fids: null,
/**
* Constructor: OpenLayers.Rule.FeatureId
* Creates an ogc:FeatureId rule.
*
* Parameters:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule.FeatureId>}
*/
initialize: function(options) {
this.fids = [];
OpenLayers.Rule.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
* For vector features, the check is run against the fid,
* for plain features against the id.
*
* Returns:
* {boolean} true if the rule applies, false if it does not
*/
evaluate: function(feature) {
for (var i=0; i<this.fids.length; i++) {
var fid = feature.fid || feature.id;
if (fid == this.fids[i]) {
return true;
}
}
return false;
},
CLASS_NAME: "OpenLayers.Rule.FeatureId"
});
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Rule.js
*/
/**
* Class: OpenLayers.Rule.FeatureId
* This class represents a ogc:FeatureId Rule, as being used for rule-based SLD
* styling
*
* Inherits from
* - <OpenLayers.Rule>
*/
OpenLayers.Rule.FeatureId = OpenLayers.Class(OpenLayers.Rule, {
/**
* APIProperty: fids
* {Array(<String>)} Feature Ids to evaluate this rule against. To be passed
* To be passed inside the params object.
*/
fids: null,
/**
* Constructor: OpenLayers.Rule.FeatureId
* Creates an ogc:FeatureId rule.
*
* Parameters:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule.FeatureId>}
*/
initialize: function(options) {
this.fids = [];
OpenLayers.Rule.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
* For vector features, the check is run against the fid,
* for plain features against the id.
*
* Returns:
* {boolean} true if the rule applies, false if it does not
*/
evaluate: function(feature) {
if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) {
return false;
}
for (var i=0; i<this.fids.length; i++) {
var fid = feature.fid || feature.id;
if (fid == this.fids[i]) {
return true;
}
}
return false;
},
CLASS_NAME: "OpenLayers.Rule.FeatureId"
});

View File

@@ -1,101 +1,104 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Rule.js
*/
/**
* Class: OpenLayers.Rule.Logical
* This class represents ogc:And, ogc:Or and ogc:Not rules.
*
* Inherits from
* - <OpenLayers.Rule>
*/
OpenLayers.Rule.Logical = OpenLayers.Class(OpenLayers.Rule, {
/**
* APIProperty: children
* {Array(<OpenLayers.Rule>)} child rules for this rule
*/
children: null,
/**
* APIProperty: type
* {String} type of logical operator. Available types are:
* - OpenLayers.Rule.Locical.AND = "&&";
* - OpenLayers.Rule.Logical.OR = "||";
* - OpenLayers.Rule.Logical.NOT = "!";
*/
type: null,
/**
* Constructor: OpenLayers.Rule.Logical
* Creates a logical rule (And, Or, Not).
*
* Parameters:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule.Logical>}
*/
initialize: function(options) {
this.children = [];
OpenLayers.Rule.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: destroy
* nullify references to prevent circular references and memory leaks
*/
destroy: function() {
for (var i=0; i<this.children.length; i++) {
this.children[i].destroy();
}
this.children = null;
OpenLayers.Rule.prototype.destroy.apply(this, arguments);
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
*
* Returns:
* {boolean} true if the rule applies, false if it does not
*/
evaluate: function(feature) {
switch(this.type) {
case OpenLayers.Rule.Logical.AND:
for (var i=0; i<this.children.length; i++) {
if (this.children[i].evaluate(feature) == false) {
return false;
}
}
return true;
case OpenLayers.Rule.Logical.OR:
for (var i=0; i<this.children.length; i++) {
if (this.children[i].evaluate(feature) == true) {
return true;
}
}
return false;
case OpenLayers.Rule.Logical.NOT:
return (!this.children[0].evaluate(feature));
}
},
CLASS_NAME: "OpenLayers.Rule.Logical"
});
OpenLayers.Rule.Logical.AND = "&&";
OpenLayers.Rule.Logical.OR = "||";
OpenLayers.Rule.Logical.NOT = "!";
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Rule.js
*/
/**
* Class: OpenLayers.Rule.Logical
* This class represents ogc:And, ogc:Or and ogc:Not rules.
*
* Inherits from
* - <OpenLayers.Rule>
*/
OpenLayers.Rule.Logical = OpenLayers.Class(OpenLayers.Rule, {
/**
* APIProperty: children
* {Array(<OpenLayers.Rule>)} child rules for this rule
*/
rules: null,
/**
* APIProperty: type
* {String} type of logical operator. Available types are:
* - OpenLayers.Rule.Locical.AND = "&&";
* - OpenLayers.Rule.Logical.OR = "||";
* - OpenLayers.Rule.Logical.NOT = "!";
*/
type: null,
/**
* Constructor: OpenLayers.Rule.Logical
* Creates a logical rule (And, Or, Not).
*
* Parameters:
* options - {Object} An optional object with properties to set on the
* rule
*
* Returns:
* {<OpenLayers.Rule.Logical>}
*/
initialize: function(options) {
this.rules = [];
OpenLayers.Rule.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: destroy
* nullify references to prevent circular references and memory leaks
*/
destroy: function() {
for (var i=0; i<this.rules.length; i++) {
this.rules[i].destroy();
}
this.rules = null;
OpenLayers.Rule.prototype.destroy.apply(this, arguments);
},
/**
* APIMethod: evaluate
* evaluates this rule for a specific feature
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
*
* Returns:
* {boolean} true if the rule applies, false if it does not
*/
evaluate: function(feature) {
if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) {
return false;
}
switch(this.type) {
case OpenLayers.Rule.Logical.AND:
for (var i=0; i<this.rules.length; i++) {
if (this.rules[i].evaluate(feature) == false) {
return false;
}
}
return true;
case OpenLayers.Rule.Logical.OR:
for (var i=0; i<this.rules.length; i++) {
if (this.rules[i].evaluate(feature) == true) {
return true;
}
}
return false;
case OpenLayers.Rule.Logical.NOT:
return (!this.rules[0].evaluate(feature));
}
},
CLASS_NAME: "OpenLayers.Rule.Logical"
});
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
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Util.js
* @requires OpenLayers/Feature/Vector.js
*/
/**
* Class: OpenLayers.Style
* This class represents a UserStyle obtained
* from a SLD, containing styling rules.
*/
OpenLayers.Style = OpenLayers.Class({
/**
* APIProperty: name
* {String}
*/
name: null,
/**
* APIProperty: layerName
* {<String>} name of the layer that this style belongs to, usually
* according to the NamedLayer attribute of an SLD document.
*/
layerName: null,
/**
* APIProperty: isDefault
* {Boolean}
*/
isDefault: false,
/**
* Property: rules
* Array({<OpenLayers.Rule>})
*/
rules: null,
/**
* Property: defaultStyle
* {Object} hash of style properties to use as default for merging
* rule-based style symbolizers onto. If no rules are defined, createStyle
* will return this style.
*/
defaultStyle: null,
/**
* Property: propertyStyles
* {Hash of Boolean} cache of style properties that need to be parsed for
* propertyNames. Property names are keys, values won't be used.
*/
propertyStyles: null,
/**
* Constructor: OpenLayers.Style
* Creates a UserStyle.
*
* Parameters:
* style - {Object} Optional hash of style properties that will be
* used as default style for this style object. This style
* applies if no rules are specified. Symbolizers defined in
* rules will extend this default style.
* options - {Object} An optional object with properties to set on the
* userStyle
*
* Return:
* {<OpenLayers.Style>}
*/
initialize: function(style, options) {
this.rules = [];
// use the default style from OpenLayers.Feature.Vector if no style
// was given in the constructor
this.setDefaultStyle(style ||
OpenLayers.Feature.Vector.style["default"]);
OpenLayers.Util.extend(this, options);
},
/**
* APIMethod: destroy
* nullify references to prevent circular references and memory leaks
*/
destroy: function() {
for (var i=0; i<this.rules.length; i++) {
this.rules[i].destroy();
this.rules[i] = null;
}
this.rules = null;
this.defaultStyle = null;
},
/**
* APIMethod: createStyle
* creates a style by applying all feature-dependent rules to the base
* style.
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to evaluate rules for
* baseStyle - {Object} hash of styles feature styles to extend
*
* Returns:
* {<OpenLayers.Feature.Vector.style>} hash of feature styles
*/
createStyle: function(feature) {
var style = OpenLayers.Util.extend({}, this.defaultStyle);
var rules = this.rules;
var rule;
var elseRules = [];
var appliedRules = false;
for(var i=0; i<rules.length; i++) {
rule = rules[i];
// does the rule apply?
var applies = rule.evaluate(feature);
if (rule.minScaleDenominator || rule.maxScaleDenominator) {
var scale = feature.layer.map.getScale();
}
// check if within minScale/maxScale bounds
if (rule.minScaleDenominator) {
applies = scale >= OpenLayers.Style.createLiteral(
rule.minScaleDenominator, feature);
}
if (applies && rule.maxScaleDenominator) {
applies = scale < OpenLayers.Style.createLiteral(
rule.maxScaleDenominator, feature);
}
if(applies) {
if(rule instanceof OpenLayers.Rule && rule.elseFilter) {
elseRules.push(rule);
} else {
appliedRules = true;
this.applySymbolizer(rule, style, feature);
}
}
}
// if no other rules apply, apply the rules with else filters
if(appliedRules == false && elseRules.length > 0) {
appliedRules = true;
for(var i=0; i<elseRules.length; i++) {
this.applySymbolizer(elseRules[i], style, feature);
}
}
// calculate literals for all styles in the propertyStyles cache
this.createLiterals(style, feature);
// don't display if there were rules but none applied
if(rules.length > 0 && appliedRules == false) {
style.display = "none";
} else {
style.display = "";
}
return style;
},
/**
* Method: applySymbolizer
*
* Parameters:
* rule - {OpenLayers.Rule}
* style - {Object}
* feature - {<OpenLayer.Feature.Vector>}
*
* Returns:
* {Object} A style with new symbolizer applied.
*/
applySymbolizer: function(rule, style, feature) {
var symbolizerPrefix = feature.geometry ?
this.getSymbolizerPrefix(feature.geometry) :
OpenLayers.Style.SYMBOLIZER_PREFIXES[0];
// merge the style with the current style
var symbolizer = rule.symbolizer[symbolizerPrefix];
return OpenLayers.Util.extend(style, symbolizer);
},
/**
* Method: createLiterals
* creates literals for all style properties that have an entry in
* <this.propertyStyles>.
*
* Parameters:
* style - {Object} style to create literals for. Will be modified
* inline.
* feature - {<OpenLayers.Feature.Vector>} feature to take properties from
*
* Returns;
* {Object} the modified style
*/
createLiterals: function(style, feature) {
for (var i in this.propertyStyles) {
style[i] = OpenLayers.Style.createLiteral(style[i], feature);
}
return style;
},
/**
* Method: findPropertyStyles
* Looks into all rules for this style and the defaultStyle to collect
* all the style hash property names containing ${...} strings that have
* to be replaced using the createLiteral method before returning them.
*
* Returns:
* {Object} hash of property names that need createLiteral parsing. The
* name of the property is the key, and the value is true;
*/
findPropertyStyles: function() {
var propertyStyles = {};
// check the default style
var style = this.defaultStyle;
for (var i in style) {
if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) {
propertyStyles[i] = true;
}
}
// walk through all rules to check for properties in their symbolizer
var rules = this.rules;
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) {
if (typeof style[j] == "string" &&
style[j].match(/\$\{\w+\}/)) {
propertyStyles[j] = true;
}
}
}
}
return propertyStyles;
},
/**
* APIMethod: addRules
* Adds rules to this style.
*
* Parameters:
* rules - {Array(<OpenLayers.Rule>)}
*/
addRules: function(rules) {
this.rules = this.rules.concat(rules);
this.propertyStyles = this.findPropertyStyles();
},
/**
* APIMethod: setDefaultStyle
* Sets the default style for this style object.
*
* Parameters:
* style - {Object} Hash of style properties
*/
setDefaultStyle: function(style) {
this.defaultStyle = style;
this.propertyStyles = this.findPropertyStyles();
},
/**
* Method: getSymbolizerPrefix
* Returns the correct symbolizer prefix according to the
* geometry type of the passed geometry
*
* Parameters:
* geometry {<OpenLayers.Geometry>}
*
* Returns:
* {String} key of the according symbolizer
*/
getSymbolizerPrefix: function(geometry) {
var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES;
for (var i=0; i<prefixes.length; i++) {
if (geometry.CLASS_NAME.indexOf(prefixes[i]) != -1) {
return prefixes[i];
}
}
},
CLASS_NAME: "OpenLayers.Style"
});
/**
* Function: createLiteral
* converts a style value holding a combination of PropertyName and Literal
* into a Literal, taking the property values from the passed features.
*
* 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
* feature.
* feature {<OpenLayers.Feature>} feature to take attribute values from
*
* Returns:
* {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
* attribute named "bar" with the value "valueOfBar".
*/
OpenLayers.Style.createLiteral = function(value, feature) {
if (typeof value == "string" && value.indexOf("${") != -1) {
var attributes = feature.attributes || feature.data;
value = OpenLayers.String.format(value, attributes)
value = isNaN(value) ? value : parseFloat(value);
}
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'];
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Util.js
* @requires OpenLayers/Feature/Vector.js
*/
/**
* Class: OpenLayers.Style
* This class represents a UserStyle obtained
* from a SLD, containing styling rules.
*/
OpenLayers.Style = OpenLayers.Class({
/**
* APIProperty: name
* {String}
*/
name: null,
/**
* APIProperty: layerName
* {<String>} name of the layer that this style belongs to, usually
* according to the NamedLayer attribute of an SLD document.
*/
layerName: null,
/**
* APIProperty: isDefault
* {Boolean}
*/
isDefault: false,
/**
* Property: rules
* Array({<OpenLayers.Rule>})
*/
rules: null,
/**
* Property: defaultStyle
* {Object} hash of style properties to use as default for merging
* rule-based style symbolizers onto. If no rules are defined, createStyle
* will return this style.
*/
defaultStyle: null,
/**
* Property: propertyStyles
* {Hash of Boolean} cache of style properties that need to be parsed for
* propertyNames. Property names are keys, values won't be used.
*/
propertyStyles: null,
/**
* Constructor: OpenLayers.Style
* Creates a UserStyle.
*
* Parameters:
* style - {Object} Optional hash of style properties that will be
* used as default style for this style object. This style
* applies if no rules are specified. Symbolizers defined in
* rules will extend this default style.
* options - {Object} An optional object with properties to set on the
* userStyle
*
* Return:
* {<OpenLayers.Style>}
*/
initialize: function(style, options) {
this.rules = [];
// use the default style from OpenLayers.Feature.Vector if no style
// was given in the constructor
this.setDefaultStyle(style ||
OpenLayers.Feature.Vector.style["default"]);
OpenLayers.Util.extend(this, options);
},
/**
* APIMethod: destroy
* nullify references to prevent circular references and memory leaks
*/
destroy: function() {
for (var i=0; i<this.rules.length; i++) {
this.rules[i].destroy();
this.rules[i] = null;
}
this.rules = null;
this.defaultStyle = null;
},
/**
* APIMethod: createStyle
* creates a style by applying all feature-dependent rules to the base
* style.
*
* Parameters:
* feature - {<OpenLayers.Feature>} feature to evaluate rules for
* baseStyle - {Object} hash of styles feature styles to extend
*
* Returns:
* {<OpenLayers.Feature.Vector.style>} hash of feature styles
*/
createStyle: function(feature) {
var style = OpenLayers.Util.extend({}, this.defaultStyle);
var rules = this.rules;
var rule, context;
var elseRules = [];
var appliedRules = false;
for(var i=0; i<rules.length; i++) {
rule = rules[i];
context = rule.context;
if (!context) {
context = feature.attributes || feature.data;
}
// does the rule apply?
var applies = rule.evaluate(feature);
if(applies) {
if(rule instanceof OpenLayers.Rule && rule.elseFilter) {
elseRules.push(rule);
} else {
appliedRules = true;
this.applySymbolizer(rule, style, feature, context);
}
}
}
// if no other rules apply, apply the rules with else filters
if(appliedRules == false && elseRules.length > 0) {
appliedRules = true;
for(var i=0; i<elseRules.length; i++) {
this.applySymbolizer(elseRules[i], style, feature, context);
}
}
// don't display if there were rules but none applied
if(rules.length > 0 && appliedRules == false) {
style.display = "none";
} else {
style.display = "";
}
return style;
},
/**
* Method: applySymbolizer
*
* Parameters:
* rule - {OpenLayers.Rule}
* style - {Object}
* feature - {<OpenLayer.Feature.Vector>}
* context - {Object}
*
* Returns:
* {Object} A style with new symbolizer applied.
*/
applySymbolizer: function(rule, style, feature, context) {
var symbolizerPrefix = feature.geometry ?
this.getSymbolizerPrefix(feature.geometry) :
OpenLayers.Style.SYMBOLIZER_PREFIXES[0];
var symbolizer = rule.symbolizer[symbolizerPrefix];
// merge the style with the current style
return this.createLiterals(
OpenLayers.Util.extend(style, symbolizer), context);
},
/**
* Method: createLiterals
* creates literals for all style properties that have an entry in
* <this.propertyStyles>.
*
* Parameters:
* style - {Object} style to create literals for. Will be modified
* inline.
* context - {Object} context to take property values from. Defaults to
* feature.attributes (or feature.data, if attributes are not
* available)
*
* Returns;
* {Object} the modified style
*/
createLiterals: function(style, context) {
for (var i in this.propertyStyles) {
style[i] = OpenLayers.Style.createLiteral(style[i], context);
}
return style;
},
/**
* Method: findPropertyStyles
* Looks into all rules for this style and the defaultStyle to collect
* all the style hash property names containing ${...} strings that have
* to be replaced using the createLiteral method before returning them.
*
* Returns:
* {Object} hash of property names that need createLiteral parsing. The
* name of the property is the key, and the value is true;
*/
findPropertyStyles: function() {
var propertyStyles = {};
// check the default style
var style = this.defaultStyle;
for (var i in style) {
if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) {
propertyStyles[i] = true;
}
}
// walk through all rules to check for properties in their symbolizer
var rules = this.rules;
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) {
if (typeof style[j] == "string" &&
style[j].match(/\$\{\w+\}/)) {
propertyStyles[j] = true;
}
}
}
}
return propertyStyles;
},
/**
* APIMethod: addRules
* Adds rules to this style.
*
* Parameters:
* rules - {Array(<OpenLayers.Rule>)}
*/
addRules: function(rules) {
this.rules = this.rules.concat(rules);
this.propertyStyles = this.findPropertyStyles();
},
/**
* APIMethod: setDefaultStyle
* Sets the default style for this style object.
*
* Parameters:
* style - {Object} Hash of style properties
*/
setDefaultStyle: function(style) {
this.defaultStyle = style;
this.propertyStyles = this.findPropertyStyles();
},
/**
* Method: getSymbolizerPrefix
* Returns the correct symbolizer prefix according to the
* geometry type of the passed geometry
*
* Parameters:
* geometry {<OpenLayers.Geometry>}
*
* Returns:
* {String} key of the according symbolizer
*/
getSymbolizerPrefix: function(geometry) {
var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES;
for (var i=0; i<prefixes.length; i++) {
if (geometry.CLASS_NAME.indexOf(prefixes[i]) != -1) {
return prefixes[i];
}
}
},
CLASS_NAME: "OpenLayers.Style"
});
/**
* Function: createLiteral
* converts a style value holding a combination of PropertyName and Literal
* into a Literal, taking the property values from the passed features.
*
* 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
* feature.
* context {Object} context to take attribute values from
*
* Returns:
* {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
* attribute named "bar" with the value "valueOfBar".
*/
OpenLayers.Style.createLiteral = function(value, context) {
if (typeof value == "string" && value.indexOf("${") != -1) {
value = OpenLayers.String.format(value, context)
value = isNaN(value) ? value : parseFloat(value);
}
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>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Logical_constructor(t) {
t.plan(3);
var options = {'foo': 'bar'};
var rule = new OpenLayers.Rule.Logical(options);
t.ok(rule instanceof OpenLayers.Rule.Logical,
"new OpenLayers.Rule.Logical returns object" );
t.eq(rule.foo, "bar", "constructor sets options correctly");
t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");
}
function test_Logical_destroy(t) {
t.plan(1);
var rule = new OpenLayers.Rule.Logical();
rule.destroy();
t.eq(rule.children, null, "children array nulled properly");
}
function test_Logical_evaluate(t) {
t.plan(1);
var rule = new OpenLayers.Rule.Logical({
type: OpenLayers.Rule.Logical.NOT});
rule.children.push(new OpenLayers.Rule());
var feature = new OpenLayers.Feature.Vector();
t.eq(rule.evaluate(feature), false,
"feature evaluates to false correctly.");
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Logical_constructor(t) {
t.plan(3);
var options = {'foo': 'bar'};
var rule = new OpenLayers.Rule.Logical(options);
t.ok(rule instanceof OpenLayers.Rule.Logical,
"new OpenLayers.Rule.Logical returns object" );
t.eq(rule.foo, "bar", "constructor sets options correctly");
t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");
}
function test_Logical_destroy(t) {
t.plan(1);
var rule = new OpenLayers.Rule.Logical();
rule.destroy();
t.eq(rule.rules, null, "rules array nulled properly");
}
function test_Logical_evaluate(t) {
t.plan(1);
var rule = new OpenLayers.Rule.Logical({
type: OpenLayers.Rule.Logical.NOT});
rule.rules.push(new OpenLayers.Rule());
var feature = new OpenLayers.Feature.Vector();
t.eq(rule.evaluate(feature), false,
"feature evaluates to false correctly.");
}
</script>
</head>
<body>
</body>
</html>

View File

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