Consider resolution when applying rules

This commit is contained in:
Tim Schaub
2013-09-27 11:46:48 +02:00
parent 78c54addb3
commit bbef15e50e
7 changed files with 131 additions and 24 deletions

View File

@@ -425,12 +425,13 @@ ol.layer.Vector.prototype.getPolygonVertices = function() {
/**
* @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.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
function(features) {
function(features, resolution) {
var uniqueLiterals = {},
featuresBySymbolizer = [],
style = this.style_,
@@ -447,7 +448,7 @@ ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
if (goog.isNull(style)) {
style = ol.style.getDefault();
}
literals = style.createLiterals(feature);
literals = style.createLiterals(feature, resolution);
}
numLiterals = literals.length;
for (j = 0; j < numLiterals; ++j) {

View File

@@ -509,7 +509,8 @@ ol.renderer.canvas.VectorLayer.prototype.renderFrame =
renderByGeometryType:
for (type in featuresToRender) {
groups = layer.groupFeaturesBySymbolizerLiteral(featuresToRender[type]);
groups = layer.groupFeaturesBySymbolizerLiteral(
featuresToRender[type], tileResolution);
numGroups = groups.length;
for (j = 0; j < numGroups; ++j) {
group = groups[j];

View File

@@ -58,11 +58,16 @@ ol.style.Rule = function(options) {
/**
* @param {ol.Feature} feature Feature.
* @param {number} resolution Map resolution.
* @return {boolean} Does the rule apply to the feature?
*/
ol.style.Rule.prototype.applies = function(feature) {
return goog.isNull(this.filter_) ?
true : !!ol.expr.evaluateFeature(this.filter_, feature);
ol.style.Rule.prototype.applies = function(feature, resolution) {
var applies = resolution >= this.minResolution_ &&
resolution < this.maxResolution_;
if (applies && !goog.isNull(this.filter_)) {
applies = !!ol.expr.evaluateFeature(this.filter_, feature);
}
return applies;
};

View File

@@ -45,17 +45,18 @@ ol.style.Style = function(options) {
/**
* Create an array of symbolizer literals for a feature.
* @param {ol.Feature} feature Feature.
* @param {number} resolution Map resolution.
* @return {Array.<ol.style.Literal>} Symbolizer literals for the
* feature.
*/
ol.style.Style.prototype.createLiterals = function(feature) {
ol.style.Style.prototype.createLiterals = function(feature, resolution) {
var rules = this.rules_,
symbolizers = [],
applies = false,
rule;
for (var i = 0, ii = rules.length; i < ii; ++i) {
rule = rules[i];
if (rule.applies(feature)) {
if (rule.applies(feature, resolution)) {
applies = true;
symbolizers.push.apply(symbolizers, rule.getSymbolizers());
}

View File

@@ -134,7 +134,7 @@ describe('ol.layer.Vector', function() {
})
];
var groups = layer.groupFeaturesBySymbolizerLiteral(features);
var groups = layer.groupFeaturesBySymbolizerLiteral(features, 1);
expect(groups.length).to.be(2);
expect(groups[0][0].length).to.be(1);
expect(groups[0][1].color).to.be('#BADA55');
@@ -164,7 +164,7 @@ describe('ol.layer.Vector', function() {
anotherFeatureWithSymbolizers.setSymbolizers([anotherSymbolizer]);
features.push(featureWithSymbolizers, anotherFeatureWithSymbolizers);
var groups = layer.groupFeaturesBySymbolizerLiteral(features);
var groups = layer.groupFeaturesBySymbolizerLiteral(features, 1);
expect(groups).to.have.length(3);
expect(groups[2][0].length).to.be(2);
expect(groups[2][1].width).to.be(3);

View File

@@ -33,22 +33,121 @@ describe('ol.style.Rule', function() {
it('returns true for a rule without filter', function() {
rule = new ol.style.Rule({});
expect(rule.applies(feature)).to.be(true);
expect(rule.applies(feature, 1)).to.be(true);
});
it('returns false when the rule does not apply', function() {
it('returns false when the filter evaluates to false', function() {
rule = new ol.style.Rule({
filter: new ol.expr.Literal(false)
});
expect(rule.applies(feature)).to.be(false);
expect(rule.applies(feature, 1)).to.be(false);
});
it('returns true when the rule applies', function() {
it('returns true when the filter evaluates to true', function() {
rule = new ol.style.Rule({
filter: new ol.expr.Literal(true)
});
expect(rule.applies(feature)).to.be(true);
expect(rule.applies(feature, 1)).to.be(true);
});
it('returns false when the resolution is less than min', function() {
rule = new ol.style.Rule({
minResolution: 10
});
expect(rule.applies(feature, 9)).to.be(false);
});
it('returns true when the resolution is greater than min', function() {
rule = new ol.style.Rule({
minResolution: 10
});
expect(rule.applies(feature, 11)).to.be(true);
});
it('returns true when the resolution is equal to min', function() {
rule = new ol.style.Rule({
minResolution: 10
});
expect(rule.applies(feature, 10)).to.be(true);
});
it('returns false if filter evaluates to false (with min res)', function() {
rule = new ol.style.Rule({
filter: new ol.expr.Literal(false),
minResolution: 10
});
expect(rule.applies(feature, 11)).to.be(false);
});
it('returns true if filter evaluates to true (with min res)', function() {
rule = new ol.style.Rule({
filter: new ol.expr.Literal(true),
minResolution: 10
});
expect(rule.applies(feature, 11)).to.be(true);
});
it('returns false when the resolution is greater than max', function() {
rule = new ol.style.Rule({
maxResolution: 100
});
expect(rule.applies(feature, 101)).to.be(false);
});
it('returns true when the resolution is less than max', function() {
rule = new ol.style.Rule({
maxResolution: 100
});
expect(rule.applies(feature, 99)).to.be(true);
});
it('returns false when the resolution is equal to max', function() {
rule = new ol.style.Rule({
maxResolution: 100
});
expect(rule.applies(feature, 100)).to.be(false);
});
it('returns false if filter evaluates to false (with max res)', function() {
rule = new ol.style.Rule({
filter: new ol.expr.Literal(false),
maxResolution: 100
});
expect(rule.applies(feature, 99)).to.be(false);
});
it('returns true if filter evaluates to true (with max res)', function() {
rule = new ol.style.Rule({
filter: new ol.expr.Literal(true),
maxResolution: 100
});
expect(rule.applies(feature, 99)).to.be(true);
});
it('returns true if resolution is between min and max', function() {
rule = new ol.style.Rule({
minResolution: 10,
maxResolution: 100
});
expect(rule.applies(feature, 55)).to.be(true);
});
it('returns false if resolution is greater than min and max', function() {
rule = new ol.style.Rule({
minResolution: 10,
maxResolution: 100
});
expect(rule.applies(feature, 1000)).to.be(false);
});
it('returns false if resolution is less than min and max', function() {
rule = new ol.style.Rule({
minResolution: 10,
maxResolution: 100
});
expect(rule.applies(feature, 5)).to.be(false);
});
});
});

View File

@@ -57,12 +57,12 @@ describe('ol.style.Style', function() {
});
feature.set('foo', 'bar');
var literals = style.createLiterals(feature);
var literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
expect(literals[0].fillColor).to.be('#BADA55');
feature.set('foo', 'baz');
expect(style.createLiterals(feature)).to.have.length(0);
expect(style.createLiterals(feature, 1)).to.have.length(0);
});
it('uses the "else" symbolizers when no rules are provided', function() {
@@ -78,7 +78,7 @@ describe('ol.style.Style', function() {
geometry: new ol.geom.LineString([[1, 2], [3, 4]])
});
var literals = style.createLiterals(feature);
var literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
expect(literals[0].color).to.be('#ff0000');
});
@@ -107,7 +107,7 @@ describe('ol.style.Style', function() {
geometry: new ol.geom.LineString([[1, 2], [3, 4]])
});
var literals = style.createLiterals(feature);
var literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
expect(literals[0].color).to.be('#00ff00');
@@ -115,7 +115,7 @@ describe('ol.style.Style', function() {
name: 'match',
geometry: new ol.geom.LineString([[1, 2], [3, 4]])
});
literals = style.createLiterals(feature);
literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
expect(literals[0].color).to.be('#ff00ff');
});
@@ -133,7 +133,7 @@ describe('ol.style.Style', function() {
it('returns an empty array for features without geometry', function() {
var feature = new ol.Feature();
expect(style.createLiterals(feature))
expect(style.createLiterals(feature, 1))
.to.have.length(0);
});
@@ -141,7 +141,7 @@ describe('ol.style.Style', function() {
var feature = new ol.Feature();
feature.setGeometry(new ol.geom.Point([0, 0]));
var literals = style.createLiterals(feature);
var literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
var literal = literals[0];
@@ -158,7 +158,7 @@ describe('ol.style.Style', function() {
var feature = new ol.Feature();
feature.setGeometry(new ol.geom.LineString([[0, 0], [1, 1]]));
var literals = style.createLiterals(feature);
var literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
var literal = literals[0];
@@ -172,7 +172,7 @@ describe('ol.style.Style', function() {
var feature = new ol.Feature();
feature.setGeometry(new ol.geom.Polygon([[[0, 0], [1, 1], [0, 0]]]));
var literals = style.createLiterals(feature);
var literals = style.createLiterals(feature, 1);
expect(literals).to.have.length(1);
var literal = literals[0];