From 1fbb9216a075d808e37417aa90d5ec5cf137fc42 Mon Sep 17 00:00:00 2001 From: xivk Date: Tue, 14 Nov 2023 10:32:35 +0100 Subject: [PATCH] Fixed obscure issue when lon/lat diff are exactly the same. --- NetTopologySuite.IO.VectorTiles.Common.props | 2 +- .../Tilers/Shared.cs | 16 ++++++++-------- .../Tilers/LineStringTilerTests.cs | 19 +++++++++++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/NetTopologySuite.IO.VectorTiles.Common.props b/NetTopologySuite.IO.VectorTiles.Common.props index 22eaaad..19e5054 100644 --- a/NetTopologySuite.IO.VectorTiles.Common.props +++ b/NetTopologySuite.IO.VectorTiles.Common.props @@ -6,7 +6,7 @@ latest NetTopologySuite - Team NetTopologySuite - Team - 1.0.2 + 1.0.3 MIT https://github.com/NetTopologySuite/NetTopologySuite.IO.VectorTiles icon.png diff --git a/src/NetTopologySuite.IO.VectorTiles/Tilers/Shared.cs b/src/NetTopologySuite.IO.VectorTiles/Tilers/Shared.cs index 182a6f1..8a9455c 100644 --- a/src/NetTopologySuite.IO.VectorTiles/Tilers/Shared.cs +++ b/src/NetTopologySuite.IO.VectorTiles/Tilers/Shared.cs @@ -19,20 +19,20 @@ internal static class Shared { double xDiff = x2 - x1; double yDiff = y2 - y1; - - if (Math.Abs(xDiff) <= double.Epsilon && + + if (Math.Abs(xDiff) <= double.Epsilon && Math.Abs(yDiff) <= double.Epsilon) yield break; - - if (Math.Abs(xDiff) > Math.Abs(yDiff)) + + if (Math.Abs(xDiff) >= Math.Abs(yDiff)) { // we take x as denominator. int yPrevious = (int)Math.Floor(y1); int xPrevious = (int)Math.Floor(x1); int xLast = (int) Math.Floor(x2); int yLast = (int) Math.Floor(y2); - + double slope = (yDiff / xDiff); double y0 = y1 - (slope * x1); - + // with an increment of 1 we calculate y. bool right = xDiff > 0; // var up = (yDiff > 0); @@ -42,12 +42,12 @@ internal static class Shared yield return (xPrevious, yPrevious); for (int x = start; x != end + xStep; x += xStep) { - // REMARK: this can be more efficient but this way numerically more stable. + // REMARK: this can be more efficient but this way numerically more stable. // we prefer correctness over performance here. // calculate next y. double y = x * slope + y0; int yRounded = (int)Math.Floor(y); - + if (yPrevious != yRounded) { // we have moved to a new y diff --git a/test/NetTopologySuite.IO.VectorTiles.Tests/Tilers/LineStringTilerTests.cs b/test/NetTopologySuite.IO.VectorTiles.Tests/Tilers/LineStringTilerTests.cs index 2809600..5e09f59 100644 --- a/test/NetTopologySuite.IO.VectorTiles.Tests/Tilers/LineStringTilerTests.cs +++ b/test/NetTopologySuite.IO.VectorTiles.Tests/Tilers/LineStringTilerTests.cs @@ -133,7 +133,7 @@ public void LineStringTiler_Cut_LineString_CutAt21_ShouldBeCutIn0OrMore() } } } - + [Fact] public void LineStringTiler_Cut_LineString_CutAt22_ShouldBeCutIn0OrMore() { @@ -200,7 +200,7 @@ public void LineStringTiler_Tiles_LineStringInMultipleTiles_Zoom13_ShouldBeInEac }); var tiles = lineString.Tiles(13).ToList(); - + Assert.Equal(5, tiles.Count); Assert.Equal(44869043UL, tiles[0]); Assert.Equal(44860851UL, tiles[1]); @@ -208,5 +208,20 @@ public void LineStringTiler_Tiles_LineStringInMultipleTiles_Zoom13_ShouldBeInEac Assert.Equal(44852660UL, tiles[3]); Assert.Equal(44852661UL, tiles[4]); } + + [Fact] + public void LineStringTile_Tiles_Regression_ExactLonLatDiff_Zoom14_ShouldReturnTiles() + { + var reader = new WKTReader(); + var lineString = + reader.Read( + "LINESTRING (-61.122810782967036 -34.2440222744931, -61.144767342032964 -34.225843342865723)") + as + LineString; + + + var tiles = lineString.Tiles(14).ToList(); + Assert.NotEmpty(tiles); + } } }