More types

This commit is contained in:
Christian Paul 2024-11-03 11:06:19 +01:00
parent e18d1214d8
commit c55157878f
4 changed files with 33 additions and 21 deletions

View File

@ -270,7 +270,7 @@ class Mapscii {
} }
_draw() { _draw() {
this.renderer.draw(this.center, this.zoom).then((frame) => { this.renderer?.draw(this.center, this.zoom).then((frame) => {
this._write(frame); this._write(frame);
this.notify(this._getFooter()); this.notify(this._getFooter());
}).catch(() => { }).catch(() => {

View File

@ -4,8 +4,9 @@
The Console Vector Tile renderer - bäm! The Console Vector Tile renderer - bäm!
*/ */
import x256 from 'x256'; import RBush from 'rbush';
import simplify from 'simplify-js'; import simplify from 'simplify-js';
import x256 from 'x256';
import Canvas from './Canvas.ts'; import Canvas from './Canvas.ts';
import LabelBuffer from './LabelBuffer.ts'; import LabelBuffer from './LabelBuffer.ts';
@ -15,7 +16,14 @@ import config from './config.ts';
import TileSource from './TileSource.ts'; import TileSource from './TileSource.ts';
import Tile from './Tile.ts'; import Tile from './Tile.ts';
export type Feature = unknown; export type Layer = {
extent: unknown,
tree: RBush,
};
export type Layers = Record<string, Layer>;
export type Feature = {
properties: Record<string, unknown>,
};
class Renderer { class Renderer {
private canvas: Canvas | undefined; private canvas: Canvas | undefined;
@ -51,7 +59,7 @@ class Renderer {
this.canvas = new Canvas(width, height); this.canvas = new Canvas(width, height);
} }
async draw(center, zoom: number) { async draw(center, zoom: number): Promise<string> {
if (this.isDrawing) return Promise.reject(); if (this.isDrawing) return Promise.reject();
this.isDrawing = true; this.isDrawing = true;
@ -65,10 +73,10 @@ class Renderer {
void 0 void 0
); );
if (color) { if (color) {
this.canvas.setBackground(x256(utils.hex2rgb(color))); this.canvas?.setBackground(x256(utils.hex2rgb(color)));
} }
this.canvas.clear(); this.canvas?.clear();
try { try {
let tiles = this._visibleTiles(center, zoom); let tiles = this._visibleTiles(center, zoom);

View File

@ -9,6 +9,8 @@
anonymous functions to improve rendering speed compared to realtime parsing. anonymous functions to improve rendering speed compared to realtime parsing.
*/ */
import { Feature } from "./Renderer.ts";
class Styler { class Styler {
public styleById: Record<string, Record<string, unknown>>; public styleById: Record<string, Record<string, unknown>>;
public styleByLayer: Record<string, unknown[]>; public styleByLayer: Record<string, unknown[]>;
@ -43,7 +45,7 @@ class Styler {
} }
} }
getStyleFor(layer, feature) { getStyleFor(layer, feature): unknown | false {
if (!this.styleByLayer[layer]) { if (!this.styleByLayer[layer]) {
return false; return false;
} }
@ -57,7 +59,7 @@ class Styler {
return false; return false;
} }
_replaceConstants(constants, tree) { _replaceConstants(constants, tree: RBush): void {
for (const id in tree) { for (const id in tree) {
const node = tree[id]; const node = tree[id];
switch (typeof node) { switch (typeof node) {
@ -76,7 +78,7 @@ class Styler {
} }
//TODO Better translation of the long cases. //TODO Better translation of the long cases.
_compileFilter(filter) { _compileFilter(filter): (feature: Feature) => boolean {
let filters; let filters;
switch (filter != null ? filter[0] : void 0) { switch (filter != null ? filter[0] : void 0) {
case 'all': case 'all':

View File

@ -14,12 +14,14 @@ import x256 from 'x256';
import config from './config.ts'; import config from './config.ts';
import utils from './utils.ts'; import utils from './utils.ts';
import Styler from './Styler.ts'; import Styler from './Styler.ts';
import { Feature, Layers } from './Renderer.ts';
class Tile { class Tile {
public layers: any[]; public layers: Layers;
private styler: Styler; private styler: Styler;
private tile: VectorTile; private tile: VectorTile;
public size: number;
public position: { public position: {
x: number, x: number,
y: number, y: number,
@ -35,7 +37,7 @@ class Tile {
this.styler = styler; this.styler = styler;
} }
load(buffer) { load(buffer: Buffer) {
return this._unzipIfNeeded(buffer).then((buffer) => { return this._unzipIfNeeded(buffer).then((buffer) => {
return this._loadTile(buffer); return this._loadTile(buffer);
}).then(() => { }).then(() => {
@ -45,11 +47,11 @@ class Tile {
}); });
} }
private _loadTile(buffer) { private _loadTile(buffer: Buffer) {
this.tile = new VectorTile(new Protobuf(buffer)); this.tile = new VectorTile(new Protobuf(buffer));
} }
private _unzipIfNeeded(buffer) { private _unzipIfNeeded(buffer: Buffer): Promise<Buffer> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this._isGzipped(buffer)) { if (this._isGzipped(buffer)) {
zlib.gunzip(buffer, (err, data) => { zlib.gunzip(buffer, (err, data) => {
@ -64,13 +66,13 @@ class Tile {
}); });
} }
private _isGzipped(buffer) { private _isGzipped(buffer): boolean {
return buffer.slice(0, 2).indexOf(Buffer.from([0x1f, 0x8b])) === 0; return buffer.slice(0, 2).indexOf(Buffer.from([0x1f, 0x8b])) === 0;
} }
private _loadLayers() { private _loadLayers(): Layers {
const layers = {}; const layers: Layers = {};
const colorCache = {}; const colorCache: Record<string, string> = {};
for (const name in this.tile.layers) { for (const name in this.tile.layers) {
const layer = this.tile.layers[name]; const layer = this.tile.layers[name];
const nodes = []; const nodes = [];
@ -137,7 +139,7 @@ class Tile {
return this.layers = layers; return this.layers = layers;
} }
private _addBoundaries(deep, data) { private _addBoundaries(deep: boolean, data) {
let minX = 2e308; let minX = 2e308;
let maxX = -2e308; let maxX = -2e308;
let minY = 2e308; let minY = 2e308;
@ -156,11 +158,11 @@ class Tile {
return data; return data;
} }
private _reduceGeometry(feature, factor) { private _reduceGeometry(feature: Feature, factor: number): {x: number, y: number}[][] {
const results = []; const results: {x: number, y: number}[][] = [];
const geometries = feature.loadGeometry(); const geometries = feature.loadGeometry();
for (const points of geometries) { for (const points of geometries) {
const reduced = []; const reduced: {x: number, y: number}[] = [];
let last; let last;
for (const point of points) { for (const point of points) {
const p = { const p = {