/
Expression.elm
560 lines (380 loc) · 9.48 KB
/
Expression.elm
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
module Mapbox.GL.Expression
exposing
( Expression
, encode
, case_
, at
, get
, get_
, has
, length
, stringLength
, not
, and
, or
, eq
, neq
, gt
, lt
, lte
, gte
, all
, any
, add
, sub
, mult
, div
, mod
, pow
, neg
, abs
, acos
, asin
, atan
, ceil
, cos
, floor
, ln
, ln2
, log10
, round
, sin
, sqrt
, tan
, min
, max
, interpolate
, linear
, exponential
, cubicBezier
, step
, concat
, downcase
, upcase
, zoom
, heatmapDensity
, e
, pi
, number
, string
, color
, bool
, rgb
, rgba
, toRgba
, letIn
, letIns
)
{-|
@docs Expression, encode
## Feature data
@docs at, get, get_, has, length
## Decision
@docs not,and,or,eq,neq,gt,lt,lte,gte,all,any,case_
## Math
@docs add, sub, mult, div, mod, pow, neg, abs, acos, asin,atan, ceil, cos, floor, ln, ln2, log10, round,sin,sqrt,tan,min,max
## Ramps, scales, curves
@docs interpolate ,linear, exponential , cubicBezier ,step
## String
@docs concat, stringLength, downcase, upcase
## Color
@docs rgb, rgba, toRgba
## Constants
@docs e, pi
## Literals
@docs number, string, bool, color
## Other
@docs zoom, heatmapDensity, letIn , letIns
-}
import Color exposing (Color)
import Json.Encode as Encode
import Mapbox.GL.Expression.Internal as Internal exposing (Expression(E), Expr(..), Lit(..), BinOp(..), UnOp(..), Interpolation(..))
{-| -}
type alias Expression a =
Internal.Expression a
{-| -}
encode : Expression a -> Encode.Value
encode (E expr) =
Internal.encode expr
type GeometryTy
= Point
| MultiPoint
| LineString
| MultiLineString
| Polygon
| MultiPolygon
-- Binding ---------------------------------------------------------------------
{-| -}
letIn : String -> Expr -> Expr -> Expr
letIn var binding body =
LetIn [ ( var, binding ) ] body
{-| -}
letIns : List ( String, Expr ) -> Expr -> Expr
letIns bindings body =
LetIn bindings body
-- Constants -------------------------------------------------------------------
{-| -}
e : Expression Float
e =
E ConstantE
{-| -}
pi : Expression Float
pi =
E ConstantPi
{-| -}
zoom : Expression Float
zoom =
E Zoom
{-| -}
heatmapDensity : Expression Float
heatmapDensity =
E HeatmapDensity
-- Literals --------------------------------------------------------------------
{-| -}
number : Float -> Expression Float
number n =
E <| Literal <| LitNumber n
{-| -}
string : String -> Expression String
string str =
E <| Literal <| LitString str
{-| -}
bool : Bool -> Expression Bool
bool b =
E <| Literal <| LitBool b
{-| -}
color : Color -> Expression Color
color c =
E <| Literal <| LitColor c
-- Math ------------------------------------------------------------------------
{-| -}
add : Expression Float -> Expression Float -> Expression Float
add (E a) (E b) =
E <| BinOp Add a b
{-| -}
sub : Expression Float -> Expression Float -> Expression Float
sub (E a) (E b) =
E <| BinOp Sub a b
{-| -}
mult : Expression Float -> Expression Float -> Expression Float
mult (E a) (E b) =
E <| BinOp Mult a b
{-| -}
div : Expression Float -> Expression Float -> Expression Float
div (E a) (E b) =
E <| BinOp Div a b
{-| -}
mod : Expression Float -> Expression Float -> Expression Float
mod (E a) (E b) =
E <| BinOp Mod a b
{-| -}
pow : Expression Float -> Expression Float -> Expression Float
pow (E a) (E b) =
E <| BinOp Pow a b
{-| -}
neg : Expression Float -> Expression Float
neg (E expr) =
E <| UnOp Neg expr
{-| -}
abs : Expression Float -> Expression Float
abs (E expr) =
E <| UnOp Abs expr
{-| -}
acos : Expression Float -> Expression Float
acos (E expr) =
E <| UnOp Acos expr
{-| -}
asin : Expression Float -> Expression Float
asin (E expr) =
E <| UnOp Asin expr
{-| -}
atan : Expression Float -> Expression Float
atan (E expr) =
E <| UnOp Atan expr
{-| -}
ceil : Expression Float -> Expression Float
ceil (E expr) =
E <| UnOp Ceil expr
{-| -}
cos : Expression Float -> Expression Float
cos (E expr) =
E <| UnOp Cos expr
{-| -}
floor : Expression Float -> Expression Float
floor (E expr) =
E <| UnOp Floor expr
{-| -}
ln : Expression Float -> Expression Float
ln (E expr) =
E <| UnOp Ln expr
{-| -}
ln2 : Expression Float -> Expression Float
ln2 (E expr) =
E <| UnOp Ln2 expr
{-| -}
log10 : Expression Float -> Expression Float
log10 (E expr) =
E <| UnOp Log10 expr
{-| -}
round : Expression Float -> Expression Float
round (E expr) =
E <| UnOp Round expr
{-| -}
sin : Expression Float -> Expression Float
sin (E expr) =
E <| UnOp Sin expr
{-| -}
sqrt : Expression Float -> Expression Float
sqrt (E expr) =
E <| UnOp Sqrt expr
{-| -}
tan : Expression Float -> Expression Float
tan (E expr) =
E <| UnOp Tan expr
{-| -}
min : List (Expression Float) -> Expression Float
min bs =
E <| Min <| List.map Internal.unE bs
{-| -}
max : List (Expression Float) -> Expression Float
max bs =
E <| Max <| List.map Internal.unE bs
-- Interpolation ---------------------------------------------------------------
{-| -}
linear : Interpolation
linear =
Linear
{-| -}
exponential : Float -> Interpolation
exponential base =
Exponential base
{-| -}
cubicBezier : Float -> Float -> Float -> Float -> Interpolation
cubicBezier x1 y1 x2 y2 =
CubicBezier x1 y1 x2 y2
{-| -}
interpolate : Interpolation -> Expression Float -> List ( Expression Float, Expression a ) -> Expression a
interpolate method (E input) stops =
E <| Interpolate method input <| List.map (\( E x, E y ) -> ( x, y )) stops
{-| -}
step : Expression Float -> Expression a -> List ( Expression Float, Expression a ) -> Expression a
step (E input) (E output0) stops =
E <| Step input output0 <| List.map (\( E x, E y ) -> ( x, y )) stops
-- Feature data ----------------------------------------------------------------
{-| -}
geometryType : Expression GeometryTy
geometryType =
E GeometryType
{-| -}
properties : Expression a
properties =
E Properties
{-| -}
id : Expression String
id =
E Id
-- Lookup ----------------------------------------------------------------------
{-| -}
at : Int -> Expression (List a) -> Expression a
at n (E array) =
E <| At n <| array
{-| -}
get : String -> Expression a -> Expression b
get field (E object) =
E <| Get field (Just object)
{-| -}
get_ : String -> Expression a
get_ field =
E <| Get field Nothing
{-| -}
has : String -> (a -> b) -> Expression a -> Expression b
has field getter (E object) =
E <| Has field (Just object)
{-| -}
length : Expression (List a) -> Expression Float
length (E expr) =
E <| Length expr
{-| -}
stringLength : Expression String -> Expression Float
stringLength (E expr) =
E <| Length expr
-- Logical operations ----------------------------------------------------------
{-| -}
case_ : List ( Expression Bool, Expression a ) -> Expression a -> Expression a
case_ cases (E default) =
let
cs =
List.map (\( E b, E a ) -> ( b, a )) cases
in
E <| Case cs default
{-| Logical negation. Returns true if the input is false, and false if the input is true.
-}
not : Expression Bool -> Expression Bool
not (E a) =
E <| UnOp Not a
{-| -}
and : Expression Bool -> Expression Bool -> Expression Bool
and (E a) (E b) =
E <| All [ a, b ]
{-| -}
or : Expression Bool -> Expression Bool -> Expression Bool
or (E a) (E b) =
E <| Any [ a, b ]
{-| -}
eq : Expression a -> Expression a -> Expression Bool
eq (E a) (E b) =
E <| BinOp Eq a b
{-| -}
neq : Expression a -> Expression a -> Expression Bool
neq (E a) (E b) =
E <| BinOp NEq a b
{-| -}
gt : Expression a -> Expression a -> Expression Bool
gt (E a) (E b) =
E <| BinOp Internal.GT a b
{-| -}
lt : Expression a -> Expression a -> Expression Bool
lt (E a) (E b) =
E <| BinOp Internal.LT a b
{-| -}
lte : Expression a -> Expression a -> Expression Bool
lte (E a) (E b) =
E <| BinOp LTE a b
{-| -}
gte : Expression a -> Expression a -> Expression Bool
gte (E a) (E b) =
E <| BinOp GTE a b
{-| -}
all : List (Expression Bool) -> Expression Bool
all bs =
E <| All <| List.map Internal.unE bs
{-| -}
any : List (Expression Bool) -> Expression Bool
any bs =
E <| Any <| List.map Internal.unE bs
-- String ----------------------------------------------------------------------
{-| -}
concat : List (Expression String) -> Expression String
concat bs =
E <| Concat <| List.map Internal.unE bs
{-| -}
downcase : Expression String -> Expression String
downcase (E expr) =
E <| Downcase expr
{-| -}
upcase : Expression String -> Expression String
upcase (E expr) =
E <| Upcase expr
-- Color -----------------------------------------------------------------------
{-| -}
rgb : Expression Float -> Expression Float -> Expression Float -> Expression Color
rgb (E r) (E g) (E b) =
E <| Rgb r g b
{-| -}
rgba : Expression Float -> Expression Float -> Expression Float -> Expression Float -> Expression Color
rgba (E r) (E g) (E b) (E a) =
E <| Rgba r g b a
{-| -}
toRgba : Expression Color -> Expression (List Float)
toRgba (E color) =
E <| ToRgba color