aboutsummaryrefslogtreecommitdiffstats
path: root/src/Mapbox/Style.elm
blob: 8d1f12e688899e395fa1dbb82722d0e9aca41ff5 (plain)
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
module Mapbox.Style exposing
    ( Style(..), encode, StyleDef, decode, MiscDef, decodeMiscDef, miscDefToList
    , Light, defaultLight
    , Transition, defaultTransition
    , MiscAttr, sprite, glyphs, name, defaultCenter, defaultZoomLevel, defaultBearing, defaultPitch, metadata
    , streets, outdoors, light, dark, satellite, satelliteStreets
    )

{-| A Mapbox style is a document that defines the visual appearance of a map: what data to draw, the order to draw it in, and how to style the data when drawing it. A style document is a JSON object with specific root level and nested properties. This specification defines and describes these properties.

@docs Style, encode, StyleDef, decode, MiscDef, decodeMiscDef, miscDefToList


### Light

@docs Light, defaultLight


### Transition

@docs Transition, defaultTransition


### Misc Attributes

@docs MiscAttr, sprite, glyphs, name, defaultCenter, defaultZoomLevel, defaultBearing, defaultPitch, metadata


### Predefined styles

You can also use one of these predefined styles.

@docs streets, outdoors, light, dark, satellite, satelliteStreets

-}

import Array exposing (Array)
import Dict
import Internal exposing (Supported)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode exposing (Value)
import LngLat exposing (LngLat)
import Mapbox.Expression exposing (CameraExpression, Color, Expression, float, floats, rgba, viewport)
import Mapbox.Layer exposing (Layer)
import Mapbox.Source exposing (Source)


{-| A mapbox style.

You can either create a style spec in Elm (for which the rest of this library is useful :) or load one from a remote URL.

To load a style from the Mapbox API, you can use a URL of the form `FromUrl "mapbox://styles/:owner/:style"`, where `:owner` is your Mapbox account name and `:style` is the style ID.

-}
type Style
    = Style StyleDef
    | FromUrl String


{-| -}
streets : Style
streets =
    FromUrl "mapbox://styles/mapbox/streets-v10"


{-| -}
outdoors : Style
outdoors =
    FromUrl "mapbox://styles/mapbox/outdoors-v10"


{-| -}
light : Style
light =
    FromUrl "mapbox://styles/mapbox/light-v9"


{-| -}
dark : Style
dark =
    FromUrl "mapbox://styles/mapbox/dark-v9"


{-| -}
satellite : Style
satellite =
    FromUrl "mapbox://styles/mapbox/satellite-v9"


{-| -}
satelliteStreets : Style
satelliteStreets =
    FromUrl "mapbox://styles/mapbox/satellite-streets-v10"


{-| Encodes the style into JSON.
-}
encode : Style -> Value
encode style =
    case style of
        Style styleDef ->
            [ ( "version", Encode.int 8 )
            , ( "transition", encodeTransition styleDef.transition )
            , ( "light", encodeLight styleDef.light )
            , ( "sources", Encode.object <| List.map (\source -> ( Mapbox.Source.getId source, Mapbox.Source.encode source )) styleDef.sources )
            , ( "layers", Encode.list Mapbox.Layer.encode styleDef.layers )
            ]
                ++ List.map (\(MiscAttr key value) -> ( key, value )) styleDef.misc
                |> Encode.object

        FromUrl s ->
            Encode.string s


encodeTransition : Transition -> Value
encodeTransition { duration, delay } =
    Encode.object [ ( "duration", Encode.int duration ), ( "delay", Encode.int delay ) ]


encodeLight : Light -> Value
encodeLight { anchor, position, color, intensity } =
    Encode.object
        [ ( "anchor", Mapbox.Expression.encode anchor )
        , ( "position", Mapbox.Expression.encode position )
        , ( "color", Mapbox.Expression.encode color )
        , ( "intensity", Mapbox.Expression.encode intensity )
        ]


{-| This is the core representation of a Mapbox style. It has the following keys:


### Layers

These define what is actually rendered on screen. See the Mapbox.Layer module on how to configure these.


### Sources

These define the data sources that feed the Layers. See the Mapbox.Source module for more.


### Misc

These are all optional attributes.

All the other keys are values defined below.

-}
type alias StyleDef =
    { transition : Transition
    , light : Light
    , layers : List Layer
    , sources : List Source
    , misc : List MiscAttr
    }


{-| A simple record to be used, when a style is read from a json file.

use `decodeMiscDef json` to read out default values, you can reset them by
setting them to `Nothing` use afterwards `miscDefToList` to generate a misc-list
to be used in your StyleDef.

-}
type alias MiscDef =
    { sprite : Maybe MiscAttr
    , glyphs : Maybe MiscAttr
    , name : Maybe MiscAttr
    , center : Maybe MiscAttr
    , zoom : Maybe MiscAttr
    , bearing : Maybe MiscAttr
    , pitch : Maybe MiscAttr
    , meta : Maybe MiscAttr
    }


{-| -}
type MiscAttr
    = MiscAttr String Value


{-| A human-readable name for the style.
-}
name : String -> MiscAttr
name =
    Encode.string >> MiscAttr "name"


{-| Default map center in longitude and latitude. The style center will be used only if the map has not been positioned by other means (e.g. map options or user interaction).
-}
defaultCenter : LngLat -> MiscAttr
defaultCenter =
    LngLat.encodeAsPair >> MiscAttr "center"


{-| Default zoom level. The style zoom will be used only if the map has not been positioned by other means (e.g. map options or user interaction).
-}
defaultZoomLevel : Float -> MiscAttr
defaultZoomLevel =
    Encode.float >> MiscAttr "zoom"


{-| Arbitrary properties useful to track with the stylesheet, but do not influence rendering. Properties should be prefixed to avoid collisions, like 'mapbox:'.
-}
metadata : List ( String, Value ) -> MiscAttr
metadata =
    Encode.object >> MiscAttr "metadata"


{-| Default bearing, in degrees. The bearing is the compass direction that is "up"; for example, a bearing of 90° orients the map so that east is up. This value will be used only if the map has not been positioned by other means (e.g. map options or user interaction).
-}
defaultBearing : Float -> MiscAttr
defaultBearing =
    Encode.float >> MiscAttr "bearing"


{-| Default pitch, in degrees. Zero is perpendicular to the surface, for a look straight down at the map, while a greater value like 60 looks ahead towards the horizon. The style pitch will be used only if the map has not been positioned by other means (e.g. map options or user interaction).
-}
defaultPitch : Float -> MiscAttr
defaultPitch =
    Encode.float >> MiscAttr "pitch"


{-| A base URL for retrieving the sprite image and metadata. The extensions .png, .json and scale factor @2x.png will be automatically appended. This property is required if any layer uses the `backgroundPattern`, `fillPattern`, `linePattern`, `fillExtrusionPattern`, or `iconImage` properties.
-}
sprite : String -> MiscAttr
sprite =
    Encode.string >> MiscAttr "sprite"


{-| A URL template for loading signed-distance-field glyph sets in PBF format. The URL must include `{fontstack}` and `{range}` tokens. This property is required if any layer uses the `textField` layout property.
-}
glyphs : String -> MiscAttr
glyphs =
    Encode.string >> MiscAttr "glyphs"



-- Light


{-| The global light source.


### `anchor`

Whether extruded geometries are lit relative to the map or viewport.


### `position`

Position of the light source relative to lit (extruded) geometries, in `[r radial coordinate, a azimuthal angle, p polar angle]` where `r` indicates the distance from the center of the base of an object to its light, `a` indicates the position of the light relative to 0° (0° when the `anchor` is set to `viewport` corresponds to the top of the viewport, or 0° when `anchor` is set to `map` corresponds to due north, and degrees proceed clockwise), and `p` indicates the height of the light (from 0°, directly above, to 180°, directly below).


### `color`

Color tint for lighting extruded geometries.


### `intensity`

Intensity of lighting (on a scale from 0 to 1). Higher numbers will present as more extreme contrast.

-}
type alias Light =
    { anchor : Expression CameraExpression { map : Supported, viewport : Supported }
    , position : Expression CameraExpression (Array Float)
    , color : Expression CameraExpression Color
    , intensity : Expression CameraExpression Float
    }


{-| A decent default light.
-}
defaultLight : Light
defaultLight =
    { anchor = viewport
    , position = floats [ 1.15, 210, 30 ]
    , color = rgba 255 255 255 1
    , intensity = float 0.5
    }


{-| A transition property controls timing for the interpolation between a transitionable style property's previous value and new value.


### `duration`

Time (in ms) allotted for transitions to complete.


### `delay`

Length of time (in ms) before a transition begins.

-}
type alias Transition =
    { duration : Int, delay : Int }


{-| The defaults for a transition
-}
defaultTransition : Transition
defaultTransition =
    { duration = 300
    , delay = 0
    }


{-| -}
decode : Decoder StyleDef
decode =
    Decode.map5 StyleDef
        (Decode.maybe decTransition
            |> Decode.map (Maybe.withDefault defaultTransition)
        )
        (Decode.succeed defaultLight)
        (Decode.field "layers" Mapbox.Layer.decode)
        (Decode.field "sources" Mapbox.Source.decode)
        (Decode.succeed [])


decTransition : Decoder Transition
decTransition =
    Decode.map2 Transition
        (Decode.field "duration" Decode.int)
        (Decode.field "delay" Decode.int)


maybeDecode : String -> Decoder a -> (a -> b) -> Decoder (Maybe b)
maybeDecode id dec fn =
    Decode.field id (dec |> Decode.map fn)
        |> Decode.maybe


{-| -}
decodeMiscDef : Decoder MiscDef
decodeMiscDef =
    Decode.map8 MiscDef
        (maybeDecode "sprite" Decode.string sprite)
        (maybeDecode "glyphs" Decode.string glyphs)
        (maybeDecode "name" Decode.string name)
        (maybeDecode "center"
            (Decode.float
                |> Decode.list
                |> Decode.andThen
                    (\list ->
                        case list of
                            [ lng, lat ] ->
                                Decode.succeed <| LngLat lng lat

                            _ ->
                                Decode.fail "not a valid position"
                    )
            )
            defaultCenter
        )
        (maybeDecode "zoom" Decode.float defaultZoomLevel)
        (maybeDecode "pitch" Decode.float defaultPitch)
        (maybeDecode "bearing" Decode.float defaultBearing)
        (maybeDecode "metadata" (Decode.dict Decode.value) (Dict.toList >> metadata))


maybeSingleton : Maybe MiscAttr -> List MiscAttr
maybeSingleton =
    Maybe.map List.singleton >> Maybe.withDefault []


{-| -}
miscDefToList : MiscDef -> List MiscAttr
miscDefToList miscDef =
    maybeSingleton miscDef.sprite
        ++ maybeSingleton miscDef.glyphs
        ++ maybeSingleton miscDef.name
        ++ maybeSingleton miscDef.center
        ++ maybeSingleton miscDef.zoom
        ++ maybeSingleton miscDef.bearing
        ++ maybeSingleton miscDef.pitch