Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 10x 3x 7x 1x 6x 5x 1x 60x 60x 58x 3x 55x 55x 60x 4x 4x 1x 2x 51x 3x 48x | import { Feature, Geometry, LineString, MultiLineString, Position } from "geojson";
export interface RouteGeometryOptions {
baseUrl: string;
signal?: AbortSignal;
}
function lineParts(geometry: Geometry): Position[][] {
if (geometry.type === "LineString") {
return [geometry.coordinates];
}
if (geometry.type === "MultiLineString") {
return geometry.coordinates;
}
if (geometry.type === "GeometryCollection") {
return geometry.geometries.flatMap(lineParts);
}
throw new Error(`Unsupported route geometry: ${geometry.type}`);
}
export async function getRouteGeometry(
routeId: string,
directionId: number,
{ baseUrl, signal }: RouteGeometryOptions
): Promise<Feature<LineString | MultiLineString>> {
const routePath = `${routeId}/${routeId}_${directionId}.json`;
const response = await fetch(
`${baseUrl.replace(/\/+$/, "")}/data/trimet/geoJSON/${routePath}`,
{ signal }
);
if (!response.ok) {
throw new Error(
`Unable to load route geometry ${routePath}: ${response.status} ${response.statusText}`
);
}
const feature: Feature = await response.json();
const geometry = feature?.geometry;
if (geometry?.type === "GeometryCollection") {
const coordinates = lineParts(geometry);
if (!coordinates.length) {
throw new Error(`Empty route geometry ${routePath}`);
}
return { ...feature, geometry: { type: "MultiLineString", coordinates } };
}
if (
(geometry?.type !== "LineString" &&
geometry?.type !== "MultiLineString") ||
!geometry.coordinates.length
) {
throw new Error(`Invalid route geometry ${routePath}`);
}
return { ...feature, geometry };
}
|