Separate stroke and fill

This commit is contained in:
Tim Schaub
2013-08-14 17:44:22 -04:00
parent dc54128c77
commit c36ceab2a0
30 changed files with 1157 additions and 1568 deletions

View File

@@ -0,0 +1,117 @@
goog.provide('ol.style.Fill');
goog.require('goog.asserts');
goog.require('ol.geom.GeometryType');
goog.require('ol.expr');
goog.require('ol.expr.Expression');
goog.require('ol.expr.Literal');
goog.require('ol.style.PolygonLiteral');
goog.require('ol.style.Symbolizer');
/**
* @constructor
* @extends {ol.style.Symbolizer}
* @param {ol.style.FillOptions=} opt_options Polygon options.
*/
ol.style.Fill = function(opt_options) {
goog.base(this);
var options = opt_options || {};
/**
* @type {ol.expr.Expression}
* @private
*/
this.color_ = !goog.isDefAndNotNull(options.color) ?
new ol.expr.Literal(ol.style.FillDefaults.color) :
(options.color instanceof ol.expr.Expression) ?
options.color : new ol.expr.Literal(options.color);
/**
* @type {ol.expr.Expression}
* @private
*/
this.opacity_ = !goog.isDefAndNotNull(options.opacity) ?
new ol.expr.Literal(ol.style.FillDefaults.opacity) :
(options.opacity instanceof ol.expr.Expression) ?
options.opacity : new ol.expr.Literal(options.opacity);
};
goog.inherits(ol.style.Fill, ol.style.Symbolizer);
/**
* @inheritDoc
* @return {ol.style.PolygonLiteral} Literal shape symbolizer.
*/
ol.style.Fill.prototype.createLiteral = function(type, opt_feature) {
var literal = null;
if (type === ol.geom.GeometryType.POLYGON ||
type === ol.geom.GeometryType.MULTIPOLYGON) {
var color = ol.expr.evaluateFeature(this.color_, opt_feature);
goog.asserts.assertString(
color, 'color must be a string');
var opacity = ol.expr.evaluateFeature(this.opacity_, opt_feature);
goog.asserts.assertNumber(
opacity, 'color must be a number');
literal = new ol.style.PolygonLiteral({
fillColor: color,
fillOpacity: opacity
});
}
return literal;
};
/**
* Get the fill color.
* @return {ol.expr.Expression} Fill color.
*/
ol.style.Fill.prototype.getColor = function() {
return this.color_;
};
/**
* Get the fill opacity.
* @return {ol.expr.Expression} Fill opacity.
*/
ol.style.Fill.prototype.getOpacity = function() {
return this.opacity_;
};
/**
* Set the fill color.
* @param {ol.expr.Expression} color Fill color.
*/
ol.style.Fill.prototype.setColor = function(color) {
goog.asserts.assertInstanceof(color, ol.expr.Expression);
this.color_ = color;
};
/**
* Set the fill opacity.
* @param {ol.expr.Expression} opacity Fill opacity.
*/
ol.style.Fill.prototype.setOpacity = function(opacity) {
goog.asserts.assertInstanceof(opacity, ol.expr.Expression);
this.opacity_ = opacity;
};
/**
* @typedef {{fillColor: (string),
* fillOpacity: (number)}}
*/
ol.style.FillDefaults = {
color: '#ffffff',
opacity: 0.4
};

View File

@@ -1,6 +1,7 @@
goog.provide('ol.style.Icon');
goog.require('goog.asserts');
goog.require('ol.geom.GeometryType');
goog.require('ol.expr');
goog.require('ol.expr.Expression');
goog.require('ol.expr.Literal');
@@ -70,7 +71,7 @@ ol.style.Icon = function(options) {
* @inheritDoc
* @return {ol.style.IconLiteral} Literal shape symbolizer.
*/
ol.style.Icon.prototype.createLiteral = function(opt_feature) {
ol.style.Icon.prototype.createLiteral = function(type, opt_feature) {
var url = ol.expr.evaluateFeature(this.url_, opt_feature);
goog.asserts.assertString(url, 'url must be a string');
@@ -200,10 +201,10 @@ ol.style.Icon.prototype.setWidth = function(width) {
/**
* @type {ol.style.IconLiteral}
* @typedef {{opacity: (number),
* rotation: (number)}}
*/
ol.style.IconDefaults = new ol.style.IconLiteral({
url: '#',
ol.style.IconDefaults = {
opacity: 1,
rotation: 0
});
};

View File

@@ -1,143 +0,0 @@
goog.provide('ol.style.Line');
goog.require('goog.asserts');
goog.require('ol.expr');
goog.require('ol.expr.Expression');
goog.require('ol.expr.Literal');
goog.require('ol.style.LineLiteral');
goog.require('ol.style.Symbolizer');
goog.require('ol.style.Literal');
/**
* @constructor
* @extends {ol.style.Symbolizer}
* @param {ol.style.LineOptions} options Line options.
*/
ol.style.Line = function(options) {
goog.base(this);
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeColor_ = !goog.isDef(options.strokeColor) ?
new ol.expr.Literal(ol.style.LineDefaults.strokeColor) :
(options.strokeColor instanceof ol.expr.Expression) ?
options.strokeColor : new ol.expr.Literal(options.strokeColor);
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeOpacity_ = !goog.isDef(options.strokeOpacity) ?
new ol.expr.Literal(ol.style.LineDefaults.strokeOpacity) :
(options.strokeOpacity instanceof ol.expr.Expression) ?
options.strokeOpacity : new ol.expr.Literal(options.strokeOpacity);
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeWidth_ = !goog.isDef(options.strokeWidth) ?
new ol.expr.Literal(ol.style.LineDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.expr.Expression) ?
options.strokeWidth : new ol.expr.Literal(options.strokeWidth);
};
goog.inherits(ol.style.Line, ol.style.Symbolizer);
/**
* @inheritDoc
* @return {ol.style.LineLiteral} Literal line symbolizer.
*/
ol.style.Line.prototype.createLiteral = function(opt_feature) {
var strokeColor = ol.expr.evaluateFeature(
this.strokeColor_, opt_feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
var strokeOpacity = ol.expr.evaluateFeature(
this.strokeOpacity_, opt_feature);
goog.asserts.assertNumber(strokeOpacity, 'strokeOpacity must be a number');
var strokeWidth = ol.expr.evaluateFeature(
this.strokeWidth_, opt_feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
return new ol.style.LineLiteral({
strokeColor: strokeColor,
strokeOpacity: strokeOpacity,
strokeWidth: strokeWidth
});
};
/**
* Get the stroke color.
* @return {ol.expr.Expression} Stroke color.
*/
ol.style.Line.prototype.getStrokeColor = function() {
return this.strokeColor_;
};
/**
* Get the stroke opacity.
* @return {ol.expr.Expression} Stroke opacity.
*/
ol.style.Line.prototype.getStrokeOpacity = function() {
return this.strokeOpacity_;
};
/**
* Get the stroke width.
* @return {ol.expr.Expression} Stroke width.
*/
ol.style.Line.prototype.getStrokeWidth = function() {
return this.strokeWidth_;
};
/**
* Set the stroke color.
* @param {ol.expr.Expression} strokeColor Stroke color.
*/
ol.style.Line.prototype.setStrokeColor = function(strokeColor) {
goog.asserts.assertInstanceof(strokeColor, ol.expr.Expression);
this.strokeColor_ = strokeColor;
};
/**
* Set the stroke opacity.
* @param {ol.expr.Expression} strokeOpacity Stroke opacity.
*/
ol.style.Line.prototype.setStrokeOpacity = function(strokeOpacity) {
goog.asserts.assertInstanceof(strokeOpacity, ol.expr.Expression);
this.strokeOpacity_ = strokeOpacity;
};
/**
* Set the stroke width.
* @param {ol.expr.Expression} strokeWidth Stroke width.
*/
ol.style.Line.prototype.setStrokeWidth = function(strokeWidth) {
goog.asserts.assertInstanceof(strokeWidth, ol.expr.Expression);
this.strokeWidth_ = strokeWidth;
};
/**
* @type {ol.style.LineLiteral}
*/
ol.style.LineDefaults = new ol.style.LineLiteral({
strokeColor: '#696969',
strokeOpacity: 0.75,
strokeWidth: 1.5
});

View File

@@ -1,286 +0,0 @@
goog.provide('ol.style.Polygon');
goog.require('goog.asserts');
goog.require('ol.expr');
goog.require('ol.expr.Expression');
goog.require('ol.expr.Literal');
goog.require('ol.style.PolygonLiteral');
goog.require('ol.style.Symbolizer');
/**
* @constructor
* @extends {ol.style.Symbolizer}
* @param {ol.style.PolygonOptions} options Polygon options.
*/
ol.style.Polygon = function(options) {
goog.base(this);
// fill handling - if any fill property is supplied, use all defaults
var fillColor = null,
fillOpacity = null;
if (goog.isDefAndNotNull(options.fillColor) ||
goog.isDefAndNotNull(options.fillOpacity)) {
if (goog.isDefAndNotNull(options.fillColor)) {
fillColor = (options.fillColor instanceof ol.expr.Expression) ?
options.fillColor :
new ol.expr.Literal(options.fillColor);
} else {
fillColor = new ol.expr.Literal(
/** @type {string} */ (ol.style.PolygonDefaults.fillColor));
}
if (goog.isDefAndNotNull(options.fillOpacity)) {
fillOpacity = (options.fillOpacity instanceof ol.expr.Expression) ?
options.fillOpacity :
new ol.expr.Literal(options.fillOpacity);
} else {
fillOpacity = new ol.expr.Literal(
/** @type {number} */ (ol.style.PolygonDefaults.fillOpacity));
}
}
/**
* @type {ol.expr.Expression}
* @private
*/
this.fillColor_ = fillColor;
/**
* @type {ol.expr.Expression}
* @private
*/
this.fillOpacity_ = fillOpacity;
// stroke handling - if any stroke property is supplied, use defaults
var strokeColor = null,
strokeOpacity = null,
strokeWidth = null;
if (goog.isDefAndNotNull(options.strokeColor) ||
goog.isDefAndNotNull(options.strokeOpacity) ||
goog.isDefAndNotNull(options.strokeWidth)) {
if (goog.isDefAndNotNull(options.strokeColor)) {
strokeColor = (options.strokeColor instanceof ol.expr.Expression) ?
options.strokeColor :
new ol.expr.Literal(options.strokeColor);
} else {
strokeColor = new ol.expr.Literal(
/** @type {string} */ (ol.style.PolygonDefaults.strokeColor));
}
if (goog.isDefAndNotNull(options.strokeOpacity)) {
strokeOpacity = (options.strokeOpacity instanceof ol.expr.Expression) ?
options.strokeOpacity :
new ol.expr.Literal(options.strokeOpacity);
} else {
strokeOpacity = new ol.expr.Literal(
/** @type {number} */ (ol.style.PolygonDefaults.strokeOpacity));
}
if (goog.isDefAndNotNull(options.strokeWidth)) {
strokeWidth = (options.strokeWidth instanceof ol.expr.Expression) ?
options.strokeWidth :
new ol.expr.Literal(options.strokeWidth);
} else {
strokeWidth = new ol.expr.Literal(
/** @type {number} */ (ol.style.PolygonDefaults.strokeWidth));
}
}
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeColor_ = strokeColor;
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeOpacity_ = strokeOpacity;
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeWidth_ = strokeWidth;
// one of stroke or fill can be null, both null is user error
var fill = !goog.isNull(this.fillColor_) && !goog.isNull(this.fillOpacity_);
var stroke = !goog.isNull(this.strokeColor_) &&
!goog.isNull(this.strokeOpacity_) &&
!goog.isNull(this.strokeWidth_);
goog.asserts.assert(fill || stroke,
'Stroke or fill properties must be provided');
};
goog.inherits(ol.style.Polygon, ol.style.Symbolizer);
/**
* @inheritDoc
* @return {ol.style.PolygonLiteral} Literal shape symbolizer.
*/
ol.style.Polygon.prototype.createLiteral = function(opt_feature) {
var fillColor;
if (!goog.isNull(this.fillColor_)) {
fillColor = ol.expr.evaluateFeature(this.fillColor_, opt_feature);
goog.asserts.assertString(fillColor, 'fillColor must be a string');
}
var fillOpacity;
if (!goog.isNull(this.fillOpacity_)) {
fillOpacity = ol.expr.evaluateFeature(this.fillOpacity_, opt_feature);
goog.asserts.assertNumber(fillOpacity, 'fillOpacity must be a number');
}
var strokeColor;
if (!goog.isNull(this.strokeColor_)) {
strokeColor = ol.expr.evaluateFeature(this.strokeColor_, opt_feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
}
var strokeOpacity;
if (!goog.isNull(this.strokeOpacity_)) {
strokeOpacity = ol.expr.evaluateFeature(this.strokeOpacity_, opt_feature);
goog.asserts.assertNumber(strokeOpacity, 'strokeOpacity must be a number');
}
var strokeWidth;
if (!goog.isNull(this.strokeWidth_)) {
strokeWidth = ol.expr.evaluateFeature(this.strokeWidth_, opt_feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
}
var fill = goog.isDef(fillColor) && goog.isDef(fillOpacity);
var stroke = goog.isDef(strokeColor) && goog.isDef(strokeOpacity) &&
goog.isDef(strokeWidth);
goog.asserts.assert(fill || stroke,
'either fill or stroke properties must be defined');
return new ol.style.PolygonLiteral({
fillColor: fillColor,
fillOpacity: fillOpacity,
strokeColor: strokeColor,
strokeOpacity: strokeOpacity,
strokeWidth: strokeWidth
});
};
/**
* Get the fill color.
* @return {ol.expr.Expression} Fill color.
*/
ol.style.Polygon.prototype.getFillColor = function() {
return this.fillColor_;
};
/**
* Get the fill opacity.
* @return {ol.expr.Expression} Fill opacity.
*/
ol.style.Polygon.prototype.getFillOpacity = function() {
return this.fillOpacity_;
};
/**
* Get the stroke color.
* @return {ol.expr.Expression} Stroke color.
*/
ol.style.Polygon.prototype.getStrokeColor = function() {
return this.strokeColor_;
};
/**
* Get the stroke opacity.
* @return {ol.expr.Expression} Stroke opacity.
*/
ol.style.Polygon.prototype.getStrokeOpacity = function() {
return this.strokeOpacity_;
};
/**
* Get the stroke width.
* @return {ol.expr.Expression} Stroke width.
*/
ol.style.Polygon.prototype.getStrokeWidth = function() {
return this.strokeWidth_;
};
/**
* Set the fill color.
* @param {ol.expr.Expression} fillColor Fill color.
*/
ol.style.Polygon.prototype.setFillColor = function(fillColor) {
goog.asserts.assertInstanceof(fillColor, ol.expr.Expression);
this.fillColor_ = fillColor;
};
/**
* Set the fill opacity.
* @param {ol.expr.Expression} fillOpacity Fill opacity.
*/
ol.style.Polygon.prototype.setFillOpacity = function(fillOpacity) {
goog.asserts.assertInstanceof(fillOpacity, ol.expr.Expression);
this.fillOpacity_ = fillOpacity;
};
/**
* Set the stroke color.
* @param {ol.expr.Expression} strokeColor Stroke color.
*/
ol.style.Polygon.prototype.setStrokeColor = function(strokeColor) {
goog.asserts.assertInstanceof(strokeColor, ol.expr.Expression);
this.strokeColor_ = strokeColor;
};
/**
* Set the stroke opacity.
* @param {ol.expr.Expression} strokeOpacity Stroke opacity.
*/
ol.style.Polygon.prototype.setStrokeOpacity = function(strokeOpacity) {
goog.asserts.assertInstanceof(strokeOpacity, ol.expr.Expression);
this.strokeOpacity_ = strokeOpacity;
};
/**
* Set the stroke width.
* @param {ol.expr.Expression} strokeWidth Stroke width.
*/
ol.style.Polygon.prototype.setStrokeWidth = function(strokeWidth) {
goog.asserts.assertInstanceof(strokeWidth, ol.expr.Expression);
this.strokeWidth_ = strokeWidth;
};
/**
* @type {ol.style.PolygonLiteral}
*/
ol.style.PolygonDefaults = new ol.style.PolygonLiteral({
fillColor: '#ffffff',
fillOpacity: 0.4,
strokeColor: '#696969',
strokeOpacity: 0.8,
strokeWidth: 1.5
});

View File

@@ -1,13 +1,16 @@
goog.provide('ol.style.Shape');
goog.require('goog.asserts');
goog.require('ol.geom.GeometryType');
goog.require('ol.expr');
goog.require('ol.expr.Expression');
goog.require('ol.expr.Literal');
goog.require('ol.style.Fill');
goog.require('ol.style.Point');
goog.require('ol.style.PointLiteral');
goog.require('ol.style.ShapeLiteral');
goog.require('ol.style.ShapeType');
goog.require('ol.style.Stroke');
@@ -34,109 +37,21 @@ ol.style.Shape = function(options) {
(options.size instanceof ol.expr.Expression) ?
options.size : new ol.expr.Literal(options.size);
// fill handling - if any fill property is supplied, use all defaults
var fillColor = null,
fillOpacity = null;
if (goog.isDefAndNotNull(options.fillColor) ||
goog.isDefAndNotNull(options.fillOpacity)) {
if (goog.isDefAndNotNull(options.fillColor)) {
fillColor = (options.fillColor instanceof ol.expr.Expression) ?
options.fillColor :
new ol.expr.Literal(options.fillColor);
} else {
fillColor = new ol.expr.Literal(
/** @type {string} */ (ol.style.ShapeDefaults.fillColor));
}
if (goog.isDefAndNotNull(options.fillOpacity)) {
fillOpacity = (options.fillOpacity instanceof ol.expr.Expression) ?
options.fillOpacity :
new ol.expr.Literal(options.fillOpacity);
} else {
fillOpacity = new ol.expr.Literal(
/** @type {number} */ (ol.style.ShapeDefaults.fillOpacity));
}
}
/**
* @type {ol.expr.Expression}
* @type {ol.style.Fill}
* @private
*/
this.fillColor_ = fillColor;
this.fill_ = goog.isDefAndNotNull(options.fill) ? options.fill : null;
/**
* @type {ol.expr.Expression}
* @type {ol.style.Stroke}
* @private
*/
this.fillOpacity_ = fillOpacity;
// stroke handling - if any stroke property is supplied, use defaults
var strokeColor = null,
strokeOpacity = null,
strokeWidth = null;
if (goog.isDefAndNotNull(options.strokeColor) ||
goog.isDefAndNotNull(options.strokeOpacity) ||
goog.isDefAndNotNull(options.strokeWidth)) {
if (goog.isDefAndNotNull(options.strokeColor)) {
strokeColor = (options.strokeColor instanceof ol.expr.Expression) ?
options.strokeColor :
new ol.expr.Literal(options.strokeColor);
} else {
strokeColor = new ol.expr.Literal(
/** @type {string} */ (ol.style.ShapeDefaults.strokeColor));
}
if (goog.isDefAndNotNull(options.strokeOpacity)) {
strokeOpacity = (options.strokeOpacity instanceof ol.expr.Expression) ?
options.strokeOpacity :
new ol.expr.Literal(options.strokeOpacity);
} else {
strokeOpacity = new ol.expr.Literal(
/** @type {number} */ (ol.style.ShapeDefaults.strokeOpacity));
}
if (goog.isDefAndNotNull(options.strokeWidth)) {
strokeWidth = (options.strokeWidth instanceof ol.expr.Expression) ?
options.strokeWidth :
new ol.expr.Literal(options.strokeWidth);
} else {
strokeWidth = new ol.expr.Literal(
/** @type {number} */ (ol.style.ShapeDefaults.strokeWidth));
}
}
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeColor_ = strokeColor;
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeOpacity_ = strokeOpacity;
/**
* @type {ol.expr.Expression}
* @private
*/
this.strokeWidth_ = strokeWidth;
this.stroke_ = goog.isDefAndNotNull(options.stroke) ? options.stroke : null;
// one of stroke or fill can be null, both null is user error
var fill = !goog.isNull(this.fillColor_) && !goog.isNull(this.fillOpacity_);
var stroke = !goog.isNull(this.strokeColor_) &&
!goog.isNull(this.strokeOpacity_) &&
!goog.isNull(this.strokeWidth_);
goog.asserts.assert(fill || stroke,
'Stroke or fill properties must be provided');
goog.asserts.assert(this.fill_ || this.stroke_,
'Stroke or fill must be provided');
};
@@ -145,75 +60,59 @@ ol.style.Shape = function(options) {
* @inheritDoc
* @return {ol.style.ShapeLiteral} Literal shape symbolizer.
*/
ol.style.Shape.prototype.createLiteral = function(opt_feature) {
ol.style.Shape.prototype.createLiteral = function(type, opt_feature) {
var literal = null;
var size = ol.expr.evaluateFeature(this.size_, opt_feature);
goog.asserts.assertNumber(size, 'size must be a number');
if (type === ol.geom.GeometryType.POINT ||
type === ol.geom.GeometryType.MULTIPOINT) {
var size = ol.expr.evaluateFeature(this.size_, opt_feature);
goog.asserts.assertNumber(size, 'size must be a number');
var fillColor;
if (!goog.isNull(this.fillColor_)) {
fillColor = ol.expr.evaluateFeature(this.fillColor_, opt_feature);
goog.asserts.assertString(fillColor, 'fillColor must be a string');
var fillColor, fillOpacity;
if (!goog.isNull(this.fill_)) {
fillColor = ol.expr.evaluateFeature(this.fill_.getColor(), opt_feature);
goog.asserts.assertString(
fillColor, 'fillColor must be a string');
fillOpacity = ol.expr.evaluateFeature(this.fill_.getOpacity(), opt_feature);
goog.asserts.assertNumber(
fillOpacity, 'fillOpacity must be a number');
}
var strokeColor, strokeOpacity, strokeWidth;
if (!goog.isNull(this.stroke_)) {
strokeColor = ol.expr.evaluateFeature(this.stroke_.getColor(), opt_feature);
goog.asserts.assertString(
strokeColor, 'strokeColor must be a string');
strokeOpacity = ol.expr.evaluateFeature(this.stroke_.getOpacity(),
opt_feature);
goog.asserts.assertNumber(
strokeOpacity, 'strokeOpacity must be a number');
strokeWidth = ol.expr.evaluateFeature(this.stroke_.getWidth(), opt_feature);
goog.asserts.assertNumber(
strokeWidth, 'strokeWidth must be a number');
}
literal = new ol.style.ShapeLiteral({
type: this.type_,
size: size,
fillColor: fillColor,
fillOpacity: fillOpacity,
strokeColor: strokeColor,
strokeOpacity: strokeOpacity,
strokeWidth: strokeWidth
});
}
var fillOpacity;
if (!goog.isNull(this.fillOpacity_)) {
fillOpacity = ol.expr.evaluateFeature(this.fillOpacity_, opt_feature);
goog.asserts.assertNumber(fillOpacity, 'fillOpacity must be a number');
}
var strokeColor;
if (!goog.isNull(this.strokeColor_)) {
strokeColor = ol.expr.evaluateFeature(this.strokeColor_, opt_feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
}
var strokeOpacity;
if (!goog.isNull(this.strokeOpacity_)) {
strokeOpacity = ol.expr.evaluateFeature(this.strokeOpacity_, opt_feature);
goog.asserts.assertNumber(strokeOpacity, 'strokeOpacity must be a number');
}
var strokeWidth;
if (!goog.isNull(this.strokeWidth_)) {
strokeWidth = ol.expr.evaluateFeature(this.strokeWidth_, opt_feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
}
var fill = goog.isDef(fillColor) && goog.isDef(fillOpacity);
var stroke = goog.isDef(strokeColor) && goog.isDef(strokeOpacity) &&
goog.isDef(strokeWidth);
goog.asserts.assert(fill || stroke,
'either fill or stroke properties must be defined');
return new ol.style.ShapeLiteral({
type: this.type_,
size: size,
fillColor: fillColor,
fillOpacity: fillOpacity,
strokeColor: strokeColor,
strokeOpacity: strokeOpacity,
strokeWidth: strokeWidth
});
return literal;
};
/**
* Get the fill color.
* @return {ol.expr.Expression} Fill color.
* Get the fill.
* @return {ol.style.Fill} Shape fill.
*/
ol.style.Shape.prototype.getFillColor = function() {
return this.fillColor_;
};
/**
* Get the fill opacity.
* @return {ol.expr.Expression} Fill opacity.
*/
ol.style.Shape.prototype.getFillOpacity = function() {
return this.fillOpacity_;
ol.style.Shape.prototype.getFill = function() {
return this.fill_;
};
@@ -227,29 +126,11 @@ ol.style.Shape.prototype.getSize = function() {
/**
* Get the stroke color.
* @return {ol.expr.Expression} Stroke color.
* Get the stroke.
* @return {ol.style.Stroke} Shape stroke.
*/
ol.style.Shape.prototype.getStrokeColor = function() {
return this.strokeColor_;
};
/**
* Get the stroke opacity.
* @return {ol.expr.Expression} Stroke opacity.
*/
ol.style.Shape.prototype.getStrokeOpacity = function() {
return this.strokeOpacity_;
};
/**
* Get the stroke width.
* @return {ol.expr.Expression} Stroke width.
*/
ol.style.Shape.prototype.getStrokeWidth = function() {
return this.strokeWidth_;
ol.style.Shape.prototype.getStroke = function() {
return this.stroke_;
};
@@ -263,22 +144,14 @@ ol.style.Shape.prototype.getType = function() {
/**
* Set the fill color.
* @param {ol.expr.Expression} fillColor Fill color.
* Set the fill.
* @param {ol.style.Fill} fill Shape fill.
*/
ol.style.Shape.prototype.setFillColor = function(fillColor) {
goog.asserts.assertInstanceof(fillColor, ol.expr.Expression);
this.fillColor_ = fillColor;
};
/**
* Set the fill opacity.
* @param {ol.expr.Expression} fillOpacity Fill opacity.
*/
ol.style.Shape.prototype.setFillOpacity = function(fillOpacity) {
goog.asserts.assertInstanceof(fillOpacity, ol.expr.Expression);
this.fillOpacity_ = fillOpacity;
ol.style.Shape.prototype.setFill = function(fill) {
if (!goog.isNull(fill)) {
goog.asserts.assertInstanceof(fill, ol.style.Fill);
}
this.fill_ = fill;
};
@@ -293,32 +166,14 @@ ol.style.Shape.prototype.setSize = function(size) {
/**
* Set the stroke color.
* @param {ol.expr.Expression} strokeColor Stroke color.
* Set the stroke.
* @param {ol.style.Stroke} stroke Shape stroke.
*/
ol.style.Shape.prototype.setStrokeColor = function(strokeColor) {
goog.asserts.assertInstanceof(strokeColor, ol.expr.Expression);
this.strokeColor_ = strokeColor;
};
/**
* Set the stroke opacity.
* @param {ol.expr.Expression} strokeOpacity Stroke opacity.
*/
ol.style.Shape.prototype.setStrokeOpacity = function(strokeOpacity) {
goog.asserts.assertInstanceof(strokeOpacity, ol.expr.Expression);
this.strokeOpacity_ = strokeOpacity;
};
/**
* Set the stroke width.
* @param {ol.expr.Expression} strokeWidth Stroke width.
*/
ol.style.Shape.prototype.setStrokeWidth = function(strokeWidth) {
goog.asserts.assertInstanceof(strokeWidth, ol.expr.Expression);
this.strokeWidth_ = strokeWidth;
ol.style.Shape.prototype.setStroke = function(stroke) {
if (!goog.isNull(stroke)) {
goog.asserts.assertInstanceof(stroke, ol.style.Stroke);
}
this.stroke_ = stroke;
};
@@ -332,14 +187,10 @@ ol.style.Shape.prototype.setType = function(type) {
/**
* @type {ol.style.ShapeLiteral}
* @typedef {{type: (ol.style.ShapeType),
* size: (number)}}
*/
ol.style.ShapeDefaults = new ol.style.ShapeLiteral({
ol.style.ShapeDefaults = {
type: ol.style.ShapeType.CIRCLE,
size: 5,
fillColor: '#ffffff',
fillOpacity: 0.4,
strokeColor: '#696969',
strokeOpacity: 0.8,
strokeWidth: 1.5
});
size: 5
};

View File

@@ -0,0 +1,158 @@
goog.provide('ol.style.Stroke');
goog.provide('ol.style.StrokeDefaults');
goog.require('goog.asserts');
goog.require('ol.geom.GeometryType');
goog.require('ol.expr');
goog.require('ol.expr.Expression');
goog.require('ol.expr.Literal');
goog.require('ol.style.LineLiteral');
goog.require('ol.style.Symbolizer');
goog.require('ol.style.Literal');
/**
* @constructor
* @extends {ol.style.Symbolizer}
* @param {ol.style.StrokeOptions=} opt_options Stroke options.
*/
ol.style.Stroke = function(opt_options) {
goog.base(this);
var options = opt_options || {};
/**
* @type {ol.expr.Expression}
* @private
*/
this.color_ = !goog.isDefAndNotNull(options.color) ?
new ol.expr.Literal(ol.style.StrokeDefaults.color) :
(options.color instanceof ol.expr.Expression) ?
options.color : new ol.expr.Literal(options.color);
/**
* @type {ol.expr.Expression}
* @private
*/
this.opacity_ = !goog.isDefAndNotNull(options.opacity) ?
new ol.expr.Literal(ol.style.StrokeDefaults.opacity) :
(options.opacity instanceof ol.expr.Expression) ?
options.opacity : new ol.expr.Literal(options.opacity);
/**
* @type {ol.expr.Expression}
* @private
*/
this.width_ = !goog.isDefAndNotNull(options.width) ?
new ol.expr.Literal(ol.style.StrokeDefaults.width) :
(options.width instanceof ol.expr.Expression) ?
options.width : new ol.expr.Literal(options.width);
};
goog.inherits(ol.style.Stroke, ol.style.Symbolizer);
/**
* @inheritDoc
* @return {ol.style.LineLiteral|ol.style.PolygonLiteral} Symbolizer literal.
*/
ol.style.Stroke.prototype.createLiteral = function(type, opt_feature) {
var color = ol.expr.evaluateFeature(
this.color_, opt_feature);
goog.asserts.assertString(color, 'color must be a string');
var opacity = ol.expr.evaluateFeature(
this.opacity_, opt_feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
var width = ol.expr.evaluateFeature(
this.width_, opt_feature);
goog.asserts.assertNumber(width, 'width must be a number');
var literal = null;
if (type === ol.geom.GeometryType.LINESTRING ||
type === ol.geom.GeometryType.MULTILINESTRING) {
literal = new ol.style.LineLiteral({
strokeColor: color,
strokeOpacity: opacity,
strokeWidth: width
});
} else if (type === ol.geom.GeometryType.POLYGON ||
type === ol.geom.GeometryType.MULTIPOLYGON) {
literal = new ol.style.PolygonLiteral({
strokeColor: color,
strokeOpacity: opacity,
strokeWidth: width
});
}
return literal;
};
/**
* Get the stroke color.
* @return {ol.expr.Expression} Stroke color.
*/
ol.style.Stroke.prototype.getColor = function() {
return this.color_;
};
/**
* Get the stroke opacity.
* @return {ol.expr.Expression} Stroke opacity.
*/
ol.style.Stroke.prototype.getOpacity = function() {
return this.opacity_;
};
/**
* Get the stroke width.
* @return {ol.expr.Expression} Stroke width.
*/
ol.style.Stroke.prototype.getWidth = function() {
return this.width_;
};
/**
* Set the stroke color.
* @param {ol.expr.Expression} color Stroke color.
*/
ol.style.Stroke.prototype.setColor = function(color) {
goog.asserts.assertInstanceof(color, ol.expr.Expression);
this.color_ = color;
};
/**
* Set the stroke opacity.
* @param {ol.expr.Expression} opacity Stroke opacity.
*/
ol.style.Stroke.prototype.setOpacity = function(opacity) {
goog.asserts.assertInstanceof(opacity, ol.expr.Expression);
this.opacity_ = opacity;
};
/**
* Set the stroke width.
* @param {ol.expr.Expression} width Stroke width.
*/
ol.style.Stroke.prototype.setWidth = function(width) {
goog.asserts.assertInstanceof(width, ol.expr.Expression);
this.width_ = width;
};
/**
* @typedef {{strokeColor: (string),
* strokeOpacity: (number),
* strokeWidth: (number)}}
*/
ol.style.StrokeDefaults = {
color: '#696969',
opacity: 0.75,
width: 1.5
};

View File

@@ -1,10 +1,14 @@
goog.provide('ol.style.Style');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.geom.GeometryType');
goog.require('ol.style.Rule');
goog.require('ol.style.Fill');
goog.require('ol.style.Literal');
goog.require('ol.style.PolygonLiteral');
goog.require('ol.style.Rule');
goog.require('ol.style.Shape');
goog.require('ol.style.Stroke');
@@ -31,15 +35,20 @@ ol.style.Style = function(options) {
ol.style.Style.prototype.apply = function(feature) {
var rules = this.rules_,
literals = [],
geometry = feature.getGeometry(),
rule, symbolizers;
for (var i = 0, ii = rules.length; i < ii; ++i) {
rule = rules[i];
if (rule.applies(feature)) {
symbolizers = rule.getSymbolizers();
for (var j = 0, jj = symbolizers.length; j < jj; ++j) {
literals.push(symbolizers[j].createLiteral(feature));
var type = geometry ? geometry.getType() : null;
if (!goog.isNull(type)) {
for (var i = 0, ii = rules.length; i < ii; ++i) {
rule = rules[i];
if (rule.applies(feature)) {
symbolizers = rule.getSymbolizers();
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;
};
@@ -51,32 +60,54 @@ ol.style.Style.prototype.apply = function(feature) {
* the feature.
*/
ol.style.Style.applyDefaultStyle = function(feature) {
var geometry = feature.getGeometry(),
symbolizerLiterals = [];
if (!goog.isNull(geometry)) {
var type = geometry.getType();
if (type === ol.geom.GeometryType.POINT ||
type === ol.geom.GeometryType.MULTIPOINT) {
symbolizerLiterals.push(ol.style.ShapeDefaults);
} else if (type === ol.geom.GeometryType.LINESTRING ||
type === ol.geom.GeometryType.MULTILINESTRING) {
symbolizerLiterals.push(ol.style.LineDefaults);
} else if (type === ol.geom.GeometryType.LINEARRING ||
type === ol.geom.GeometryType.POLYGON ||
type === ol.geom.GeometryType.MULTIPOLYGON) {
symbolizerLiterals.push(ol.style.PolygonDefaults);
}
}
return symbolizerLiterals;
return ol.style.Style.defaults.apply(feature);
};
/**
* Collapse partial polygon symbolizers.
* The default style.
* @type {ol.style.Style}
*/
ol.style.Style.defaults = new ol.style.Style({
rules: [
new ol.style.Rule({
symbolizers: [
new ol.style.Shape({
fill: new ol.style.Fill(),
stroke: new ol.style.Stroke()
}),
new ol.style.Fill(),
new ol.style.Stroke()
]
})
]
});
/**
* Given an array of symbolizers, generate an array of literals.
* @param {Array.<ol.style.Symbolizer>} symbolizers List of symbolizers.
* @param {ol.geom.GeometryType} type Geometry type.
* @param {ol.Feature=} opt_feature Optional feature.
* @return {Array.<ol.style.Literal>} Array of literals.
*/
ol.style.Style.createLiterals = function(symbolizers, type, opt_feature) {
var length = symbolizers.length;
var literals = new Array(length);
for (var i = 0; i < length; ++i) {
literals[i] = symbolizers[i].createLiteral(type, opt_feature);
}
return ol.style.Style.reduceLiterals_(literals);
}
/**
* Collapse partial polygon symbolizers and remove null symbolizers.
* @param {Array.<ol.style.Literal>} literals Input literals.
* @return {Array.<ol.style.Literal>} Reduced literals.
* @private
*/
ol.style.Style.reduceLiterals = function(literals) {
ol.style.Style.reduceLiterals_ = function(literals) {
var reduced = [];
var literal, stroke, fill, key, value;
for (var i = 0, ii = literals.length; i < ii; ++i) {
@@ -116,7 +147,7 @@ ol.style.Style.reduceLiterals = function(literals) {
// both stroke and fill, proceed
reduced.push(literal);
}
} else {
} else if (literal) {
reduced.push(literal);
}
}

View File

@@ -12,6 +12,7 @@ ol.style.Symbolizer = function() {};
/**
* @param {ol.geom.GeometryType} type Geometry type.
* @param {ol.Feature=} opt_feature Feature for evaluating expressions.
* @return {ol.style.Literal} Literal symbolizer.
*/

View File

@@ -67,7 +67,7 @@ goog.inherits(ol.style.Text, ol.style.Symbolizer);
* @inheritDoc
* @return {ol.style.TextLiteral} Literal text symbolizer.
*/
ol.style.Text.prototype.createLiteral = function(opt_feature) {
ol.style.Text.prototype.createLiteral = function(type, opt_feature) {
var color = ol.expr.evaluateFeature(this.color_, opt_feature);
goog.asserts.assertString(color, 'color must be a string');
@@ -190,12 +190,14 @@ ol.style.Text.prototype.setText = function(text) {
/**
* @type {ol.style.TextLiteral}
* @typedef {{color: string,
* fontFamily: string,
* fontSize: number,
* opacity: number}}
*/
ol.style.TextDefaults = new ol.style.TextLiteral({
ol.style.TextDefaults = {
color: '#000',
fontFamily: 'sans-serif',
fontSize: 10,
text: '',
opacity: 1
});
};