All files / util/routing/src routeSegment.ts

92.1% Statements 35/38
83.33% Branches 30/36
100% Functions 6/6
92.1% Lines 35/38

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                        31x       62x   62x 62x 64x 1776x 1776x 1776x 1776x 1776x       1776x       1776x   1776x 908x 908x       62x     62x     31x 31x 31x     31x 31x 31x 31x 32x     32x 32x 32x         32x 727x   32x 1x   32x   31x     31x              
import type { Feature, LineString, MultiLineString, Position } from "geojson";
 
interface SegmentStop {
  lat: number;
  lng: number;
}
 
export function getSelectedRouteSegment(
  feature: Feature<LineString | MultiLineString>,
  origin: SegmentStop,
  destination: SegmentStop
): Feature<LineString | MultiLineString> {
  const lines = feature.geometry.type === "LineString"
    ? [feature.geometry.coordinates]
    : feature.geometry.coordinates;
  function closestPoint(stop: SegmentStop) {
    const longitudeScale = Math.cos(stop.lat * Math.PI / 180);
    let nearest: { line: number; index: number; fraction: number; point: Position };
    let minimum = Infinity;
    lines.forEach((line, lineIndex) => {
      line.slice(1).forEach((end, index) => {
        const start = line[index];
        const dx = (end[0] - start[0]) * longitudeScale;
        const dy = end[1] - start[1];
        const lengthSquared = dx * dx + dy * dy;
        const fraction = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1,
          ((stop.lng - start[0]) * longitudeScale * dx +
            (stop.lat - start[1]) * dy) / lengthSquared
        ));
        const point = [
          start[0] + (end[0] - start[0]) * fraction,
          start[1] + (end[1] - start[1]) * fraction
        ];
        const distance = Math.pow((point[0] - stop.lng) * longitudeScale, 2) +
          Math.pow(point[1] - stop.lat, 2);
        if (distance < minimum) {
          minimum = distance;
          nearest = { line: lineIndex, index, fraction, point };
        }
      });
    });
    Iif (!nearest) {
      throw new Error("Route geometry has no line segments");
    }
    return nearest;
  }
 
  const from = closestPoint(origin);
  const to = closestPoint(destination);
  const fromFirst = from.line < to.line || (
    from.line === to.line && from.index + from.fraction <= to.index + to.fraction
  );
  const start = fromFirst ? from : to;
  const end = fromFirst ? to : from;
  const selected: Position[][] = [];
  lines.forEach((line, index) => {
    Iif (index < start.line || index > end.line) {
      return;
    }
    const first = index === start.line ? start.index + 1 : 0;
    const last = index === end.line ? end.index + 1 : line.length;
    const points = [
      ...(index === start.line ? [start.point] : []),
      ...line.slice(first, last),
      ...(index === end.line ? [end.point] : [])
    ];
    const part = points.filter((point, i) =>
      i === 0 || point[0] !== points[i - 1][0] || point[1] !== points[i - 1][1]
    );
    if (part.length === 1) {
      part.push(part[0]);
    }
    selected.push(part);
  });
  Iif (!selected.length) {
    throw new Error("Selected stops do not span a route segment");
  }
  return {
    ...feature,
    geometry: selected.length === 1
      ? { type: "LineString", coordinates: selected[0] }
      : { type: "MultiLineString", coordinates: selected }
  };
}