OpenLayers OpenLayers

Ticket #1183: styles-no-georss-flickr.diff

File styles-no-georss-flickr.diff, 38.6 kB (added by ahocevar, 9 months ago)

same as styles.diff, but without the georss-flickr example, because its georss feed prevents the patch from being displayed as html in trac

  • tests/Rule/test_FeatureId.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_FeatureId_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule.FeatureId(options);  
     11        t.ok(rule instanceof OpenLayers.Rule.FeatureId,  
     12             "new OpenLayers.Rule.FeatureId returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_FeatureId_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule.FeatureId(); 
     21        rule.destroy(); 
     22        t.eq(rule.symbolizer, null, "symbolizer hash nulled properly"); 
     23    } 
     24     
     25    function test_FeatureId_evaluate(t) { 
     26        t.plan(3); 
     27         
     28        var rule = new OpenLayers.Rule.FeatureId( 
     29                {fids: ["fid_1", "fid_3"]}); 
     30 
     31        var ruleResults = { 
     32                "fid_1" : true, 
     33                "fid_2" : false, 
     34                "fid_3" : true}; 
     35        for (var i in ruleResults) { 
     36            var feature = new OpenLayers.Feature.Vector(); 
     37            feature.fid = i; 
     38            var result = rule.evaluate(feature); 
     39            t.eq(result, ruleResults[i], "feature "+i+" evaluates to "+result.toString()+" correctly."); 
     40            feature.destroy(); 
     41        } 
     42    } 
     43    </script>  
     44</head>  
     45<body>  
     46</body>  
     47</html>  
  • tests/Rule/test_Comparison.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Comparison_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule.Comparison(options);  
     11        t.ok(rule instanceof OpenLayers.Rule.Comparison,  
     12             "new OpenLayers.Rule.Comparison returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_Comparison_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule.Comparison(); 
     21        rule.destroy(); 
     22        t.eq(rule.symbolizer, null, "symbolizer hash nulled properly"); 
     23    } 
     24     
     25    function test_Comparison_value2regex(t) { 
     26        t.plan(2); 
     27         
     28        var rule = new OpenLayers.Rule.Comparison({ 
     29                property: "foo", 
     30                value: "*b?r\\*\\?*", 
     31                type: OpenLayers.Rule.Comparison.LIKE}); 
     32        rule.value2regex("*", "?", "\\"); 
     33        t.eq(rule.value, ".*b.r\\*\\?.*", "Regular expression generated correctly."); 
     34         
     35        rule.value = "%b.r!%!.%"; 
     36        rule.value2regex("%", ".", "!"); 
     37        t.eq(rule.value, ".*b.r\\%\\..*", "Regular expression with different wildcard and escape chars generated correctly."); 
     38    } 
     39     
     40    function test_Comparison_evaluate(t) { 
     41        t.plan(3); 
     42         
     43        var rule = new OpenLayers.Rule.Comparison({ 
     44                property: "area", 
     45                lowerBoundary: 1000, 
     46                upperBoundary: 5000, 
     47                type: OpenLayers.Rule.Comparison.BETWEEN}); 
     48 
     49        var features = [ 
     50                new OpenLayers.Feature.Vector(null, { 
     51                        area: 2000}), 
     52                new OpenLayers.Feature.Vector(null, { 
     53                        area: 6000}), 
     54                new OpenLayers.Feature.Vector(null, { 
     55                        area: 4999})]; 
     56        var ruleResults = { 
     57                0: true, 
     58                1: false, 
     59                2: true}; 
     60        for (var i in ruleResults) { 
     61            var result = rule.evaluate(features[i]); 
     62            t.eq(result, ruleResults[i], "feature "+i+ 
     63                    " evaluates to "+result.toString()+" correctly."); 
     64        } 
     65    } 
     66    </script>  
     67</head>  
     68<body>  
     69</body>  
     70</html>  
  • tests/Rule/test_Logical.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Logical_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule.Logical(options);  
     11        t.ok(rule instanceof OpenLayers.Rule.Logical,  
     12             "new OpenLayers.Rule.Logical returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_Logical_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule.Logical(); 
     21        rule.destroy(); 
     22        t.eq(rule.children, null, "children array nulled properly"); 
     23    } 
     24     
     25    function test_Logical_evaluate(t) { 
     26        t.plan(1); 
     27         
     28        var rule = new OpenLayers.Rule.Logical({ 
     29                type: OpenLayers.Rule.Logical.NOT}); 
     30        rule.children.push(new OpenLayers.Rule()); 
     31         
     32        var feature = new OpenLayers.Feature.Vector(); 
     33 
     34        t.eq(rule.evaluate(feature), false, 
     35                "feature evaluates to false correctly."); 
     36    } 
     37    </script>  
     38</head>  
     39<body>  
     40</body>  
     41</html>  
  • tests/test_Style.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Style_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var style = new OpenLayers.Style(null, options);  
     11        t.ok(style instanceof OpenLayers.Style,  
     12             "new OpenLayers.Style returns object" );  
     13        t.eq(style.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof style.createStyle, "function", "style has a createStyle function");  
     15    } 
     16     
     17    function test_Style_create(t) { 
     18        t.plan(5); 
     19         
     20        var map = new OpenLayers.Map("map"); 
     21         
     22        var layer = new OpenLayers.Layer.Vector("layer"); 
     23          
     24        var baseStyle = {externalGraphic: "bar${foo}.png"}; 
     25         
     26        var style = new OpenLayers.Style(baseStyle); 
     27         
     28        var rule = new OpenLayers.Rule.FeatureId({ 
     29                fids: ["1"], 
     30                symbolizer: {"Point": {fillColor: "green"}}, 
     31                maxScale: 2000000}); 
     32        style.addRules([rule]); 
     33         
     34        var feature = new OpenLayers.Feature.Vector( 
     35                new OpenLayers.Geometry.Point(3,5), 
     36                {"foo": "bar"}, 
     37                style); 
     38 
     39        feature.fid = "1"; 
     40        // for this fid, the above rule should apply 
     41                 
     42        layer.addFeatures([feature]); 
     43        map.addLayer(layer); 
     44        map.setBaseLayer(layer); 
     45 
     46        map.setCenter(new OpenLayers.LonLat(3,5), 8); 
     47        // at this scale, the feature should be visible 
     48        var createdStyle = style.createStyle(feature); 
     49        t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style correctly."); 
     50        t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 
     51 
     52        map.setCenter(new OpenLayers.LonLat(3,5), 7); 
     53        // at this scale, the feature should be invisible 
     54        createdStyle = style.createStyle(feature); 
     55        t.eq(createdStyle.display, "none", "Feature is invisible at scale "+map.getScale()); 
     56        t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule for fid=\"1\" applied correctly."); 
     57         
     58        feature.fid = "2"; 
     59        // now the rule should not apply 
     60         
     61        createdStyle = style.createStyle(feature); 
     62        t.eq(createdStyle.fillColor, undefined, "Correct style for rule that does not apply to fid=\"2\"."); 
     63         
     64    } 
     65 
     66    function test_Style_destroy(t) { 
     67        t.plan(1); 
     68         
     69        var style = new OpenLayers.Style(); 
     70        style.destroy(); 
     71        t.eq(style.rules, null, "rules array nulled properly"); 
     72    } 
     73 
     74    </script>  
     75</head>  
     76<body>  
     77  <div id="map" style="width:500px;height:500px"></div> 
     78</body>  
     79</html>  
  • tests/test_Rule.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Rule_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule(options);  
     11        t.ok(rule instanceof OpenLayers.Rule,  
     12             "new OpenLayers.Rule returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_Rule_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule(); 
     21        rule.destroy(); 
     22        t.eq(rule.symbolizer, null, "symbolizer hash nulled properly"); 
     23    } 
     24 
     25    </script>  
     26</head>  
     27<body>  
     28</body>  
     29</html>  
  • tests/list-tests.html

    old new  
    3435    <li>Popup/test_Anchored.html</li> 
    3536    <li>test_Feature.html</li> 
    3637    <li>Feature/test_Vector.html</li> 
     38    <li>test_Style.html</li> 
     39    <li>test_Rule.html</li> 
     40    <li>Rule/test_FeatureId.html</li> 
     41    <li>Rule/test_Logical.html</li> 
     42    <li>Rule/test_Comparison.html</li> 
    3743    <li>test_Events.html</li> 
    3844    <li>test_Util.html</li> 
    3945    <li>test_Layer.html</li> 
  • lib/OpenLayers/Rule/FeatureId.js

    old new  
     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/Rule.js 
     8 * 
     9 * Class: OpenLayers.Rule 
     10 *  
     11 * This class represents a ogc:FeatureId Rule, as being used for rule-based SLD 
     12 * styling 
     13 *  
     14 * Inherits from 
     15 * - <OpenLayers.Rule> 
     16 */ 
     17OpenLayers.Rule.FeatureId = OpenLayers.Class(OpenLayers.Rule, { 
     18 
     19    /**  
     20     * APIProperty: fid  
     21     * {Array(<String>)} Feature Ids to evaluate this rule against. To be passed 
     22     * To be passed inside the params object. 
     23     */ 
     24    fids: null, 
     25     
     26    /**  
     27     * Constructor: OpenLayers.Rule.FeatureId 
     28     * Creates an ogc:FeatureId rule. 
     29     * 
     30     * Parameters: 
     31     * options - {Object} An optional object with properties to set on the 
     32     *           rule 
     33     *  
     34     * Returns: 
     35     * {<OpenLayers.Rule>} 
     36     *  
     37     * Inherits from: 
     38     *  - <OpenLayers.Rule> 
     39     */ 
     40    initialize: function(options) { 
     41        this.fids = []; 
     42        OpenLayers.Rule.prototype.initialize.apply( 
     43                this, [options]); 
     44    }, 
     45 
     46    /** 
     47     * APIMethod: evaluate 
     48     * evaluates this rule for a specific feature 
     49     *  
     50     * Parameters: 
     51     * feature - {<OpenLayers.Feature>} feature to apply the rule to. 
     52     *           For vector features, the check is run against the fid, 
     53     *           for plain features against the id. 
     54     *  
     55     * Returns: 
     56     * {boolean} true if the rule applies, false if it does not 
     57     */ 
     58    evaluate: function(feature) { 
     59        for (var i=0; i<this.fids.length; i++) { 
     60            var fid = feature.fid || feature.id; 
     61            if (fid == this.fids[i]) { 
     62                return true; 
     63            } 
     64        } 
     65        return false; 
     66    }, 
     67     
     68    CLASS_NAME: "OpenLayers.Rule.FeatureId" 
     69}); 
  • lib/OpenLayers/Rule/Comparison.js

    old new  
     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/Rule.js 
     8 * 
     9 * Class: OpenLayers.Rule.Comparison 
     10 *  
     11 * This class represents the comparison rules, as being used for rule-based  
     12 * SLD styling 
     13 *  
     14 * Inherits from 
     15 * - <OpenLayers.Rule> 
     16 */ 
     17OpenLayers.Rule.Comparison = OpenLayers.Class(OpenLayers.Rule, { 
     18 
     19    /** 
     20     * APIProperty: type 
     21     * {String} type: type of the comparison. This is one of 
     22     * - OpenLayers.Rule.Comparison.EQUAL_TO                 = "=="; 
     23     * - OpenLayers.Rule.Comparison.NOT_EQUAL_TO             = "!="; 
     24     * - OpenLayers.Rule.Comparison.LESS_THAN                = "<"; 
     25     * - OpenLayers.Rule.Comparison.GREATER_THAN             = ">"; 
     26     * - OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO    = "<="; 
     27     * - OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; 
     28     * - OpenLayers.Rule.Comparison.BETWEEN                  = ".."; 
     29     * - OpenLayers.Rule.Comparison.LIKE                     = "~";  
     30     */ 
     31    type: null, 
     32     
     33    /** 
     34     * APIProperty: property 
     35     * {String} 
     36     * name of the feature attribute to compare 
     37     */ 
     38    property: null, 
     39     
     40    /** 
     41     * APIProperty: value 
     42     * {Number} or {String} 
     43     * comparison value for binary comparisons. In the case of a String, this 
     44     * can be a combination of text and propertyNames in the form 
     45     * "literal ${propertyName}" 
     46     */ 
     47    value: null, 
     48     
     49    /** 
     50     * APIProperty: lowerBoundary 
     51     * {Number} or {String} 
     52     * lower boundary for between comparisons. In the case of a String, this 
     53     * can be a combination of text and propertyNames in the form 
     54     * "literal ${propertyName}" 
     55     */ 
     56    lowerBoundary: null, 
     57     
     58    /** 
     59     * APIProperty: upperBoundary 
     60     * {Number} or {String} 
     61     * upper boundary for between comparisons. In the case of a String, this 
     62     * can be a combination of text and propertyNames in the form 
     63     * "literal ${propertyName}" 
     64     */ 
     65    upperBoundary: null, 
     66 
     67    /**  
     68     * Constructor: OpenLayers.Rule.Logical 
     69     * Creates a logical rule (And, Or, Not). 
     70     * 
     71     * Parameters: 
     72     * params  - {Object} Hash of parameters for this rule: 
     73     *              -  
     74     *              - value:  
     75     * options - {Object} An optional object with properties to set on the 
     76     *           rule 
     77     *  
     78     * Returns: 
     79     * {<OpenLayers.Rule>} 
     80     */ 
     81    initialize: function(options) { 
     82        OpenLayers.Rule.prototype.initialize.apply( 
     83                this, [options]); 
     84    }, 
     85 
     86    /** 
     87     * APIMethod: evaluate 
     88     * evaluates this rule for a specific feature 
     89     *  
     90     * Parameters: 
     91     * feature - {<OpenLayers.Feature>} feature to apply the rule to. 
     92     *  
     93     * Returns: 
     94     * {boolean} true if the rule applies, false if it does not 
     95     */ 
     96    evaluate: function(feature) { 
     97        var attributes = feature.attributes || feature.data; 
     98        switch(this.type) { 
     99            case OpenLayers.Rule.Comparison.EQUAL_TO: 
     100            case OpenLayers.Rule.Comparison.LESS_THAN: 
     101            case OpenLayers.Rule.Comparison.GREATER_THAN: 
     102            case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO: 
     103            case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO: 
     104                return this.binaryCompare(feature, this.property, this.value); 
     105             
     106            case OpenLayers.Rule.Comparison.BETWEEN: 
     107                var result = 
     108                        attributes[this.property] > this.lowerBoundary; 
     109                result = result && 
     110                        attributes[this.property] < this.upperBoundary; 
     111                return result; 
     112            case OpenLayers.Rule.Comparison.LIKE: 
     113                var regexp = new RegExp(this.value, 
     114                                "gi"); 
     115                return regexp.test(attributes[this.property]);  
     116        } 
     117    }, 
     118     
     119    /** 
     120     * APIMethod: value2regex 
     121     * Converts the value of this rule into a regular expression string, 
     122     * according to the wildcard characters specified. This method has to 
     123     * be called after instantiation of this class, if the value is not a 
     124     * regular expression already. 
     125     *  
     126     * Parameters: 
     127     * wildCard   - {<Char>} wildcard character in the above value, default 
     128     *              is "*" 
     129     * singleChar - {<Char>) single-character wildcard in the above value 
     130     *              default is "." 
     131     * escape     - {<Char>) escape character in the above value, default is 
     132     *              "!" 
     133     *  
     134     * Returns: 
     135     * {String} regular expression string 
     136     */ 
     137    value2regex: function(wildCard, singleChar, escapeChar) { 
     138        if (wildCard == ".") { 
     139            var msg = "'.' is an unsupported wildCard character for "+ 
     140                    "OpenLayers.Rule.Comparison"; 
     141            OpenLayers.Console.error(msg); 
     142            return null; 
     143        } 
     144         
     145        // set UMN MapServer defaults for unspecified parameters 
     146        wildCard = wildCard ? wildCard : "*"; 
     147        singleChar = singleChar ? singleChar : "."; 
     148        escapeChar = escapeChar ? escapeChar : "!"; 
     149         
     150        this.value = this.value.replace( 
     151                new RegExp("\\"+escapeChar, "g"), "\\"); 
     152        this.value = this.value.replace( 
     153                new RegExp("\\"+singleChar, "g"), "."); 
     154        this.value = this.value.replace( 
     155                new RegExp("\\"+wildCard, "g"), ".*"); 
     156        this.value = this.value.replace( 
     157                new RegExp("\\\\.\\*", "g"), "\\"+wildCard); 
     158        this.value = this.value.replace( 
     159                new RegExp("\\\\\\.", "g"), "\\"+singleChar); 
     160         
     161        return this.value; 
     162    }, 
     163     
     164    /** 
     165     * Function: binaryCompare 
     166     * Compares a feature property to a rule value 
     167     *  
     168     * Parameters: 
     169     * feature  - {<OpenLayers.Feature>} 
     170     * property - {String} or {Number} 
     171     * value    - {String} or {Number}, same as property 
     172     *  
     173     * Returns: 
     174     * {boolean} 
     175     */ 
     176    binaryCompare: function(feature, property, value) { 
     177        var attributes = feature.attributes || feature.data; 
     178        switch (this.type) { 
     179            case OpenLayers.Rule.Comparison.EQUAL_TO: 
     180                return attributes[property] == value; 
     181            case OpenLayers.Rule.Comparison.NOT_EQUAL_TO: 
     182                return attributes[property] != value; 
     183            case OpenLayers.Rule.Comparison.LESS_THAN: 
     184                return attributes[property] < value; 
     185            case OpenLayers.Rule.Comparison.GREATER_THAN: 
     186                return attributes[property] > value; 
     187            case OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO: 
     188                return attributes[property] <= value; 
     189            case OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO: 
     190                return attributes[property] >= value; 
     191        }       
     192    }, 
     193     
     194    CLASS_NAME: "OpenLayers.Rule.Comparison" 
     195}); 
     196 
     197 
     198OpenLayers.Rule.Comparison.EQUAL_TO                 = "=="; 
     199OpenLayers.Rule.Comparison.NOT_EQUAL_TO             = "!="; 
     200OpenLayers.Rule.Comparison.LESS_THAN                = "<"; 
     201OpenLayers.Rule.Comparison.GREATER_THAN             = ">"; 
     202OpenLayers.Rule.Comparison.LESS_THAN_OR_EQUAL_TO    = "<="; 
     203OpenLayers.Rule.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; 
     204OpenLayers.Rule.Comparison.BETWEEN                  = ".."; 
     205OpenLayers.Rule.Comparison.LIKE                     = "~"; 
  • lib/OpenLayers/Rule/Logical.js

    old new  
     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/Rule.js 
     8 * 
     9 * Class: OpenLayers.Rule.Logical 
     10 *  
     11 * This class represents ogc:And, ogc:Or and ogc:Not rules. 
     12 *  
     13 * Inherits from 
     14 * - <OpenLayers.Rule> 
     15 */ 
     16OpenLayers.Rule.Logical = OpenLayers.Class(OpenLayers.Rule, { 
     17 
     18    /** 
     19     * APIProperty: children 
     20     * {Array(<OpenLayers.Rule>)} child rules for this rule 
     21     */ 
     22    children: null,  
     23      
     24    /** 
     25     * APIProperty: type 
     26     * {String} type of logical operator. Available types are: 
     27     * - OpenLayers.Rule.Locical.AND = "&&"; 
     28     * - OpenLayers.Rule.Logical.OR  = "||"; 
     29     * - OpenLayers.Rule.Logical.NOT = "!"; 
     30     */ 
     31    type: null, 
     32 
     33    /**  
     34     * Constructor: OpenLayers.Rule.Logical 
     35     * Creates a logical rule (And, Or, Not). 
     36     * 
     37     * Parameters: 
     38     * options - {Object} An optional object with properties to set on the 
     39     *           rule 
     40     *  
     41     * Returns: 
     42     * {<OpenLayers.Rule>} 
     43     */ 
     44    initialize: function(options) { 
     45        this.children = []; 
     46        OpenLayers.Rule.prototype.initialize.apply( 
     47                this, [options]); 
     48    }, 
     49     
     50    /**  
     51     * APIMethod: destroy 
     52     * nullify references to prevent circular references and memory leaks 
     53     */ 
     54    destroy: function() { 
     55        for (var i=0; i<this.children.length; i++) { 
     56            this.children[i].destroy(); 
     57        } 
     58        this.children = null; 
     59        OpenLayers.Rule.prototype.destroy.apply(this, arguments); 
     60    }, 
     61 
     62    /** 
     63     * APIMethod: evaluate 
     64     * evaluates this rule for a specific feature 
     65     *  
     66     * Parameters: 
     67     * feature - {<OpenLayers.Feature>} feature to apply the rule to. 
     68     *  
     69     * Returns: 
     70     * {boolean} true if the rule applies, false if it does not 
     71     */ 
     72    evaluate: function(feature) { 
     73        switch(this.type) { 
     74            case OpenLayers.Rule.Logical.AND: 
     75                for (var i=0; i<this.children.length; i++) { 
     76                    if (this.children[i].evaluate(feature) == false) { 
     77                        return false; 
     78                    } 
     79                } 
     80                return true; 
     81                 
     82            case OpenLayers.Rule.Logical.OR: 
     83                for (var i=0; i<this.children.length; i++) { 
     84                    if (this.children[i].evaluate(feature) == true) { 
     85                        return true; 
     86                    } 
     87                } 
     88                return false; 
     89             
     90            case OpenLayers.Rule.Logical.NOT: 
     91                return (!this.children[0].evaluate(feature)); 
     92        } 
     93    }, 
     94     
     95    CLASS_NAME: "OpenLayers.Rule.Logical" 
     96}); 
     97 
     98 
     99OpenLayers.Rule.Logical.AND = "&&"; 
     100OpenLayers.Rule.Logical.OR  = "||"; 
     101OpenLayers.Rule.Logical.NOT = "!"; 
  • lib/OpenLayers/Rule.js

    old new  
     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 * Class: OpenLayers.Rule 
     11 *  
     12 * This class represents a OGC Rule, as being used for rule-based SLD styling 
     13 */ 
     14OpenLayers.Rule = OpenLayers.Class({ 
     15     
     16    /** 
     17     * APIProperty: name 
     18     * {String} name of this rule 
     19     */ 
     20    name: 'default', 
     21 
     22    /** 
     23     * Property: symbolizer 
     24     * {Object} Hash of styles for this rule. Contains hashes of feature 
     25     * styles. Keys are one or more of ["Point", "Line", "Polygon"] 
     26     */ 
     27    symbolizer: null, 
     28     
     29    /** 
     30     * APIProperty: minScale 
     31     * {Number} or {String} minimum scale at which to draw the feature. 
     32     * In the case of a String, this can be a combination of text and 
     33     * propertyNames in the form "literal ${propertyName}" 
     34     */ 
     35    minScale: null, 
     36 
     37    /** 
     38     * APIProperty: maxScale 
     39     * {Number} or {String} maximum scale at which to draw the feature. 
     40     * In the case of a String, this can be a combination of text and 
     41     * propertyNames in the form "literal ${propertyName}" 
     42     */ 
     43    maxScale: null, 
     44 
     45    /**  
     46     * Constructor: OpenLayers.Rule 
     47     * Creates a Rule. 
     48     * 
     49     * Parameters: 
     50     * options - {Object} An optional object with properties to set on the 
     51     *           rule 
     52     *  
     53     * Returns: 
     54     * {<OpenLayers.Rule>} 
     55     */ 
     56    initialize: function(options) { 
     57        this.symbolizer = {}; 
     58 
     59        OpenLayers.Util.extend(this, options); 
     60    }, 
     61 
     62    /**  
     63     * APIMethod: destroy 
     64     * nullify references to prevent circular references and memory leaks 
     65     */ 
     66    destroy: function() { 
     67        for (var i in this.symbolizer) { 
     68            this.symbolizer[i] = null; 
     69        } 
     70        this.symbolizer = null; 
     71    }, 
     72     
     73    /** 
     74     * APIMethod: evaluate 
     75     * evaluates this rule for a specific feature 
     76     *  
     77     * Parameters: 
     78     * feature - {<OpenLayers.Feature>} feature to apply the rule to. 
     79     *  
     80     * Returns: 
     81     * {boolean} true if the rule applies, false if it does not. 
     82     * This rule is the default rule and always returns true. 
     83     */ 
     84    evaluate: function(feature) { 
     85        // Default rule always applies. Subclasses will want to override this. 
     86        return true; 
     87    }, 
     88     
     89    CLASS_NAME: "OpenLayers.Rule" 
     90}); 
  • lib/OpenLayers/Style.js

    old new  
     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/Feature/Vector.js 
     9 * 
     10 * Class: OpenLayers.Style 
     11 *  
     12 * This class represents a UserStyle obtained 
     13 * from a SLD, containing styling rules. 
     14 */ 
     15OpenLayers.Style = OpenLayers.Class({ 
     16 
     17    /** 
     18     * APIProperty: name 
     19     * {String} 
     20     */ 
     21    name: null, 
     22     
     23    /** 
     24     * APIProperty: layerName 
     25     * {<String>} name of the layer that this style belongs to, usually 
     26     * according to the NamedLayer attribute of an SLD document. 
     27     */ 
     28    layerName: null, 
     29     
     30    /** 
     31     * APIProperty: isDefault 
     32     * {Boolean} 
     33     */ 
     34    isDefault: false, 
     35      
     36    /**  
     37     * Property: rules  
     38     * Array({<OpenLayers.Rule>})  
     39     */ 
     40    rules: null, 
     41     
     42    /** 
     43     * Property: defaultStyle 
     44     * {Object} hash of style properties to use as default for merging 
     45     * rule-based style symbolizers onto. If no rules are defined, createStyle 
     46     * will return this style. 
     47     */ 
     48    defaultStyle: null, 
     49     
     50    /** 
     51     * Property: propertyStyles 
     52     * {Hash of Boolean} cache of style properties that need to be parsed for 
     53     * propertyNames. Property names are keys, values won't be used. 
     54     */ 
     55    propertyStyles: null, 
     56     
     57 
     58    /**  
     59     * Constructor: OpenLayers.Style 
     60     * Creates a UserStyle. 
     61     * 
     62     * Parameters: 
     63     * style        - {Object} Optional hash of style properties that will be 
     64     *                used as default style for this style object. This style 
     65     *                applies if no rules are specified. Symbolizers defined in 
     66     *                rules will extend this default style. 
     67     * options      - {Object} An optional object with properties to set on the 
     68     *                userStyle 
     69     *  
     70     * Return: 
     71     * {<OpenLayers.Style>} 
     72     */ 
     73    initialize: function(style, options) { 
     74        this.rules = []; 
     75 
     76        // use the default style from OpenLayers.Feature.Vector if no style 
     77        // was given in the constructor 
     78        this.setDefaultStyle(style ||  
     79                OpenLayers.Feature.Vector.style["default"]); 
     80         
     81        OpenLayers.Util.extend(this, options); 
     82    }, 
     83 
     84    /**  
     85     * APIMethod: destroy 
     86     * nullify references to prevent circular references and memory leaks 
     87     */ 
     88    destroy: function() { 
     89        for (var i=0; i<this.rules.length; i++) { 
     90            this.rules[i].destroy(); 
     91            this.rules[i] = null; 
     92        } 
     93        this.rules = null; 
     94        this.defaultStyle = null; 
     95    }, 
     96     
     97    /** 
     98     * APIMethod: createStyle 
     99     * creates a style by applying all feature-dependent rules to the base 
     100     * style. 
     101     *  
     102     * Parameters: 
     103     * feature - {<OpenLayers.Feature>} feature to evaluate rules for 
     104     * baseStyle - {Object} hash of styles feature styles to extend 
     105     *  
     106     * Returns: 
     107     * {<OpenLayers.Feature.Vector.style>} hash of feature styles 
     108     */ 
     109    createStyle: function(feature, baseStyle) { 
     110        if (!baseStyle) { 
     111            baseStyle = this.defaultStyle; 
     112        } 
     113        var style = OpenLayers.Util.extend({}, baseStyle); 
     114         
     115        var draw = true; 
     116 
     117        for (var i=0; i<this.rules.length; i++) { 
     118            // does the rule apply?         
     119            var applies = this.rules[i].evaluate(feature); 
     120            if (applies) { 
     121                // check if within minScale/maxScale bounds 
     122                var scale = feature.layer.map.getScale(); 
     123                if (this.rules[i].minScale) { 
     124                    draw = scale > OpenLayers.Style.createLiteral( 
     125                            this.rules[i].minScale, feature); 
     126                } 
     127                if (draw && this.rules[i].maxScale) { 
     128                    draw = scale < OpenLayers.Style.createLiteral( 
     129                            this.rules[i].maxScale, feature); 
     130                } 
     131                 
     132                // determine which symbolizer (Point, Line, Polygon) to use 
     133                var symbolizerPrefix = feature.geometry ? 
     134                        this.getSymbolizerPrefix(feature.geometry) : 
     135                        OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; 
     136 
     137                // now merge the style with the current style 
     138                var symbolizer = this.rules[i].symbolizer[symbolizerPrefix]; 
     139                OpenLayers.Util.extend(style, symbolizer); 
     140            } 
     141        } 
     142 
     143        style.display = draw ? "" : "none"; 
     144 
     145        // calculate literals for all styles in the propertyStyles cache 
     146        for (var i in this.propertyStyles) { 
     147            style[i] = OpenLayers.Style.createLiteral(style[i], feature); 
     148        } 
     149         
     150        return style; 
     151    }, 
     152     
     153    /** 
     154     * Method: findPropertyStyles 
     155     * Looks into all rules for this style and the defaultStyle to collect 
     156     * all the style hash property names containing ${...} strings that have 
     157     * to be replaced using the createLiteral method before returning them. 
     158     *  
     159     * Returns: 
     160     * {Object} hash of property names that need createLiteral parsing. The 
     161     * name of the property is the key, and the value is true; 
     162     */ 
     163    findPropertyStyles: function() { 
     164        var propertyStyles = {}; 
     165 
     166        // check the default style 
     167        var style = this.defaultStyle; 
     168        for (var i in style) { 
     169            if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) {   
     170                propertyStyles[i] = true; 
     171            } 
     172        } 
     173 
     174        // walk through all rules to check for properties in their symbolizer 
     175        var rules = this.rules; 
     176        var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES; 
     177        for (var i in rules) { 
     178            for (var s=0; s<prefixes.length; s++) { 
     179                style = rules[i].symbolizer[prefixes[s]]; 
     180                for (var j in style) { 
     181                    if (typeof style[j] == "string" && 
     182                            style[j].match(/\$\{\w+\}/)) { 
     183                        propertyStyles[j] = true; 
     184                    } 
     185                } 
     186            } 
     187        } 
     188        return propertyStyles; 
     189    }, 
     190     
     191    /** 
     192     * APIMethod: addRules 
     193     * Adds rules to this style. 
     194     *  
     195     * Parameters: 
     196     * rules - {Array(<OpenLayers.Rule>)} 
     197     */ 
     198    addRules: function(rules) { 
     199        this.rules = this.rules.concat(rules); 
     200        this.propertyStyles = this.findPropertyStyles(); 
     201    }, 
     202     
     203    /** 
     204     * APIMethod: setDefaultStyle 
     205     * Sets the default style for this style object. 
     206     *  
     207     * Parameters: 
     208     * style - {Object} Hash of style properties 
     209     */ 
     210    setDefaultStyle: function(style) { 
     211        this.defaultStyle = style;  
     212        this.propertyStyles = this.findPropertyStyles(); 
     213    }, 
     214         
     215    /** 
     216     * Method: getSymbolizerPrefix 
     217     * Returns the correct symbolizer prefix according to the 
     218     * geometry type of the passed geometry 
     219     *  
     220     * Parameters: 
     221     * geometry {<OpenLayers.Geometry>} 
     222     *  
     223     * Returns: 
     224     * {String} key of the according symbolizer 
     225     */ 
     226    getSymbolizerPrefix: function(geometry) { 
     227        var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES; 
     228        for (var i=0; i<prefixes.length; i++) { 
     229            if (geometry.CLASS_NAME.indexOf(prefixes[i]) != -1) { 
     230                return prefixes[i]; 
     231            } 
     232        } 
     233    }, 
     234     
     235    CLASS_NAME: "OpenLayers.Style" 
     236}); 
     237 
     238 
     239/** 
     240 * Function: createLiteral 
     241 * converts a style value holding a combination of PropertyName and Literal 
     242 * into a Literal, taking the property values from the passed features. 
     243 *  
     244 * Parameters: 
     245 * value   {String} value to parse. If this string contains a construct like 
     246 *         "foo ${bar}", then "foo " will be taken as literal, and "${bar}" 
     247 *         will be replaced by the value of the "bar" attribute of the passed 
     248 *         feature. 
     249 * feature {<OpenLayers.Feature>} feature to take attribute values from 
     250 *  
     251 * Returns: 
     252 * {String} the parsed value. In the example of the value parameter above, the 
     253 * result would be "foo valueOfBar", assuming that the passed feature has an 
     254 * attribute named "bar" with the value "valueOfBar". 
     255 */ 
     256OpenLayers.Style.createLiteral = function(value, feature) { 
     257    if (typeof value == "string" && value.indexOf("${") != -1) { 
     258        var attributes = feature.attributes || feature.data; 
     259        // the following line depends on #1133 
     260        value = OpenLayers.String.format(value, attributes) 
     261        value = isNaN(value) ? value : parseFloat(value); 
     262    } 
     263    return value; 
     264} 
     265     
     266/** 
     267 * Constant: OpenLayers.Style.SYMBOLIZER_PREFIXES 
     268 * {Array} prefixes of the sld symbolizers. These are the 
     269 * same as the main geometry types 
     270 */ 
     271OpenLayers.Style.SYMBOLIZER_PREFIXES = ['Point', 'Line', 'Polygon']; 
  • lib/OpenLayers.js

    old