Moving all style functionality from source to style

This commit is contained in:
Tim Schaub
2013-11-15 14:19:57 -07:00
parent 307e425891
commit bfaed4d52d
5 changed files with 172 additions and 182 deletions

View File

@@ -18,6 +18,7 @@ goog.require('ol.layer.VectorLayerRenderIntent');
goog.require('ol.renderer.canvas.Layer');
goog.require('ol.renderer.canvas.Vector');
goog.require('ol.source.VectorEventType');
goog.require('ol.style');
goog.require('ol.tilegrid.TileGrid');
@@ -475,7 +476,7 @@ ol.renderer.canvas.VectorLayer.prototype.renderFrame =
var dirty = false;
var layer = this.getVectorLayer();
var source = layer.getSource();
var tileExtent, groups, group, j, numGroups, featuresObject, tileHasFeatures;
var tileExtent, featuresObject, tileHasFeatures;
fetchTileData:
for (x = tileRange.minX; x <= tileRange.maxX; ++x) {
for (y = tileRange.minY; y <= tileRange.maxY; ++y) {
@@ -509,10 +510,15 @@ ol.renderer.canvas.VectorLayer.prototype.renderFrame =
}
this.dirty_ = dirty;
groups = source.groupFeaturesBySymbolizerLiteral(layer.getStyle(),
var style = layer.getStyle();
if (goog.isNull(style)) {
style = ol.style.getDefault();
}
var groups = style.groupFeaturesBySymbolizerLiteral(
featuresToRender, tileResolution);
numGroups = groups.length;
for (j = 0; j < numGroups; ++j) {
var numGroups = groups.length;
var group;
for (var j = 0; j < numGroups; ++j) {
group = groups[j];
deferred = sketchCanvasRenderer.renderFeatures(group[0], group[1],
group[2]);

View File

@@ -13,9 +13,6 @@ goog.require('ol.extent');
goog.require('ol.proj');
goog.require('ol.source.Source');
goog.require('ol.structs.RTree');
goog.require('ol.style');
goog.require('ol.style.Style');
goog.require('ol.style.TextLiteral');
/**
@@ -150,66 +147,6 @@ ol.source.Vector.prototype.getFeaturesObjectForExtent = function(extent,
};
/**
* TODO: This should be a ol.style.Style method.
* @param {ol.style.Style} style Style.
* @param {Object.<string, ol.Feature>} features Features.
* @param {number} resolution Map resolution.
* @return {Array.<Array>} symbolizers for features. Each array in this array
* contains 3 items: an array of features, the symbolizer literal, and
* an array with optional additional data for each feature.
*/
ol.source.Vector.prototype.groupFeaturesBySymbolizerLiteral =
function(style, features, resolution) {
var uniqueLiterals = {},
featuresBySymbolizer = [],
i, j, l, feature, symbolizers, literals, numLiterals, literal,
uniqueLiteral, key, item;
for (i in features) {
feature = features[i];
// feature level symbolizers take precedence
symbolizers = feature.getSymbolizers();
if (!goog.isNull(symbolizers)) {
literals = ol.style.Style.createLiterals(symbolizers, feature);
} else {
// layer style second
if (goog.isNull(style)) {
style = ol.style.getDefault();
}
literals = style.createLiterals(feature, resolution);
}
numLiterals = literals.length;
for (j = 0; j < numLiterals; ++j) {
literal = literals[j];
for (l in uniqueLiterals) {
uniqueLiteral = featuresBySymbolizer[uniqueLiterals[l]][1];
if (literal.equals(uniqueLiteral)) {
literal = uniqueLiteral;
break;
}
}
key = goog.getUid(literal);
if (!goog.object.containsKey(uniqueLiterals, key)) {
uniqueLiterals[key] = featuresBySymbolizer.length;
featuresBySymbolizer.push([
/** @type {Array.<ol.Feature>} */ ([]),
/** @type {ol.style.Literal} */ (literal),
/** @type {Array} */ ([])
]);
}
item = featuresBySymbolizer[uniqueLiterals[key]];
item[0].push(feature);
if (literal instanceof ol.style.TextLiteral) {
item[2].push(literals[j].text);
}
}
}
// TODO: move sort function to ol.style.Style
featuresBySymbolizer.sort(this.sortByZIndex_);
return featuresBySymbolizer;
};
/**
* @param {Object|Element|Document|string} data Feature data.
* @param {ol.proj.Projection} projection This sucks. The layer should be a
@@ -365,18 +302,6 @@ ol.source.Vector.prototype.removeFeatures = function(features) {
};
/**
* Sort function for `groupFeaturesBySymbolizerLiteral`.
* @private
* @param {Array} a 1st item for the sort comparison.
* @param {Array} b 2nd item for the sort comparison.
* @return {number} Comparison result.
*/
ol.source.Vector.prototype.sortByZIndex_ = function(a, b) {
return a[1].zIndex - b[1].zIndex;
};
/**
* @constructor

View File

@@ -15,6 +15,7 @@ goog.require('ol.style.Rule');
goog.require('ol.style.Shape');
goog.require('ol.style.Stroke');
goog.require('ol.style.Symbolizer');
goog.require('ol.style.TextLiteral');
@@ -82,6 +83,71 @@ ol.style.Style.prototype.createLiterals = function(feature, resolution) {
};
/**
* @param {Object.<string, ol.Feature>} features Features.
* @param {number} resolution Map resolution.
* @return {Array.<Array>} symbolizers for features. Each array in this array
* contains 3 items: an array of features, the symbolizer literal, and
* an array with optional additional data for each feature.
*/
ol.style.Style.prototype.groupFeaturesBySymbolizerLiteral =
function(features, resolution) {
var uniqueLiterals = {},
featuresBySymbolizer = [],
i, j, l, feature, symbolizers, literals, numLiterals, literal,
uniqueLiteral, key, item;
for (i in features) {
feature = features[i];
// feature level symbolizers take precedence
symbolizers = feature.getSymbolizers();
if (!goog.isNull(symbolizers)) {
literals = ol.style.Style.createLiterals(symbolizers, feature);
} else {
literals = this.createLiterals(feature, resolution);
}
numLiterals = literals.length;
for (j = 0; j < numLiterals; ++j) {
literal = literals[j];
for (l in uniqueLiterals) {
uniqueLiteral = featuresBySymbolizer[uniqueLiterals[l]][1];
if (literal.equals(uniqueLiteral)) {
literal = uniqueLiteral;
break;
}
}
key = goog.getUid(literal);
if (!goog.object.containsKey(uniqueLiterals, key)) {
uniqueLiterals[key] = featuresBySymbolizer.length;
featuresBySymbolizer.push([
/** @type {Array.<ol.Feature>} */ ([]),
/** @type {ol.style.Literal} */ (literal),
/** @type {Array} */ ([])
]);
}
item = featuresBySymbolizer[uniqueLiterals[key]];
item[0].push(feature);
if (literal instanceof ol.style.TextLiteral) {
item[2].push(literals[j].text);
}
}
}
featuresBySymbolizer.sort(this.sortByZIndex_);
return featuresBySymbolizer;
};
/**
* Sort function for `groupFeaturesBySymbolizerLiteral`.
* @private
* @param {Array} a 1st item for the sort comparison.
* @param {Array} b 2nd item for the sort comparison.
* @return {number} Comparison result.
*/
ol.style.Style.prototype.sortByZIndex_ = function(a, b) {
return a[1].zIndex - b[1].zIndex;
};
/**
* The default style.
* @type {ol.style.Style}