]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Rule.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Rule.js
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. */
4
5
6 /**
7  * @requires OpenLayers/Util.js
8  * @requires OpenLayers/Style.js
9  */
10
11 /**
12  * Class: OpenLayers.Rule
13  * This class represents an SLD Rule, as being used for rule-based SLD styling.
14  */
15 OpenLayers.Rule = OpenLayers.Class({
16     
17     /**
18      * Property: id
19      * {String} A unique id for this session.
20      */
21     id: null,
22     
23     /**
24      * APIProperty: name
25      * {String} name of this rule
26      */
27     name: 'default',
28     
29     /**
30      * Property: title
31      * {String} Title of this rule (set if included in SLD)
32      */
33     title: null,
34     
35     /**
36      * Property: description
37      * {String} Description of this rule (set if abstract is included in SLD)
38      */
39     description: null,
40
41     /**
42      * Property: context
43      * {Object} An optional object with properties that the rule should be
44      * evaluated against. If no context is specified, feature.attributes will
45      * be used.
46      */
47     context: null,
48     
49     /**
50      * Property: filter
51      * {<OpenLayers.Filter>} Optional filter for the rule.
52      */
53     filter: null,
54
55     /**
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 
61      * ignored.
62      */
63     elseFilter: false,
64     
65     /**
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
72      * SLD.
73      */
74     symbolizer: null,
75     
76     /**
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}"
81      */
82     minScaleDenominator: null,
83
84     /**
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}"
89      */
90     maxScaleDenominator: null,
91     
92     /** 
93      * Constructor: OpenLayers.Rule
94      * Creates a Rule.
95      *
96      * Parameters:
97      * options - {Object} An optional object with properties to set on the
98      *           rule
99      * 
100      * Returns:
101      * {<OpenLayers.Rule>}
102      */
103     initialize: function(options) {
104         this.symbolizer = {};
105         OpenLayers.Util.extend(this, options);
106         this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
107     },
108
109     /** 
110      * APIMethod: destroy
111      * nullify references to prevent circular references and memory leaks
112      */
113     destroy: function() {
114         for (var i in this.symbolizer) {
115             this.symbolizer[i] = null;
116         }
117         this.symbolizer = null;
118     },
119     
120     /**
121      * APIMethod: evaluate
122      * evaluates this rule for a specific feature
123      * 
124      * Parameters:
125      * feature - {<OpenLayers.Feature>} feature to apply the rule to.
126      * 
127      * Returns:
128      * {Boolean} true if the rule applies, false if it does not.
129      * This rule is the default rule and always returns true.
130      */
131     evaluate: function(feature) {
132         var context = this.getContext(feature);
133         var applies = true;
134
135         if (this.minScaleDenominator || this.maxScaleDenominator) {
136             var scale = feature.layer.map.getScale();
137         }
138         
139         // check if within minScale/maxScale bounds
140         if (this.minScaleDenominator) {
141             applies = scale >= OpenLayers.Style.createLiteral(
142                     this.minScaleDenominator, context);
143         }
144         if (applies && this.maxScaleDenominator) {
145             applies = scale < OpenLayers.Style.createLiteral(
146                     this.maxScaleDenominator, context);
147         }
148         
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);
154             } else {
155                 applies = this.filter.evaluate(context);
156             }
157         }
158
159         return applies;
160     },
161     
162     /**
163      * Method: getContext
164      * Gets the context for evaluating this rule
165      * 
166      * Paramters:
167      * feature - {<OpenLayers.Feature>} feature to take the context from if
168      *           none is specified.
169      */
170     getContext: function(feature) {
171         var context = this.context;
172         if (!context) {
173             context = feature.attributes || feature.data;
174         }
175         if (typeof this.context == "function") {
176             context = this.context(feature);
177         }
178         return context;
179     },
180     
181     /**
182      * APIMethod: clone
183      * Clones this rule.
184      * 
185      * Returns:
186      * {<OpenLayers.Rule>} Clone of this rule.
187      */
188     clone: function() {
189         var options = OpenLayers.Util.extend({}, this);
190         // clone symbolizer
191         options.symbolizer = {};
192         for(var key in this.symbolizer) {
193             value = this.symbolizer[key];
194             type = typeof value;
195             if(type === "object") {
196                 options.symbolizer[key] = OpenLayers.Util.extend({}, value);
197             } else if(type === "string") {
198                 options.symbolizer[key] = value;
199             }
200         }
201         // clone filter
202         options.filter = this.filter && this.filter.clone();
203         // clone context
204         options.context = this.context && OpenLayers.Util.extend({}, this.context);
205         return new OpenLayers.Rule(options);
206     },
207         
208     CLASS_NAME: "OpenLayers.Rule"
209 });