Skip to content

Commit

Permalink
Bring in TS logic from ATIP to split routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed May 20, 2024
1 parent 0b6a09f commit 1fbff7f
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 14 deletions.
227 changes: 225 additions & 2 deletions route-snapper-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions route-snapper-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "route-snapper-ts",
"description": "Draw routes in MapLibre snapped to a street network using client-side routing. TypeScript bindings",
"version": "0.0.3",
"version": "0.0.4",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand All @@ -25,6 +25,11 @@
},
"dependencies": {
"maplibre-gl": "^4.0.0",
"route-snapper": "^0.4.0"
"route-snapper": "^0.4.0",
"@turf/helpers": "^6.5.0",
"@turf/length": "^6.5.0",
"@turf/line-slice": "^6.5.0",
"@turf/line-split": "^6.5.0",
"@turf/nearest-point-on-line": "^6.5.0"
}
}
41 changes: 31 additions & 10 deletions route-snapper-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ interface Writable<T> {
set(value: T): void;
}

// TODO This isn't complete
export interface Props {
export interface RouteProps {
waypoints: Waypoint[];
length_meters: number;
route_name: string;
}

export interface AreaProps {
waypoints: Waypoint[];
}

Expand All @@ -25,8 +30,12 @@ export class RouteTool {
map: Map;
inner: JsRouteSnapper;
active: boolean;
eventListenersSuccess: ((f: Feature<LineString | Polygon, Props>) => void)[];
eventListenersUpdated: ((f: Feature<LineString | Polygon, Props>) => void)[];
eventListenersSuccess: ((
f: Feature<LineString, RouteProps> | Feature<Polygon, AreaProps>,
) => void)[];
eventListenersUpdated: ((
f: Feature<LineString, RouteProps> | Feature<Polygon, AreaProps>,
) => void)[];
eventListenersFailure: (() => void)[];

routeToolGj: Writable<GeoJSON>;
Expand Down Expand Up @@ -206,7 +215,7 @@ export class RouteTool {
// properties returned originally. If waypoints are missing (maybe because
// the route was produced by a different tool, or an older version of this
// tool), the edited line-string may differ from the input.
editExistingRoute(feature: Feature<LineString, Props>) {
editExistingRoute(feature: Feature<LineString, RouteProps>) {
if (this.active) {
window.alert("Bug: editExistingRoute called when tool is already active");
}
Expand Down Expand Up @@ -238,7 +247,7 @@ export class RouteTool {
}

// This only handles features previously returned by this tool.
editExistingArea(feature: Feature<Polygon, Props>) {
editExistingArea(feature: Feature<Polygon, AreaProps>) {
if (this.active) {
window.alert("Bug: editExistingArea called when tool is already active");
}
Expand All @@ -255,12 +264,16 @@ export class RouteTool {
}

addEventListenerSuccess(
callback: (f: Feature<LineString | Polygon, Props>) => void,
callback: (
f: Feature<LineString, RouteProps> | Feature<Polygon, AreaProps>,
) => void,
) {
this.eventListenersSuccess.push(callback);
}
addEventListenerUpdated(
callback: (f: Feature<LineString | Polygon, Props>) => void,
callback: (
f: Feature<LineString, RouteProps> | Feature<Polygon, AreaProps>,
) => void,
) {
this.eventListenersUpdated.push(callback);
}
Expand All @@ -283,7 +296,11 @@ export class RouteTool {
if (rawJSON) {
// Pass copies to each callback
for (let cb of this.eventListenersSuccess) {
cb(JSON.parse(rawJSON) as Feature<LineString | Polygon, Props>);
cb(
JSON.parse(rawJSON) as
| Feature<LineString, RouteProps>
| Feature<Polygon, AreaProps>,
);
}
} else {
for (let cb of this.eventListenersFailure) {
Expand Down Expand Up @@ -335,7 +352,11 @@ export class RouteTool {
if (rawJSON) {
// Pass copies to each callback
for (let cb of this.eventListenersUpdated) {
cb(JSON.parse(rawJSON) as Feature<LineString | Polygon, Props>);
cb(
JSON.parse(rawJSON) as
| Feature<LineString, RouteProps>
| Feature<Polygon, AreaProps>,
);
}
}
}
Expand Down
Loading

0 comments on commit 1fbff7f

Please sign in to comment.