Create literal with feature or geometry type

This commit is contained in:
Tim Schaub
2013-08-15 08:08:10 -04:00
parent c36ceab2a0
commit d5d0262b4c
13 changed files with 156 additions and 116 deletions
+33 -38
View File
@@ -384,50 +384,45 @@ ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
var uniqueLiterals = {}, var uniqueLiterals = {},
featuresBySymbolizer = [], featuresBySymbolizer = [],
style = this.style_, style = this.style_,
i, j, l, feature, geom, type, symbolizers, literals, numLiterals, literal, i, j, l, feature, symbolizers, literals, numLiterals, literal,
uniqueLiteral, key, item; uniqueLiteral, key, item;
for (i in features) { for (i in features) {
feature = features[i]; feature = features[i];
geom = feature.getGeometry(); // feature level symbolizers take precedence
if (geom) { symbolizers = feature.getSymbolizers();
type = geom.getType(); if (!goog.isNull(symbolizers)) {
// feature level symbolizers take precedence literals = ol.style.Style.createLiterals(symbolizers, feature);
symbolizers = feature.getSymbolizers(); } else {
if (!goog.isNull(symbolizers)) { if (!goog.isNull(style)) {
literals = ol.style.Style.createLiterals( // layer style second
symbolizers, type, feature); literals = style.apply(feature);
} else { } else {
if (!goog.isNull(style)) { literals = ol.style.Style.applyDefaultStyle(feature);
// layer style second }
literals = style.apply(feature); }
} else { numLiterals = literals.length;
literals = ol.style.Style.applyDefaultStyle(feature); 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;
} }
} }
numLiterals = literals.length; key = goog.getUid(literal);
for (j = 0; j < numLiterals; ++j) { if (!goog.object.containsKey(uniqueLiterals, key)) {
literal = literals[j]; uniqueLiterals[key] = featuresBySymbolizer.length;
for (l in uniqueLiterals) { featuresBySymbolizer.push([
uniqueLiteral = featuresBySymbolizer[uniqueLiterals[l]][1]; /** @type {Array.<ol.Feature>} */ ([]),
if (literal.equals(uniqueLiteral)) { /** @type {ol.style.Literal} */ (literal),
literal = uniqueLiteral; /** @type {Array} */ ([])
break; ]);
} }
} item = featuresBySymbolizer[uniqueLiterals[key]];
key = goog.getUid(literal); item[0].push(feature);
if (!goog.object.containsKey(uniqueLiterals, key)) { if (literal instanceof ol.style.TextLiteral) {
uniqueLiterals[key] = featuresBySymbolizer.length; item[2].push(literals[j].text);
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);
}
} }
} }
} }
+11 -3
View File
@@ -45,17 +45,25 @@ goog.inherits(ol.style.Fill, ol.style.Symbolizer);
* @inheritDoc * @inheritDoc
* @return {ol.style.PolygonLiteral} Literal shape symbolizer. * @return {ol.style.PolygonLiteral} Literal shape symbolizer.
*/ */
ol.style.Fill.prototype.createLiteral = function(type, opt_feature) { ol.style.Fill.prototype.createLiteral = function(featureOrType) {
var feature, type;
if (featureOrType instanceof ol.Feature) {
feature = featureOrType;
var geometry = feature.getGeometry();
type = geometry ? geometry.getType() : null;
} else {
type = featureOrType;
}
var literal = null; var literal = null;
if (type === ol.geom.GeometryType.POLYGON || if (type === ol.geom.GeometryType.POLYGON ||
type === ol.geom.GeometryType.MULTIPOLYGON) { type === ol.geom.GeometryType.MULTIPOLYGON) {
var color = ol.expr.evaluateFeature(this.color_, opt_feature); var color = ol.expr.evaluateFeature(this.color_, feature);
goog.asserts.assertString( goog.asserts.assertString(
color, 'color must be a string'); color, 'color must be a string');
var opacity = ol.expr.evaluateFeature(this.opacity_, opt_feature); var opacity = ol.expr.evaluateFeature(this.opacity_, feature);
goog.asserts.assertNumber( goog.asserts.assertNumber(
opacity, 'color must be a number'); opacity, 'color must be a number');
+42 -27
View File
@@ -71,37 +71,52 @@ ol.style.Icon = function(options) {
* @inheritDoc * @inheritDoc
* @return {ol.style.IconLiteral} Literal shape symbolizer. * @return {ol.style.IconLiteral} Literal shape symbolizer.
*/ */
ol.style.Icon.prototype.createLiteral = function(type, opt_feature) { ol.style.Icon.prototype.createLiteral = function(featureOrType) {
var feature, type;
var url = ol.expr.evaluateFeature(this.url_, opt_feature); if (featureOrType instanceof ol.Feature) {
goog.asserts.assertString(url, 'url must be a string'); feature = featureOrType;
goog.asserts.assert(url != '#', 'url must not be "#"'); var geometry = feature.getGeometry();
type = geometry ? geometry.getType() : null;
var width; } else {
if (!goog.isNull(this.width_)) { type = featureOrType;
width = ol.expr.evaluateFeature(this.width_, opt_feature);
goog.asserts.assertNumber(width, 'width must be a number');
} }
var height; var literal = null;
if (!goog.isNull(this.height_)) { if (type === ol.geom.GeometryType.POINT ||
height = ol.expr.evaluateFeature(this.height_, opt_feature); type === ol.geom.GeometryType.MULTIPOINT) {
goog.asserts.assertNumber(height, 'height must be a number');
var url = ol.expr.evaluateFeature(this.url_, feature);
goog.asserts.assertString(url, 'url must be a string');
goog.asserts.assert(url != '#', 'url must not be "#"');
var width;
if (!goog.isNull(this.width_)) {
width = ol.expr.evaluateFeature(this.width_, feature);
goog.asserts.assertNumber(width, 'width must be a number');
}
var height;
if (!goog.isNull(this.height_)) {
height = ol.expr.evaluateFeature(this.height_, feature);
goog.asserts.assertNumber(height, 'height must be a number');
}
var opacity = ol.expr.evaluateFeature(this.opacity_, feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
var rotation = ol.expr.evaluateFeature(this.rotation_, feature);
goog.asserts.assertNumber(rotation, 'rotation must be a number');
literal = new ol.style.IconLiteral({
url: url,
width: width,
height: height,
opacity: opacity,
rotation: rotation
});
} }
var opacity = ol.expr.evaluateFeature(this.opacity_, opt_feature); return literal;
goog.asserts.assertNumber(opacity, 'opacity must be a number');
var rotation = ol.expr.evaluateFeature(this.rotation_, opt_feature);
goog.asserts.assertNumber(rotation, 'rotation must be a number');
return new ol.style.IconLiteral({
url: url,
width: width,
height: height,
opacity: opacity,
rotation: rotation
});
}; };
+16 -8
View File
@@ -60,34 +60,42 @@ ol.style.Shape = function(options) {
* @inheritDoc * @inheritDoc
* @return {ol.style.ShapeLiteral} Literal shape symbolizer. * @return {ol.style.ShapeLiteral} Literal shape symbolizer.
*/ */
ol.style.Shape.prototype.createLiteral = function(type, opt_feature) { ol.style.Shape.prototype.createLiteral = function(featureOrType) {
var literal = null; var feature, type;
if (featureOrType instanceof ol.Feature) {
feature = featureOrType;
var geometry = feature.getGeometry();
type = geometry ? geometry.getType() : null;
} else {
type = featureOrType;
}
var literal = null;
if (type === ol.geom.GeometryType.POINT || if (type === ol.geom.GeometryType.POINT ||
type === ol.geom.GeometryType.MULTIPOINT) { type === ol.geom.GeometryType.MULTIPOINT) {
var size = ol.expr.evaluateFeature(this.size_, opt_feature); var size = ol.expr.evaluateFeature(this.size_, feature);
goog.asserts.assertNumber(size, 'size must be a number'); goog.asserts.assertNumber(size, 'size must be a number');
var fillColor, fillOpacity; var fillColor, fillOpacity;
if (!goog.isNull(this.fill_)) { if (!goog.isNull(this.fill_)) {
fillColor = ol.expr.evaluateFeature(this.fill_.getColor(), opt_feature); fillColor = ol.expr.evaluateFeature(this.fill_.getColor(), feature);
goog.asserts.assertString( goog.asserts.assertString(
fillColor, 'fillColor must be a string'); fillColor, 'fillColor must be a string');
fillOpacity = ol.expr.evaluateFeature(this.fill_.getOpacity(), opt_feature); fillOpacity = ol.expr.evaluateFeature(this.fill_.getOpacity(), feature);
goog.asserts.assertNumber( goog.asserts.assertNumber(
fillOpacity, 'fillOpacity must be a number'); fillOpacity, 'fillOpacity must be a number');
} }
var strokeColor, strokeOpacity, strokeWidth; var strokeColor, strokeOpacity, strokeWidth;
if (!goog.isNull(this.stroke_)) { if (!goog.isNull(this.stroke_)) {
strokeColor = ol.expr.evaluateFeature(this.stroke_.getColor(), opt_feature); strokeColor = ol.expr.evaluateFeature(this.stroke_.getColor(), feature);
goog.asserts.assertString( goog.asserts.assertString(
strokeColor, 'strokeColor must be a string'); strokeColor, 'strokeColor must be a string');
strokeOpacity = ol.expr.evaluateFeature(this.stroke_.getOpacity(), strokeOpacity = ol.expr.evaluateFeature(this.stroke_.getOpacity(),
opt_feature); feature);
goog.asserts.assertNumber( goog.asserts.assertNumber(
strokeOpacity, 'strokeOpacity must be a number'); strokeOpacity, 'strokeOpacity must be a number');
strokeWidth = ol.expr.evaluateFeature(this.stroke_.getWidth(), opt_feature); strokeWidth = ol.expr.evaluateFeature(this.stroke_.getWidth(), feature);
goog.asserts.assertNumber( goog.asserts.assertNumber(
strokeWidth, 'strokeWidth must be a number'); strokeWidth, 'strokeWidth must be a number');
} }
+14 -4
View File
@@ -56,17 +56,26 @@ goog.inherits(ol.style.Stroke, ol.style.Symbolizer);
* @inheritDoc * @inheritDoc
* @return {ol.style.LineLiteral|ol.style.PolygonLiteral} Symbolizer literal. * @return {ol.style.LineLiteral|ol.style.PolygonLiteral} Symbolizer literal.
*/ */
ol.style.Stroke.prototype.createLiteral = function(type, opt_feature) { ol.style.Stroke.prototype.createLiteral = function(featureOrType) {
var feature, type;
if (featureOrType instanceof ol.Feature) {
feature = featureOrType;
var geometry = feature.getGeometry();
type = geometry ? geometry.getType() : null;
} else {
type = featureOrType;
}
var color = ol.expr.evaluateFeature( var color = ol.expr.evaluateFeature(
this.color_, opt_feature); this.color_, feature);
goog.asserts.assertString(color, 'color must be a string'); goog.asserts.assertString(color, 'color must be a string');
var opacity = ol.expr.evaluateFeature( var opacity = ol.expr.evaluateFeature(
this.opacity_, opt_feature); this.opacity_, feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number'); goog.asserts.assertNumber(opacity, 'opacity must be a number');
var width = ol.expr.evaluateFeature( var width = ol.expr.evaluateFeature(
this.width_, opt_feature); this.width_, feature);
goog.asserts.assertNumber(width, 'width must be a number'); goog.asserts.assertNumber(width, 'width must be a number');
var literal = null; var literal = null;
@@ -85,6 +94,7 @@ ol.style.Stroke.prototype.createLiteral = function(type, opt_feature) {
strokeWidth: width strokeWidth: width
}); });
} }
return literal; return literal;
}; };
+11 -16
View File
@@ -35,22 +35,17 @@ ol.style.Style = function(options) {
ol.style.Style.prototype.apply = function(feature) { ol.style.Style.prototype.apply = function(feature) {
var rules = this.rules_, var rules = this.rules_,
literals = [], literals = [],
geometry = feature.getGeometry(),
rule, symbolizers; rule, symbolizers;
var type = geometry ? geometry.getType() : null; for (var i = 0, ii = rules.length; i < ii; ++i) {
if (!goog.isNull(type)) { rule = rules[i];
for (var i = 0, ii = rules.length; i < ii; ++i) { if (rule.applies(feature)) {
rule = rules[i]; symbolizers = rule.getSymbolizers();
if (rule.applies(feature)) { for (var j = 0, jj = symbolizers.length; j < jj; ++j) {
symbolizers = rule.getSymbolizers(); literals.push(symbolizers[j].createLiteral(feature));
for (var j = 0, jj = symbolizers.length; j < jj; ++j) {
literals.push(symbolizers[j].createLiteral(type, feature));
}
} }
} }
literals = ol.style.Style.reduceLiterals_(literals);
} }
return literals; return ol.style.Style.reduceLiterals_(literals);
}; };
@@ -87,15 +82,15 @@ ol.style.Style.defaults = new ol.style.Style({
/** /**
* Given an array of symbolizers, generate an array of literals. * Given an array of symbolizers, generate an array of literals.
* @param {Array.<ol.style.Symbolizer>} symbolizers List of symbolizers. * @param {Array.<ol.style.Symbolizer>} symbolizers List of symbolizers.
* @param {ol.geom.GeometryType} type Geometry type. * @param {ol.Feature|ol.geom.GeometryType} featureOrType Feature or geometry
* @param {ol.Feature=} opt_feature Optional feature. * type.
* @return {Array.<ol.style.Literal>} Array of literals. * @return {Array.<ol.style.Literal>} Array of literals.
*/ */
ol.style.Style.createLiterals = function(symbolizers, type, opt_feature) { ol.style.Style.createLiterals = function(symbolizers, featureOrType) {
var length = symbolizers.length; var length = symbolizers.length;
var literals = new Array(length); var literals = new Array(length);
for (var i = 0; i < length; ++i) { for (var i = 0; i < length; ++i) {
literals[i] = symbolizers[i].createLiteral(type, opt_feature); literals[i] = symbolizers[i].createLiteral(featureOrType);
} }
return ol.style.Style.reduceLiterals_(literals); return ol.style.Style.reduceLiterals_(literals);
} }
+4 -2
View File
@@ -12,8 +12,10 @@ ol.style.Symbolizer = function() {};
/** /**
* @param {ol.geom.GeometryType} type Geometry type. * Create a literal from the symbolizer given a complete feature or a geometry
* @param {ol.Feature=} opt_feature Feature for evaluating expressions. * type.
* @param {ol.geom.GeometryType|ol.Feature} featureOrType Feature for evaluating
* expressions or a geometry type.
* @return {ol.style.Literal} Literal symbolizer. * @return {ol.style.Literal} Literal symbolizer.
*/ */
ol.style.Symbolizer.prototype.createLiteral = goog.abstractMethod; ol.style.Symbolizer.prototype.createLiteral = goog.abstractMethod;
+14 -6
View File
@@ -67,21 +67,29 @@ goog.inherits(ol.style.Text, ol.style.Symbolizer);
* @inheritDoc * @inheritDoc
* @return {ol.style.TextLiteral} Literal text symbolizer. * @return {ol.style.TextLiteral} Literal text symbolizer.
*/ */
ol.style.Text.prototype.createLiteral = function(type, opt_feature) { ol.style.Text.prototype.createLiteral = function(featureOrType) {
var feature, type;
if (featureOrType instanceof ol.Feature) {
feature = featureOrType;
var geometry = feature.getGeometry();
type = geometry ? geometry.getType() : null;
} else {
type = featureOrType;
}
var color = ol.expr.evaluateFeature(this.color_, opt_feature); var color = ol.expr.evaluateFeature(this.color_, feature);
goog.asserts.assertString(color, 'color must be a string'); goog.asserts.assertString(color, 'color must be a string');
var fontFamily = ol.expr.evaluateFeature(this.fontFamily_, opt_feature); var fontFamily = ol.expr.evaluateFeature(this.fontFamily_, feature);
goog.asserts.assertString(fontFamily, 'fontFamily must be a string'); goog.asserts.assertString(fontFamily, 'fontFamily must be a string');
var fontSize = ol.expr.evaluateFeature(this.fontSize_, opt_feature); var fontSize = ol.expr.evaluateFeature(this.fontSize_, feature);
goog.asserts.assertNumber(fontSize, 'fontSize must be a number'); goog.asserts.assertNumber(fontSize, 'fontSize must be a number');
var text = ol.expr.evaluateFeature(this.text_, opt_feature); var text = ol.expr.evaluateFeature(this.text_, feature);
goog.asserts.assertString(text, 'text must be a string'); goog.asserts.assertString(text, 'text must be a string');
var opacity = ol.expr.evaluateFeature(this.opacity_, opt_feature); var opacity = ol.expr.evaluateFeature(this.opacity_, feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number'); goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.TextLiteral({ return new ol.style.TextLiteral({
+5 -3
View File
@@ -31,11 +31,12 @@ describe('ol.style.Fill', function() {
var feature = new ol.Feature({ var feature = new ol.Feature({
value: 42, value: 42,
fillAttr: '#ff0000' fillAttr: '#ff0000',
geometry: new ol.geom.Polygon(
[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]])
}); });
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POLYGON, var literal = symbolizer.createLiteral(feature);
feature);
expect(literal).to.be.a(ol.style.PolygonLiteral); expect(literal).to.be.a(ol.style.PolygonLiteral);
expect(literal.fillOpacity).to.be(42 / 100); expect(literal.fillOpacity).to.be(42 / 100);
expect(literal.fillColor).to.be('#ff0000'); expect(literal.fillColor).to.be('#ff0000');
@@ -158,5 +159,6 @@ goog.require('ol.Feature');
goog.require('ol.expr'); goog.require('ol.expr');
goog.require('ol.expr.Literal'); goog.require('ol.expr.Literal');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Polygon');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
goog.require('ol.style.PolygonLiteral'); goog.require('ol.style.PolygonLiteral');
+1 -2
View File
@@ -48,8 +48,7 @@ describe('ol.style.Icon', function() {
geometry: new ol.geom.Point([1, 2]) geometry: new ol.geom.Point([1, 2])
}); });
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT, var literal = symbolizer.createLiteral(feature);
feature);
expect(literal).to.be.a(ol.style.IconLiteral); expect(literal).to.be.a(ol.style.IconLiteral);
expect(literal.height).to.be(42); expect(literal.height).to.be(42);
expect(literal.width).to.be(.42); expect(literal.width).to.be(.42);
+1 -2
View File
@@ -43,8 +43,7 @@ describe('ol.style.Shape', function() {
geometry: new ol.geom.Point([1, 2]) geometry: new ol.geom.Point([1, 2])
}); });
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT, var literal = symbolizer.createLiteral(feature);
feature);
expect(literal).to.be.a(ol.style.ShapeLiteral); expect(literal).to.be.a(ol.style.ShapeLiteral);
expect(literal.size).to.be(42); expect(literal.size).to.be(42);
expect(literal.fillOpacity).to.be(0.4); expect(literal.fillOpacity).to.be(0.4);
+3 -3
View File
@@ -32,11 +32,11 @@ describe('ol.style.Stroke', function() {
var feature = new ol.Feature({ var feature = new ol.Feature({
value: 42, value: 42,
widthAttr: 1.5 widthAttr: 1.5,
geometry: new ol.geom.LineString([[1, 2], [3, 4]])
}); });
var literal = symbolizer.createLiteral(ol.geom.GeometryType.LINESTRING, var literal = symbolizer.createLiteral(feature);
feature);
expect(literal).to.be.a(ol.style.LineLiteral); expect(literal).to.be.a(ol.style.LineLiteral);
expect(literal.strokeOpacity).to.be(42 / 100); expect(literal.strokeOpacity).to.be(42 / 100);
expect(literal.strokeWidth).to.be(1.5); expect(literal.strokeWidth).to.be(1.5);
+1 -2
View File
@@ -47,8 +47,7 @@ describe('ol.style.Text', function() {
opacityAttr: 0.4 opacityAttr: 0.4
}); });
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT, var literal = symbolizer.createLiteral(feature);
feature);
expect(literal).to.be.a(ol.style.TextLiteral); expect(literal).to.be.a(ol.style.TextLiteral);
expect(literal.color).to.be('#ff0000'); expect(literal.color).to.be('#ff0000');
expect(literal.fontFamily).to.be('Dingbats'); expect(literal.fontFamily).to.be('Dingbats');