mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
fix(maps): measure a booking line along the road before hiding it (#2275)
A short transport hop is hidden once its two ends sit closer than a few dozen pixels, so a booking that would draw as a dot does not clutter the map. That check compared the endpoints, which was the whole line while every hop was straight. A car booking is routed along the road now, and a road that leaves a town, rounds a lake and comes back to the next town along can run across half the screen while its ends project sixty pixels apart. The whole drive vanished until the map was zoomed far enough in for the ends to separate, which is the gap in the route the report shows. The decision now walks the line that will actually be drawn, road route or arcs, and keeps the hop as soon as that reaches the floor. A straight two-point hop measures exactly what it measured before, so every existing threshold holds for the case it was written for. One helper serves both renderers, which also folds the two hand-copied floor tables into it.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { isRoutableReservation, visibleRouteReservations } from './reservationRoutes'
|
||||
import { hopIsVisible, isRoutableReservation, labelFloorPx, lineFloorPx, visibleRouteReservations } from './reservationRoutes'
|
||||
import type { Day, Reservation, ReservationEndpoint } from '../types'
|
||||
|
||||
function endpoint(role: 'from' | 'to', lat: number, lng: number): ReservationEndpoint {
|
||||
@@ -116,3 +116,41 @@ describe('visibleRouteReservations with a selected day', () => {
|
||||
expect(visibleRouteReservations([r], { visibleConnectionIds: [], showTransitRoutes: true, selectedDayId: null, days })).toEqual([r])
|
||||
})
|
||||
})
|
||||
|
||||
describe('hopIsVisible (#2275)', () => {
|
||||
// A flat projection: one unit of lat or lng is one pixel.
|
||||
const px = (p: readonly [number, number]) => ({ x: p[1], y: p[0] })
|
||||
|
||||
it('keeps the type floors of the old endpoint check', () => {
|
||||
expect(lineFloorPx('car')).toBe(80)
|
||||
expect(lineFloorPx('flight')).toBe(50)
|
||||
expect(lineFloorPx('cruise')).toBe(150)
|
||||
expect(lineFloorPx('train')).toBe(200)
|
||||
expect(labelFloorPx('car')).toBe(150)
|
||||
expect(labelFloorPx('transit')).toBe(900)
|
||||
expect(labelFloorPx('bus')).toBe(400)
|
||||
})
|
||||
|
||||
it('measures a straight two-point line exactly as the endpoint gap', () => {
|
||||
expect(hopIsVisible('car', [[[0, 0], [0, 79]]], px)).toBe(false)
|
||||
expect(hopIsVisible('car', [[[0, 0], [0, 80]]], px)).toBe(true)
|
||||
})
|
||||
|
||||
it('walks a routed line, so a loop between close ends counts its full length', () => {
|
||||
const loop: [number, number][] = [[0, 0], [30, 0], [30, 30], [0, 30], [0, 5]]
|
||||
expect(hopIsVisible('car', [loop], px)).toBe(true)
|
||||
expect(hopIsVisible('train', [loop], px)).toBe(false)
|
||||
})
|
||||
|
||||
it('adds the legs of a multi-stop booking together', () => {
|
||||
const legs: [number, number][][] = [[[0, 0], [0, 50]], [[0, 50], [0, 100]]]
|
||||
expect(hopIsVisible('car', legs, px)).toBe(false)
|
||||
expect(hopIsVisible('car', [[[0, 0], [0, 50], [0, 100]]], px)).toBe(true)
|
||||
})
|
||||
|
||||
it('draws nothing for an empty or one-point line', () => {
|
||||
expect(hopIsVisible('car', [], px)).toBe(false)
|
||||
expect(hopIsVisible('car', [[]], px)).toBe(false)
|
||||
expect(hopIsVisible('car', [[[0, 0]]], px)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -52,3 +52,63 @@ export function visibleRouteReservations(reservations: Reservation[], options: R
|
||||
set.has(r.id)
|
||||
)
|
||||
}
|
||||
|
||||
/** The type-specific floors, in screen pixels, under which a hop is not worth drawing. */
|
||||
const LINE_FLOOR_PX: Record<string, number> = { flight: 50, cruise: 150, car: 80 }
|
||||
const LINE_FLOOR_DEFAULT_PX = 200
|
||||
|
||||
/** Under this floor the endpoint labels stay off, so short hops do not stack text. */
|
||||
const LABEL_FLOOR_PX: Record<string, number> = { flight: 50, cruise: 300, car: 150, transit: 900 }
|
||||
const LABEL_FLOOR_DEFAULT_PX = 400
|
||||
|
||||
/** How far the line is allowed to disappear before its endpoints cannot be told apart. */
|
||||
export function lineFloorPx(type: string): number {
|
||||
return LINE_FLOOR_PX[type] ?? LINE_FLOOR_DEFAULT_PX
|
||||
}
|
||||
|
||||
export function labelFloorPx(type: string): number {
|
||||
return LABEL_FLOOR_PX[type] ?? LABEL_FLOOR_DEFAULT_PX
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a hop is worth drawing at the current zoom.
|
||||
*
|
||||
* The declutter exists for a tiny straight connector, the kind that would sit
|
||||
* under its own endpoint markers and add nothing but clutter. It used to be
|
||||
* decided from the two endpoints alone, which was fine while every hop was a
|
||||
* straight line: the line was never longer than the gap it bridged.
|
||||
*
|
||||
* A routed car booking is not that line. It follows the road, and a road that
|
||||
* leaves a town, loops round a lake and comes back to the next town along
|
||||
* can run across half the screen while its two ends project a handful of
|
||||
* pixels apart. The old check then hid the whole drive, and the person looking
|
||||
* at their trip saw a gap in the route until they zoomed in far enough for
|
||||
* the endpoints to separate (#2275). A booking with stops on the way has the
|
||||
* same shape: the ends may be close, the drawn path is not.
|
||||
*
|
||||
* So the decision looks at what will actually be drawn. A hop stays hidden
|
||||
* only while every polyline it would draw is shorter on screen than the
|
||||
* floor, walked point to point. A straight two-point line measures the same
|
||||
* as before, so every existing floor holds for the case it was written for.
|
||||
*
|
||||
* `project` is whichever renderer's own projection: Leaflet's
|
||||
* latLngToContainerPoint or Mapbox's project, wrapped to take [lat, lng].
|
||||
*/
|
||||
export function hopIsVisible(
|
||||
type: string,
|
||||
lines: readonly (readonly [number, number][])[],
|
||||
project: (point: readonly [number, number]) => { x: number; y: number },
|
||||
): boolean {
|
||||
const floor = lineFloorPx(type)
|
||||
for (const line of lines) {
|
||||
let length = 0
|
||||
let prev = line.length > 0 ? project(line[0]) : null
|
||||
for (let i = 1; i < line.length && prev; i++) {
|
||||
const next = project(line[i])
|
||||
length += Math.hypot(next.x - prev.x, next.y - prev.y)
|
||||
if (length >= floor) return true
|
||||
prev = next
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user