Skip to content

Commit

Permalink
now rounds to 8th decimal on compare coordinate changes (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Mar 25, 2023
1 parent 8608dfe commit 08fae08
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private async Task<bool> IsValidNodeUpdate(RouteNode shadowTableNode, RouteNode
}

private bool AlreadyUpdated(RouteNode routeNode, RouteNode routeNodeShadowTable)
=> routeNode.MarkAsDeleted == routeNodeShadowTable.MarkAsDeleted && routeNode.GetGeoJsonCoordinate() == routeNodeShadowTable.GetGeoJsonCoordinate();
=> routeNode.MarkAsDeleted == routeNodeShadowTable.MarkAsDeleted && routeNode.GetGeoJsonCoordinate(true) == routeNodeShadowTable.GetGeoJsonCoordinate(true);

private bool IsCreatedByApplication(RouteNode routeNode)
=> routeNode.ApplicationName == _applicationSettings.ApplicationName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public RouteNodeAdded CreateAdded(RouteNode routeNode)
routeNode?.MappingInfo,
routeNode?.SafetyInfo,
routeNode.Mrid,
routeNode.GetGeoJsonCoordinate(),
routeNode.GetGeoJsonCoordinate(false),
routeNode.RouteNodeInfo);
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public RouteNodeGeometryModified CreateGeometryModified(RouteNode routeNode)
routeNode?.ApplicationName,
routeNode?.ApplicationInfo,
routeNode.Mrid,
routeNode.GetGeoJsonCoordinate());
routeNode.GetGeoJsonCoordinate(false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private List<INotification> HandleExistingRouteSegmentSplitted(int intersectingS

private bool AlreadyUpdated(RouteSegment routeSegment, RouteSegment shadowTableRouteSegment)
{
return routeSegment.MarkAsDeleted == shadowTableRouteSegment.MarkAsDeleted && routeSegment.GetGeoJsonCoordinate() == shadowTableRouteSegment.GetGeoJsonCoordinate();
return routeSegment.MarkAsDeleted == shadowTableRouteSegment.MarkAsDeleted && routeSegment.GetGeoJsonCoordinate(true) == shadowTableRouteSegment.GetGeoJsonCoordinate(true);
}

private bool IsCreatedByApplication(RouteSegment routeSegment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public RouteSegmentGeometryModified CreateGeometryModified(RouteSegment routeSeg
useApplicationName ? _applicationSettings.ApplicationName : routeSegment?.ApplicationName,
routeSegment?.ApplicationInfo,
routeSegment.Mrid,
routeSegment.GetGeoJsonCoordinate());
routeSegment.GetGeoJsonCoordinate(false));
}

public RouteSegmentMarkedForDeletion CreateMarkedForDeletion(RouteSegment routeSegment, bool useApplicationName = false)
Expand Down Expand Up @@ -78,7 +78,7 @@ public RouteSegmentAdded CreateAdded(RouteSegment routeSegment, RouteNode startR
routeSegment.Mrid,
startRouteNode.Mrid,
endRouteNode.Mrid,
routeSegment.GetGeoJsonCoordinate(),
routeSegment.GetGeoJsonCoordinate(false),
routeSegment?.RouteSegmentInfo);
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/OpenFTTH.GDBIntegrator.RouteNetwork/RouteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public virtual Point GetPoint()
return (Point)geometry;
}

public virtual string GetGeoJsonCoordinate()
public virtual string GetGeoJsonCoordinate(bool round)
{
var wkbReader = new WKBReader();
var geometry = wkbReader.Read(Coord);
var point = (Point)geometry;
var point = round ? RoundPoint((Point)geometry) : (Point)geometry;

var serializer = GeoJsonSerializer.Create();

using (var stringWriter = new StringWriter())
Expand All @@ -68,5 +69,25 @@ public virtual string GetGeoJsonCoordinate()
return JObject.Parse(geoJson)["coordinates"].ToString(Formatting.None);
};
}

private Point RoundPoint(Point inputPoint)
{
const int roundDecimal = 8;
var newCoord = new CoordinateZ();

newCoord.X = Math.Round(inputPoint.X, roundDecimal);
newCoord.Y = Math.Round(inputPoint.Y, roundDecimal);

if (inputPoint.Z != Coordinate.NullOrdinate)
{
newCoord.Z = Math.Round(inputPoint.Z, roundDecimal);
}
else
{
newCoord.Z = Coordinate.NullOrdinate;
}

return new Point(newCoord);
}
}
}
31 changes: 27 additions & 4 deletions src/OpenFTTH.GDBIntegrator.RouteNetwork/RouteSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json.Linq;
using OpenFTTH.Events.Core.Infos;
using OpenFTTH.Events.RouteNetwork.Infos;
using System.Collections.Generic;

namespace OpenFTTH.GDBIntegrator.RouteNetwork
{
Expand Down Expand Up @@ -43,9 +44,9 @@ public virtual Point FindEndPoint()
return endPoint;
}

public virtual string GetGeoJsonCoordinate()
public virtual string GetGeoJsonCoordinate(bool round)
{
var lineString = GetLineString();
var lineString = round ? RoundLineString(GetLineString()) : GetLineString();
var serializer = GeoJsonSerializer.Create();

using (var stringWriter = new StringWriter())
Expand All @@ -54,15 +55,37 @@ public virtual string GetGeoJsonCoordinate()
var geoJson = stringWriter.ToString();
return JObject.Parse(geoJson)["coordinates"].ToString(Formatting.None);
};

}

public virtual LineString GetLineString()
{
var wkbReader = new WKBReader();
var geometry = wkbReader.Read(Coord);
var lineString = (LineString)geometry;
return (LineString)geometry;
}

private LineString RoundLineString(LineString inputLineString)
{
const int roundDecimal = 8;

var newCoords = new List<CoordinateZ>();
foreach (var coord in inputLineString.Coordinates)
{
var c = new CoordinateZ();
c.X = Math.Round(coord.X, roundDecimal);
c.Y = Math.Round(coord.Y, roundDecimal);
if (coord.Z != Coordinate.NullOrdinate)
{
c.Z = Math.Round(coord.Z, roundDecimal);
}
else
{
c.Z = Coordinate.NullOrdinate;
}
}

return lineString;
return new LineString(newCoords.ToArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ public async Task CreateUpdatedEvent_ShouldReturnRollbackInvalidRouteNodeOperati
A.CallTo(() => geoDatabase.GetIntersectingRouteSegments(afterNode)).Returns(new List<RouteSegment>());
A.CallTo(() => afterNode.GetPoint()).Returns(CreatePoint(565931.4446905176, 6197297.75114815));
A.CallTo(() => shadowTableRouteNode.GetPoint()).Returns(CreatePoint(565920.4446905176, 6197297.74114815));
A.CallTo(() => afterNode.GetGeoJsonCoordinate()).Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate()).Returns("[565920.4446905176,6197297.74114815]");
A.CallTo(() => afterNode.GetGeoJsonCoordinate(true)).Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate(true)).Returns("[565920.4446905176,6197297.74114815]");
A.CallTo(() => geoDatabase.GetIntersectingRouteNodes(afterNode)).Returns(new List<RouteNode> { A.Fake<RouteNode>() });

var point = A.Fake<Point>();
Expand Down Expand Up @@ -269,11 +269,11 @@ public async Task CreateUpdatedEvent_ShouldReturnDoNothing_OnRouteNodeMarkedAsDe
A.CallTo(() => geoDatabase.GetRouteNodeShadowTable(afterNode.Mrid, false))
.Returns(integratorRouteNode);

A.CallTo(() => afterNode.GetGeoJsonCoordinate())
A.CallTo(() => afterNode.GetGeoJsonCoordinate(true))
.Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => afterNode.MarkAsDeleted).Returns(true);

A.CallTo(() => integratorRouteNode.GetGeoJsonCoordinate())
A.CallTo(() => integratorRouteNode.GetGeoJsonCoordinate(true))
.Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => integratorRouteNode.MarkAsDeleted).Returns(true);

Expand Down Expand Up @@ -302,10 +302,10 @@ public async Task CreateUpdatedEvent_ShouldReturnRollbackInvalidRouteNode_OnIsMo

A.CallTo(() => applicationSetting.Value).Returns(appSettings);
A.CallTo(() => afterNode.Mrid).Returns(Guid.NewGuid());
A.CallTo(() => afterNode.GetGeoJsonCoordinate()).Returns("[552428.7508312801, 6188868.185819111]");
A.CallTo(() => afterNode.GetGeoJsonCoordinate(true)).Returns("[552428.7508312801, 6188868.185819111]");
A.CallTo(() => afterNode.GetPoint()).Returns(point);
A.CallTo(() => geoDatabase.GetRouteNodeShadowTable(afterNode.Mrid, false)).Returns(shadowTableRouteNode);
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate()).Returns("[552428.7515896157,6188868.184787691]");
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate(true)).Returns("[552428.7515896157,6188868.184787691]");
A.CallTo(() => shadowTableRouteNode.GetPoint()).Returns(CreatePoint(552428.7515896157, 6188868.184787691));

var routeNodeValidator = A.Fake<IRouteNodeValidator>();
Expand Down Expand Up @@ -409,9 +409,9 @@ public async Task CreateUpdatedEvent_ShouldReturnRollbackInvalidRouteNode_OnInte
A.CallTo(() => applicationSetting.Value).Returns(appSettingsValue);
A.CallTo(() => afterNode.Mrid).Returns(Guid.NewGuid());
A.CallTo(() => geoDatabase.GetRouteNodeShadowTable(afterNode.Mrid, false)).Returns(shadowTableRouteNode);
A.CallTo(() => afterNode.GetGeoJsonCoordinate()).Returns("[665931.4446905176,7197297.75114815]");
A.CallTo(() => afterNode.GetGeoJsonCoordinate(true)).Returns("[665931.4446905176,7197297.75114815]");
A.CallTo(() => afterNode.MarkAsDeleted).Returns(false);
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate()).Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate(true)).Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.MarkAsDeleted).Returns(false);
A.CallTo(() => afterNode.GetPoint()).Returns(CreatePoint(665931.4446905176, 7197297.75114815));
A.CallTo(() => shadowTableRouteNode.GetPoint()).Returns(CreatePoint(565931.4446905176, 6197297.75114815));
Expand Down Expand Up @@ -449,9 +449,9 @@ public async Task CreateUpdatedEvent_ShouldReturnRouteNodeLocationChanged_OnRout
A.CallTo(() => geoDatabase.GetRouteNodeShadowTable(afterNode.Mrid, false)).Returns(shadowTableRouteNode);
A.CallTo(() => afterNode.GetPoint()).Returns(CreatePoint(665931.4446905176, 7197297.75114815));
A.CallTo(() => shadowTableRouteNode.GetPoint()).Returns(CreatePoint(565931.4446905176, 6197297.75114815));
A.CallTo(() => afterNode.GetGeoJsonCoordinate()).Returns("[665931.4446905176,7197297.75114815]");
A.CallTo(() => afterNode.GetGeoJsonCoordinate(true)).Returns("[665931.4446905176,7197297.75114815]");
A.CallTo(() => afterNode.MarkAsDeleted).Returns(false);
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate()).Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate(true)).Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.MarkAsDeleted).Returns(false);

var point = A.Fake<Point>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ public async Task CreateUpdatedEvent_ShouldReturnDoNothing_OnRouteSegmentAfterAn
A.CallTo(() => routeSegmentAfter.Mrid).Returns(Guid.NewGuid());
A.CallTo(() => routeSegmentShadowTable.Mrid).Returns(routeSegmentAfter.Mrid);
A.CallTo(() => geoDatabase.GetRouteSegmentShadowTable(routeSegmentAfter.Mrid, false)).Returns(routeSegmentShadowTable);
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.MarkAsDeleted).Returns(false);
A.CallTo(() => routeSegmentAfter.MarkAsDeleted).Returns(false);

Expand All @@ -550,8 +550,8 @@ public async Task CreateUpdatedEvent_ShouldThrowException_OnGeometryBeingInvalid
A.CallTo(() => routeSegmentShadowTable.Mrid).Returns(routeSegmentAfter.Mrid);
A.CallTo(() => routeSegmentAfter.GetLineString()).Returns(A.Fake<LineString>());

A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.MarkAsDeleted).Returns(false);
A.CallTo(() => routeSegmentShadowTable.MarkAsDeleted).Returns(false);

Expand Down Expand Up @@ -600,8 +600,8 @@ public async Task CreateUpdatedEvent_ShouldReturnTwoSplitEventsAndConnectivityCh
A.CallTo(() => routeSegmentShadowTable.Mrid).Returns(routeSegmentAfter.Mrid);
A.CallTo(() => routeSegmentAfter.GetLineString()).Returns(A.Fake<LineString>());

A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.MarkAsDeleted).Returns(false);
A.CallTo(() => routeSegmentShadowTable.MarkAsDeleted).Returns(false);

Expand Down Expand Up @@ -645,8 +645,8 @@ public async Task CreateUpdatedEvent_ShouldReturnRouteSegmentLocationChanged_OnG
A.CallTo(() => routeSegmentShadowTable.Mrid).Returns(routeSegmentAfter.Mrid);
A.CallTo(() => routeSegmentAfter.GetLineString()).Returns(A.Fake<LineString>());

A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.MarkAsDeleted).Returns(false);
A.CallTo(() => routeSegmentShadowTable.MarkAsDeleted).Returns(false);

Expand Down Expand Up @@ -747,8 +747,8 @@ public async Task CreateUpdatedEvent_ShouldThrowException_OnStartRouteNodeCountB
.Returns(new ApplicationSetting { ApplicationName = "GDB_INTEGRATOR" });

A.CallTo(() => routeSegmentAfter.GetLineString()).Returns(A.Fake<LineString>());
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.MarkAsDeleted).Returns(false);
A.CallTo(() => routeSegmentShadowTable.MarkAsDeleted).Returns(false);

Expand Down Expand Up @@ -777,8 +777,8 @@ public async Task CreateUpdatedEvent_ShouldThrowException_OnEndRouteNodeCountBei
.Returns(new ApplicationSetting { ApplicationName = "GDB_INTEGRATOR" });

A.CallTo(() => routeSegmentAfter.GetLineString()).Returns(A.Fake<LineString>());
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate()).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 578238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentShadowTable.GetGeoJsonCoordinate(true)).Returns("LINESTRING(578223.64355838 6179284.23759438, 378238.4182511 6179279.78494725)");
A.CallTo(() => routeSegmentAfter.MarkAsDeleted).Returns(false);
A.CallTo(() => routeSegmentShadowTable.MarkAsDeleted).Returns(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void GetGeoJsonCoordinate_ShouldReturnGeoJsonCoordinate_OnCalled()

var routeNode = new RouteNode(mrid, coord, workTaskMrId, username, applicationName);

var result = routeNode.GetGeoJsonCoordinate();
var result = routeNode.GetGeoJsonCoordinate(false);

result.Should().BeEquivalentTo("[565931.4446905176,6197297.75114815]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void GetGeoJsonCoordinate_ShouldReturnGeoJsonCoordinate_OnBeingCalled(str
Coord = Convert.FromBase64String(coord),
};

var geoJson = routeSegment.GetGeoJsonCoordinate();
var geoJson = routeSegment.GetGeoJsonCoordinate(false);

geoJson.Should().BeEquivalentTo(expected);
}
Expand Down

0 comments on commit 08fae08

Please sign in to comment.