* Style and Rule now have separate context properties
* new convenience method addUniqueValueRules in OL.!StyleMap. This can actually be used to achieve what I was trying to show in the example of this ticket's description. * some refactoring of OL.Style to remove duplicate code (with tests) * a new example showing how to add a "unique value" legend to a point layer using the new addUniqueValueRules method * Rule.symbolizer can now also be just a symbolizer, instead of a hash of symbolizers keyed by "Point", "Line", "Polygon". This will make things even simpler (as can be seen in the styles-unique.html example) r=tschaub (closes #1373) git-svn-id: http://svn.openlayers.org/trunk/openlayers@6396 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -22,9 +22,9 @@ OpenLayers.Rule = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* 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
|
||||
* {Object} An optional object with properties that the rule should be
|
||||
* evaluatad against. If no context is specified, feature.attributes will
|
||||
* be used.
|
||||
*/
|
||||
context: null,
|
||||
|
||||
@@ -40,8 +40,8 @@ OpenLayers.Rule = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* Property: symbolizer
|
||||
* {Object} Hash of styles for this rule. Contains hashes of feature
|
||||
* styles. Keys are one or more of ["Point", "Line", "Polygon"]
|
||||
* {Object} Symbolizer or hash of symbolizers for this rule. If hash of
|
||||
* symbolizers, keys are one or more of ["Point", "Line", "Polygon"]
|
||||
*/
|
||||
symbolizer: null,
|
||||
|
||||
|
||||
@@ -40,6 +40,14 @@ OpenLayers.Style = OpenLayers.Class({
|
||||
*/
|
||||
rules: null,
|
||||
|
||||
/**
|
||||
* Property: context
|
||||
* {Object} An optional object with properties that symbolizers' property
|
||||
* values should be evaluatad against. If no context is specified,
|
||||
* feature.attributes will be used
|
||||
*/
|
||||
context: null,
|
||||
|
||||
/**
|
||||
* Property: defaultStyle
|
||||
* {Object} hash of style properties to use as default for merging
|
||||
@@ -116,10 +124,6 @@ OpenLayers.Style = OpenLayers.Class({
|
||||
var appliedRules = false;
|
||||
for(var i=0; i<rules.length; i++) {
|
||||
rule = rules[i];
|
||||
context = rule.context;
|
||||
if (!context) {
|
||||
context = feature.attributes || feature.data;
|
||||
}
|
||||
// does the rule apply?
|
||||
var applies = rule.evaluate(feature);
|
||||
|
||||
@@ -128,7 +132,7 @@ OpenLayers.Style = OpenLayers.Class({
|
||||
elseRules.push(rule);
|
||||
} else {
|
||||
appliedRules = true;
|
||||
this.applySymbolizer(rule, style, feature, context);
|
||||
this.applySymbolizer(rule, style, feature);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,7 +141,7 @@ OpenLayers.Style = OpenLayers.Class({
|
||||
if(appliedRules == false && elseRules.length > 0) {
|
||||
appliedRules = true;
|
||||
for(var i=0; i<elseRules.length; i++) {
|
||||
this.applySymbolizer(elseRules[i], style, feature, context);
|
||||
this.applySymbolizer(elseRules[i], style, feature);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,18 +162,19 @@ OpenLayers.Style = OpenLayers.Class({
|
||||
* rule - {OpenLayers.Rule}
|
||||
* style - {Object}
|
||||
* feature - {<OpenLayer.Feature.Vector>}
|
||||
* context - {Object}
|
||||
*
|
||||
* Returns:
|
||||
* {Object} A style with new symbolizer applied.
|
||||
*/
|
||||
applySymbolizer: function(rule, style, feature, context) {
|
||||
applySymbolizer: function(rule, style, feature) {
|
||||
var symbolizerPrefix = feature.geometry ?
|
||||
this.getSymbolizerPrefix(feature.geometry) :
|
||||
OpenLayers.Style.SYMBOLIZER_PREFIXES[0];
|
||||
|
||||
var symbolizer = rule.symbolizer[symbolizerPrefix];
|
||||
var symbolizer = rule.symbolizer[symbolizerPrefix] || rule.symbolizer;
|
||||
|
||||
var context = this.context || feature.attributes || feature.data;
|
||||
|
||||
// merge the style with the current style
|
||||
return this.createLiterals(
|
||||
OpenLayers.Util.extend(style, symbolizer), context);
|
||||
@@ -212,29 +217,51 @@ OpenLayers.Style = OpenLayers.Class({
|
||||
|
||||
// check the default style
|
||||
var style = this.defaultStyle;
|
||||
for (var i in style) {
|
||||
if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) {
|
||||
propertyStyles[i] = true;
|
||||
}
|
||||
}
|
||||
this.addPropertyStyles(propertyStyles, style);
|
||||
|
||||
// walk through all rules to check for properties in their symbolizer
|
||||
var rules = this.rules;
|
||||
var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES;
|
||||
var symbolizer, value;
|
||||
for (var i=0; i<rules.length; i++) {
|
||||
for (var s=0; s<prefixes.length; s++) {
|
||||
style = rules[i].symbolizer[prefixes[s]];
|
||||
for (var j in style) {
|
||||
if (typeof style[j] == "string" &&
|
||||
style[j].match(/\$\{\w+\}/)) {
|
||||
propertyStyles[j] = true;
|
||||
}
|
||||
var symbolizer = rules[i].symbolizer;
|
||||
for (var key in symbolizer) {
|
||||
value = symbolizer[key];
|
||||
if (typeof value == "object") {
|
||||
// symbolizer key is "Point", "Line" or "Polygon"
|
||||
this.addPropertyStyles(propertyStyles, value);
|
||||
} else {
|
||||
// symbolizer is a hash of style properties
|
||||
this.addPropertyStyles(propertyStyles, symbolizer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return propertyStyles;
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: addPropertyStyles
|
||||
*
|
||||
* Parameters:
|
||||
* propertyStyles - {Object} hash to add new property styles to. Will be
|
||||
* modified inline
|
||||
* symbolizer - {Object} search this symbolizer for property styles
|
||||
*
|
||||
* Returns:
|
||||
* {Object} propertyStyles hash
|
||||
*/
|
||||
addPropertyStyles: function(propertyStyles, symbolizer) {
|
||||
var property;
|
||||
for (var key in symbolizer) {
|
||||
property = symbolizer[key];
|
||||
if (typeof property == "string" &&
|
||||
property.match(/\$\{\w+\}/)) {
|
||||
propertyStyles[key] = true;
|
||||
}
|
||||
}
|
||||
return propertyStyles;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: addRules
|
||||
* Adds rules to this style.
|
||||
|
||||
@@ -110,6 +110,32 @@ OpenLayers.StyleMap = OpenLayers.Class({
|
||||
return OpenLayers.Util.extend(defaultSymbolizer,
|
||||
this.styles[intent].createSymbolizer(feature));
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: addUniqueValueRules
|
||||
* Convenience method to create comparison rules for unique values of a
|
||||
* property. The rules will be added to the style object for a specified
|
||||
* rendering intent. This method is a shortcut for creating something like
|
||||
* the "unique value legends" familiar from well known desktop GIS systems
|
||||
*
|
||||
* Parameters:
|
||||
* renderIntent - {String} rendering intent to add the rules to
|
||||
* property - {String} values of feature attributes to create the
|
||||
* rules for
|
||||
* symbolizers - {Object} Hash of symbolizers, keyed by the desired
|
||||
* property values
|
||||
*/
|
||||
addUniqueValueRules: function(renderIntent, property, symbolizers) {
|
||||
var rules = [];
|
||||
for (var value in symbolizers) {
|
||||
rules.push(new OpenLayers.Rule.Comparison({
|
||||
type: OpenLayers.Rule.Comparison.EQUAL_TO,
|
||||
property: property,
|
||||
value: value,
|
||||
symbolizer: symbolizers[value]}));
|
||||
}
|
||||
this.styles[renderIntent].addRules(rules);
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.StyleMap"
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user