mapscii/src/Styler.js

134 lines
3.9 KiB
JavaScript
Raw Normal View History

/*
termap - Terminal Map Viewer
by Michael Strassburger <codepoet@cpan.org>
Minimalistic parser and compiler for Mapbox (Studio) Map Style files
See: https://www.mapbox.com/mapbox-gl-style-spec/
Compiles layer filter instructions into a chain of true/false returning
anonymous functions to improve rendering speed compared to realtime parsing.
*/
2017-11-24 10:42:09 +01:00
'use strict';
2017-11-24 10:42:09 +01:00
class Styler {
2018-11-19 09:13:00 +01:00
constructor(style) {
this.styleById = {};
this.styleByLayer = {};
var base, name;
2018-11-19 09:13:00 +01:00
this.styleName = style.name;
if (style.constants) {
this._replaceConstants(style.constants, style.layers);
}
2018-11-19 09:13:00 +01:00
for (const layer of style.layers) {
if (layer.ref && this.styleById[layer.ref]) {
for (const ref of ['type', 'source-layer', 'minzoom', 'maxzoom', 'filter']) {
2018-11-19 09:13:00 +01:00
if (this.styleById[layer.ref][ref] && !layer[ref]) {
layer[ref] = this.styleById[layer.ref][ref];
}
}
}
2018-11-19 09:13:00 +01:00
layer.appliesTo = this._compileFilter(layer.filter);
//TODO Better translation of: @styleByLayer[style['source-layer']] ?= []
2018-11-19 09:13:00 +01:00
if ((base = this.styleByLayer)[name = layer['source-layer']] == null) {
base[name] = [];
}
2018-11-19 09:13:00 +01:00
this.styleByLayer[layer['source-layer']].push(layer);
this.styleById[layer.id] = layer;
}
}
2017-12-23 09:16:01 +01:00
getStyleFor(layer, feature) {
if (!this.styleByLayer[layer]) {
return false;
}
for (const style of this.styleByLayer[layer]) {
if (style.appliesTo(feature)) {
return style;
}
}
return false;
}
_replaceConstants(constants, tree) {
for (const id in tree) {
const node = tree[id];
switch (typeof node) {
case 'object':
if (node.constructor.name.match(/Stream/)) {
continue;
}
this._replaceConstants(constants, node);
break;
case 'string':
if (node.charAt(0) === '@') {
tree[id] = constants[node];
}
}
}
}
//TODO Better translation of the long cases.
_compileFilter(filter) {
let filters;
switch (filter != null ? filter[0] : void 0) {
case 'all':
filter = filter.slice(1);
filters = (() => {
return filter.map((sub) => this._compileFilter(sub));
}).call(this);
return (feature) => !!filters.find((appliesTo) => {
return !appliesTo(feature);
});
case 'any':
filter = filter.slice(1);
filters = (() => {
return filter.map((sub) => this._compileFilter(sub));
}).call(this);
return (feature) => !!filters.find((appliesTo) => {
return appliesTo(feature);
});
case 'none':
filter = filter.slice(1);
filters = (() => {
return filter.map((sub) => this._compileFilter(sub));
}).call(this);
return (feature) => !filters.find((appliesTo) => {
return !appliesTo(feature);
});
case '==':
return (feature) => feature.properties[filter[1]] === filter[2];
2017-12-23 09:07:42 +01:00
case '!=':
return (feature) => feature.properties[filter[1]] !== filter[2];
2017-12-23 09:07:42 +01:00
case 'in':
return (feature) => !!filter.slice(2).find((value) => {
return feature.properties[filter[1]] === value;
});
case '!in':
return (feature) => !filter.slice(2).find((value) => {
return feature.properties[filter[1]] === value;
});
2017-12-23 09:07:42 +01:00
case 'has':
return (feature) => !!feature.properties[filter[1]];
2017-12-23 09:07:42 +01:00
case '!has':
return (feature) => !feature.properties[filter[1]];
2017-12-23 09:07:42 +01:00
case '>':
return (feature) => feature.properties[filter[1]] > filter[2];
2017-12-23 09:07:42 +01:00
case '>=':
return (feature) => feature.properties[filter[1]] >= filter[2];
2017-12-23 09:07:42 +01:00
case '<':
return (feature) => feature.properties[filter[1]] < filter[2];
2017-12-23 09:07:42 +01:00
case '<=':
return (feature) => feature.properties[filter[1]] <= filter[2];
default:
return () => true;
}
}
}
2017-11-24 10:42:09 +01:00
module.exports = Styler;