From 1317c6de361af9d1f84abd9f0dba0408c5ef1831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Dietrich?= Date: Thu, 12 Sep 2019 18:38:20 +0200 Subject: added special functions for decoding StyleDefinitions --- src/Mapbox/Layer.elm | 45 +++++++++++++++++++++++--- src/Mapbox/Source.elm | 18 +++++++++-- src/Mapbox/Style.elm | 89 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 143 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Mapbox/Layer.elm b/src/Mapbox/Layer.elm index dea484c..8d9cb63 100644 --- a/src/Mapbox/Layer.elm +++ b/src/Mapbox/Layer.elm @@ -1,6 +1,6 @@ module Mapbox.Layer exposing ( Layer, SourceId, encode - , background, fill, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade + , background, fill, json, jsonList, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade , Background, Fill, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade , LayerAttr , metadata, sourceLayer, minzoom, maxzoom, filter, visible @@ -13,6 +13,7 @@ module Mapbox.Layer exposing , rasterBrightnessMax, rasterBrightnessMin, rasterContrast, rasterFadeDuration, rasterHueRotate, rasterOpacity, rasterResampling, rasterSaturation , hillshadeAccentColor, hillshadeExaggeration, hillshadeHighlightColor, hillshadeIlluminationAnchor, hillshadeIlluminationDirection, hillshadeShadowColor , backgroundColor, backgroundOpacity, backgroundPattern + , decode ) {-| Layers specify what is actually rendered on the map and are rendered in order. @@ -46,8 +47,8 @@ Paint properties are applied later in the rendering process. Changes to a paint ### Layer Types -@docs background, fill, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade -@docs Background, Fill, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade +@docs background, fill, json, jsonList, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade +@docs Background, Fill, Json, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade ### General Attributes @@ -100,10 +101,16 @@ Paint properties are applied later in the rendering process. Changes to a paint @docs backgroundColor, backgroundOpacity, backgroundPattern + +### List Decoder + +@docs decode + -} import Array exposing (Array) import Internal exposing (Supported) +import Json.Decode as Decode exposing (Decoder) import Json.Encode as Encode exposing (Value) import Mapbox.Expression as Expression exposing (CameraExpression, Color, DataExpression, Expression, FormattedText) @@ -165,6 +172,11 @@ type Hillshade = HillshadeLayer +{-| -} +type Json + = JsonLayer + + {-| Turns a layer into JSON -} encode : Layer -> Value @@ -173,8 +185,8 @@ encode (Layer value) = layerImpl tipe id source attrs = - [ ( "type", Encode.string tipe ) - , ( "id", Encode.string id ) + [ ( "id", Encode.string id ) + , ( "type", Encode.string tipe ) , ( "source", Encode.string source ) ] ++ encodeAttrs attrs @@ -271,6 +283,29 @@ hillshade = layerImpl "hillshade" +{-| Directly aass a json value layer +-} +json : Value -> Layer +json = + Layer + + +{-| Directly aass a json value layer +-} +jsonList : String -> List Layer +jsonList = + Decode.decodeString (Decode.list Decode.value) + >> Result.withDefault [] + >> List.map json + + +{-| Directly aass a json value layer +-} +decode : Decoder (List Layer) +decode = + Decode.list (Decode.value |> Decode.map json) + + {-| -} type LayerAttr tipe = Top String Value diff --git a/src/Mapbox/Source.elm b/src/Mapbox/Source.elm index 68d1862..5ca38ea 100644 --- a/src/Mapbox/Source.elm +++ b/src/Mapbox/Source.elm @@ -1,6 +1,6 @@ module Mapbox.Source exposing ( Source, SourceOption - , Id, Url + , Id, Url, decode , vector, vectorFromUrl, VectorSource , raster, tileSize, rasterFromUrl, RasterSource , rasterDEMMapbox, rasterDEMTerrarium @@ -17,7 +17,7 @@ module Mapbox.Source exposing @docs Source, SourceOption -@docs Id, Url +@docs Id, Url, decode ### Vector @@ -58,7 +58,9 @@ Tiled sources can also take the following attributes: -} +import Dict import Internal +import Json.Decode as Decode exposing (Decoder) import Json.Encode exposing (Value) import LngLat exposing (LngLat) import Mapbox.Expression as Expression exposing (DataExpression, Expression) @@ -122,6 +124,18 @@ encode (Source _ value) = value +{-| -} +decode : Decoder (List Source) +decode = + Decode.dict Decode.value + |> Decode.map (Dict.toList >> List.map tupleToSource) + + +tupleToSource : ( String, Value ) -> Source +tupleToSource ( id, value ) = + Source id value + + {-| -} getId : Source -> String getId (Source k _) = diff --git a/src/Mapbox/Style.elm b/src/Mapbox/Style.elm index c33de45..20a91c1 100644 --- a/src/Mapbox/Style.elm +++ b/src/Mapbox/Style.elm @@ -1,5 +1,5 @@ module Mapbox.Style exposing - ( Style(..), encode, StyleDef + ( Style(..), encode, StyleDef, decode, MiscDef, decodeMiscDef, miscDefToList , Light, defaultLight , Transition, defaultTransition , MiscAttr, sprite, glyphs, name, defaultCenter, defaultZoomLevel, defaultBearing, defaultPitch, metadata @@ -8,7 +8,7 @@ module Mapbox.Style exposing {-| 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 +@docs Style, encode, StyleDef, decode, MiscDef, decodeMiscDef, miscDefToList ### Light @@ -35,7 +35,9 @@ You can also use one of these predefined styles. -} 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) @@ -154,6 +156,18 @@ type alias 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 @@ -285,3 +299,74 @@ 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 -- cgit v1.2.3 From 83572c1cd013c7e2752d6f9e9cb3873b4ab53463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Dietrich?= Date: Thu, 12 Sep 2019 19:16:13 +0200 Subject: removed obsolete json --- src/Mapbox/Layer.elm | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src') diff --git a/src/Mapbox/Layer.elm b/src/Mapbox/Layer.elm index 8d9cb63..e441e86 100644 --- a/src/Mapbox/Layer.elm +++ b/src/Mapbox/Layer.elm @@ -48,7 +48,7 @@ Paint properties are applied later in the rendering process. Changes to a paint ### Layer Types @docs background, fill, json, jsonList, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade -@docs Background, Fill, Json, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade +@docs Background, Fill, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade ### General Attributes @@ -172,11 +172,6 @@ type Hillshade = HillshadeLayer -{-| -} -type Json - = JsonLayer - - {-| Turns a layer into JSON -} encode : Layer -> Value -- cgit v1.2.3 From e7947aef7cfa6c9cb447d4532245c534857487a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Dietrich?= Date: Thu, 12 Sep 2019 19:33:41 +0200 Subject: a little comment --- src/Mapbox/Style.elm | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/Mapbox/Style.elm b/src/Mapbox/Style.elm index 20a91c1..8d1f12e 100644 --- a/src/Mapbox/Style.elm +++ b/src/Mapbox/Style.elm @@ -156,6 +156,13 @@ type alias StyleDef = } +{-| 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 -- cgit v1.2.3 From 9d091092b75fa90dda83868440ef04f43694e068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Dietrich?= Date: Mon, 23 Sep 2019 10:54:57 +0200 Subject: added additional visibility function visible2 that directly uses a boolean --- src/Mapbox/Layer.elm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Mapbox/Layer.elm b/src/Mapbox/Layer.elm index e441e86..1249b0a 100644 --- a/src/Mapbox/Layer.elm +++ b/src/Mapbox/Layer.elm @@ -3,7 +3,7 @@ module Mapbox.Layer exposing , background, fill, json, jsonList, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade , Background, Fill, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade , LayerAttr - , metadata, sourceLayer, minzoom, maxzoom, filter, visible + , metadata, sourceLayer, minzoom, maxzoom, filter, visible, visible2 , fillAntialias, fillColor, fillOpacity, fillOutlineColor, fillPattern, fillTranslate, fillTranslateAnchor , lineBlur, lineCap, lineColor, lineDasharray, lineGapWidth, lineGradient, lineJoin, lineMiterLimit, lineOffset, lineOpacity, linePattern, lineRoundLimit, lineTranslate, lineTranslateAnchor, lineWidth , circleBlur, circleColor, circleOpacity, circlePitchAlignment, circlePitchScale, circleRadius, circleStrokeColor, circleStrokeOpacity, circleStrokeWidth, circleTranslate, circleTranslateAnchor @@ -54,7 +54,7 @@ Paint properties are applied later in the rendering process. Changes to a paint ### General Attributes @docs LayerAttr -@docs metadata, sourceLayer, minzoom, maxzoom, filter, visible +@docs metadata, sourceLayer, minzoom, maxzoom, filter, visible, visible2 ### Fill Attributes @@ -354,6 +354,21 @@ visible vis = Layout "visibility" <| Expression.encode <| Expression.ifElse vis (Expression.str "visible") (Expression.str "none") +{-| Directly use a boolean value to set a layer to visible or not. +-} +visible2 : Bool -> LayerAttr any +visible2 vis = + (if vis then + "visible" + + else + "none" + ) + |> Expression.str + |> Expression.encode + |> Layout "visibility" + + -- Fill -- cgit v1.2.3