Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changeset/funny-hounds-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@navigraph/app": patch
"@navigraph/charts": minor
---

Add `calculateChartBounds` utility
7 changes: 7 additions & 0 deletions packages/app/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ export class DeviceFlowTokenExpiredError extends Error {
this.name = "DeviceFlowTokenExpiredError";
}
}

export class NonGeoreferencedChartError extends Error {
constructor(indexNumber: string) {
super(`Could not calculate bounds for ${indexNumber || "a chart"} since it is not georeferenced`);
this.name = "NonGeoreferencedChartError";
}
}
1 change: 1 addition & 0 deletions packages/charts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./api";
export * from "./public-types";
export * from "./lib";
50 changes: 50 additions & 0 deletions packages/charts/src/lib/calculateChartBounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { NonGeoreferencedChartError } from "@navigraph/app";
import { Chart } from "src/public-types";

type LngLat = {
lng: number;
lat: number;
};

interface Bounds {
/** The coordinates of the south-west corner of the chart */
sw: LngLat;
/** The coordinates of the north-east corner of the chart */
ne: LngLat;
}

/** Calculates the bounding box of the georeferenced part of a given chart. */
export default function calculateChartBounds(chart: Chart): Bounds {
if (!chart.is_georeferenced) {
throw new NonGeoreferencedChartError(chart.index_number);
}

const planview = chart.bounding_boxes.planview;
const { height, width } = chart;

const longDegreesPerPixel =
Math.abs(planview.latlng.lng2 - planview.latlng.lng1) / (planview.pixels.x2 - planview.pixels.x1);
const latDegreesPerPixel =
Math.abs(planview.latlng.lat2 - planview.latlng.lat1) / (planview.pixels.y1 - planview.pixels.y2);

const dYLowerDegrees = (height - planview.pixels.y1) * latDegreesPerPixel;
const dYUpperDegrees = planview.pixels.y2 * latDegreesPerPixel;

const dXLeftHandDegrees = planview.pixels.x1 * longDegreesPerPixel;
const dXRightHandDegrees = Math.abs(width - planview.pixels.x2) * longDegreesPerPixel;

const sw = {
lng: planview.latlng.lng1 - dXLeftHandDegrees,
lat: planview.latlng.lat1 - dYLowerDegrees,
};

const ne = {
lng: planview.latlng.lng2 + dXRightHandDegrees,
lat: planview.latlng.lat2 + dYUpperDegrees,
};

return {
sw,
ne,
};
}
1 change: 1 addition & 0 deletions packages/charts/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as calculateChartBounds } from "./calculateChartBounds";