Giving vector layers the style they deserve

Now vector layers can have a style. ol.Style instances have an
apply method to get the symbolizer literals for a feature. If the
layer does not have a style defined, there is also a static
applyDefaultStyle function on ol.Style to get the default
symbolizer literals for a feature. The vector layer also got a
groupFeaturesBySymbolizerLiteral method, which returns an array
with features grouped by symbolizer, as needed by the canvas
renderer.
This commit is contained in:
ahocevar
2013-03-02 17:38:01 +01:00
parent fcd5804d2d
commit f4a4522eb4
8 changed files with 162 additions and 35 deletions
+46
View File
@@ -1,6 +1,9 @@
goog.provide('ol.style.Style');
goog.require('ol.Feature');
goog.require('ol.geom.GeometryType');
goog.require('ol.style.Rule');
goog.require('ol.style.SymbolizerLiteral');
/**
@@ -23,3 +26,46 @@ ol.style.Style = function(options) {
this.rules_ = goog.isDef(options.rules) ? options.rules : [];
};
/**
* @param {ol.Feature} feature Feature.
* @return {Array.<ol.style.SymbolizerLiteral>} Symbolizer literals for the
* feature.
*/
ol.style.Style.prototype.apply = function(feature) {
var rules = this.rules_,
literals = [],
rule, symbolizers;
for (var i = 0, ii = rules.length; i < ii; ++i) {
rule = rules[i];
if (rule.applies(feature)) {
symbolizers = rule.getSymbolizers();
for (var j = 0, jj = symbolizers.length; j < jj; ++j) {
literals.push(symbolizers[j].createLiteral(feature));
}
}
}
return literals;
};
/**
* @param {ol.Feature} feature Feature.
* @return {Array.<ol.style.SymbolizerLiteral>} Default symbolizer literals for
* the feature.
*/
ol.style.Style.applyDefaultStyle = function(feature) {
var type = feature.getGeometry().getType();
if (type === ol.geom.GeometryType.POINT ||
type === ol.geom.GeometryType.MULTIPOINT) {
return [ol.style.ShapeDefaults];
} else if (type === ol.geom.GeometryType.LINESTRING ||
type === ol.geom.GeometryType.MULTILINESTRING) {
return [ol.style.LineDefaults];
} else if (type === ol.geom.GeometryType.LINEARRING ||
type === ol.geom.GeometryType.POLYGON ||
type === ol.geom.GeometryType.MULTIPOLYGON) {
return [ol.style.PolygonDefaults];
}
};