OpenLayers OpenLayers

Ticket #1297: 1297-r5900-A2.patch

File 1297-r5900-A2.patch, 15.4 kB (added by ahocevar, 1 year ago)

New patch that represents the current state of the discussion

  • examples/georss-flickr.html

    old new  
    2828             
    2929            // create a property style that reads the externalGraphic url from 
    3030            // the thumbail attribute of the rss item 
    31             style = new OpenLayers.Style({ 
    32                 externalGraphic: "${thumbnail}", 
    33                 pointRadius: 20}); 
     31            style = new OpenLayers.Style({externalGraphic: "${thumbnail}"}); 
    3432             
    3533            // create a rule with a point symbolizer that will make the thumbnail 
    3634            // larger if the title of the rss item conatins "powder" 
     
    4139                    symbolizer: {"Point": {pointRadius: 30}}}); 
    4240            rule.value2regex("*"); 
    4341             
    44             style.addRules([rule]); 
     42            // If the above rule does not apply, use a smaller pointRadius. 
     43            var elseRule = new OpenLayers.Rule({ 
     44                    symbolizer: {"Point": {pointRadius: 20}}}); 
     45             
     46            style.addRules([rule, elseRule]); 
    4547             
    4648            markerLayer = new OpenLayers.Layer.Vector("", {style: style}); 
    4749            map.addLayer(markerLayer); 
  • examples/sld.html

    old new  
    6565                    OpenLayers.Feature.Vector.style["select"]; 
    6666            hover = new OpenLayers.Control.SelectFeature(gmlLayers[2], { 
    6767                    selectStyle: waterStyle["Hover Styler"], 
    68                     hover: true, 
    69                     select: select 
     68                    hover: true 
    7069                }); 
    7170            map.addControl(hover); 
    7271            hover.activate(); 
     
    7271            hover.activate(); 
    7372        } 
    7473 
    75         // replaces OpenLayers.Control.Select.select 
    76         var select = function(feature) { 
    77             // store layer style 
    78             var style = feature.layer.style; 
    79             // set temporary layer style for hover rendering 
    80             feature.layer.style = hover.selectStyle; 
    81             OpenLayers.Control.SelectFeature.prototype.select.apply(hover, arguments); 
    82             // restore layer style 
    83             feature.layer.style = style; 
    84         } 
    85          
    8674        // set a new style when the radio button changes 
    8775        function setStyle(styleName) { 
    8876            // change the style of the features of the WaterBodies layer 
  • examples/tasmania/sld-tasmania.xml

    old new  
    5151            </sld:Stroke> 
    5252          </sld:PolygonSymbolizer> 
    5353        </sld:Rule> 
     54        <sld:Rule> 
     55          <sld:Name>testRuleNameElse</sld:Name> 
     56          <sld:Title>title</sld:Title> 
     57          <sld:Abstract>Abstract</sld:Abstract> 
     58          <ogc:ElseFilter/> 
     59        </sld:Rule> 
    5460      </sld:FeatureTypeStyle> 
    5561    </sld:UserStyle> 
    5662 
     
    113119            </sld:Stroke> 
    114120          </sld:PolygonSymbolizer> 
    115121        </sld:Rule> 
    116  
     122        <sld:Rule> 
     123          <sld:Name>testRuleNameHoverElse</sld:Name> 
     124          <sld:Title>title</sld:Title> 
     125          <sld:Abstract>Abstract</sld:Abstract> 
     126          <ogc:ElseFilter/> 
     127        </sld:Rule> 
    117128      </sld:FeatureTypeStyle> 
    118129    </sld:UserStyle> 
    119130 
  • lib/OpenLayers/Format/SLD.js

    old new  
    106106            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 
    107107        } 
    108108         
     109        options = options || {}; 
    109110        OpenLayers.Util.applyDefaults(options, { 
    110111            withNamedLayer: false, 
    111112            overrideDefaultStyleKey: true 
     
    206207        if (filter && filter.length > 0) { 
    207208            var rule = this.parseFilter(filter[0]); 
    208209        } else { 
    209             // rule applies to all features 
     210            // rule applies to all features (ElseFilter) 
    210211            var rule = new OpenLayers.Rule(); 
    211212        } 
    212213        rule.name = name; 
     
    218219            xmlNode, this.sldns, "MinScaleDenominator" 
    219220        ); 
    220221        if (minScale && minScale.length > 0) { 
    221             rule.minScale = parseFloat(this.getChildValue(minScale[0])); 
     222            rule.minScaleDenominator =  
     223                parseFloat(this.getChildValue(minScale[0])); 
    222224        } 
    223225         
    224226        // MaxScaleDenominator 
     
    226228            xmlNode, this.sldns, "MaxScaleDenominator" 
    227229        ); 
    228230        if (maxScale && maxScale.length > 0) { 
    229             rule.maxScale = parseFloat(this.getChildValue(maxScale[0])); 
     231            rule.maxScaleDenominator = 
     232                parseFloat(this.getChildValue(maxScale[0])); 
    230233        } 
    231234         
    232235        // STYLES 
  • lib/OpenLayers/Rule.js

    old new  
    2828    symbolizer: null, 
    2929     
    3030    /** 
    31      * APIProperty: minScale 
     31     * APIProperty: minScaleDenominator 
    3232     * {Number} or {String} minimum scale at which to draw the feature. 
    3333     * In the case of a String, this can be a combination of text and 
    3434     * propertyNames in the form "literal ${propertyName}" 
     
    3333     * In the case of a String, this can be a combination of text and 
    3434     * propertyNames in the form "literal ${propertyName}" 
    3535     */ 
    36     minScale: null, 
     36    minScaleDenominator: null, 
    3737 
    3838    /** 
    39      * APIProperty: maxScale 
     39     * APIProperty: maxScaleDenominator 
    4040     * {Number} or {String} maximum scale at which to draw the feature. 
    4141     * In the case of a String, this can be a combination of text and 
    4242     * propertyNames in the form "literal ${propertyName}" 
     
    4141     * In the case of a String, this can be a combination of text and 
    4242     * propertyNames in the form "literal ${propertyName}" 
    4343     */ 
    44     maxScale: null, 
    45  
     44    maxScaleDenominator: null, 
     45     
    4646    /**  
    4747     * Constructor: OpenLayers.Rule 
    4848     * Creates a Rule. 
     
    8888    }, 
    8989     
    9090    CLASS_NAME: "OpenLayers.Rule" 
    9191 
  • lib/OpenLayers/Style.js

    old new  
    101101     * style. 
    102102     *  
    103103     * Parameters: 
    104      * feature - {<OpenLayers.Feature>} feature to evaluate rules for 
    105      * baseStyle - {Object} hash of styles feature styles to extend 
     104     * feature   - {<OpenLayers.Feature>} feature to evaluate rules for 
     105     * options   - {Object} additional options that modify the returned result 
     106     *             of this method. 
     107     *             - <extend> {Boolean} if set to false, output will be an 
     108     *                        array of styles (one for every rule that  
     109     *                        applies) instead of one style that gets 
     110     *                        extended with every rule that applies. 
     111     *                        Default is true. 
    106112     *  
    107113     * Returns: 
    108114     * {<OpenLayers.Feature.Vector.style>} hash of feature styles 
     
    107113     * Returns: 
    108114     * {<OpenLayers.Feature.Vector.style>} hash of feature styles 
    109115     */ 
    110     createStyle: function(feature, baseStyle) { 
    111         if (!baseStyle) { 
    112             baseStyle = this.defaultStyle; 
    113         } 
    114         var style = OpenLayers.Util.extend({}, baseStyle); 
     116    createStyle: function(feature, options) { 
     117        options = options || {}; 
     118        OpenLayers.Util.applyDefaults(options, { 
     119            extend: true 
     120        }); 
     121 
     122        var style = OpenLayers.Util.extend({}, this.defaultStyle); 
    115123         
    116         var draw = true; 
     124        var rules = this.rules; 
     125        var draw = rules.length == 0 ? true : false; 
    117126 
    118         for (var i=0; i<this.rules.length; i++) { 
     127        var styles = []; 
     128        var rule; 
     129        for (var i=0; i<rules.length; i++) { 
     130            rule = rules[i]; 
    119131            // does the rule apply?         
    120             var applies = this.rules[i].evaluate(feature); 
     132            var applies = rule.evaluate(feature); 
     133             
     134            if (rule.minScaleDenominator || rule.maxScaleDenominator) { 
     135                var scale = feature.layer.map.getScale(); 
     136            } 
     137             
     138            // check if within minScale/maxScale bounds 
     139            if (rule.minScaleDenominator) { 
     140                applies = scale >= OpenLayers.Style.createLiteral( 
     141                        rule.minScaleDenominator, feature); 
     142            } 
     143            if (applies && rule.maxScaleDenominator) { 
     144                applies = scale < OpenLayers.Style.createLiteral( 
     145                        rule.maxScaleDenominator, feature); 
     146            } 
     147             
     148            if (draw && rule.CLASS_NAME == "OpenLayers.Rule") { 
     149                // apply plain rules only if no other applied (ElseFilter) 
     150                applies = false; 
     151            } 
     152 
    121153            if (applies) { 
    122                 // check if within minScale/maxScale bounds 
    123                 var scale = feature.layer.map.getScale(); 
    124                 if (this.rules[i].minScale) { 
    125                     draw = scale > OpenLayers.Style.createLiteral( 
    126                             this.rules[i].minScale, feature); 
    127                 } 
    128                 if (draw && this.rules[i].maxScale) { 
    129                     draw = scale < OpenLayers.Style.createLiteral( 
    130                             this.rules[i].maxScale, feature); 
    131                 } 
    132                  
     154                draw = true; 
     155 
    133156                // determine which symbolizer (Point, Line, Polygon) to use 
    134157                var symbolizerPrefix = feature.geometry ? 
    135158                        this.getSymbolizerPrefix(feature.geometry) : 
     
    135158                        this.getSymbolizerPrefix(feature.geometry) : 
    136159                        OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; 
    137160 
    138                 // now merge the style with the current style 
     161                // merge the style with the current style 
    139162                var symbolizer = this.rules[i].symbolizer[symbolizerPrefix]; 
    140                 OpenLayers.Util.extend(style, symbolizer); 
     163                if (options.extend == false) { 
     164                    style = OpenLayers.Util.extend({}, this.defaultStyle); 
     165                    OpenLayers.Util.extend(style, symbolizer); 
     166                    styles.push(style); 
     167                } else { 
     168                    OpenLayers.Util.extend(style, symbolizer); 
     169                } 
    141170            } 
    142171        } 
    143172 
    144         style.display = draw ? "" : "none"; 
    145  
    146173        // calculate literals for all styles in the propertyStyles cache 
     174        if (options.extend == true) { 
     175            this.createLiterals(style, feature); 
     176            style.display = draw ? "" : "none"; 
     177        } 
     178         
     179        return options.extend == false ? styles : style; 
     180    }, 
     181     
     182    /** 
     183     * Method: createLiterals 
     184     * creates literals for all style properties that have an entry in 
     185     * <this.propertyStyles>. 
     186     *  
     187     * Parameters: 
     188     * style   - {Object} style to create literals for. Will be modified 
     189     *           inline. 
     190     * feature - {<OpenLayers.Feature.Vector>} feature to take properties from 
     191     *  
     192     * Returns; 
     193     * {Object} the modified style 
     194     */ 
     195    createLiterals: function(style, feature) { 
    147196        for (var i in this.propertyStyles) { 
    148197            style[i] = OpenLayers.Style.createLiteral(style[i], feature); 
    149198        } 
    150          
    151199        return style; 
    152200    }, 
    153201     
  • tests/test_Style.html

    old new  
    1515    } 
    1616     
    1717    function test_Style_create(t) { 
    18         t.plan(5); 
     18        t.plan(11); 
    1919         
    2020        var map = new OpenLayers.Map("map"); 
    2121         
     
    2727         
    2828        var style = new OpenLayers.Style(baseStyle); 
    2929         
    30         var rule = new OpenLayers.Rule.FeatureId({ 
     30        var rule1 = new OpenLayers.Rule.FeatureId({ 
    3131                fids: ["1"], 
    3232                symbolizer: {"Point": {fillColor: "green"}}, 
    33                 maxScale: 2000000}); 
    34         style.addRules([rule]); 
     33                maxScaleDenominator: 500000}); 
     34        var rule2 = new OpenLayers.Rule.FeatureId({ 
     35                fids: ["1"], 
     36                symbolizer: {"Point": {fillColor: "yellow"}}, 
     37                minScaleDenominator: 500000, 
     38                maxScaleDenominator: 1000000}); 
     39        var rule3 = new OpenLayers.Rule.FeatureId({ 
     40                fids: ["1"], 
     41                symbolizer: {"Point": {fillColor: "red"}}, 
     42                minScaleDenominator: 1000000, 
     43                maxScaleDenominator: 2500000}); 
     44        style.addRules([rule1, rule2, rule3]); 
    3545         
    3646        var feature = new OpenLayers.Feature.Vector( 
    3747                new OpenLayers.Geometry.Point(3,5), 
     
    4555        map.addLayer(layer); 
    4656        map.setBaseLayer(layer); 
    4757 
    48         map.setCenter(new OpenLayers.LonLat(3,5), 8); 
    49         // at this scale, the feature should be visible 
     58        map.setCenter(new OpenLayers.LonLat(3,5), 10); 
     59 
     60        var createdStyles = style.createStyle(feature, {extend: false}); 
     61        t.eq(createdStyles[0].fillColor, "green", "Returned styles array is correct if extend option set to false"); 
     62 
     63        // at this scale, the feature should be green 
    5064        var createdStyle = style.createStyle(feature); 
    5165        t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style correctly."); 
    5266        t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 
     67        t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule applied correctly."); 
     68 
     69        map.setCenter(new OpenLayers.LonLat(3,5), 9); 
     70        // at this scale, the feature should be red 
     71        createdStyle = style.createStyle(feature); 
     72        t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 
     73        t.eq(createdStyle.fillColor, "yellow", "Point symbolizer from rule applied correctly."); 
     74 
     75        map.setCenter(new OpenLayers.LonLat(3,5), 8); 
     76        // at this scale, the feature should be yellow 
     77        createdStyle = style.createStyle(feature); 
     78        t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 
     79        t.eq(createdStyle.fillColor, "red", "Point symbolizer from rule applied correctly."); 
    5380 
    5481        map.setCenter(new OpenLayers.LonLat(3,5), 7); 
    5582        // at this scale, the feature should be invisible 
     
    5582        // at this scale, the feature should be invisible 
    5683        createdStyle = style.createStyle(feature); 
    5784        t.eq(createdStyle.display, "none", "Feature is invisible at scale "+map.getScale()); 
    58         t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule for fid=\"1\" applied correctly."); 
     85        t.eq(createdStyle.fillColor, baseStyle.fillColor, "Point symbolizer from base style applied correctly."); 
    5986         
    6087        feature.fid = "2"; 
    6188        // now the rule should not apply 
     
    6289         
    6390        createdStyle = style.createStyle(feature); 
    6491        t.eq(createdStyle.fillColor, baseStyle.fillColor, "Correct style for rule that does not apply to fid=\"2\"."); 
    65          
    6692    } 
    6793 
    6894    function test_Style_destroy(t) {