diff --git a/src/Mapscii.js b/src/Mapscii.js index e9be433..3d9b995 100644 --- a/src/Mapscii.js +++ b/src/Mapscii.js @@ -5,6 +5,7 @@ UI and central command center */ 'use strict'; +const fs = require('fs'); const keypress = require('keypress'); const TermMouse = require('term-mouse'); @@ -83,8 +84,8 @@ class Mapscii { } _initRenderer() { - this.renderer = new Renderer(config.output, this.tileSource); - this.renderer.loadStyleFile(config.styleFile); + const style = JSON.parse(fs.readFileSync(config.styleFile, 'utf8')); + this.renderer = new Renderer(config.output, this.tileSource, style); config.output.on('resize', () => { this._resizeRenderer(); diff --git a/src/Renderer.js b/src/Renderer.js index 47c0125..2747bef 100644 --- a/src/Renderer.js +++ b/src/Renderer.js @@ -16,14 +16,11 @@ const utils = require('./utils'); const config = require('./config'); class Renderer { - constructor(output, tileSource) { + constructor(output, tileSource, style) { this.output = output; this.tileSource = tileSource; this.labelBuffer = new LabelBuffer(); - } - - loadStyleFile(file) { - this.styler = new Styler(file); + this.styler = new Styler(style); this.tileSource.useStyler(this.styler); } diff --git a/src/Styler.js b/src/Styler.js index 3ca4d74..cce1647 100644 --- a/src/Styler.js +++ b/src/Styler.js @@ -10,36 +10,33 @@ */ 'use strict'; -const fs = require('fs'); - class Styler { - constructor(file) { + constructor(style) { this.styleById = {}; this.styleByLayer = {}; var base, name; - const json = JSON.parse(fs.readFileSync(file).toString()); - this.styleName = json.name; - if (json.constants) { - this._replaceConstants(json.constants, json.layers); + this.styleName = style.name; + if (style.constants) { + this._replaceConstants(style.constants, style.layers); } - for (const style of json.layers) { - if (style.ref && this.styleById[style.ref]) { + for (const layer of style.layers) { + if (layer.ref && this.styleById[layer.ref]) { for (const ref of ['type', 'source-layer', 'minzoom', 'maxzoom', 'filter']) { - if (this.styleById[style.ref][ref] && !style[ref]) { - style[ref] = this.styleById[style.ref][ref]; + if (this.styleById[layer.ref][ref] && !layer[ref]) { + layer[ref] = this.styleById[layer.ref][ref]; } } } - style.appliesTo = this._compileFilter(style.filter); + layer.appliesTo = this._compileFilter(layer.filter); //TODO Better translation of: @styleByLayer[style['source-layer']] ?= [] - if ((base = this.styleByLayer)[name = style['source-layer']] == null) { + if ((base = this.styleByLayer)[name = layer['source-layer']] == null) { base[name] = []; } - this.styleByLayer[style['source-layer']].push(style); - this.styleById[style.id] = style; + this.styleByLayer[layer['source-layer']].push(layer); + this.styleById[layer.id] = layer; } }