/
shape.js
230 lines (186 loc) · 6.4 KB
/
shape.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
goog.provide('ol.style.Shape');
goog.provide('ol.style.ShapeLiteral');
goog.provide('ol.style.ShapeType');
goog.require('ol.Expression');
goog.require('ol.ExpressionLiteral');
goog.require('ol.style.Point');
goog.require('ol.style.PointLiteral');
/**
* @enum {string}
*/
ol.style.ShapeType = {
CIRCLE: 'circle'
};
/**
* @typedef {{type: (ol.style.ShapeType),
* size: (number),
* fillColor: (string|undefined),
* strokeColor: (string|undefined),
* strokeWidth: (number|undefined),
* opacity: (number)}}
*/
ol.style.ShapeLiteralOptions;
/**
* @constructor
* @extends {ol.style.PointLiteral}
* @param {ol.style.ShapeLiteralOptions} config Symbolizer properties.
*/
ol.style.ShapeLiteral = function(config) {
goog.asserts.assertString(config.type, 'type must be a string');
/** @type {ol.style.ShapeType} */
this.type = config.type;
goog.asserts.assertNumber(config.size, 'size must be a number');
/** @type {number} */
this.size = config.size;
/** @type {string|undefined} */
this.fillColor = config.fillColor;
if (goog.isDef(config.fillColor)) {
goog.asserts.assertString(config.fillColor, 'fillColor must be a string');
}
/** @type {string|undefined} */
this.strokeColor = config.strokeColor;
if (goog.isDef(this.strokeColor)) {
goog.asserts.assertString(
this.strokeColor, 'strokeColor must be a string');
}
/** @type {number|undefined} */
this.strokeWidth = config.strokeWidth;
if (goog.isDef(this.strokeWidth)) {
goog.asserts.assertNumber(
this.strokeWidth, 'strokeWidth must be a number');
}
goog.asserts.assert(
goog.isDef(this.fillColor) ||
(goog.isDef(this.strokeColor) && goog.isDef(this.strokeWidth)),
'Either fillColor or strokeColor and strokeWidth must be set');
goog.asserts.assertNumber(config.opacity, 'opacity must be a number');
/** @type {number} */
this.opacity = config.opacity;
};
goog.inherits(ol.style.ShapeLiteral, ol.style.PointLiteral);
/**
* @inheritDoc
*/
ol.style.ShapeLiteral.prototype.equals = function(shapeLiteral) {
return this.type == shapeLiteral.type &&
this.size == shapeLiteral.size &&
this.fillColor == shapeLiteral.fillColor &&
this.strokeColor == shapeLiteral.strokeColor &&
this.strokeWidth == shapeLiteral.strokeWidth &&
this.opacity == shapeLiteral.opacity;
};
/**
* @constructor
* @extends {ol.style.Point}
* @param {ol.style.ShapeOptions} options Symbolizer properties.
*/
ol.style.Shape = function(options) {
/**
* @type {ol.style.ShapeType}
* @private
*/
this.type_ = /** @type {ol.style.ShapeType} */ (goog.isDef(options.type) ?
options.type : ol.style.ShapeDefaults.type);
/**
* @type {ol.Expression}
* @private
*/
this.size_ = !goog.isDef(options.size) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.size) :
(options.size instanceof ol.Expression) ?
options.size : new ol.ExpressionLiteral(options.size);
/**
* @type {ol.Expression}
* @private
*/
this.fillColor_ = !goog.isDefAndNotNull(options.fillColor) ?
null :
(options.fillColor instanceof ol.Expression) ?
options.fillColor : new ol.ExpressionLiteral(options.fillColor);
// stroke handling - if any stroke property is supplied, use defaults
var strokeColor = null,
strokeWidth = null;
if (goog.isDefAndNotNull(options.strokeColor) ||
goog.isDefAndNotNull(options.strokeWidth)) {
strokeColor = !goog.isDefAndNotNull(options.strokeColor) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.strokeColor) :
(options.strokeColor instanceof ol.Expression) ?
options.strokeColor : new ol.ExpressionLiteral(options.strokeColor);
strokeWidth = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
}
/**
* @type {ol.Expression}
* @private
*/
this.strokeColor_ = strokeColor;
/**
* @type {ol.Expression}
* @private
*/
this.strokeWidth_ = strokeWidth;
// one of stroke or fill can be null, both null is user error
goog.asserts.assert(!goog.isNull(this.fillColor_) ||
!(goog.isNull(this.strokeColor_) && goog.isNull(this.strokeWidth_)),
'Stroke or fill properties must be provided');
/**
* @type {ol.Expression}
* @private
*/
this.opacity_ = !goog.isDef(options.opacity) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.opacity) :
(options.opacity instanceof ol.Expression) ?
options.opacity : new ol.ExpressionLiteral(options.opacity);
};
/**
* @inheritDoc
* @return {ol.style.ShapeLiteral} Literal shape symbolizer.
*/
ol.style.Shape.prototype.createLiteral = function(opt_feature) {
var attrs,
feature = opt_feature;
if (goog.isDef(feature)) {
attrs = feature.getAttributes();
}
var size = this.size_.evaluate(feature, attrs);
goog.asserts.assertNumber(size, 'size must be a number');
var fillColor = goog.isNull(this.fillColor_) ?
undefined :
/** @type {string} */ (this.fillColor_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(fillColor) || goog.isString(fillColor));
var strokeColor = goog.isNull(this.strokeColor_) ?
undefined :
/** @type {string} */ (this.strokeColor_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(strokeColor) || goog.isString(strokeColor));
var strokeWidth = goog.isNull(this.strokeWidth_) ?
undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));
goog.asserts.assert(
goog.isDef(fillColor) ||
(goog.isDef(strokeColor) && goog.isDef(strokeWidth)),
'either fill style or strokeColor and strokeWidth must be defined');
var opacity = this.opacity_.evaluate(feature, attrs);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.ShapeLiteral({
type: this.type_,
size: size,
fillColor: fillColor,
strokeColor: strokeColor,
strokeWidth: strokeWidth,
opacity: opacity
});
};
/**
* @type {ol.style.ShapeLiteral}
*/
ol.style.ShapeDefaults = new ol.style.ShapeLiteral({
type: ol.style.ShapeType.CIRCLE,
size: 5,
fillColor: '#ffffff',
strokeColor: '#696969',
strokeWidth: 1.5,
opacity: 0.75
});