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
*/
'use strict';
const fs = require('fs');
const keypress = require('keypress');
const TermMouse = require('term-mouse');
@ -86,8 +87,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();

View File

@ -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);
}

View File

@ -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;
}
}