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
|
function generateProperties(spec) {
const layouts = spec.layout;
const paints = spec.paint;
var codes = {};
var docs = {};
var enums = {};
layouts.forEach(l => {
const layerType = titleCase(l.split('_')[1])
docs[layerType] = [];
codes[layerType] = [];
Object.entries(spec[l]).forEach(([name, prop]) => {
if (name == 'visibility') return '';
if (prop.type === 'enum') {
enums[name] = Object.keys(prop.values).join(' | ');
}
codes[layerType].push(generateElmProperty(name, prop, layerType, 'Layout'));
docs[layerType].push(camelCase(name));
})
})
paints.forEach(l => {
const layerType = titleCase(l.split('_')[1])
Object.entries(spec[l]).forEach(([name, prop]) => {
if (name == 'visibility') return '';
if (prop.type === 'enum') {
enums[name] = Object.keys(prop.values).join(' | ');
}
codes[layerType].push(generateElmProperty(name, prop, layerType, 'Paint'))
docs[layerType].push(camelCase(name));
})
})
Object.values(docs).forEach(d => d.sort())
Object.values(codes).forEach(d => d.sort());
console.log(enums);
return `
module Mapbox.Layer exposing (
Layer, SourceId, Background, Fill, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade, LayerAttr,
encode,
background, fill, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade,
metadata, source, sourceLayer, minzoom, maxzoom, filter, visible,
${Object.values(docs).map(d => d.join(', ')).join(',\n ')})
{-|
Layers specify what is actually rendered on the map and are rendered in order.
Except for layers of the background type, each layer needs to refer to a source. Layers take the data that they get from a source, optionally filter features, and then define how those features are styled.
There are two kinds of properties: *Layout* and *Paint* properties.
Layout properties are applied early in the rendering process and define how data for that layer is passed to the GPU. Changes to a layout property require an asynchronous "layout" step.
Paint properties are applied later in the rendering process. Changes to a paint property are cheap and happen synchronously.
### Working with layers
@docs Layer, SourceId, encode
### Layer Types
@docs background, fill, symbol, line, raster, circle, fillExtrusion, heatmap, hillshade
@docs Background, Fill, Symbol, Line, Raster, Circle, FillExtrusion, Heatmap, Hillshade
### General Attributes
@docs LayerAttr
@docs metadata, source, sourceLayer, minzoom, maxzoom, filter, visible
${Object.entries(docs).map(([section, docs]) => `### ${section} Attributes\n\n@docs ${docs.join(', ')}`).join('\n\n')}
-}
import Array exposing (Array)
import Json.Decode
import Json.Encode as Encode exposing (Value)
import Mapbox.Expression as Expression exposing (Anchor, CameraExpression, Color, DataExpression, Expression, LineJoin)
{-| Represents a layer. -}
type Layer
= Layer Value
{-| All layers (except background layers) need a source -}
type alias SourceId = String
{-| -}
type Background
= BackgroundLayer
{-| -}
type Fill
= FillLayer
{-| -}
type Symbol
= SymbolLayer
{-| -}
type Line
= LineLayer
{-| -}
type Raster
= RasterLayer
{-| -}
type Circle
= CircleLayer
{-| -}
type FillExtrusion
= FillExtrusionLayer
{-| -}
type Heatmap
= HeatmapLayer
{-| -}
type Hillshade
= HillshadeLayer
{-| Turns a layer into JSON -}
encode : Layer -> Value
encode (Layer value) =
value
layerImpl tipe source id attrs =
[ ( "type", Encode.string tipe )
, ( "id", Encode.string id )
, ( "source", Encode.string source)
]
++ encodeAttrs attrs
|> Encode.object
|> Layer
encodeAttrs attrs =
let
{ top, layout, paint } =
List.foldl
(\\attr ({ top, layout, paint } as lists) ->
case attr of
Top key val ->
{ lists | top = ( key, val ) :: top }
Paint key val ->
{ lists | paint = ( key, val ) :: paint }
Layout key val ->
{ lists | layout = ( key, val ) :: layout }
)
{ top = [], layout = [], paint = [] }
attrs
in
( "layout", Encode.object layout ) :: ( "paint", Encode.object paint ) :: top
{-| The background color or pattern of the map. -}
background : String -> List (LayerAttr Background) -> Layer
background tipe id attrs =
[ ( "type", Encode.string "background" )
, ( "id", Encode.string id )
]
++ encodeAttrs attrs
|> Encode.object
|> Layer
{-| A filled polygon with an optional stroked border. -}
fill : String -> SourceId -> List (LayerAttr Fill) -> Layer
fill =
layerImpl "fill"
{-| A stroked line. -}
line : String -> SourceId -> List (LayerAttr Line) -> Layer
line =
layerImpl "line"
{-| An icon or a text label. -}
symbol : String -> SourceId -> List (LayerAttr Symbol) -> Layer
symbol =
layerImpl "symbol"
{-| Raster map textures such as satellite imagery. -}
raster : String -> SourceId -> List (LayerAttr Raster) -> Layer
raster =
layerImpl "raster"
{-| A filled circle. -}
circle : String -> SourceId -> List (LayerAttr Circle) -> Layer
circle =
layerImpl "circle"
{-| An extruded (3D) polygon. -}
fillExtrusion : String -> SourceId -> List (LayerAttr FillExtrusion) -> Layer
fillExtrusion =
layerImpl "fill-extrusion"
{-| A heatmap. -}
heatmap : String -> SourceId -> List (LayerAttr Heatmap) -> Layer
heatmap =
layerImpl "heatmap"
{-| Client-side hillshading visualization based on DEM data. Currently, the implementation only supports Mapbox Terrain RGB and Mapzen Terrarium tiles. -}
hillshade : String -> SourceId -> List (LayerAttr Hillshade) -> Layer
hillshade =
layerImpl "hillshade"
{-| -}
type LayerAttr tipe
= Top String Value
| Paint String Value
| Layout String Value
-- General Attributes
{-| Arbitrary properties useful to track with the layer, but do not influence rendering. Properties should be prefixed to avoid collisions, like 'mapbox:'. -}
metadata : Value -> LayerAttr all
metadata =
Top "metadata"
{-| Layer to use from a vector tile source. Required for vector tile sources; prohibited for all other source types, including GeoJSON sources. -}
sourceLayer : String -> LayerAttr all
sourceLayer =
Encode.string >> Top "source-layer"
{-| The minimum zoom level for the layer. At zoom levels less than the minzoom, the layer will be hidden. A number between 0 and 24 inclusive. -}
minzoom : Float -> LayerAttr all
minzoom =
Encode.float >> Top "minzoom"
{-| The maximum zoom level for the layer. At zoom levels equal to or greater than the maxzoom, the layer will be hidden. A number between 0 and 24 inclusive. -}
maxzoom : Float -> LayerAttr all
maxzoom =
Encode.float >> Top "maxzoom"
{-| A expression specifying conditions on source features. Only features that match the filter are displayed. -}
filter : Expression any Bool -> LayerAttr all
filter =
Expression.encode >> Top "filter"
{-| Whether this layer is displayed. -}
visible : Expression CameraExpression Bool -> LayerAttr any
visible vis =
Layout "visibility" <| Expression.encode <| Expression.ifElse vis (Expression.str "visible") (Expression.str "none")
${Object.entries(codes).map(([section, codes]) => `-- ${section}\n\n${codes.join('\n')}`).join('\n\n')}
`
}
function requires(req) {
if (typeof req === 'string') {
return `Requires \`${camelCase(req)}\`.`;
} else if (req['!']) {
return `Disabled by \`${camelCase(req['!'])}\`.`;
} else if (req['<=']) {
return `Must be less than or equal to \`${camelCase(req['<='])}\`.`;
} else {
const [name, value] = Object.entries(req)[0];
if (Array.isArray(value)) {
return `Requires \`${camelCase(name)}\` to be ${
value
.reduce((prev, curr) => [prev, ', or ', curr])}.`;
} else {
return `Requires \`${camelCase(name)}\` to be \`${value}\`.`;
}
}
}
function generateElmProperty(name, prop, layerType, position) {
if (name == 'visibility') return ''
if (prop['property-type'] === 'constant') throw "Constant property type not supported";
const elmName = camelCase(name);
const exprKind = prop['sdk-support']['data-driven styling'] && prop['sdk-support']['data-driven styling'].js ? 'any' : 'CameraExpression';
const exprType = getElmType(prop);
let bounds = '';
if ('minimum' in prop && 'maximum' in prop) {
bounds = `\n\nShould be between \`${prop.minimum}\` and \`${prop.maximum}\` inclusive. `
} else if ('minimum' in prop) {
bounds = `\n\nShould be greater than or equal to \`${prop.minimum}\`. `
} else if ('maximum' in prop) {
bounds = `\n\nShould be less than or equal to \`${prop.maximum}\`. `
}
return `
{-| ${prop.doc.replace(/`(\w+\-.+?)`/g, str => '`' + camelCase(str.substr(1)))} ${position} property. ${bounds}${prop.units ? `\nUnits in ${prop.units}. ` : ''}${prop.default !== undefined ? 'Defaults to `' + prop.default + '`. ' : ''}${prop.requires ? prop.requires.map(requires).join(' ') : ''}
-}
${elmName} : Expression ${exprKind} ${exprType} -> LayerAttr ${layerType}
${elmName} =
Expression.encode >> ${position} "${name}"`
}
function getElmType({type, value, values}) {
switch(type) {
case 'number':
return 'Float';
case 'boolean':
return "Bool";
case 'string':
return 'String';
case 'color':
return 'Color';
case 'array':
switch(value) {
case 'number':
return '(Array Float)';
case 'string':
return '(Array String)';
}
case 'enum':
switch(Object.keys(values).join(' | ')) {
case "map | viewport":
return 'Anchor';
case "map | viewport | auto":
return 'AnchorAuto';
case "center | left | right | top | bottom | top-left | top-right | bottom-left | bottom-right":
return 'Position';
case 'none | width | height | both':
return 'TextFit';
case 'butt | round | square':
return 'LineCap';
case 'bevel | round | miter':
return 'LineJoin';
case 'point | line':
return 'SymbolPlacement';
case 'left | center | right':
return 'TextJustify';
case 'none | uppercase | lowercase':
return 'TextTransform';
}
}
throw `Unknown type ${type}`
}
function titleCase(str) {
return str.replace(/\-/, ' ').replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
).replace(/\s/, '');
}
function camelCase(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\-\w)/g, function(letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/(?:\s|\-)+/g, '');
}
function makeSignatures(name, constants) {
return `{-| -}
type ${name} = ${name}
${constants.split(' | ').map(c => `
{-| -}
${camelCase(name + ' ' + c)} : Expression exprType ${name}
${camelCase(name + ' ' + c)} = Expression (Json.Encode.string "${c}")
`).join('\n')}`
}
|