OpenDataVisualizer/Schaad.OpenData.Traffic/Storage/TrafficCacheStorage.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
3.2 KiB
C#

using System.Globalization;
using System.Text.Json;
using Microsoft.Extensions.Options;
using Schaad.OpenData.Traffic.Models;
namespace Schaad.OpenData.Traffic.Storage;
public class TrafficCacheStorage(IOptions<TrafficOptions> options)
{
private const string DayFormat = "yyyy-MM-dd";
private const string GeometryFileName = "geometry-v7.json";
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) { WriteIndented = true };
private string RootPath => Path.Combine(Path.GetFullPath(options.Value.DataPath), "traffic", "thurgau");
private string CountsPath => Path.Combine(RootPath, "counts");
public virtual async Task<TrafficSnapshot> LoadAsync(CancellationToken cancellationToken)
{
var stations = await ReadAsync<List<Station>>(Path.Combine(RootPath, "stations.json"), cancellationToken) ?? [];
var geometry = await ReadAsync<List<StationGeometry>>(Path.Combine(RootPath, GeometryFileName), cancellationToken) ?? [];
var meta = await ReadAsync<TrafficCacheMeta>(Path.Combine(RootPath, "meta.json"), cancellationToken);
var counts = new List<HourlyCount>();
if (Directory.Exists(CountsPath))
{
foreach (var file in Directory.EnumerateFiles(CountsPath, "*.json").Order(StringComparer.Ordinal))
{
counts.AddRange(await ReadAsync<List<HourlyCount>>(file, cancellationToken) ?? []);
}
}
return new TrafficSnapshot(stations, counts, geometry, meta?.LastRefresh);
}
public virtual async Task SaveAsync(TrafficSnapshot snapshot, IReadOnlyCollection<DateOnly> changedDays, CancellationToken cancellationToken)
{
await WriteAsync(Path.Combine(RootPath, "stations.json"), snapshot.Stations.Values.OrderBy(s => s.Code).ToList(), cancellationToken);
foreach (var day in changedDays)
{
var path = Path.Combine(CountsPath, day.ToString(DayFormat, CultureInfo.InvariantCulture) + ".json");
await WriteAsync(path, snapshot.CountsByDate.GetValueOrDefault(day).ToList(), cancellationToken);
}
await WriteAsync(Path.Combine(RootPath, "meta.json"), new TrafficCacheMeta(snapshot.LastRefresh), cancellationToken);
}
public virtual Task SaveGeometryAsync(TrafficSnapshot snapshot, CancellationToken cancellationToken) =>
WriteAsync(Path.Combine(RootPath, GeometryFileName), snapshot.Geometry.Values.OrderBy(g => g.StationCode).ToList(), cancellationToken);
private static async Task<T?> ReadAsync<T>(string path, CancellationToken cancellationToken)
{
if (!File.Exists(path))
{
return default;
}
await using var stream = File.OpenRead(path);
return await JsonSerializer.DeserializeAsync<T>(stream, JsonOptions, cancellationToken);
}
private static async Task WriteAsync<T>(string path, T value, CancellationToken cancellationToken)
{
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
var tempPath = path + ".tmp";
await using (var stream = File.Create(tempPath))
{
await JsonSerializer.SerializeAsync(stream, value, JsonOptions, cancellationToken);
}
File.Move(tempPath, path, true);
}
}