Cast to numbers for literals
There are times when we parse from XML without a schema (e.g. KML). In these cases, features attributes will always be strings. We can cast to number when creating literals from symbolizers and then assert `!isNaN` instead of asserting that they are numbers before.
This commit is contained in:
@@ -64,9 +64,8 @@ ol.style.Fill.prototype.createLiteral = function(featureOrType) {
|
||||
goog.asserts.assertString(
|
||||
color, 'color must be a string');
|
||||
|
||||
var opacity = ol.expr.evaluateFeature(this.opacity_, feature);
|
||||
goog.asserts.assertNumber(
|
||||
opacity, 'color must be a number');
|
||||
var opacity = Number(ol.expr.evaluateFeature(this.opacity_, feature));
|
||||
goog.asserts.assert(!isNaN(opacity), 'opacity must be a number');
|
||||
|
||||
literal = new ol.style.PolygonLiteral({
|
||||
fillColor: color,
|
||||
|
||||
@@ -90,21 +90,21 @@ ol.style.Icon.prototype.createLiteral = function(featureOrType) {
|
||||
|
||||
var width;
|
||||
if (!goog.isNull(this.width_)) {
|
||||
width = ol.expr.evaluateFeature(this.width_, feature);
|
||||
goog.asserts.assertNumber(width, 'width must be a number');
|
||||
width = Number(ol.expr.evaluateFeature(this.width_, feature));
|
||||
goog.asserts.assert(!isNaN(width), 'width must be a number');
|
||||
}
|
||||
|
||||
var height;
|
||||
if (!goog.isNull(this.height_)) {
|
||||
height = ol.expr.evaluateFeature(this.height_, feature);
|
||||
height = Number(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 opacity = Number(ol.expr.evaluateFeature(this.opacity_, feature));
|
||||
goog.asserts.assert(!isNaN(opacity), 'opacity must be a number');
|
||||
|
||||
var rotation = ol.expr.evaluateFeature(this.rotation_, feature);
|
||||
goog.asserts.assertNumber(rotation, 'rotation must be a number');
|
||||
var rotation = Number(ol.expr.evaluateFeature(this.rotation_, feature));
|
||||
goog.asserts.assert(!isNaN(rotation), 'rotation must be a number');
|
||||
|
||||
literal = new ol.style.IconLiteral({
|
||||
url: url,
|
||||
|
||||
@@ -73,17 +73,17 @@ ol.style.Shape.prototype.createLiteral = function(featureOrType) {
|
||||
var literal = null;
|
||||
if (type === ol.geom.GeometryType.POINT ||
|
||||
type === ol.geom.GeometryType.MULTIPOINT) {
|
||||
var size = ol.expr.evaluateFeature(this.size_, feature);
|
||||
goog.asserts.assertNumber(size, 'size must be a number');
|
||||
var size = Number(ol.expr.evaluateFeature(this.size_, feature));
|
||||
goog.asserts.assert(!isNaN(size), 'size must be a number');
|
||||
|
||||
var fillColor, fillOpacity;
|
||||
if (!goog.isNull(this.fill_)) {
|
||||
fillColor = ol.expr.evaluateFeature(this.fill_.getColor(), feature);
|
||||
goog.asserts.assertString(
|
||||
fillColor, 'fillColor must be a string');
|
||||
fillOpacity = ol.expr.evaluateFeature(this.fill_.getOpacity(), feature);
|
||||
goog.asserts.assertNumber(
|
||||
fillOpacity, 'fillOpacity must be a number');
|
||||
fillOpacity = Number(ol.expr.evaluateFeature(
|
||||
this.fill_.getOpacity(), feature));
|
||||
goog.asserts.assert(!isNaN(fillOpacity), 'fillOpacity must be a number');
|
||||
}
|
||||
|
||||
var strokeColor, strokeOpacity, strokeWidth;
|
||||
@@ -91,13 +91,13 @@ ol.style.Shape.prototype.createLiteral = function(featureOrType) {
|
||||
strokeColor = ol.expr.evaluateFeature(this.stroke_.getColor(), feature);
|
||||
goog.asserts.assertString(
|
||||
strokeColor, 'strokeColor must be a string');
|
||||
strokeOpacity = ol.expr.evaluateFeature(this.stroke_.getOpacity(),
|
||||
feature);
|
||||
goog.asserts.assertNumber(
|
||||
strokeOpacity, 'strokeOpacity must be a number');
|
||||
strokeWidth = ol.expr.evaluateFeature(this.stroke_.getWidth(), feature);
|
||||
goog.asserts.assertNumber(
|
||||
strokeWidth, 'strokeWidth must be a number');
|
||||
strokeOpacity = Number(ol.expr.evaluateFeature(
|
||||
this.stroke_.getOpacity(), feature));
|
||||
goog.asserts.assert(!isNaN(strokeOpacity),
|
||||
'strokeOpacity must be a number');
|
||||
strokeWidth = Number(ol.expr.evaluateFeature(
|
||||
this.stroke_.getWidth(), feature));
|
||||
goog.asserts.assert(!isNaN(strokeWidth), 'strokeWidth must be a number');
|
||||
}
|
||||
|
||||
literal = new ol.style.ShapeLiteral({
|
||||
|
||||
@@ -71,13 +71,13 @@ ol.style.Stroke.prototype.createLiteral = function(featureOrType) {
|
||||
this.color_, feature);
|
||||
goog.asserts.assertString(color, 'color must be a string');
|
||||
|
||||
var opacity = ol.expr.evaluateFeature(
|
||||
this.opacity_, feature);
|
||||
goog.asserts.assertNumber(opacity, 'opacity must be a number');
|
||||
var opacity = Number(ol.expr.evaluateFeature(
|
||||
this.opacity_, feature));
|
||||
goog.asserts.assert(!isNaN(opacity), 'opacity must be a number');
|
||||
|
||||
var width = ol.expr.evaluateFeature(
|
||||
this.width_, feature);
|
||||
goog.asserts.assertNumber(width, 'width must be a number');
|
||||
var width = Number(ol.expr.evaluateFeature(
|
||||
this.width_, feature));
|
||||
goog.asserts.assert(!isNaN(width), 'width must be a number');
|
||||
|
||||
var literal = null;
|
||||
if (type === ol.geom.GeometryType.LINESTRING ||
|
||||
|
||||
@@ -84,14 +84,14 @@ ol.style.Text.prototype.createLiteral = function(featureOrType) {
|
||||
var fontFamily = ol.expr.evaluateFeature(this.fontFamily_, feature);
|
||||
goog.asserts.assertString(fontFamily, 'fontFamily must be a string');
|
||||
|
||||
var fontSize = ol.expr.evaluateFeature(this.fontSize_, feature);
|
||||
goog.asserts.assertNumber(fontSize, 'fontSize must be a number');
|
||||
var fontSize = Number(ol.expr.evaluateFeature(this.fontSize_, feature));
|
||||
goog.asserts.assert(!isNaN(fontSize), 'fontSize must be a number');
|
||||
|
||||
var text = ol.expr.evaluateFeature(this.text_, feature);
|
||||
goog.asserts.assertString(text, 'text must be a string');
|
||||
|
||||
var opacity = ol.expr.evaluateFeature(this.opacity_, feature);
|
||||
goog.asserts.assertNumber(opacity, 'opacity must be a number');
|
||||
var opacity = Number(ol.expr.evaluateFeature(this.opacity_, feature));
|
||||
goog.asserts.assert(!isNaN(opacity), 'opacity must be a number');
|
||||
|
||||
return new ol.style.TextLiteral({
|
||||
color: color,
|
||||
|
||||
@@ -64,6 +64,23 @@ describe('ol.style.Fill', function() {
|
||||
expect(literal.fillOpacity).to.be(0.8);
|
||||
});
|
||||
|
||||
it('casts opacity to number', function() {
|
||||
var symbolizer = new ol.style.Fill({
|
||||
opacity: ol.expr.parse('opacity'),
|
||||
color: '#ff00ff'
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
opacity: '0.55',
|
||||
geometry: new ol.geom.Polygon(
|
||||
[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.PolygonLiteral);
|
||||
expect(literal.fillOpacity).to.be(0.55);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#getColor()', function() {
|
||||
|
||||
@@ -75,19 +75,82 @@ describe('ol.style.Icon', function() {
|
||||
expect(literal.url).to.be('http://example.com/1.png');
|
||||
});
|
||||
|
||||
it('applies default type if none provided', function() {
|
||||
it('applies default opacity if none provided', function() {
|
||||
var symbolizer = new ol.style.Icon({
|
||||
height: ol.expr.parse('10'),
|
||||
width: ol.expr.parse('20'),
|
||||
url: ol.expr.parse('"http://example.com/1.png"')
|
||||
height: 10,
|
||||
width: 20,
|
||||
url: 'http://example.com/1.png'
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT);
|
||||
expect(literal).to.be.a(ol.style.IconLiteral);
|
||||
expect(literal.opacity).to.be(1);
|
||||
});
|
||||
|
||||
it('applies default rotation if none provided', function() {
|
||||
var symbolizer = new ol.style.Icon({
|
||||
height: 10,
|
||||
width: 20,
|
||||
url: 'http://example.com/1.png'
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT);
|
||||
expect(literal).to.be.a(ol.style.IconLiteral);
|
||||
expect(literal.rotation).to.be(0);
|
||||
});
|
||||
|
||||
it('casts opacity to number', function() {
|
||||
var symbolizer = new ol.style.Icon({
|
||||
opacity: ol.expr.parse('opacity'),
|
||||
height: 10,
|
||||
width: 20,
|
||||
url: 'http://example.com/1.png'
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
opacity: '0.53',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.IconLiteral);
|
||||
expect(literal.opacity).to.be(0.53);
|
||||
});
|
||||
|
||||
it('casts width to number', function() {
|
||||
var symbolizer = new ol.style.Icon({
|
||||
width: ol.expr.parse('width'),
|
||||
height: 10,
|
||||
url: 'http://example.com/1.png'
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
width: '42',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.IconLiteral);
|
||||
expect(literal.width).to.be(42);
|
||||
});
|
||||
|
||||
it('casts height to number', function() {
|
||||
var symbolizer = new ol.style.Icon({
|
||||
height: ol.expr.parse('height'),
|
||||
width: 10,
|
||||
url: 'http://example.com/1.png'
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
height: '42',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.IconLiteral);
|
||||
expect(literal.height).to.be(42);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#getHeight()', function() {
|
||||
|
||||
@@ -71,6 +71,95 @@ describe('ol.style.Shape', function() {
|
||||
expect(literal.strokeWidth).to.be(2);
|
||||
});
|
||||
|
||||
it('casts size to number', function() {
|
||||
var symbolizer = new ol.style.Shape({
|
||||
size: ol.expr.parse('size'),
|
||||
fill: new ol.style.Fill({
|
||||
color: '#BADA55'
|
||||
}),
|
||||
stroke: new ol.style.Stroke({
|
||||
color: '#013',
|
||||
opacity: 1,
|
||||
width: 2
|
||||
})
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
size: '42',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.ShapeLiteral);
|
||||
expect(literal.size).to.be(42);
|
||||
});
|
||||
|
||||
it('casts stroke width to number', function() {
|
||||
var symbolizer = new ol.style.Shape({
|
||||
fill: new ol.style.Fill({
|
||||
color: '#BADA55'
|
||||
}),
|
||||
stroke: new ol.style.Stroke({
|
||||
color: '#013',
|
||||
opacity: 1,
|
||||
width: ol.expr.parse('strokeWidth')
|
||||
})
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
strokeWidth: '4.2',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.ShapeLiteral);
|
||||
expect(literal.strokeWidth).to.be(4.2);
|
||||
});
|
||||
|
||||
it('casts stroke opacity to number', function() {
|
||||
var symbolizer = new ol.style.Shape({
|
||||
fill: new ol.style.Fill({
|
||||
color: '#BADA55'
|
||||
}),
|
||||
stroke: new ol.style.Stroke({
|
||||
color: '#013',
|
||||
opacity: ol.expr.parse('strokeOpacity'),
|
||||
width: 3
|
||||
})
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
strokeOpacity: '.2',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.ShapeLiteral);
|
||||
expect(literal.strokeOpacity).to.be(0.2);
|
||||
});
|
||||
|
||||
it('casts fill opacity to number', function() {
|
||||
var symbolizer = new ol.style.Shape({
|
||||
fill: new ol.style.Fill({
|
||||
opacity: ol.expr.parse('fillOpacity'),
|
||||
color: '#BADA55'
|
||||
}),
|
||||
stroke: new ol.style.Stroke({
|
||||
color: '#013',
|
||||
width: 3
|
||||
})
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
fillOpacity: '.42',
|
||||
geometry: new ol.geom.Point([1, 2])
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal).to.be.a(ol.style.ShapeLiteral);
|
||||
expect(literal.fillOpacity).to.be(0.42);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#getFill()', function() {
|
||||
|
||||
@@ -74,7 +74,7 @@ describe('ol.style.Text', function() {
|
||||
expect(literal.opacity).to.be(0.6);
|
||||
});
|
||||
|
||||
it('applies default type if none provided', function() {
|
||||
it('applies defaults if none provided', function() {
|
||||
var symbolizer = new ol.style.Text({
|
||||
text: 'Test'
|
||||
});
|
||||
@@ -88,6 +88,34 @@ describe('ol.style.Text', function() {
|
||||
expect(literal.opacity).to.be(1);
|
||||
});
|
||||
|
||||
it('casts size to number', function() {
|
||||
var symbolizer = new ol.style.Text({
|
||||
text: 'test',
|
||||
fontSize: ol.expr.parse('size')
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
size: '42'
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal.fontSize).to.be(42);
|
||||
});
|
||||
|
||||
it('casts opacity to number', function() {
|
||||
var symbolizer = new ol.style.Text({
|
||||
text: 'test',
|
||||
opacity: ol.expr.parse('opacity')
|
||||
});
|
||||
|
||||
var feature = new ol.Feature({
|
||||
opacity: '0.42'
|
||||
});
|
||||
|
||||
var literal = symbolizer.createLiteral(feature);
|
||||
expect(literal.opacity).to.be(0.42);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#getColor()', function() {
|
||||
|
||||
Reference in New Issue
Block a user