forked from NathanW2/qgsexpressionsplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
446 lines (363 loc) · 14.2 KB
/
functions.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
Extra functions for QgsExpression
register=False in order to delay registring of functions before we load the plugin
"""
from qgis.utils import qgsfunction
from qgis.core import QgsStyleV2, QgsExpression, QgsSymbolLayerV2Utils
from PyQt4.QtCore import QObject, QDateTime, QDate
from PyQt4.QtGui import QColor
def getFloat(value):
try:
return value, None
except ValueError:
return 0, "Can not convert {} to float".format(value)
@qgsfunction(2, "Expressions +", register=False)
def ramp_color_rgb(values, feature, parent):
"""
Return only the rgb part of a defined color ramp
<p><h4>Syntax</h4>
ramp_color_rgb(<i>ramp_name,value</i>)</p>
<p><h4>Arguments</h4>
<i> ramp_name</i> → the name of the color ramp as a string, for example 'Spectral'.<br>
<i> value</i> → the position on the ramp to select the color from as a real number between 0 and 1.<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
ramp_color_rgb('Spectral',0.3) → '253,190,115'</p>
<p><h4>Note:</h4>
The color ramps available vary between QGIS installations. This function
may not give the expected results if you move your Quantum project.
</p>
"""
ramp_name = values[0]
ramp_position = values[1]
ramp = QgsStyleV2.defaultStyle().colorRampRef(ramp_name)
if not ramp:
parent.setEvalErrorString( QObject.tr( '"{}" is not a valid color ramp'.format(ramp_name)))
return QColor(0,0,0).name()
value, error = getFloat(ramp_position)
if error:
parent.setEvalErrorString(error)
color = ramp.color(value)
return "{},{},{}".format(color.red(), color.green(), color.blue())
@qgsfunction(1, "Expressions +", register=False)
def red(values, feature, parent):
"""
Returns the red component of a color
<p><h4>Syntax</h4>
red(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
red('255,0,0') → 255</p>
"""
try:
return QgsSymbolLayerV2Utils.decodeColor(values[0]).red()
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def green(values, feature, parent):
"""
Returns the green component of a color
<p><h4>Syntax</h4>
green(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
green('0,255,0') → 255</p>
"""
try:
return QgsSymbolLayerV2Utils.decodeColor(values[0]).green()
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def blue(values, feature, parent):
"""
Returns the blue component of a color
<p><h4>Syntax</h4>
blue(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
blue('0,0,255') → 255</p>
"""
try:
return QgsSymbolLayerV2Utils.decodeColor(values[0]).blue()
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def alpha(values, feature, parent):
"""
Returns the alpha component of a color
<p><h4>Syntax</h4>
alpha(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
alpha('255,255,255,125') → 125</p>
"""
try:
return QgsSymbolLayerV2Utils.decodeColor(values[0]).alpha()
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def hue(values, feature, parent):
"""
Returns the hue component of a color, an integer between 0-360
<p><h4>Syntax</h4>
hue(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
hue('255,0,0') → 0</p>
"""
try:
# Hue ranges from 0 - 360
return int(QgsSymbolLayerV2Utils.decodeColor(values[0]).hueF() * 360)
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def saturation(values, feature, parent):
"""
Returns the saturation of a color, an integer between 0-100
<p><h4>Syntax</h4>
saturation(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
saturation('125,255,125') → 50</p>
"""
try:
# Saturation ranges from 0 - 100
return int(QgsSymbolLayerV2Utils.decodeColor(values[0]).saturationF() * 100)
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def lightness(values, feature, parent):
"""
Returns the lightness of a color, an integer between 0-100
<p><h4>Syntax</h4>
lightness(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
lightness('125,255,125') → 74</p>
"""
try:
# Lightness ranges from 0 - 100
return int(QgsSymbolLayerV2Utils.decodeColor(values[0]).lightnessF() * 100)
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def hsv_value(values, feature, parent):
"""
Returns the hsv value component of a color, an integer between 0-100
<p><h4>Syntax</h4>
hsv_value(<i>color</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
hsv_value('125,255,125') → 100</p>
"""
try:
# Value ranges from 0 - 100
return int(QgsSymbolLayerV2Utils.decodeColor(values[0]).valueF() * 100)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_red(values, feature, parent):
"""
Sets the red component of a color
<p><h4>Syntax</h4>
set_red(<i>color</i>, <i>red</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> red</i> → a integer between 0 and 255<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_red('255,255,255', 125) → '125,255,255'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setRed(values[1])
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_green(values, feature, parent):
"""
Sets the blue component of a color
<p><h4>Syntax</h4>
set_green(<i>color</i>, <i>blue</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> green</i> → a integer between 0 and 255<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_green('255,255,255', 125) → '255,125,255'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setGreen(values[1])
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_blue(values, feature, parent):
"""
Sets the blue component of a color
<p><h4>Syntax</h4>
set_blue(<i>color</i>, <i>blue</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> blue</i> → a integer between 0 and 255<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_blue('255,255,255', 125) → '255,255,125'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setBlue(values[1])
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_hue(values, feature, parent):
"""
Sets the hue component of a color
<p><h4>Syntax</h4>
set_hue(<i>color</i>, <i>hue</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> hue</i> → a integer between 0 and 360<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_hue('0,255,0,255', 0) → '255,0,0,255'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setHslF(values[1] / 360.0, color.saturationF(), color.lightnessF(), color.alphaF())
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_saturation(values, feature, parent):
"""
Sets the saturation of a color
<p><h4>Syntax</h4>
set_saturation(<i>color</i>, <i>saturation</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> saturation</i> → a integer between 0 and 100<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_saturation('0,255,0,255', 0) → '128,128,128,125'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setHslF(color.hueF(), values[1] / 100.0, color.lightnessF(), color.alphaF())
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_lightness(values, feature, parent):
"""
Sets the lightness of a color
<p><h4>Syntax</h4>
set_lightness(<i>color</i>, <i>lightness</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> lightness</i> → a integer between 0 and 100<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_lightness('0,255,0,255', 10) → '0,51,0,255'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setHslF(color.hueF(), color.saturationF(), values[1] / 100.0, color.alphaF())
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_hsv_value(values, feature, parent):
"""
Sets the value of a color
<p><h4>Syntax</h4>
set_hsv_value(<i>color</i>, <i>value</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> value</i> → a integer between 0 and 100<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_hsv_value('0,255,0,255', 50) → '0,128,0,255'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setHsvF(color.hueF(), color.saturationF(), values[1] / 100.0, color.alphaF())
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(2, "Expressions +", register=False)
def set_alpha(values, feature, parent):
"""
Sets the alpha component of a color
<p><h4>Syntax</h4>
set_alpha(<i>color</i>, <i>alpha</i>)</p>
<p><h4>Arguments</h4>
<i> color</i> → a color<br>
<i> alpha</i> → an alpha value between 0 and 255<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
set_alpha('255,255,255,255', 125) → '255,255,255,125'</p>
"""
try:
color = QgsSymbolLayerV2Utils.decodeColor(values[0])
color.setAlpha(values[1])
return QgsSymbolLayerV2Utils.encodeColor(color)
except:
return None
@qgsfunction(1, "Expressions +", register=False)
def dow(values, feature, parent):
"""
Returns an integer representing the day of week for a given date. Returned
values range from 0-6, where 0 is Sunday.
<p><h4>Syntax</h4>
dow(<i>date</i>)</p>
<p><h4>Arguments</h4>
<i> date</i> → a date value. Must be a valid date or datetime field, or a
string in the format 'yyyy-mm-dd'.<br></p>
<p><h4>Example</h4>
<!-- Show example of function.-->
dow('2013-07-01') → 1</p>
"""
input_date = values[0]
# Return dayOfWeek() % 7 so that values range from 0 (sun) to 6 (sat)
# to match Postgresql behaviour
if type(input_date) == QDateTime:
return input_date.date().dayOfWeek() % 7
elif type(input_date) == QDate:
return input_date.dayOfWeek() % 7
elif type(input_date) in (str, unicode):
# Convert string to qdate
input_qdate = QDate.fromString(input_date, 'yyyy-MM-dd')
if input_qdate.isValid():
return input_qdate.dayOfWeek() % 7
else:
return None
functions = [ramp_color_rgb, red, green, blue, hue, saturation, lightness, hsv_value, alpha,
set_alpha, set_red, set_hue, set_saturation, set_lightness, set_hsv_value, dow]
def registerFunctions():
for func in functions:
if QgsExpression.registerFunction(func):
yield func.name()
def unregisterFunctions():
# Unload all the functions that we created.
for func in functions:
QgsExpression.unregisterFunction(func.name())