Files
openlayers/lib/OpenLayers/Rule/Comparison.js
crschmidt fb3c02354f Add support for the extremely awesomely super-comprehensive rule based styling
that Andreas has been working so hard on. I think this is the single most
awesome commit I've ever had the pleasure of committing. The results of this
commit are described on http://trac.openlayers.org/wiki/Styles: essentially,
this makes it possible to style features in all kinds of fun ways based on
rules, and will also form the underlying basis for #533. Things this patch
adds:

 * OL.Rule classes. These classes allow you to do tests against the propertie
   of a feature, and set a style based on these properties -- so you can
   compare the 'speedlimit' property of a line, and test if it is > 60, and if
   it is greater than 60, render it in a different color. You can also test
   combinations of rules using the OL.Rule.Logical class, and test featureids
   with the FeatureID class. 
 * OL.Style class: The OL.Style class lets you wrap up Rules into styles that 
   can be used with drawFeature to draw the feature in the selected style.
 * OL.Layer.Vector.drawFeature will check if the given style is an OL.Style
   object, and if so, it will draw the feature accordingly.

examples/georss-flickr.html shows usage of these classes.

Many, many thanks go to Andreas for all his hard work on this: this code really
is very pretty, and includes unit tests for all the classes (and we know that I
am a big fan of tests.)

Three cheers for Andreas: Hip hip, hooray! hip hip, hooray! hip hip, hooray!


git-svn-id: http://svn.openlayers.org/trunk/openlayers@5429 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2007-12-15 16:17:50 +00:00

205 lines
7.2 KiB
JavaScript

/* Copyright (c) 2006-2007 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.Logical
* Creates a logical rule (And, Or, Not).
*
* 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>}
*/
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 = "~";