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:
@@ -1,7 +1,9 @@
|
||||
goog.provide('ol.layer.Vector');
|
||||
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.layer.Layer');
|
||||
goog.require('ol.source.Vector');
|
||||
goog.require('ol.style.Style');
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +14,13 @@ goog.require('ol.source.Vector');
|
||||
*/
|
||||
ol.layer.Vector = function(layerOptions) {
|
||||
goog.base(this, layerOptions);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.style.Style}
|
||||
*/
|
||||
this.style_ = goog.isDef(layerOptions.style) ? layerOptions.style : null;
|
||||
|
||||
};
|
||||
goog.inherits(ol.layer.Vector, ol.layer.Layer);
|
||||
|
||||
@@ -22,3 +31,38 @@ goog.inherits(ol.layer.Vector, ol.layer.Layer);
|
||||
ol.layer.Vector.prototype.getVectorSource = function() {
|
||||
return /** @type {ol.source.Vector} */ (this.getSource());
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array.<ol.Feature>} features Features.
|
||||
* @return {Array.<Array>} symbolizers for features.
|
||||
*/
|
||||
ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
|
||||
function(features) {
|
||||
var uniqueLiterals = {},
|
||||
featuresBySymbolizer = [],
|
||||
style = this.style_,
|
||||
feature, literals, literal, key;
|
||||
for (var i = 0, ii = features.length; i < ii; ++i) {
|
||||
feature = features[i];
|
||||
literals = goog.isNull(style) ?
|
||||
ol.style.Style.applyDefaultStyle(feature) :
|
||||
style.apply(feature);
|
||||
for (var j = 0, jj = literals.length; j < jj; ++j) {
|
||||
literal = literals[j];
|
||||
for (var l in uniqueLiterals) {
|
||||
if (literal.equals(uniqueLiterals[l])) {
|
||||
literal = uniqueLiterals[l];
|
||||
break;
|
||||
}
|
||||
}
|
||||
key = goog.getUid(literal);
|
||||
if (!goog.object.containsKey(uniqueLiterals, key)) {
|
||||
uniqueLiterals[key] = featuresBySymbolizer.length;
|
||||
featuresBySymbolizer.push([[], literal]);
|
||||
}
|
||||
featuresBySymbolizer[uniqueLiterals[key]][0].push(feature);
|
||||
}
|
||||
}
|
||||
return featuresBySymbolizer;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user