| 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
|---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
|---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
|---|
| 4 | * full text of the license. */ |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * @requires OpenLayers/Util.js |
|---|
| 9 | * @requires OpenLayers/Style.js |
|---|
| 10 | * @requires OpenLayers/Symbolizer/Point.js |
|---|
| 11 | * @requires OpenLayers/Symbolizer/Line.js |
|---|
| 12 | * @requires OpenLayers/Symbolizer/Polygon.js |
|---|
| 13 | * @requires OpenLayers/Symbolizer/Text.js |
|---|
| 14 | * @requires OpenLayers/Symbolizer/Raster.js |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Class: OpenLayers.Rule |
|---|
| 19 | * This class represents an SLD Rule, as being used for rule-based SLD styling. |
|---|
| 20 | */ |
|---|
| 21 | OpenLayers.Rule = OpenLayers.Class({ |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Property: id |
|---|
| 25 | * {String} A unique id for this session. |
|---|
| 26 | */ |
|---|
| 27 | id: null, |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * APIProperty: name |
|---|
| 31 | * {String} name of this rule |
|---|
| 32 | */ |
|---|
| 33 | name: null, |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Property: title |
|---|
| 37 | * {String} Title of this rule (set if included in SLD) |
|---|
| 38 | */ |
|---|
| 39 | title: null, |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Property: description |
|---|
| 43 | * {String} Description of this rule (set if abstract is included in SLD) |
|---|
| 44 | */ |
|---|
| 45 | description: null, |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Property: context |
|---|
| 49 | * {Object} An optional object with properties that the rule should be |
|---|
| 50 | * evaluated against. If no context is specified, feature.attributes will |
|---|
| 51 | * be used. |
|---|
| 52 | */ |
|---|
| 53 | context: null, |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Property: filter |
|---|
| 57 | * {<OpenLayers.Filter>} Optional filter for the rule. |
|---|
| 58 | */ |
|---|
| 59 | filter: null, |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Property: elseFilter |
|---|
| 63 | * {Boolean} Determines whether this rule is only to be applied only if |
|---|
| 64 | * no other rules match (ElseFilter according to the SLD specification). |
|---|
| 65 | * Default is false. For instances of OpenLayers.Rule, if elseFilter is |
|---|
| 66 | * false, the rule will always apply. For subclasses, the else property is |
|---|
| 67 | * ignored. |
|---|
| 68 | */ |
|---|
| 69 | elseFilter: false, |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Property: symbolizer |
|---|
| 73 | * {Object} Symbolizer or hash of symbolizers for this rule. If hash of |
|---|
| 74 | * symbolizers, keys are one or more of ["Point", "Line", "Polygon"]. The |
|---|
| 75 | * latter if useful if it is required to style e.g. vertices of a line |
|---|
| 76 | * with a point symbolizer. Note, however, that this is not implemented |
|---|
| 77 | * yet in OpenLayers, but it is the way how symbolizers are defined in |
|---|
| 78 | * SLD. |
|---|
| 79 | */ |
|---|
| 80 | symbolizer: null, |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Property: symbolizers |
|---|
| 84 | * {Array} Collection of symbolizers associated with this rule. If |
|---|
| 85 | * provided at construction, the symbolizers array has precedence |
|---|
| 86 | * over the deprecated symbolizer property. Note that multiple |
|---|
| 87 | * symbolizers are not currently supported by the vector renderers. |
|---|
| 88 | * Rules with multiple symbolizers are currently only useful for |
|---|
| 89 | * maintaining elements in an SLD document. |
|---|
| 90 | */ |
|---|
| 91 | symbolizers: null, |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * APIProperty: minScaleDenominator |
|---|
| 95 | * {Number} or {String} minimum scale at which to draw the feature. |
|---|
| 96 | * In the case of a String, this can be a combination of text and |
|---|
| 97 | * propertyNames in the form "literal ${propertyName}" |
|---|
| 98 | */ |
|---|
| 99 | minScaleDenominator: null, |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * APIProperty: maxScaleDenominator |
|---|
| 103 | * {Number} or {String} maximum scale at which to draw the feature. |
|---|
| 104 | * In the case of a String, this can be a combination of text and |
|---|
| 105 | * propertyNames in the form "literal ${propertyName}" |
|---|
| 106 | */ |
|---|
| 107 | maxScaleDenominator: null, |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Constructor: OpenLayers.Rule |
|---|
| 111 | * Creates a Rule. |
|---|
| 112 | * |
|---|
| 113 | * Parameters: |
|---|
| 114 | * options - {Object} An optional object with properties to set on the |
|---|
| 115 | * rule |
|---|
| 116 | * |
|---|
| 117 | * Returns: |
|---|
| 118 | * {<OpenLayers.Rule>} |
|---|
| 119 | */ |
|---|
| 120 | initialize: function(options) { |
|---|
| 121 | this.symbolizer = {}; |
|---|
| 122 | OpenLayers.Util.extend(this, options); |
|---|
| 123 | if (this.symbolizers) { |
|---|
| 124 | delete this.symbolizer; |
|---|
| 125 | } |
|---|
| 126 | this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); |
|---|
| 127 | }, |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * APIMethod: destroy |
|---|
| 131 | * nullify references to prevent circular references and memory leaks |
|---|
| 132 | */ |
|---|
| 133 | destroy: function() { |
|---|
| 134 | for (var i in this.symbolizer) { |
|---|
| 135 | this.symbolizer[i] = null; |
|---|
| 136 | } |
|---|
| 137 | this.symbolizer = null; |
|---|
| 138 | delete this.symbolizers; |
|---|
| 139 | }, |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * APIMethod: evaluate |
|---|
| 143 | * evaluates this rule for a specific feature |
|---|
| 144 | * |
|---|
| 145 | * Parameters: |
|---|
| 146 | * feature - {<OpenLayers.Feature>} feature to apply the rule to. |
|---|
| 147 | * |
|---|
| 148 | * Returns: |
|---|
| 149 | * {Boolean} true if the rule applies, false if it does not. |
|---|
| 150 | * This rule is the default rule and always returns true. |
|---|
| 151 | */ |
|---|
| 152 | evaluate: function(feature) { |
|---|
| 153 | var context = this.getContext(feature); |
|---|
| 154 | var applies = true; |
|---|
| 155 | |
|---|
| 156 | if (this.minScaleDenominator || this.maxScaleDenominator) { |
|---|
| 157 | var scale = feature.layer.map.getScale(); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // check if within minScale/maxScale bounds |
|---|
| 161 | if (this.minScaleDenominator) { |
|---|
| 162 | applies = scale >= OpenLayers.Style.createLiteral( |
|---|
| 163 | this.minScaleDenominator, context); |
|---|
| 164 | } |
|---|
| 165 | if (applies && this.maxScaleDenominator) { |
|---|
| 166 | applies = scale < OpenLayers.Style.createLiteral( |
|---|
| 167 | this.maxScaleDenominator, context); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // check if optional filter applies |
|---|
| 171 | if(applies && this.filter) { |
|---|
| 172 | // feature id filters get the feature, others get the context |
|---|
| 173 | if(this.filter.CLASS_NAME == "OpenLayers.Filter.FeatureId") { |
|---|
| 174 | applies = this.filter.evaluate(feature); |
|---|
| 175 | } else { |
|---|
| 176 | applies = this.filter.evaluate(context); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | return applies; |
|---|
| 181 | }, |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | * Method: getContext |
|---|
| 185 | * Gets the context for evaluating this rule |
|---|
| 186 | * |
|---|
| 187 | * Paramters: |
|---|
| 188 | * feature - {<OpenLayers.Feature>} feature to take the context from if |
|---|
| 189 | * none is specified. |
|---|
| 190 | */ |
|---|
| 191 | getContext: function(feature) { |
|---|
| 192 | var context = this.context; |
|---|
| 193 | if (!context) { |
|---|
| 194 | context = feature.attributes || feature.data; |
|---|
| 195 | } |
|---|
| 196 | if (typeof this.context == "function") { |
|---|
| 197 | context = this.context(feature); |
|---|
| 198 | } |
|---|
| 199 | return context; |
|---|
| 200 | }, |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * APIMethod: clone |
|---|
| 204 | * Clones this rule. |
|---|
| 205 | * |
|---|
| 206 | * Returns: |
|---|
| 207 | * {<OpenLayers.Rule>} Clone of this rule. |
|---|
| 208 | */ |
|---|
| 209 | clone: function() { |
|---|
| 210 | var options = OpenLayers.Util.extend({}, this); |
|---|
| 211 | if (this.symbolizers) { |
|---|
| 212 | // clone symbolizers |
|---|
| 213 | var len = this.symbolizers.length; |
|---|
| 214 | options.symbolizers = new Array(len); |
|---|
| 215 | for (var i=0; i<len; ++i) { |
|---|
| 216 | options.symbolizers[i] = this.symbolizers[i].clone(); |
|---|
| 217 | } |
|---|
| 218 | } else { |
|---|
| 219 | // clone symbolizer |
|---|
| 220 | options.symbolizer = {}; |
|---|
| 221 | var value, type; |
|---|
| 222 | for(var key in this.symbolizer) { |
|---|
| 223 | value = this.symbolizer[key]; |
|---|
| 224 | type = typeof value; |
|---|
| 225 | if(type === "object") { |
|---|
| 226 | options.symbolizer[key] = OpenLayers.Util.extend({}, value); |
|---|
| 227 | } else if(type === "string") { |
|---|
| 228 | options.symbolizer[key] = value; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | // clone filter |
|---|
| 233 | options.filter = this.filter && this.filter.clone(); |
|---|
| 234 | // clone context |
|---|
| 235 | options.context = this.context && OpenLayers.Util.extend({}, this.context); |
|---|
| 236 | return new OpenLayers.Rule(options); |
|---|
| 237 | }, |
|---|
| 238 | |
|---|
| 239 | CLASS_NAME: "OpenLayers.Rule" |
|---|
| 240 | }); |
|---|