Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Adds CurvedLineSymbolizer #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Label {
draw: (ctx: any, drawExtra?: DrawExtra) => void;
deduplicationKey?: string;
deduplicationDistance?: number;
allowCollisions?: boolean;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to avoid adding these two keys? at the unit of a single Label, a Label's multiple bounding boxes are allowed to collide already - this happens with line label cells. So if a curved label can be expressed as one Label, this check shouldn't be necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into it. I added that because it was removing parts of the text

collisionKey?: string;
}

export interface IndexedLabel {
Expand Down Expand Up @@ -135,7 +137,14 @@ export class Index {
public labelCollides(label: Label, order: number): boolean {
for (let bbox of label.bboxes) {
for (let match of this.tree.search(bbox)) {
if (match.indexed_label.order <= order) return true;
if (match.indexed_label.order <= order) {
if (
!!label.allowCollisions &&
match.collisionKey === label.collisionKey
)
return false;
return true;
}
}
}
return false;
Expand Down Expand Up @@ -187,6 +196,7 @@ export class Index {
for (let bbox of label.bboxes) {
var b: any = bbox;
b.indexed_label = indexed_label;
if (label.collisionKey) b.collisionKey = label.collisionKey;
this.tree.insert(b);

if (bbox.minX < 0) wrapsLeft = true;
Expand Down
9 changes: 5 additions & 4 deletions src/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ export function simpleLabel(
mls: any,
minimum: number,
repeatDistance: number,
cellSize: number
cellSize: number,
overflowAllowed = false
): LabelCandidate[] {
let longestStart;
let longestEnd;
Expand All @@ -111,7 +112,7 @@ export function simpleLabel(
for (let ls of mls) {
let segments = linelabel(ls, Math.PI / 90); // 2 degrees, close to a straight line
for (let segment of segments) {
if (segment.length >= minimum + cellSize) {
if (segment.length >= minimum + cellSize || overflowAllowed) {
let start = new Point(
ls[segment.beginIndex].x,
ls[segment.beginIndex].y
Expand All @@ -125,8 +126,8 @@ export function simpleLabel(
// offset from the start by cellSize to allow streets that meet at right angles
// to both be labeled.
for (
var i = cellSize;
i < segment.length - minimum;
var i = overflowAllowed ? 0 : cellSize;
i < segment.length - (overflowAllowed ? 0 : minimum);
i += repeatDistance
) {
candidates.push({
Expand Down
8 changes: 8 additions & 0 deletions src/maths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-ignore
import Point from "@mapbox/point-geometry";
export type Vector = Point;

export const normalize = (v: Vector): Vector => {
const vectorMagnitude = Math.hypot(v.x, v.y);
return { x: v.x / vectorMagnitude, y: v.y / vectorMagnitude };
};
Loading