Load the style file in Mapscii.js

This commit is contained in:
Christian Paul 2018-11-19 00:13:00 -08:00
parent 4b97538235
commit 0ba7a7201d
3 changed files with 17 additions and 22 deletions

View File

@ -5,6 +5,7 @@
UI and central command center UI and central command center
*/ */
'use strict'; 'use strict';
const fs = require('fs');
const keypress = require('keypress'); const keypress = require('keypress');
const TermMouse = require('term-mouse'); const TermMouse = require('term-mouse');
@ -86,8 +87,8 @@ class Mapscii {
} }
_initRenderer() { _initRenderer() {
this.renderer = new Renderer(config.output, this.tileSource); const style = JSON.parse(fs.readFileSync(config.styleFile, 'utf8'));
this.renderer.loadStyleFile(config.styleFile); this.renderer = new Renderer(config.output, this.tileSource, style);
config.output.on('resize', () => { config.output.on('resize', () => {
this._resizeRenderer(); this._resizeRenderer();

View File

@ -16,14 +16,11 @@ const utils = require('./utils');
const config = require('./config'); const config = require('./config');
class Renderer { class Renderer {
constructor(output, tileSource) { constructor(output, tileSource, style) {
this.output = output; this.output = output;
this.tileSource = tileSource; this.tileSource = tileSource;
this.labelBuffer = new LabelBuffer(); this.labelBuffer = new LabelBuffer();
} this.styler = new Styler(style);
loadStyleFile(file) {
this.styler = new Styler(file);
this.tileSource.useStyler(this.styler); this.tileSource.useStyler(this.styler);
} }

View File

@ -10,36 +10,33 @@
*/ */
'use strict'; 'use strict';
const fs = require('fs');
class Styler { class Styler {
constructor(file) { constructor(style) {
this.styleById = {}; this.styleById = {};
this.styleByLayer = {}; this.styleByLayer = {};
var base, name; var base, name;
const json = JSON.parse(fs.readFileSync(file).toString()); this.styleName = style.name;
this.styleName = json.name; if (style.constants) {
if (json.constants) { this._replaceConstants(style.constants, style.layers);
this._replaceConstants(json.constants, json.layers);
} }
for (const style of json.layers) { for (const layer of style.layers) {
if (style.ref && this.styleById[style.ref]) { if (layer.ref && this.styleById[layer.ref]) {
for (const ref of ['type', 'source-layer', 'minzoom', 'maxzoom', 'filter']) { for (const ref of ['type', 'source-layer', 'minzoom', 'maxzoom', 'filter']) {
if (this.styleById[style.ref][ref] && !style[ref]) { if (this.styleById[layer.ref][ref] && !layer[ref]) {
style[ref] = this.styleById[style.ref][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']] ?= [] //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] = []; base[name] = [];
} }
this.styleByLayer[style['source-layer']].push(style); this.styleByLayer[layer['source-layer']].push(layer);
this.styleById[style.id] = style; this.styleById[layer.id] = layer;
} }
} }