Skip to content

Commit

Permalink
Tests expect geometries in the correct orientation.
Browse files Browse the repository at this point in the history
  • Loading branch information
airbreather committed Aug 4, 2023
1 parent 68d630f commit 14433e0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions test/NetTopologySuite.IO.GeoJSON.Test/GeoJsonFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Utilities;

namespace NetTopologySuite.IO.GeoJSON.Test
{
Expand All @@ -12,6 +13,18 @@ namespace NetTopologySuite.IO.GeoJSON.Test

public class GeoJsonFixture : AbstractIOFixture
{
public static Geometry InExpectedOrientation(Geometry gIn)
{
return GeoJsonSerializer.RingOrientationOption == RingOrientationOption.DoNotModify
? gIn
: new GeometryEditor().Edit(gIn, new EnsureOrientationOperation(GeoJsonSerializer.RingOrientationOption));
}

protected override void CheckEquality(Geometry gIn, Geometry gParsed, WKTWriter writer)
{
base.CheckEquality(InExpectedOrientation(gIn), gParsed, writer);
}

protected override Geometry Read(byte[] b)
{
string json;
Expand Down Expand Up @@ -62,5 +75,43 @@ public override void TestGeometryCollection()
{
base.TestGeometryCollection();
}

private sealed class EnsureOrientationOperation : GeometryEditor.IGeometryEditorOperation
{
private readonly RingOrientationOption _orientation;

public EnsureOrientationOperation(RingOrientationOption orientation)
{
_orientation = orientation;
}

public Geometry Edit(Geometry geometry, GeometryFactory factory)
{
if (geometry is not Polygon polygon || polygon.IsEmpty)
{
return geometry;
}

var rings = new LinearRing[polygon.NumInteriorRings + 1];
rings[0] = polygon.Shell;
polygon.Holes.CopyTo(rings.AsSpan(1));
bool[] shouldBeCCW = new bool[rings.Length];
shouldBeCCW.AsSpan().Fill(_orientation == RingOrientationOption.NtsGeoJsonV2);
shouldBeCCW[0] = !shouldBeCCW[0];
bool anyModified = false;
for (int i = 0; i < rings.Length; i++)
{
if (rings[i].IsCCW != shouldBeCCW[i])
{
rings[i] = factory.CreateLinearRing(rings[i].CoordinateSequence.Reversed());
anyModified = true;
}
}

return anyModified
? factory.CreatePolygon(rings[0], rings[1..])
: geometry;
}
}
}
}
2 changes: 1 addition & 1 deletion test/NetTopologySuite.IO.GeoJSON.Test/GeoJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static void Deserialize(string result, Geometry geom)
throw new Exception();

Console.WriteLine(des.AsText());
Assert.IsTrue(des.EqualsExact(geom));
Assert.IsTrue(des.EqualsExact(GeoJsonFixture.InExpectedOrientation(geom)));
}

[Test, Ignore("CoordinateConverter no longer added to serializer")]
Expand Down

0 comments on commit 14433e0

Please sign in to comment.