OpenDataVisualizer/Schaad.OpenData.Traffic/TrafficRefresher.cs
Claudio Schaad 8de869b023 Fix roundabout arm geometry for untargetable directions
Stations like "WEINFELDEN Kreisel West - Umfahrung" have directions
such as "zur Umfahrung" or "von Egelseestrasse" that cannot be
geocoded, so their dots were drawn on a nearby side road or around
the roundabout ring.

- Derive the roundabout centre from all stations sharing the name
  before " - " and point each arm outward from it
- Prefer road segments named like the station's address when
  following an arm; reject arms that don't get away from the centre
- Parse "zur", "zum", "Richtung" and "vom" direction prefixes
- Treat only swissTLM3D object kinds 12-19 as non-roads; kind 20 is
  a regular road (e.g. Dunantstrasse in Weinfelden)
- Bump the geometry cache to v7 so all stations are rebuilt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 20:50:48 +01:00

74 lines
2.7 KiB
C#

using Microsoft.Extensions.Logging;
using Schaad.OpenData.Traffic.Geometry;
using Schaad.OpenData.Traffic.Models;
using Schaad.OpenData.Traffic.Source;
using Schaad.OpenData.Traffic.Storage;
namespace Schaad.OpenData.Traffic;
public class TrafficRefresher(
ThurgauTrafficClient client,
TrafficCacheStorage storage,
TrafficDataStore store,
StationGeometryService geometryService,
TimeProvider timeProvider,
ILogger<TrafficRefresher> logger)
{
public async Task LoadCacheAsync(CancellationToken cancellationToken)
{
var snapshot = await storage.LoadAsync(cancellationToken);
store.Replace(snapshot);
logger.LogInformation("Loaded traffic cache with {Stations} stations and {Days} days", snapshot.Stations.Count, snapshot.Dates.Length);
}
public bool IsRefreshDue(TimeSpan interval) =>
store.Current.LastRefresh is not { } lastRefresh || timeProvider.GetUtcNow() - lastRefresh >= interval;
public async Task RefreshAsync(CancellationToken cancellationToken)
{
var current = store.Current;
DateOnly? since = current.Dates.IsEmpty ? null : current.Dates[^1].AddDays(-1);
var import = await client.FetchAsync(since, cancellationToken);
var updated = current.Merge(import, timeProvider.GetUtcNow(), out var changedDays);
await storage.SaveAsync(updated, changedDays, cancellationToken);
store.Replace(updated);
logger.LogInformation("Imported {Rows} traffic rows for {Days} days since {Since}", import.Counts.Count, changedDays.Count, since);
}
public async Task BuildMissingGeometryAsync(CancellationToken cancellationToken)
{
var snapshot = store.Current;
var stations = snapshot.Stations.Values.ToList();
var missing = stations.Where(s => !snapshot.Geometry.ContainsKey(s.Code)).ToList();
if (missing.Count == 0)
{
return;
}
var built = new List<StationGeometry>();
foreach (var station in missing)
{
try
{
built.Add(await geometryService.BuildAsync(station, stations, cancellationToken));
}
catch (HttpRequestException exception)
{
logger.LogWarning(exception, "Could not build road geometry for station {Code}", station.Code);
}
}
if (built.Count == 0)
{
return;
}
var updated = store.Current.WithGeometry(built);
await storage.SaveGeometryAsync(updated, cancellationToken);
store.Replace(updated);
logger.LogInformation(
"Built road geometry for {Count} stations, {Straight} without matching road",
built.Count,
built.Count(g => !g.FollowsRoad));
}
}