1 /* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
2 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
3 * for the full text of the license. */
7 * @requires OpenLayers/Util.js
8 * @requires OpenLayers/Style.js
12 * Class: OpenLayers.Rule
13 * This class represents an SLD Rule, as being used for rule-based SLD styling.
15 OpenLayers.Rule = OpenLayers.Class({
19 * {String} A unique id for this session.
25 * {String} name of this rule
31 * {String} Title of this rule (set if included in SLD)
36 * Property: description
37 * {String} Description of this rule (set if abstract is included in SLD)
43 * {Object} An optional object with properties that the rule should be
44 * evaluated against. If no context is specified, feature.attributes will
51 * {<OpenLayers.Filter>} Optional filter for the rule.
56 * Property: elseFilter
57 * {Boolean} Determines whether this rule is only to be applied only if
58 * no other rules match (ElseFilter according to the SLD specification).
59 * Default is false. For instances of OpenLayers.Rule, if elseFilter is
60 * false, the rule will always apply. For subclasses, the else property is
66 * Property: symbolizer
67 * {Object} Symbolizer or hash of symbolizers for this rule. If hash of
68 * symbolizers, keys are one or more of ["Point", "Line", "Polygon"]. The
69 * latter if useful if it is required to style e.g. vertices of a line
70 * with a point symbolizer. Note, however, that this is not implemented
71 * yet in OpenLayers, but it is the way how symbolizers are defined in
77 * APIProperty: minScaleDenominator
78 * {Number} or {String} minimum scale at which to draw the feature.
79 * In the case of a String, this can be a combination of text and
80 * propertyNames in the form "literal ${propertyName}"
82 minScaleDenominator: null,
85 * APIProperty: maxScaleDenominator
86 * {Number} or {String} maximum scale at which to draw the feature.
87 * In the case of a String, this can be a combination of text and
88 * propertyNames in the form "literal ${propertyName}"
90 maxScaleDenominator: null,
93 * Constructor: OpenLayers.Rule
97 * options - {Object} An optional object with properties to set on the
101 * {<OpenLayers.Rule>}
103 initialize: function(options) {
104 this.symbolizer = {};
105 OpenLayers.Util.extend(this, options);
106 this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
111 * nullify references to prevent circular references and memory leaks
113 destroy: function() {
114 for (var i in this.symbolizer) {
115 this.symbolizer[i] = null;
117 this.symbolizer = null;
121 * APIMethod: evaluate
122 * evaluates this rule for a specific feature
125 * feature - {<OpenLayers.Feature>} feature to apply the rule to.
128 * {Boolean} true if the rule applies, false if it does not.
129 * This rule is the default rule and always returns true.
131 evaluate: function(feature) {
132 var context = this.getContext(feature);
135 if (this.minScaleDenominator || this.maxScaleDenominator) {
136 var scale = feature.layer.map.getScale();
139 // check if within minScale/maxScale bounds
140 if (this.minScaleDenominator) {
141 applies = scale >= OpenLayers.Style.createLiteral(
142 this.minScaleDenominator, context);
144 if (applies && this.maxScaleDenominator) {
145 applies = scale < OpenLayers.Style.createLiteral(
146 this.maxScaleDenominator, context);
149 // check if optional filter applies
150 if(applies && this.filter) {
151 // feature id filters get the feature, others get the context
152 if(this.filter.CLASS_NAME == "OpenLayers.Filter.FeatureId") {
153 applies = this.filter.evaluate(feature);
155 applies = this.filter.evaluate(context);
164 * Gets the context for evaluating this rule
167 * feature - {<OpenLayers.Feature>} feature to take the context from if
170 getContext: function(feature) {
171 var context = this.context;
173 context = feature.attributes || feature.data;
175 if (typeof this.context == "function") {
176 context = this.context(feature);
186 * {<OpenLayers.Rule>} Clone of this rule.
189 var options = OpenLayers.Util.extend({}, this);
191 options.symbolizer = {};
192 for(var key in this.symbolizer) {
193 value = this.symbolizer[key];
195 if(type === "object") {
196 options.symbolizer[key] = OpenLayers.Util.extend({}, value);
197 } else if(type === "string") {
198 options.symbolizer[key] = value;
202 options.filter = this.filter && this.filter.clone();
204 options.context = this.context && OpenLayers.Util.extend({}, this.context);
205 return new OpenLayers.Rule(options);
208 CLASS_NAME: "OpenLayers.Rule"