diff --git a/index.html b/index.html index 924b85588..fcc5f79cf 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,7 @@ with the sources: src/materialSymbolsSubset.test.mjs fails when a source names a glyph this list is missing, because a missing glyph does not draw a box — the ligature never forms and the element renders the literal word. --> - +
diff --git a/src/data/entityContext.js b/src/data/entityContext.js index 78fe791bc..5b3ee7df4 100644 --- a/src/data/entityContext.js +++ b/src/data/entityContext.js @@ -53,7 +53,9 @@ const TITLE_FIELDS = [ 'registration', ]; /** Fields that identify an item, for matching a click or a search. */ -const KEY_FIELDS = ['icao24', 'mmsi', 'noradId', 'usgsId', 'id']; +// `key` is a layer's own key where it differs from the record's id (a +// transit vehicle is keyed by feed and vehicle, and named by its fleet number). +const KEY_FIELDS = ['icao24', 'mmsi', 'noradId', 'usgsId', 'id', 'key']; /** * How many records to ask a layer for when all are wanted. Layers read a * missing or unbounded count as their own small default, so this is a number @@ -103,6 +105,7 @@ const HIDDEN_FACTS = new Set([ 'scale', 'routeId', 'building', + 'key', ]); /** Facts that lead the list when a record has them, in this order. */ @@ -318,6 +321,27 @@ const isEpochMs = (value) => */ /** Codes the feeds use, in words. */ const CODE_WORDS = Object.freeze({ + motion: { + moving: 'Moving', + stopped: 'Stopped', + waiting: 'Waiting for update', + 'no-fix': 'No recent fix', + }, + // GTFS-RT vehicle states, as the operator last reported them. + status: { + IN_TRANSIT_TO: 'Heading to next stop', + INCOMING_AT: 'Arriving at a stop', + STOPPED_AT: 'Stopped at a stop', + }, + occupancy: { + EMPTY: 'Empty', + MANY_SEATS_AVAILABLE: 'Many seats free', + FEW_SEATS_AVAILABLE: 'Few seats free', + STANDING_ROOM_ONLY: 'Standing room only', + CRUSHED_STANDING_ROOM_ONLY: 'Crowded', + FULL: 'Full', + NOT_ACCEPTING_PASSENGERS: 'Not taking passengers', + }, classification: { TD: 'Tropical depression', TS: 'Tropical storm', @@ -995,7 +1019,7 @@ const AIRCRAFT_CLASS_NAMES = { */ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) { const kind = entityKind(layerId).kind; - if (!['aircraft', 'vessel', 'satellite', 'earthquake'].includes(kind)) return null; + if (!['aircraft', 'vessel', 'satellite', 'earthquake', 'vehicle'].includes(kind)) return null; let records = layerRecords(dataManager, layerId); if (rect) { const wraps = rect.west > rect.east; @@ -1024,6 +1048,22 @@ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) { splits.push({ title: 'Type', parts: tallyBy(records, (record) => AIRCRAFT_CLASS_NAMES[record.aircraftClass] || plainWord(text(record.aircraftClass))) }); } else if (kind === 'vessel') { splits.push({ title: 'Type', parts: tallyBy(records, (record) => shipTypeName(record.shipType)) }); + } else if (kind === 'vehicle') { + // What the display shows each vehicle doing, then what kind it is, who + // runs it, the operator's own report and crowding where the feed has it. + // Feeds of vehicle positions carry no timetable, so early or late is not + // something these records can say. + const word = (field) => (record) => CODE_WORDS[field][record[field]] || ''; + splits.push({ title: 'Motion', parts: tallyBy(records, word('motion')) }); + splits.push({ title: 'Mode', parts: tallyBy(records, (record) => plainWord(text(record.mode))) }); + const operators = tallyBy(records, (record) => text(record.operator)); + if (operators.length > 1) splits.push({ title: 'Operator', parts: operators }); + const reported = records.filter((record) => CODE_WORDS.status[record.status]); + if (reported.length) + splits.push({ title: 'Last reported', parts: tallyBy(reported, word('status')) }); + const crowded = records.filter((record) => CODE_WORDS.occupancy[record.occupancy]); + if (crowded.length) + splits.push({ title: 'Crowding', parts: tallyBy(crowded, word('occupancy')) }); } else if (kind === 'satellite') { splits.push({ title: 'Class', parts: tallyBy(records, (record) => plainWord(text(record.satelliteClass || record.group))) }); } else { diff --git a/src/data/entityContext.test.mjs b/src/data/entityContext.test.mjs index bfaf8fc1b..2bbe1a4d0 100644 --- a/src/data/entityContext.test.mjs +++ b/src/data/entityContext.test.mjs @@ -50,6 +50,58 @@ test('a click identity finds its record by key, past the first 2000 records', () assert.equal(record?.callsign, 'FLT2400'); }); +test('a transit click finds its vehicle by the layer key and reads its state', () => { + const buses = manager({ + transit: { + name: 'Transit', + records: [ + { + id: 'M634', + key: 'capmetro-austin:M634', + route: '801', + operator: 'CapMetro', + status: 'IN_TRANSIT_TO', + lat: 30.2, + lon: -97.7, + }, + ], + }, + }); + const card = describeEntity(buses, { + layerId: 'transit', + id: 'capmetro-austin:M634', + lat: 30.21, + lon: -97.71, + }); + assert.equal(card?.title, 'M634'); + const facts = Object.fromEntries(card.facts.map((fact) => [fact.label, fact.value])); + assert.equal(facts.Operator, 'CapMetro'); + assert.equal(facts.Status, 'Heading to next stop'); + assert.equal(facts.Key, undefined, 'the layer key is not shown'); +}); + +test('transit vehicles break down by motion, mode and reported state', () => { + const at = { lat: 30.2, lon: -97.7 }; + const buses = manager({ + transit: { + name: 'Transit', + records: [ + { id: '1', mode: 'bus', motion: 'moving', status: 'IN_TRANSIT_TO', ...at }, + { id: '2', mode: 'bus', motion: 'stopped', status: 'STOPPED_AT', ...at }, + { id: '3', mode: 'rail', motion: 'moving', occupancy: 'FULL', ...at }, + ], + }, + }); + const breakdown = layerBreakdown(buses, 'transit'); + assert.equal(breakdown.total, 3); + const split = (title) => + Object.fromEntries(breakdown.splits.find((item) => item.title === title).parts.map((part) => [part.label, part.count])); + assert.deepEqual(split('Motion'), { Moving: 2, Stopped: 1 }); + assert.deepEqual(split('Mode'), { Bus: 2, Rail: 1 }); + assert.deepEqual(split('Last reported'), { 'Heading to next stop': 1, 'Stopped at a stop': 1 }); + assert.deepEqual(split('Crowding'), { Full: 1 }); +}); + test('a moving item never borrows a nearby craft when its key is unknown', () => { const record = findRecord(dm, { layerId: 'flights', id: 'zzzzzz', label: 'NOPE', lat: 34.0004, lon: -118.05 }); assert.equal(record, null); diff --git a/src/layers/transit/queries.js b/src/layers/transit/queries.js index 28aff8b63..738a1c17c 100644 --- a/src/layers/transit/queries.js +++ b/src/layers/transit/queries.js @@ -5,12 +5,23 @@ import { transitDetectionClass, transitDetectionId, transitDetectionMetric, + vehicleReportTimeMs, } from './policy.js'; import { TRANSIT_ENABLED_FEEDS, getRegisteredTransitFeed, + transitRouteLabel, } from '../../data/transitFeeds.js'; import { transitModeTier } from '../../data/transitPresetStyle.js'; +import { displayMotion } from './movement.js'; + +/** The display's motion word as a record field for the Context card. */ +const MOTION_FIELD = Object.freeze({ + 'EN ROUTE': 'moving', + STOPPED: 'stopped', + WAITING: 'waiting', + 'NO FIX': 'no-fix', +}); /** Regions, not feeds: one region can carry several feeds (Rio has two). */ const TRANSIT_REGION_COUNT = new Set( @@ -211,10 +222,14 @@ export function createQueries({ state, parts }) { * query engine. On demand only (once per spoken query); [] while the * layer is off. * @param {number} [maxCount=Infinity] Most records to return. - * @returns {Array