diff --git a/src/default_style/style.ts b/src/default_style/style.ts index 0481faa2..2fead04d 100644 --- a/src/default_style/style.ts +++ b/src/default_style/style.ts @@ -63,10 +63,11 @@ const doShading = (params: DefaultStyleParams, shade: string) => { }; export const paintRules = ( - params: DefaultStyleParams, + originalParams: DefaultStyleParams, shade?: string, ): Rule[] => { - if (shade) params = doShading(params, shade); + let params = originalParams; + if (shade) params = doShading(originalParams, shade); return [ { dataLayer: "earth", @@ -317,12 +318,13 @@ export const paintRules = ( }; export const labelRules = ( - params: DefaultStyleParams, + originalParams: DefaultStyleParams, shade?: string, language1?: string[], language2?: string[], ): LabelRule[] => { - if (shade) params = doShading(params, shade); + let params = originalParams; + if (shade) params = doShading(originalParams, shade); let nametags = ["name"]; if (language1) nametags = language1; diff --git a/src/extra_styles/toner.ts b/src/extra_styles/toner.ts index b4594cd1..bdcd41dd 100644 --- a/src/extra_styles/toner.ts +++ b/src/extra_styles/toner.ts @@ -243,7 +243,7 @@ const Toner = (variant: string) => { fontFamily: "Inter", fontWeight: 600, fontSize: (z, f) => { - if (f!.props["pmap:rank"] === 1) return 15; + if (f?.props["pmap:rank"] === 1) return 15; return 13; }, }), @@ -267,7 +267,7 @@ const Toner = (variant: string) => { fontFamily: "Inter", fontWeight: 600, fontSize: (z, f) => { - if (f!.props["pmap:rank"] === 1) return 15; + if (f?.props["pmap:rank"] === 1) return 15; return 13; }, }), diff --git a/src/labeler.ts b/src/labeler.ts index 7c8d0d43..bcb4c078 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -28,6 +28,8 @@ export interface IndexedLabel { deduplicationDistance?: number; } +type TreeItem = Bbox & { indexedLabel: IndexedLabel }; + export interface Layout { index: Index; order: number; @@ -80,7 +82,7 @@ export const covering = ( }; export class Index { - tree: RBush; + tree: RBush; current: Map>; dim: number; maxLabeledTiles: number; @@ -114,8 +116,8 @@ export class Index { public searchBbox(bbox: Bbox, order: number): Set { const labels = new Set(); for (const match of this.tree.search(bbox)) { - if (match.indexed_label.order <= order) { - labels.add(match.indexed_label); + if (match.indexedLabel.order <= order) { + labels.add(match.indexedLabel); } } return labels; @@ -125,8 +127,8 @@ export class Index { const labels = new Set(); for (const bbox of label.bboxes) { for (const match of this.tree.search(bbox)) { - if (match.indexed_label.order <= order) { - labels.add(match.indexed_label); + if (match.indexedLabel.order <= order) { + labels.add(match.indexedLabel); } } } @@ -135,7 +137,7 @@ export class Index { public bboxCollides(bbox: Bbox, order: number): boolean { for (const match of this.tree.search(bbox)) { - if (match.indexed_label.order <= order) return true; + if (match.indexedLabel.order <= order) return true; } return false; } @@ -143,7 +145,7 @@ export class Index { public labelCollides(label: Label, order: number): boolean { for (const bbox of label.bboxes) { for (const match of this.tree.search(bbox)) { - if (match.indexed_label.order <= order) return true; + if (match.indexedLabel.order <= order) return true; } } return false; @@ -161,8 +163,8 @@ export class Index { maxY: label.anchor.y + dist, }; for (const collision of this.tree.search(test_bbox)) { - if (collision.indexed_label.deduplicationKey === label.deduplicationKey) { - if (collision.indexed_label.anchor.dist(label.anchor) < dist) { + if (collision.indexedLabel.deduplicationKey === label.deduplicationKey) { + if (collision.indexedLabel.anchor.dist(label.anchor) < dist) { return true; } } @@ -180,7 +182,7 @@ export class Index { // can put in multiple due to antimeridian wrapping public insert(label: Label, order: number, tileKey: string): void { - const indexed_label = { + const indexedLabel = { anchor: label.anchor, bboxes: label.bboxes, draw: label.draw, @@ -195,15 +197,18 @@ export class Index { this.current.set(tileKey, newSet); entry = newSet; } - entry.add(indexed_label); + entry.add(indexedLabel); let wrapsLeft = false; let wrapsRight = false; for (const bbox of label.bboxes) { - const b: any = bbox; - b.indexed_label = indexed_label; - this.tree.insert(b); - + this.tree.insert({ + minX: bbox.minX, + minY: bbox.minY, + maxX: bbox.maxX, + maxY: bbox.maxY, + indexedLabel: indexedLabel, + }); if (bbox.minX < 0) wrapsLeft = true; if (bbox.maxX > this.dim) wrapsRight = true; } @@ -230,9 +235,13 @@ export class Index { const entry = this.current.get(tileKey); if (entry) entry.add(duplicate_label); for (const bbox of new_bboxes) { - const b: any = bbox; - b.indexed_label = duplicate_label; - this.tree.insert(b); + this.tree.insert({ + minX: bbox.minX, + minY: bbox.minY, + maxX: bbox.maxX, + maxY: bbox.maxY, + indexedLabel: duplicate_label, + }); } } } @@ -267,7 +276,7 @@ export class Index { if (!indexed_labels) return; // TODO: not that clean... const entries_to_delete = []; for (const entry of this.tree.all()) { - if (indexed_labels.has(entry.indexed_label)) { + if (indexed_labels.has(entry.indexedLabel)) { entries_to_delete.push(entry); } } @@ -284,7 +293,7 @@ export class Index { public removeLabel(labelToRemove: IndexedLabel): void { const entries_to_delete = []; for (const entry of this.tree.all()) { - if (labelToRemove === entry.indexed_label) { + if (labelToRemove === entry.indexedLabel) { entries_to_delete.push(entry); } } diff --git a/src/line.ts b/src/line.ts index b1565dde..8339b06e 100644 --- a/src/line.ts +++ b/src/line.ts @@ -15,9 +15,9 @@ const linelabel = ( targetLen: number, ): LabelableSegment[] => { const chunks = []; - let a; - let b; - let c; + let a: Point; + let b: Point; + let c: Point; let i = 0; let n = 0; let d = 0; @@ -91,13 +91,11 @@ export interface LabelCandidate { } export function simpleLabel( - mls: any, + mls: Point[][], minimum: number, repeatDistance: number, cellSize: number, ): LabelCandidate[] { - let longestStart; - let longestEnd; const longestLength = 0; const candidates = []; diff --git a/src/tilecache.ts b/src/tilecache.ts index d4229291..1f010dc7 100644 --- a/src/tilecache.ts +++ b/src/tilecache.ts @@ -50,6 +50,11 @@ export interface TileSource { get(c: Zxy, tileSize: number): Promise>; } +interface ZoomAbort { + z: number; + controller: AbortController; +} + // reimplement loadGeometry with a scalefactor // so the general tile rendering case does not need rescaling. const loadGeomAndBbox = (pbf: any, geometry: number, scale: number) => { @@ -126,7 +131,7 @@ function parseTile( export class PmtilesSource implements TileSource { p: PMTiles; - controllers: any[]; + zoomaborts: ZoomAbort[]; shouldCancelZooms: boolean; constructor(url: string | PMTiles, shouldCancelZooms: boolean) { @@ -135,22 +140,22 @@ export class PmtilesSource implements TileSource { } else { this.p = url; } - this.controllers = []; + this.zoomaborts = []; this.shouldCancelZooms = shouldCancelZooms; } public async get(c: Zxy, tileSize: number): Promise> { if (this.shouldCancelZooms) { - this.controllers = this.controllers.filter((cont) => { - if (cont[0] !== c.z) { - cont[1].abort(); + this.zoomaborts = this.zoomaborts.filter((za) => { + if (za.z !== c.z) { + za.controller.abort(); return false; } return true; }); } const controller = new AbortController(); - this.controllers.push([c.z, controller]); + this.zoomaborts.push({ z: c.z, controller: controller }); const signal = controller.signal; const result = await this.p.getZxy(c.z, c.x, c.y, signal); @@ -164,20 +169,20 @@ export class PmtilesSource implements TileSource { export class ZxySource implements TileSource { url: string; - controllers: any[]; + zoomaborts: ZoomAbort[]; shouldCancelZooms: boolean; constructor(url: string, shouldCancelZooms: boolean) { this.url = url; - this.controllers = []; + this.zoomaborts = []; this.shouldCancelZooms = shouldCancelZooms; } public async get(c: Zxy, tileSize: number): Promise> { if (this.shouldCancelZooms) { - this.controllers = this.controllers.filter((cont) => { - if (cont[0] !== c.z) { - cont[1].abort(); + this.zoomaborts = this.zoomaborts.filter((za) => { + if (za.z !== c.z) { + za.controller.abort(); return false; } return true; @@ -188,7 +193,7 @@ export class ZxySource implements TileSource { .replace("{x}", c.x.toString()) .replace("{y}", c.y.toString()); const controller = new AbortController(); - this.controllers.push([c.z, controller]); + this.zoomaborts.push({ z: c.z, controller: controller }); const signal = controller.signal; return new Promise((resolve, reject) => { fetch(url, { signal: signal }) diff --git a/src/view.ts b/src/view.ts index 7953b5e9..5eb8a7c9 100644 --- a/src/view.ts +++ b/src/view.ts @@ -52,8 +52,8 @@ export const transformGeom = ( export const wrap = (val: number, z: number) => { const dim = 1 << z; - if (val < 0) val = dim + val; - if (val >= dim) val = val % dim; + if (val < 0) return dim + val; + if (val >= dim) return val % dim; return val; }; diff --git a/test/labeler.test.ts b/test/labeler.test.ts index 75004612..fd591af3 100644 --- a/test/labeler.test.ts +++ b/test/labeler.test.ts @@ -165,7 +165,7 @@ test("remove an individual label", async () => { ); assert.equal(index.tree.all().length, 1); assert.equal(index.current.get("abcd").size, 1); - const the_label = index.tree.all()[0].indexed_label; + const the_label = index.tree.all()[0].indexedLabel; index.removeLabel(the_label); assert.equal(index.current.size, 1); assert.equal(index.current.get("abcd").size, 0);