Merge pull request #56 from rastapasta/promises

Load the style file in Mapscii to keep the Renderer simpler
This commit is contained in:
Christian Paul 2019-03-24 18:00:57 -07:00 committed by GitHub
commit 044c24f6c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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');
@ -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();

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