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() {
this.renderer.draw(this.center, this.zoom).then((frame) => {
this.renderer?.draw(this.center, this.zoom).then((frame) => {
this._write(frame);
this.notify(this._getFooter());
}).catch(() => {

View File

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

View File

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

View File

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