Skip to content

Commit

Permalink
internal typing improvements [#112] (#126)
Browse files Browse the repository at this point in the history
Refactors for code quality and removing `any` types.
  • Loading branch information
bdon authored Jan 24, 2024
1 parent bc6e40c commit 14f77c3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 47 deletions.
10 changes: 6 additions & 4 deletions src/default_style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/extra_styles/toner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
}),
Expand All @@ -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;
},
}),
Expand Down
49 changes: 29 additions & 20 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface IndexedLabel {
deduplicationDistance?: number;
}

type TreeItem = Bbox & { indexedLabel: IndexedLabel };

export interface Layout {
index: Index;
order: number;
Expand Down Expand Up @@ -80,7 +82,7 @@ export const covering = (
};

export class Index {
tree: RBush<any>;
tree: RBush<TreeItem>;
current: Map<string, Set<IndexedLabel>>;
dim: number;
maxLabeledTiles: number;
Expand Down Expand Up @@ -114,8 +116,8 @@ export class Index {
public searchBbox(bbox: Bbox, order: number): Set<IndexedLabel> {
const labels = new Set<IndexedLabel>();
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;
Expand All @@ -125,8 +127,8 @@ export class Index {
const labels = new Set<IndexedLabel>();
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);
}
}
}
Expand All @@ -135,15 +137,15 @@ 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;
}

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;
Expand All @@ -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;
}
}
Expand All @@ -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,
Expand All @@ -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;
}
Expand All @@ -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,
});
}
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
29 changes: 17 additions & 12 deletions src/tilecache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface TileSource {
get(c: Zxy, tileSize: number): Promise<Map<string, Feature[]>>;
}

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) => {
Expand Down Expand Up @@ -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) {
Expand All @@ -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<Map<string, Feature[]>> {
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);
Expand All @@ -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<Map<string, Feature[]>> {
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;
Expand All @@ -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 })
Expand Down
4 changes: 2 additions & 2 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion test/labeler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 14f77c3

Please sign in to comment.