Skip to content

Commit

Permalink
Reset currentX and currentY when encoded data is not written
Browse files Browse the repository at this point in the history
Add provided unit test slightly modified

refers to #29
  • Loading branch information
FObermaier committed Aug 13, 2024
1 parent 3f510d8 commit c40b4b1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/NetTopologySuite.IO.VectorTiles.Mapbox/MapboxTileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ private static void EncodeTo(List<uint> destination, CoordinateSequence sequence
// skipping the last point for rings since ClosePath is used instead
int count = ring ? sequence.Count - 1 : sequence.Count;

// In case we decide to ditch encoded data, we must reset currentX and currentY
int initialCurrentX = currentX;
int initialCurrentY = currentY;

// If the sequence is empty there is nothing we can do with it.
if (count == 0)
return;
Expand Down Expand Up @@ -326,14 +330,22 @@ private static void EncodeTo(List<uint> destination, CoordinateSequence sequence
if (destination.Count - initialSize - 2 >= 6)
destination.Add(GenerateCommandInteger(MapboxCommandType.ClosePath, 1));
else
{
currentX = initialCurrentX;
currentY = initialCurrentY;
destination.RemoveRange(initialSize, destination.Count - initialSize);
}
}
else
{
// A line has 1 MoveTo and 1 LineTo command.
// A line is valid if it has at least 2 points
if (destination.Count - initialSize - 2 < 4)
{
currentX = initialCurrentX;
currentY = initialCurrentY;
destination.RemoveRange(initialSize, destination.Count - initialSize);
}
}
}

Expand Down
56 changes: 56 additions & 0 deletions test/NetTopologySuite.IO.VectorTiles.Tests/Issues/Issue29.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.VectorTiles.Tiles;
using NetTopologySuite.IO.VectorTiles.Mapbox;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace NetTopologySuite.IO.VectorTiles.Tests.Issues
{
public class Issue29
{
[Fact]
public void MvtWriterGeometryIssue()
{
writeTile(8, 6, 4);
writeTile(34, 24, 6);
}

private static void writeTile(int x, int y, int z)
{
var geometry = new WKTReader().Read(ItalyWkt);
var feature = new Feature(geometry, new AttributesTable(new Dictionary<string, object>()
{
{ "id", 1 }
}));
var tileDefinition = new VectorTiles.Tiles.Tile(x, y, z);
var vectorTile = new VectorTile { TileId = tileDefinition.Id };
var layer = new Layer { Name = "layer1" };
vectorTile.Layers.Add(layer);
layer.Features.Add(feature);

byte[] result;
MemoryStream? ms;
using (ms = new MemoryStream(1024 * 32))
{
vectorTile.Write(ms);
result = ms.ToArray();
}

File.WriteAllBytes($"{x}_{y}_{z}.mvt", result);
}

private static string ItalyWkt
{
get
{
return File.ReadAllText(Path.Combine("Issues", $"{typeof(Issue29).Name}.wkt"));
}
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand Down Expand Up @@ -31,4 +31,10 @@
</None>
</ItemGroup>

<ItemGroup>
<None Update="Issues\Issue29.wkt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit c40b4b1

Please sign in to comment.