Changeset 6116
- Timestamp:
- 02/08/08 11:56:48 (1 year ago)
- Files:
-
- trunk/openlayers/lib/OpenLayers/Format/SLD.js (modified) (3 diffs)
- trunk/openlayers/lib/OpenLayers/Rule.js (modified) (2 diffs)
- trunk/openlayers/lib/OpenLayers/Rule/Comparison.js (modified) (6 diffs)
- trunk/openlayers/lib/OpenLayers/Rule/FeatureId.js (modified) (1 diff)
- trunk/openlayers/lib/OpenLayers/Rule/Logical.js (modified) (6 diffs)
- trunk/openlayers/lib/OpenLayers/Style.js (modified) (7 diffs)
- trunk/openlayers/tests/Rule/test_Logical.html (modified) (2 diffs)
- trunk/openlayers/tests/test_Style.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/lib/OpenLayers/Format/SLD.js
r5978 r6116 344 344 for (var i=0; i<filters.length; i++) { 345 345 if (filters[i].nodeType == 1) { 346 rule. children.push(this.parseFilter(filters[i]));346 rule.rules.push(this.parseFilter(filters[i])); 347 347 } 348 348 } … … 358 358 for (var i=0; i<filters.length; i++) { 359 359 if (filters[i].nodeType == 1) { 360 rule. children.push(this.parseFilter(filters[i]));360 rule.rules.push(this.parseFilter(filters[i])); 361 361 } 362 362 } … … 369 369 var rule = new OpenLayers.Rule.Logical( 370 370 {type: OpenLayers.Rule.Logical.NOT}); 371 rule. children.push(this.parseFilter(filter[0]));371 rule.rules.push(this.parseFilter(filter[0])); 372 372 return rule; 373 373 } trunk/openlayers/lib/OpenLayers/Rule.js
r5978 r6116 20 20 */ 21 21 name: 'default', 22 23 /** 24 * Property: context 25 * {Object} An optional object with properties that the rule and its 26 * symbolizers' property values should be evaluatad against. If no 27 * context is specified, feature.attributes will be used 28 */ 29 context: null, 22 30 23 31 /** … … 94 102 */ 95 103 evaluate: function(feature) { 96 // Default rule always applies. Subclasses will want to override this. 97 return true; 104 var context = this.getContext(feature); 105 var applies = true; 106 107 if (this.minScaleDenominator || this.maxScaleDenominator) { 108 var scale = feature.layer.map.getScale(); 109 } 110 111 // check if within minScale/maxScale bounds 112 if (this.minScaleDenominator) { 113 applies = scale >= OpenLayers.Style.createLiteral( 114 this.minScaleDenominator, context); 115 } 116 if (applies && this.maxScaleDenominator) { 117 applies = scale < OpenLayers.Style.createLiteral( 118 this.maxScaleDenominator, context); 119 } 120 121 return applies; 98 122 }, 99 123 124 /** 125 * Method: getContext 126 * Gets the context for evaluating this rule 127 * 128 * Paramters: 129 * feature - {<OpenLayers.Feature>} feature to take the context from if 130 * none is specified. 131 */ 132 getContext: function(feature) { 133 var context = this.context; 134 if (!context) { 135 context = feature.attributes || feature.data; 136 } 137 return context; 138 }, 139 100 140 CLASS_NAME: "OpenLayers.Rule" 101 141 }); trunk/openlayers/lib/OpenLayers/Rule/Comparison.js
r5614 r6116 34 34 * APIProperty: property 35 35 * {String} 36 * name of the feature attributeto compare36 * name of the context property to compare 37 37 */ 38 38 property: null, … … 85 85 /** 86 86 * APIMethod: evaluate 87 * evaluates this rule for a specific feature88 * 89 * Parameters: 90 * feature - {<OpenLayers.Feature>} featureto apply the rule to.87 * evaluates this rule for a specific context 88 * 89 * Parameters: 90 * context - {Object} context to apply the rule to. 91 91 * 92 92 * Returns: … … 94 94 */ 95 95 evaluate: function(feature) { 96 var attributes = feature.attributes || feature.data; 96 if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) { 97 return false; 98 } 99 var context = this.getContext(feature); 97 100 switch(this.type) { 98 101 case OpenLayers.Rule.Comparison.EQUAL_TO: … … 101 104 case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO: 102 105 case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO: 103 return this.binaryCompare( feature, this.property, this.value);106 return this.binaryCompare(context, this.property, this.value); 104 107 105 108 case OpenLayers.Rule.Comparison.BETWEEN: 106 109 var result = 107 attributes[this.property] > this.lowerBoundary;110 context[this.property] > this.lowerBoundary; 108 111 result = result && 109 attributes[this.property] < this.upperBoundary;112 context[this.property] < this.upperBoundary; 110 113 return result; 111 114 case OpenLayers.Rule.Comparison.LIKE: 112 115 var regexp = new RegExp(this.value, 113 116 "gi"); 114 return regexp.test( attributes[this.property]);117 return regexp.test(context[this.property]); 115 118 } 116 119 }, … … 166 169 * 167 170 * Parameters: 168 * feature - {<OpenLayers.Feature>}171 * context - {Object} 169 172 * property - {String} or {Number} 170 173 * value - {String} or {Number}, same as property … … 173 176 * {boolean} 174 177 */ 175 binaryCompare: function(feature, property, value) { 176 var attributes = feature.attributes || feature.data; 178 binaryCompare: function(context, property, value) { 177 179 switch (this.type) { 178 180 case OpenLayers.Rule.Comparison.EQUAL_TO: 179 return attributes[property] == value;181 return context[property] == value; 180 182 case OpenLayers.Rule.Comparison.NOT_EQUAL_TO: 181 return attributes[property] != value;183 return context[property] != value; 182 184 case OpenLayers.Rule.Comparison.LESS_THAN: 183 return attributes[property] < value;185 return context[property] < value; 184 186 case OpenLayers.Rule.Comparison.GREATER_THAN: 185 return attributes[property] > value;187 return context[property] > value; 186 188 case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO: 187 return attributes[property] <= value;189 return context[property] <= value; 188 190 case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO: 189 return attributes[property] >= value;191 return context[property] >= value; 190 192 } 191 193 }, trunk/openlayers/lib/OpenLayers/Rule/FeatureId.js
r5614 r6116 54 54 */ 55 55 evaluate: function(feature) { 56 if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) { 57 return false; 58 } 56 59 for (var i=0; i<this.fids.length; i++) { 57 60 var fid = feature.fid || feature.id; trunk/openlayers/lib/OpenLayers/Rule/Logical.js
r5614 r6116 21 21 * {Array(<OpenLayers.Rule>)} child rules for this rule 22 22 */ 23 children: null,23 rules: null, 24 24 25 25 /** … … 44 44 */ 45 45 initialize: function(options) { 46 this. children= [];46 this.rules = []; 47 47 OpenLayers.Rule.prototype.initialize.apply(this, [options]); 48 48 }, … … 53 53 */ 54 54 destroy: function() { 55 for (var i=0; i<this. children.length; i++) {56 this. children[i].destroy();55 for (var i=0; i<this.rules.length; i++) { 56 this.rules[i].destroy(); 57 57 } 58 this. children= null;58 this.rules = null; 59 59 OpenLayers.Rule.prototype.destroy.apply(this, arguments); 60 60 }, … … 71 71 */ 72 72 evaluate: function(feature) { 73 if (!OpenLayers.Rule.prototype.evaluate.apply(this, arguments)) { 74 return false; 75 } 73 76 switch(this.type) { 74 77 case OpenLayers.Rule.Logical.AND: 75 for (var i=0; i<this. children.length; i++) {76 if (this. children[i].evaluate(feature) == false) {78 for (var i=0; i<this.rules.length; i++) { 79 if (this.rules[i].evaluate(feature) == false) { 77 80 return false; 78 81 } … … 81 84 82 85 case OpenLayers.Rule.Logical.OR: 83 for (var i=0; i<this. children.length; i++) {84 if (this. children[i].evaluate(feature) == true) {86 for (var i=0; i<this.rules.length; i++) { 87 if (this.rules[i].evaluate(feature) == true) { 85 88 return true; 86 89 } … … 89 92 90 93 case OpenLayers.Rule.Logical.NOT: 91 return (!this. children[0].evaluate(feature));94 return (!this.rules[0].evaluate(feature)); 92 95 } 93 96 }, trunk/openlayers/lib/OpenLayers/Style.js
r5978 r6116 113 113 var rules = this.rules; 114 114 115 var rule ;115 var rule, context; 116 116 var elseRules = []; 117 117 var appliedRules = false; 118 118 for(var i=0; i<rules.length; i++) { 119 119 rule = rules[i]; 120 context = rule.context; 121 if (!context) { 122 context = feature.attributes || feature.data; 123 } 120 124 // does the rule apply? 121 125 var applies = rule.evaluate(feature); 122 126 123 if (rule.minScaleDenominator || rule.maxScaleDenominator) {124 var scale = feature.layer.map.getScale();125 }126 127 // check if within minScale/maxScale bounds128 if (rule.minScaleDenominator) {129 applies = scale >= OpenLayers.Style.createLiteral(130 rule.minScaleDenominator, feature);131 }132 if (applies && rule.maxScaleDenominator) {133 applies = scale < OpenLayers.Style.createLiteral(134 rule.maxScaleDenominator, feature);135 }136 137 127 if(applies) { 138 128 if(rule instanceof OpenLayers.Rule && rule.elseFilter) { … … 140 130 } else { 141 131 appliedRules = true; 142 this.applySymbolizer(rule, style, feature );132 this.applySymbolizer(rule, style, feature, context); 143 133 } 144 134 } … … 149 139 appliedRules = true; 150 140 for(var i=0; i<elseRules.length; i++) { 151 this.applySymbolizer(elseRules[i], style, feature); 152 } 153 } 154 155 // calculate literals for all styles in the propertyStyles cache 156 this.createLiterals(style, feature); 141 this.applySymbolizer(elseRules[i], style, feature, context); 142 } 143 } 157 144 158 145 // don't display if there were rules but none applied … … 173 160 * style - {Object} 174 161 * feature - {<OpenLayer.Feature.Vector>} 162 * context - {Object} 175 163 * 176 164 * Returns: 177 165 * {Object} A style with new symbolizer applied. 178 166 */ 179 applySymbolizer: function(rule, style, feature ) {167 applySymbolizer: function(rule, style, feature, context) { 180 168 var symbolizerPrefix = feature.geometry ? 181 169 this.getSymbolizerPrefix(feature.geometry) : 182 170 OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; 183 171 172 var symbolizer = rule.symbolizer[symbolizerPrefix]; 173 184 174 // merge the style with the current style 185 var symbolizer = rule.symbolizer[symbolizerPrefix];186 return OpenLayers.Util.extend(style, symbolizer);175 return this.createLiterals( 176 OpenLayers.Util.extend(style, symbolizer), context); 187 177 }, 188 178 … … 195 185 * style - {Object} style to create literals for. Will be modified 196 186 * inline. 197 * feature - {<OpenLayers.Feature.Vector>} feature to take properties from 187 * context - {Object} context to take property values from. Defaults to 188 * feature.attributes (or feature.data, if attributes are not 189 * available) 198 190 * 199 191 * Returns; 200 192 * {Object} the modified style 201 193 */ 202 createLiterals: function(style, feature) {194 createLiterals: function(style, context) { 203 195 for (var i in this.propertyStyles) { 204 style[i] = OpenLayers.Style.createLiteral(style[i], feature);196 style[i] = OpenLayers.Style.createLiteral(style[i], context); 205 197 } 206 198 return style; … … 303 295 * will be replaced by the value of the "bar" attribute of the passed 304 296 * feature. 305 * feature {<OpenLayers.Feature>} featureto take attribute values from297 * context {Object} context to take attribute values from 306 298 * 307 299 * Returns: … … 310 302 * attribute named "bar" with the value "valueOfBar". 311 303 */ 312 OpenLayers.Style.createLiteral = function(value, feature) {304 OpenLayers.Style.createLiteral = function(value, context) { 313 305 if (typeof value == "string" && value.indexOf("${") != -1) { 314 var attributes = feature.attributes || feature.data; 315 value = OpenLayers.String.format(value, attributes) 306 value = OpenLayers.String.format(value, context) 316 307 value = isNaN(value) ? value : parseFloat(value); 317 308 } trunk/openlayers/tests/Rule/test_Logical.html
r5429 r6116 20 20 var rule = new OpenLayers.Rule.Logical(); 21 21 rule.destroy(); 22 t.eq(rule. children, null, "childrenarray nulled properly");22 t.eq(rule.rules, null, "rules array nulled properly"); 23 23 } 24 24 … … 28 28 var rule = new OpenLayers.Rule.Logical({ 29 29 type: OpenLayers.Rule.Logical.NOT}); 30 rule. children.push(new OpenLayers.Rule());30 rule.rules.push(new OpenLayers.Rule()); 31 31 32 32 var feature = new OpenLayers.Feature.Vector(); trunk/openlayers/tests/test_Style.html
r5978 r6116 112 112 } 113 113 style.createStyle(new OpenLayers.Feature.Vector()); 114 115 114 } 115 116 function test_Style_context(t) { 117 t.plan(1); 118 var context = { 119 foo: "bar", 120 size: 10}; 121 var rule = new OpenLayers.Rule.Comparison({ 122 type: OpenLayers.Rule.Comparison.LESS_THAN, 123 context: context, 124 property: "size", 125 value: 11, 126 symbolizer: {"Point": {externalGraphic: "${foo}.png"}}}); 127 var style = new OpenLayers.Style(); 128 style.addRules([rule]); 129 var styleHash = style.createStyle(new OpenLayers.Feature.Vector()); 130 t.eq(styleHash.externalGraphic, "bar.png", "correctly evaluated rule against a custom context"); 116 131 } 117 132
