Ticket #1297: 1297-r5949-A3.patch
| File 1297-r5949-A3.patch, 12.5 kB (added by ahocevar, 1 year ago) |
|---|
-
examples/georss-flickr.html
old new 28 28 29 29 // create a property style that reads the externalGraphic url from 30 30 // 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}"}); 34 32 35 33 // create a rule with a point symbolizer that will make the thumbnail 36 34 // larger if the title of the rss item conatins "powder" … … 41 39 symbolizer: {"Point": {pointRadius: 30}}}); 42 40 rule.value2regex("*"); 43 41 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]); 45 47 46 48 markerLayer = new OpenLayers.Layer.Vector("", {style: style}); 47 49 map.addLayer(markerLayer); -
examples/tasmania/sld-tasmania.xml
old new 51 51 </sld:Stroke> 52 52 </sld:PolygonSymbolizer> 53 53 </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> 54 60 </sld:FeatureTypeStyle> 55 61 </sld:UserStyle> 56 62 … … 113 119 </sld:Stroke> 114 120 </sld:PolygonSymbolizer> 115 121 </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> 117 128 </sld:FeatureTypeStyle> 118 129 </sld:UserStyle> 119 130 -
lib/OpenLayers/Format/SLD.js
old new 106 106 data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 107 107 } 108 108 109 options = options || {}; 109 110 OpenLayers.Util.applyDefaults(options, { 110 111 withNamedLayer: false, 111 112 overrideDefaultStyleKey: true … … 206 207 if (filter && filter.length > 0) { 207 208 var rule = this.parseFilter(filter[0]); 208 209 } else { 209 // rule applies to all features 210 // rule applies to all features (no filter or ElseFilter) 210 211 var rule = new OpenLayers.Rule(); 211 212 } 212 213 rule.name = name; … … 218 219 xmlNode, this.sldns, "MinScaleDenominator" 219 220 ); 220 221 if (minScale && minScale.length > 0) { 221 rule.minScale = parseFloat(this.getChildValue(minScale[0])); 222 rule.minScaleDenominator = 223 parseFloat(this.getChildValue(minScale[0])); 222 224 } 223 225 224 226 // MaxScaleDenominator … … 226 228 xmlNode, this.sldns, "MaxScaleDenominator" 227 229 ); 228 230 if (maxScale && maxScale.length > 0) { 229 rule.maxScale = parseFloat(this.getChildValue(maxScale[0])); 231 rule.maxScaleDenominator = 232 parseFloat(this.getChildValue(maxScale[0])); 230 233 } 231 234 232 235 // STYLES -
lib/OpenLayers/Rule.js
old new 28 28 symbolizer: null, 29 29 30 30 /** 31 * APIProperty: minScale 31 * APIProperty: minScaleDenominator 32 32 * {Number} or {String} minimum scale at which to draw the feature. 33 33 * In the case of a String, this can be a combination of text and 34 34 * propertyNames in the form "literal ${propertyName}" … … 33 33 * In the case of a String, this can be a combination of text and 34 34 * propertyNames in the form "literal ${propertyName}" 35 35 */ 36 minScale : null,36 minScaleDenominator: null, 37 37 38 38 /** 39 * APIProperty: maxScale 39 * APIProperty: maxScaleDenominator 40 40 * {Number} or {String} maximum scale at which to draw the feature. 41 41 * In the case of a String, this can be a combination of text and 42 42 * propertyNames in the form "literal ${propertyName}" … … 41 41 * In the case of a String, this can be a combination of text and 42 42 * propertyNames in the form "literal ${propertyName}" 43 43 */ 44 maxScale : null,45 44 maxScaleDenominator: null, 45 46 46 /** 47 47 * Constructor: OpenLayers.Rule 48 48 * Creates a Rule. -
lib/OpenLayers/Style.js
old new 107 107 * Returns: 108 108 * {<OpenLayers.Feature.Vector.style>} hash of feature styles 109 109 */ 110 createStyle: function(feature, baseStyle) { 111 if (!baseStyle) { 112 baseStyle = this.defaultStyle; 113 } 114 var style = OpenLayers.Util.extend({}, baseStyle); 110 createStyle: function(feature) { 111 var style = OpenLayers.Util.extend({}, this.defaultStyle); 115 112 116 var draw = true; 113 var rules = this.rules; 114 var draw = rules.length == 0 ? true : false; 117 115 118 for (var i=0; i<this.rules.length; i++) { 116 var rule; 117 for (var i=0; i<rules.length; i++) { 118 rule = rules[i]; 119 119 // does the rule apply? 120 var applies = this.rules[i].evaluate(feature); 120 var applies = rule.evaluate(feature); 121 122 if (rule.minScaleDenominator || rule.maxScaleDenominator) { 123 var scale = feature.layer.map.getScale(); 124 } 125 126 // check if within minScale/maxScale bounds 127 if (rule.minScaleDenominator) { 128 applies = scale >= OpenLayers.Style.createLiteral( 129 rule.minScaleDenominator, feature); 130 } 131 if (applies && rule.maxScaleDenominator) { 132 applies = scale < OpenLayers.Style.createLiteral( 133 rule.maxScaleDenominator, feature); 134 } 135 136 if (draw && rule.CLASS_NAME == "OpenLayers.Rule") { 137 // apply plain rules only if no other applied (ElseFilter) 138 applies = false; 139 } 140 121 141 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 142 draw = true; 143 133 144 // determine which symbolizer (Point, Line, Polygon) to use 134 145 var symbolizerPrefix = feature.geometry ? 135 146 this.getSymbolizerPrefix(feature.geometry) : … … 135 146 this.getSymbolizerPrefix(feature.geometry) : 136 147 OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; 137 148 138 // nowmerge the style with the current style149 // merge the style with the current style 139 150 var symbolizer = this.rules[i].symbolizer[symbolizerPrefix]; 140 151 OpenLayers.Util.extend(style, symbolizer); 141 152 } … … 141 152 } 142 153 } 143 154 155 // calculate literals for all styles in the propertyStyles cache 156 this.createLiterals(style, feature); 144 157 style.display = draw ? "" : "none"; 145 146 // calculate literals for all styles in the propertyStyles cache 158 159 return style; 160 }, 161 162 /** 163 * Method: createLiterals 164 * creates literals for all style properties that have an entry in 165 * <this.propertyStyles>. 166 * 167 * Parameters: 168 * style - {Object} style to create literals for. Will be modified 169 * inline. 170 * feature - {<OpenLayers.Feature.Vector>} feature to take properties from 171 * 172 * Returns; 173 * {Object} the modified style 174 */ 175 createLiterals: function(style, feature) { 147 176 for (var i in this.propertyStyles) { 148 177 style[i] = OpenLayers.Style.createLiteral(style[i], feature); 149 178 } 150 151 179 return style; 152 180 }, 153 181 -
tests/test_Style.html
old new 15 15 } 16 16 17 17 function test_Style_create(t) { 18 t.plan( 5);18 t.plan(10); 19 19 20 20 var map = new OpenLayers.Map("map"); 21 21 … … 27 27 28 28 var style = new OpenLayers.Style(baseStyle); 29 29 30 var rule = new OpenLayers.Rule.FeatureId({30 var rule1 = new OpenLayers.Rule.FeatureId({ 31 31 fids: ["1"], 32 32 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]); 35 45 36 46 var feature = new OpenLayers.Feature.Vector( 37 47 new OpenLayers.Geometry.Point(3,5), … … 45 55 map.addLayer(layer); 46 56 map.setBaseLayer(layer); 47 57 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 // at this scale, the feature should be green 50 61 var createdStyle = style.createStyle(feature); 51 62 t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style correctly."); 52 63 t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 64 t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule applied correctly."); 65 66 map.setCenter(new OpenLayers.LonLat(3,5), 9); 67 // at this scale, the feature should be red 68 createdStyle = style.createStyle(feature); 69 t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 70 t.eq(createdStyle.fillColor, "yellow", "Point symbolizer from rule applied correctly."); 71 72 map.setCenter(new OpenLayers.LonLat(3,5), 8); 73 // at this scale, the feature should be yellow 74 createdStyle = style.createStyle(feature); 75 t.eq(createdStyle.display, "", "Feature is visible at scale "+map.getScale()); 76 t.eq(createdStyle.fillColor, "red", "Point symbolizer from rule applied correctly."); 53 77 54 78 map.setCenter(new OpenLayers.LonLat(3,5), 7); 55 79 // at this scale, the feature should be invisible … … 55 79 // at this scale, the feature should be invisible 56 80 createdStyle = style.createStyle(feature); 57 81 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.");82 t.eq(createdStyle.fillColor, baseStyle.fillColor, "Point symbolizer from base style applied correctly."); 59 83 60 84 feature.fid = "2"; 61 85 // now the rule should not apply … … 62 86 63 87 createdStyle = style.createStyle(feature); 64 88 t.eq(createdStyle.fillColor, baseStyle.fillColor, "Correct style for rule that does not apply to fid=\"2\"."); 65 66 89 } 67 90 68 91 function test_Style_destroy(t) {
