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
+10
View File
@@ -37,6 +37,16 @@ ol.style.LineLiteral = function(config) {
goog.inherits(ol.style.LineLiteral, ol.style.SymbolizerLiteral);
/**
* @inheritDoc
*/
ol.style.LineLiteral.prototype.equals = function(lineLiteral) {
return this.strokeStyle == lineLiteral.strokeStyle &&
this.strokeWidth == lineLiteral.strokeWidth &&
this.opacity == lineLiteral.opacity;
};
/**
* @typedef {{strokeStyle: (string|ol.Expression),
* strokeWidth: (number|ol.Expression),
+11
View File
@@ -41,6 +41,17 @@ ol.style.PolygonLiteral = function(config) {
goog.inherits(ol.style.PolygonLiteral, ol.style.SymbolizerLiteral);
/**
* @inheritDoc
*/
ol.style.PolygonLiteral.prototype.equals = function(polygonLiteral) {
return this.fillStyle == polygonLiteral.fillStyle &&
this.strokeStyle == polygonLiteral.strokeStyle &&
this.strokeWidth == polygonLiteral.strokeWidth &&
this.opacity == polygonLiteral.opacity;
};
/**
* @typedef {{fillStyle: (string|ol.Expression),
* strokeStyle: (string|ol.Expression),
+19
View File
@@ -1,5 +1,6 @@
goog.provide('ol.style.Rule');
goog.require('ol.Feature');
goog.require('ol.filter.Filter');
goog.require('ol.style.Symbolizer');
@@ -32,3 +33,21 @@ ol.style.Rule = function(options) {
options.symbolizers : [];
};
/**
* @param {ol.Feature} feature Feature.
* @return {boolean} Does the rule apply to the feature?
*/
ol.style.Rule.prototype.applies = function(feature) {
return goog.isNull(this.filter_) ? true : this.filter_.applies(feature);
};
/**
* @return {Array.<ol.style.Symbolizer>} Symbolizers.
*/
ol.style.Rule.prototype.getSymbolizers = function() {
return this.symbolizers_;
};
+13
View File
@@ -57,6 +57,19 @@ ol.style.ShapeLiteral = function(config) {
goog.inherits(ol.style.ShapeLiteral, ol.style.PointLiteral);
/**
* @inheritDoc
*/
ol.style.ShapeLiteral.prototype.equals = function(shapeLiteral) {
return this.type == shapeLiteral.type &&
this.size == shapeLiteral.size &&
this.fillStyle == shapeLiteral.fillStyle &&
this.strokeStyle == shapeLiteral.strokeStyle &&
this.strokeWidth == shapeLiteral.strokeWidth &&
this.opacity == shapeLiteral.opacity;
};
/**
* @typedef {{type: (ol.style.ShapeType),
* size: (number|ol.Expression),
+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];
}
};
+8
View File
@@ -11,6 +11,14 @@ goog.require('ol.Feature');
ol.style.SymbolizerLiteral = function() {};
/**
* @param {ol.style.SymbolizerLiteral} symbolizerLiteral Symbolizer literal to
* compare to.
* @return {boolean} Is the passed symbolizer literal equal to this instance?
*/
ol.style.SymbolizerLiteral.prototype.equals = goog.abstractMethod;
/**
* @constructor