OpenDataVisualizer/Schaad.OpenData.Traffic/Geometry/StationGeometryService.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

77 lines
3.4 KiB
C#

using Schaad.OpenData.Traffic.Models;
namespace Schaad.OpenData.Traffic.Geometry;
public class StationGeometryService(SwisstopoClient swisstopoClient)
{
private const double HalfPathLengthMeters = 300;
private const double SearchRadiusMeters = 650;
private const double MetersPerDegreeLat = 111_320;
private const double MinimumArmOffsetMeters = 3;
private const double OutwardTargetMeters = 3000;
private const string RoundaboutMarker = "Kreisel";
public virtual async Task<StationGeometry> BuildAsync(Station station, IReadOnlyList<Station> stations, CancellationToken cancellationToken)
{
var isRoundaboutArm = station.Name.Contains(RoundaboutMarker, StringComparison.OrdinalIgnoreCase);
var outwardTarget = isRoundaboutArm ? OutwardTarget(station, stations) : null;
var directions = new List<ResolvedDirection>();
foreach (var direction in station.Directions)
{
var target = DirectionTarget.Parse(direction);
var location = outwardTarget ?? await FindPlaceAsync(target.Place, station.Location, cancellationToken);
directions.Add(new ResolvedDirection(direction, target, location));
}
var targetLocation = directions.Select(d => d.TargetLocation).FirstOrDefault(l => l is not null);
var segments = await swisstopoClient.GetRoadSegmentsAsync(station.Location, SearchRadiusMeters, cancellationToken);
var roadPath = (isRoundaboutArm && targetLocation is { } armTarget
? RoadPathBuilder.BuildArmPath(station.Location, segments, armTarget, 2 * HalfPathLengthMeters, station.Address)
: null)
?? RoadPathBuilder.BuildRoadPath(station.Location, segments, HalfPathLengthMeters);
var path = roadPath ?? RoadPathBuilder.BuildStraightPath(station.Location, targetLocation, HalfPathLengthMeters);
return new StationGeometry(station.Code, roadPath is not null, RoadPathBuilder.Orient(station.Location, path, directions));
}
internal static GeoPoint? OutwardTarget(Station station, IReadOnlyList<Station> stations)
{
var key = RoundaboutKey(station.Name);
var siblings = stations.Where(s => RoundaboutKey(s.Name) == key).ToList();
if (siblings.Count < 2)
{
return null;
}
var center = new GeoPoint(siblings.Average(s => s.Location.Lat), siblings.Average(s => s.Location.Lon));
var deltaLat = station.Location.Lat - center.Lat;
var deltaLon = station.Location.Lon - center.Lon;
var meters = Math.Sqrt(
Math.Pow(deltaLat * MetersPerDegreeLat, 2)
+ Math.Pow(deltaLon * MetersPerDegreeLat * Math.Cos(center.Lat * Math.PI / 180), 2));
if (meters < MinimumArmOffsetMeters)
{
return null;
}
var scale = OutwardTargetMeters / meters;
return new GeoPoint(center.Lat + deltaLat * scale, center.Lon + deltaLon * scale);
}
private static string RoundaboutKey(string name) => name.Split(" - ", 2)[0].Trim();
private async Task<GeoPoint?> FindPlaceAsync(string place, GeoPoint near, CancellationToken cancellationToken)
{
foreach (var part in place.Split('/', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
{
if (await swisstopoClient.FindPlaceAsync(part, near, cancellationToken) is { } location)
{
return location;
}
}
return null;
}
}