141 lines
4.5 KiB
C#
141 lines
4.5 KiB
C#
using TileServer.Application.Graph;
|
|
using TileServer.Domain.Exceptions;
|
|
using TileServer.Domain.Graph;
|
|
using TileServer.Domain.Tiles;
|
|
using TileServer.Infrastructure.Graph;
|
|
using TileServer.Infrastructure.Tiles.Mvt;
|
|
|
|
namespace TileServer.UnitTests;
|
|
|
|
public sealed class RoadGraphTests
|
|
{
|
|
[Fact]
|
|
public void Haversine_MoscowBlock_IsRoughlyHundredMeters()
|
|
{
|
|
var meters = OsmSharpRoadGraphBuilder.HaversineMeters(37.6173, 55.7558, 37.6185, 55.7558);
|
|
Assert.InRange(meters, 60, 120);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateBounds_RejectsWideBbox()
|
|
{
|
|
Assert.Throws<DomainValidationException>(() =>
|
|
RoadGraphQueryService.ValidateBounds(new RoadGraphBounds(37, 55, 40, 56)));
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateBounds_AcceptsSmallBbox()
|
|
{
|
|
RoadGraphQueryService.ValidateBounds(new RoadGraphBounds(37.5, 55.7, 37.7, 55.8));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(6, "motorway")]
|
|
[InlineData(8, "trunk")]
|
|
[InlineData(10, "primary")]
|
|
[InlineData(11, "tertiary")]
|
|
public void HighwaysForZoom_IncludesMajorClasses(int z, string highway)
|
|
{
|
|
var list = RoadGraphZoomPolicy.HighwaysForZoom(z);
|
|
Assert.NotNull(list);
|
|
Assert.Contains(highway, list);
|
|
}
|
|
|
|
[Fact]
|
|
public void HighwaysForZoom_AllClassesFromZ12()
|
|
=> Assert.Null(RoadGraphZoomPolicy.HighwaysForZoom(12));
|
|
}
|
|
|
|
public sealed class RoadGraphMvtTests
|
|
{
|
|
[Fact]
|
|
public void WorldTile_CoversWebMercator()
|
|
{
|
|
var bounds = new TileCoordinate(0, 0, 0).WebMercatorBounds();
|
|
Assert.InRange(bounds.MinLon, -180.01, -179.99);
|
|
Assert.InRange(bounds.MaxLon, 179.99, 180.01);
|
|
Assert.True(bounds.MaxLat > 85);
|
|
Assert.True(bounds.MinLat < -85);
|
|
}
|
|
|
|
[Fact]
|
|
public void FromLonLat_MoscowTileContainsPoint()
|
|
{
|
|
const double lon = 37.6173;
|
|
const double lat = 55.7558;
|
|
var tile = TileCoordinate.FromLonLat(lon, lat, 14);
|
|
var bounds = tile.WebMercatorBounds();
|
|
Assert.InRange(lon, bounds.MinLon, bounds.MaxLon);
|
|
Assert.InRange(lat, bounds.MinLat, bounds.MaxLat);
|
|
}
|
|
|
|
[Fact]
|
|
public void Encode_SingleEdge_ProducesRoadsLayer()
|
|
{
|
|
var tile = TileCoordinate.FromLonLat(37.6173, 55.7558, 14);
|
|
var edge = new RoadGraphEdge(
|
|
1, 10, 11, "primary", "Tverskaya", false, 120,
|
|
37.6173, 55.7558, 37.6185, 55.7562);
|
|
var data = new RoadGraphMvtEncoder().Encode([edge], tile);
|
|
var decoded = MvtCodec.Decode(data);
|
|
var layer = Assert.Single(decoded.Layers);
|
|
Assert.Equal(RoadGraphMvtEncoder.LayerName, layer.Name);
|
|
Assert.Equal(RoadGraphMvtEncoder.Extent, (int)layer.Extent);
|
|
var feature = Assert.Single(layer.Features);
|
|
Assert.Equal(MvtCodec.LineString, feature.Type);
|
|
Assert.Contains("highway", layer.Keys);
|
|
Assert.True(feature.Geometry.Count >= 6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Encode_Empty_KeepsRoadsLayer()
|
|
{
|
|
var tile = new TileCoordinate(10, 619, 320);
|
|
var data = new RoadGraphMvtEncoder().Encode([], tile);
|
|
var decoded = MvtCodec.Decode(data);
|
|
var layer = Assert.Single(decoded.Layers);
|
|
Assert.Equal(RoadGraphMvtEncoder.LayerName, layer.Name);
|
|
Assert.Empty(layer.Features);
|
|
}
|
|
|
|
[Fact]
|
|
public void ClipSegment_RejectsFullyOutside()
|
|
{
|
|
Assert.False(RoadGraphMvtEncoder.ClipSegment((-2000, 100), (-1800, 200), out _, out _));
|
|
}
|
|
|
|
[Fact]
|
|
public void Encode_Polyline_KeepsIntermediateVertex()
|
|
{
|
|
var tile = TileCoordinate.FromLonLat(37.6173, 55.7558, 14);
|
|
var edge = new RoadGraphEdge(
|
|
1, 10, 11, "residential", null, false, 180,
|
|
37.6173, 55.7558, 37.6190, 55.7568,
|
|
[
|
|
new RoadGraphPoint(37.6173, 55.7558),
|
|
new RoadGraphPoint(37.6182, 55.7565),
|
|
new RoadGraphPoint(37.6190, 55.7568)
|
|
]);
|
|
var data = new RoadGraphMvtEncoder().Encode([edge], tile);
|
|
var decoded = MvtCodec.Decode(data);
|
|
var feature = Assert.Single(Assert.Single(decoded.Layers).Features);
|
|
var paths = MvtCodec.DecodeGeometry(feature.Geometry);
|
|
var path = Assert.Single(paths);
|
|
Assert.True(path.Count >= 3);
|
|
}
|
|
|
|
[Fact]
|
|
public void ClipPolyline_SplitsWhenLeavingTile()
|
|
{
|
|
var paths = RoadGraphMvtEncoder.ClipPolyline(
|
|
[
|
|
(100, 100),
|
|
(200, 200),
|
|
(8000, 8000),
|
|
(100, 200)
|
|
]);
|
|
Assert.NotEmpty(paths);
|
|
Assert.All(paths, path => Assert.True(path.Count >= 2));
|
|
}
|
|
}
|