/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt * for the full text of the license. */ /** * @requires OpenLayers/Util.js * @requires OpenLayers/Style.js */ /** * Class: OpenLayers.Rule * This class represents a OGC Rule, as being used for rule-based SLD styling. */ OpenLayers.Rule = OpenLayers.Class({ /** * APIProperty: name * {String} name of this rule */ name: 'default', /** * Property: context * {Object} An optional object with properties that the rule and its * symbolizers' property values should be evaluatad against. If no * context is specified, feature.attributes will be used */ context: null, /** * Property: elseFilter * {Boolean} Determines whether this rule is only to be applied only if * no other rules match (ElseFilter according to the SLD specification). * Default is false. For instances of OpenLayers.Rule, if elseFilter is * false, the rule will always apply. For subclasses, the else property is * ignored. */ elseFilter: false, /** * Property: symbolizer * {Object} Hash of styles for this rule. Contains hashes of feature * styles. Keys are one or more of ["Point", "Line", "Polygon"] */ symbolizer: null, /** * APIProperty: minScaleDenominator * {Number} or {String} minimum scale at which to draw the feature. * In the case of a String, this can be a combination of text and * propertyNames in the form "literal ${propertyName}" */ minScaleDenominator: null, /** * APIProperty: maxScaleDenominator * {Number} or {String} maximum scale at which to draw the feature. * In the case of a String, this can be a combination of text and * propertyNames in the form "literal ${propertyName}" */ maxScaleDenominator: null, /** * Constructor: OpenLayers.Rule * Creates a Rule. * * Parameters: * options - {Object} An optional object with properties to set on the * rule * * Returns: * {} */ initialize: function(options) { this.symbolizer = {}; OpenLayers.Util.extend(this, options); }, /** * APIMethod: destroy * nullify references to prevent circular references and memory leaks */ destroy: function() { for (var i in this.symbolizer) { this.symbolizer[i] = null; } this.symbolizer = null; }, /** * APIMethod: evaluate * evaluates this rule for a specific feature * * Parameters: * feature - {} feature to apply the rule to. * * Returns: * {boolean} true if the rule applies, false if it does not. * This rule is the default rule and always returns true. */ evaluate: function(feature) { var context = this.getContext(feature); var applies = true; if (this.minScaleDenominator || this.maxScaleDenominator) { var scale = feature.layer.map.getScale(); } // check if within minScale/maxScale bounds if (this.minScaleDenominator) { applies = scale >= OpenLayers.Style.createLiteral( this.minScaleDenominator, context); } if (applies && this.maxScaleDenominator) { applies = scale < OpenLayers.Style.createLiteral( this.maxScaleDenominator, context); } return applies; }, /** * Method: getContext * Gets the context for evaluating this rule * * Paramters: * feature - {} feature to take the context from if * none is specified. */ getContext: function(feature) { var context = this.context; if (!context) { context = feature.attributes || feature.data; } return context; }, CLASS_NAME: "OpenLayers.Rule" });