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>
126 lines
5.3 KiB
C#
126 lines
5.3 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using Schaad.OpenData.Traffic.Geometry;
|
|
using Schaad.OpenData.Traffic.Models;
|
|
|
|
namespace Schaad.OpenData.Tests;
|
|
|
|
public class SwisstopoClientTestShould
|
|
{
|
|
private sealed class StubHandler(params (HttpStatusCode Status, string Body)[] responses) : HttpMessageHandler
|
|
{
|
|
private int index;
|
|
|
|
public List<Uri> Requests { get; } = [];
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
Requests.Add(request.RequestUri!);
|
|
var (status, body) = responses[Math.Min(index++, responses.Length - 1)];
|
|
return Task.FromResult(new HttpResponseMessage(status) { Content = new StringContent(body, Encoding.UTF8, "application/json") });
|
|
}
|
|
}
|
|
|
|
private static SwisstopoClient Client(StubHandler handler) =>
|
|
new(new HttpClient(handler) { BaseAddress = new Uri("https://api3.geo.admin.ch/rest/services/api/") });
|
|
|
|
private const string LandquartResults = """
|
|
{"results":[
|
|
{"attrs":{"label":"<b>Landquart (GR)</b>","lat":46.956,"lon":9.578}},
|
|
{"attrs":{"label":"Landquart [district]","lat":46.961,"lon":9.560}},
|
|
{"attrs":{"label":"Populated Place <b>Landquart (TG)</b> - Arbon","lat":47.501,"lon":9.423}},
|
|
{"attrs":{"label":"Landstrasse","lat":47.502,"lon":9.425}}
|
|
]}
|
|
""";
|
|
|
|
[Fact]
|
|
public async Task PickNearestMatchingPlaceWhenNameIsAmbiguous()
|
|
{
|
|
var client = Client(new StubHandler((HttpStatusCode.OK, LandquartResults)));
|
|
|
|
var place = await client.FindPlaceAsync("Landquart", new GeoPoint(47.502, 9.425), CancellationToken.None);
|
|
|
|
place.ShouldBe(new GeoPoint(47.501, 9.423));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MatchTransliteratedNameWhenLabelHasUmlaut()
|
|
{
|
|
var body = """{"results":[{"attrs":{"label":"<b>Tägerwilen (TG)</b>","lat":47.645,"lon":9.129}}]}""";
|
|
var client = Client(new StubHandler((HttpStatusCode.OK, body)));
|
|
|
|
var place = await client.FindPlaceAsync("Taegerwilen", new GeoPoint(47.62, 9.1), CancellationToken.None);
|
|
|
|
place.ShouldBe(new GeoPoint(47.645, 9.129));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReturnNullWhenNoLabelMatches()
|
|
{
|
|
var body = """{"results":[{"attrs":{"label":"Somewhere Else","lat":47,"lon":9}}]}""";
|
|
var client = Client(new StubHandler((HttpStatusCode.OK, body)));
|
|
|
|
(await client.FindPlaceAsync("Nowhere", new GeoPoint(47, 9), CancellationToken.None)).ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RetryWhenServiceIsTemporarilyUnavailable()
|
|
{
|
|
var handler = new StubHandler((HttpStatusCode.ServiceUnavailable, ""), (HttpStatusCode.OK, LandquartResults));
|
|
var client = Client(handler);
|
|
|
|
var place = await client.FindPlaceAsync("Landquart", new GeoPoint(47.5, 9.42), CancellationToken.None);
|
|
|
|
place.ShouldNotBeNull();
|
|
handler.Requests.Count.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FlagRoadsWithTrafficImportanceAsMainRoadsWhenLoadingSegments()
|
|
{
|
|
var body = """
|
|
{"results":[
|
|
{"geometry":{"type":"MultiLineString","coordinates":[[[9.1,47.5],[9.2,47.5]]]},"properties":{"verkehrsbedeutung":200}},
|
|
{"geometry":{"type":"LineString","coordinates":[[9.1,47.6],[9.2,47.6]]},"properties":{"verkehrsbedeutung":999998}}
|
|
]}
|
|
""";
|
|
var client = Client(new StubHandler((HttpStatusCode.OK, body)));
|
|
|
|
var segments = await client.GetRoadSegmentsAsync(new GeoPoint(47.5, 9.15), 300, CancellationToken.None);
|
|
|
|
segments.Select(s => s.IsMainRoad).ShouldBe([true, false]);
|
|
segments[0].Points[0].ShouldBe(new GeoPoint(47.5, 9.1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FlagPathsAsNotDrivableWhenLoadingSegments()
|
|
{
|
|
var body = """
|
|
{"results":[
|
|
{"geometry":{"type":"LineString","coordinates":[[9.1,47.5],[9.2,47.5]]},"properties":{"objektart":9,"strassenname":"Dunantstrasse"}},
|
|
{"geometry":{"type":"LineString","coordinates":[[9.1,47.6],[9.2,47.6]]},"properties":{"objektart":15}},
|
|
{"geometry":{"type":"LineString","coordinates":[[9.1,47.7],[9.2,47.7]]},"properties":{"objektart":20}}
|
|
]}
|
|
""";
|
|
var client = Client(new StubHandler((HttpStatusCode.OK, body)));
|
|
|
|
var segments = await client.GetRoadSegmentsAsync(new GeoPoint(47.5, 9.15), 300, CancellationToken.None);
|
|
|
|
segments.Select(s => s.IsDrivable).ShouldBe([true, false, true]);
|
|
segments.Select(s => s.Name).ShouldBe(["Dunantstrasse", null, null]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RequestNextPageWhenPageIsFull()
|
|
{
|
|
var feature = """{"geometry":{"type":"LineString","coordinates":[[9.1,47.5],[9.2,47.5]]},"properties":{}}""";
|
|
var fullPage = $$"""{"results":[{{string.Join(',', Enumerable.Repeat(feature, 200))}}]}""";
|
|
var handler = new StubHandler((HttpStatusCode.OK, fullPage), (HttpStatusCode.OK, $$"""{"results":[{{feature}}]}"""));
|
|
var client = Client(handler);
|
|
|
|
var segments = await client.GetRoadSegmentsAsync(new GeoPoint(47.5, 9.15), 300, CancellationToken.None);
|
|
|
|
segments.Count.ShouldBe(201);
|
|
handler.Requests.Select(r => r.Query.Contains("offset=200")).ShouldBe([false, true]);
|
|
}
|
|
}
|