Skip to content

Commit

Permalink
now saves coordinates in a before and after coord column (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Mar 26, 2023
1 parent 6de8117 commit be21689
Show file tree
Hide file tree
Showing 28 changed files with 84 additions and 573 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ CREATE TABLE route_network.route_network_edit_operation (
seq_no BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,
event_id UUID NULL,
"before" VARCHAR NULL,
before_coord geometry NULL,
"after" VARCHAR NULL,
after_coord geometry NULL,
"type" VARCHAR NOT NULL,
event_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT route_network_edit_operation_pkey PRIMARY KEY (seq_no)
Expand All @@ -15,11 +17,13 @@ CREATE OR REPLACE FUNCTION route_network.route_node_create()
LANGUAGE plpgsql
AS $function$
BEGIN
insert into route_network.route_network_edit_operation (event_id, before, after, type)
insert into route_network.route_network_edit_operation (event_id, before, before_coord, after, after_coord, type)
values (
uuid_generate_v4(),
null,
null,
to_json(NEW),
NEW.coord,
'RouteNode'
);
RETURN NEW;
Expand All @@ -42,11 +46,13 @@ BEGIN
RETURN null;
END IF;

insert into route_network.route_network_edit_operation (event_id, before, after, type)
insert into route_network.route_network_edit_operation (event_id, before, before_coord, after, after_coord, type)
values (
uuid_generate_v4(),
to_json(OLD),
OLD.coord,
to_json(NEW),
NEW.coord,
'RouteNode'
);

Expand All @@ -60,11 +66,13 @@ CREATE OR REPLACE FUNCTION route_network.route_segment_create()
LANGUAGE plpgsql
AS $function$
BEGIN
insert into route_network.route_network_edit_operation (event_id, before, after, type)
insert into route_network.route_network_edit_operation (event_id, before, before_coord, after, after_coord, type)
values (
uuid_generate_v4(),
null,
null,
to_json(NEW),
NEW.coord,
'RouteSegment'
);
RETURN NEW;
Expand All @@ -87,11 +95,13 @@ BEGIN
RETURN null;
END IF;

insert into route_network.route_network_edit_operation (event_id, before, after, type)
insert into route_network.route_network_edit_operation (event_id, before, before_coord, after, after_coord, type)
values (
uuid_generate_v4(),
to_json(OLD),
OLD.coord,
to_json(NEW),
NEW.coord,
'RouteSegment'
);

Expand Down
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(true) == routeNodeShadowTable.GetGeoJsonCoordinate(true);
=> routeNode.MarkAsDeleted == routeNodeShadowTable.MarkAsDeleted && routeNode.GetGeoJsonCoordinate() == routeNodeShadowTable.GetGeoJsonCoordinate();

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(false),
routeNode.GetGeoJsonCoordinate(),
routeNode.RouteNodeInfo);
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public RouteNodeGeometryModified CreateGeometryModified(RouteNode routeNode)
routeNode?.ApplicationName,
routeNode?.ApplicationInfo,
routeNode.Mrid,
routeNode.GetGeoJsonCoordinate(false));
routeNode.GetGeoJsonCoordinate());
}
}
}
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(true) == shadowTableRouteSegment.GetGeoJsonCoordinate(true);
return routeSegment.MarkAsDeleted == shadowTableRouteSegment.MarkAsDeleted && routeSegment.GetGeoJsonCoordinate() == shadowTableRouteSegment.GetGeoJsonCoordinate();
}

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(false));
routeSegment.GetGeoJsonCoordinate());
}

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(false),
routeSegment.GetGeoJsonCoordinate(),
routeSegment?.RouteSegmentInfo);
}
}
Expand Down
24 changes: 2 additions & 22 deletions src/OpenFTTH.GDBIntegrator.RouteNetwork/RouteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public virtual Point GetPoint()
return (Point)geometry;
}

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

var serializer = GeoJsonSerializer.Create();

Expand All @@ -69,25 +69,5 @@ public virtual string GetGeoJsonCoordinate(bool round)
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);
}
}
}
29 changes: 2 additions & 27 deletions src/OpenFTTH.GDBIntegrator.RouteNetwork/RouteSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public virtual Point FindEndPoint()
return endPoint;
}

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

using (var stringWriter = new StringWriter())
Expand All @@ -63,30 +63,5 @@ public virtual LineString GetLineString()
var geometry = wkbReader.Read(Coord);
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;
}

newCoords.Add(c);
}

return new LineString(newCoords.ToArray());
}
}
}
28 changes: 0 additions & 28 deletions src/OpenFTTH.GDBIntegrator.Subscriber/Postgres/GeoJsonConverter.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private async Task SubscribeEditOperationEvents(
int intervalMs,
CancellationToken token = default)
{
const string GET_LATEST_SQL = @"SELECT *
const string GET_LATEST_SQL = @"SELECT seq_no, event_id, before, ST_AsBinary(before_coord) AS before_coord, after, ST_AsBinary(after_coord) AS after_coord, type
FROM route_network.route_network_edit_operation
WHERE seq_no > @seq_no
ORDER BY seq_no";
Expand All @@ -199,14 +199,18 @@ WHERE seq_no > @seq_no
var sequenceNumber = reader.GetInt64(reader.GetOrdinal("seq_no"));
var eventId = reader.GetGuid(reader.GetOrdinal("event_id"));
var before = reader.GetValue(reader.GetOrdinal("before")) as string ?? null;
var beforeCoord = reader.GetValue(reader.GetOrdinal("before_coord")) as byte[] ?? null;
var after = reader.GetValue(reader.GetOrdinal("after")) as string ?? null;
var afterCoord = reader.GetValue(reader.GetOrdinal("after_coord")) as byte[] ?? null;
var type = reader.GetString(reader.GetOrdinal("type"));

var editOperation = new RouteNetworkEditOperation(
sequenceNumber: sequenceNumber,
eventId: eventId,
before: before,
beforeCoord: beforeCoord,
after: after,
afterCoord: afterCoord,
type: type
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public sealed record PostgresRouteNode
[JsonPropertyName("mrid")]
public Guid Mrid { get; init; }

[JsonPropertyName("coord")]
[JsonConverter(typeof(GeoJsonConverter<Point>))]
public Point Coord { get; init; }

[JsonPropertyName("marked_to_be_deleted")]
public bool MarkedToBeDeleted { get; init; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public sealed record PostgresRouteSegment
[JsonPropertyName("mrid")]
public Guid Mrid { get; init; }

[JsonPropertyName("coord")]
[JsonConverter(typeof(GeoJsonConverter<LineString>))]
public LineString Coord { get; init; }

[JsonPropertyName("marked_to_be_deleted")]
public bool MarkedToBeDeleted { get; init; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using NetTopologySuite.IO;
using OpenFTTH.Events.Core.Infos;
using OpenFTTH.Events.RouteNetwork.Infos;
using OpenFTTH.GDBIntegrator.Integrator.ConsumerMessages;
Expand Down Expand Up @@ -34,12 +33,12 @@ public RouteNodeMessage Create(RouteNetworkEditOperation editOperation)

return new RouteNodeMessage(
eventId: editOperation.EventId,
before: ParseRouteNode(editOperation.Before),
after: ParseRouteNode(editOperation.After)
before: ParseRouteNode(editOperation.Before, editOperation.BeforeCoord),
after: ParseRouteNode(editOperation.After, editOperation.AfterCoord)
);
}

private RouteNode ParseRouteNode(string json)
private RouteNode ParseRouteNode(string json, byte[] coord)
{
if (String.IsNullOrEmpty(json))
{
Expand All @@ -52,13 +51,11 @@ private RouteNode ParseRouteNode(string json)
return null;
}

var wkbWriter = new WKBWriter();

var mappedRouteNode = new RouteNode
{
ApplicationInfo = pgRouteNode.ApplicationInfo,
ApplicationName = pgRouteNode.ApplicationName,
Coord = pgRouteNode.Coord is null ? null : wkbWriter.Write(pgRouteNode.Coord),
Coord = coord,
MarkAsDeleted = pgRouteNode.MarkedToBeDeleted,
DeleteMe = pgRouteNode.DeleteMe,
Mrid = pgRouteNode.Mrid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using NetTopologySuite.IO;
using OpenFTTH.Events.Core.Infos;
using OpenFTTH.Events.RouteNetwork.Infos;
using OpenFTTH.GDBIntegrator.Integrator.ConsumerMessages;
Expand Down Expand Up @@ -34,12 +33,12 @@ public RouteSegmentMessage Create(RouteNetworkEditOperation editOperation)

return new RouteSegmentMessage(
eventId: editOperation.EventId,
before: ParseRouteSegment(editOperation.Before),
after: ParseRouteSegment(editOperation.After)
before: ParseRouteSegment(editOperation.Before, editOperation.BeforeCoord),
after: ParseRouteSegment(editOperation.After, editOperation.AfterCoord)
);
}

private RouteSegment ParseRouteSegment(string json)
private RouteSegment ParseRouteSegment(string json, byte[] coord)
{
if (String.IsNullOrEmpty(json))
{
Expand All @@ -52,13 +51,11 @@ private RouteSegment ParseRouteSegment(string json)
return null;
}

var wkbWriter = new WKBWriter();

var mappedRouteSegment = new RouteSegment
{
ApplicationInfo = pgRouteSegment.ApplicationInfo,
ApplicationName = pgRouteSegment.ApplicationName,
Coord = pgRouteSegment.Coord is null ? null : wkbWriter.Write(pgRouteSegment.Coord),
Coord = coord,
MarkAsDeleted = pgRouteSegment.MarkedToBeDeleted,
DeleteMe = pgRouteSegment.DeleteMe,
Mrid = pgRouteSegment.Mrid,
Expand Down
10 changes: 10 additions & 0 deletions src/OpenFTTH.GDBIntegrator.Subscriber/RouteNetworkEditOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public sealed record RouteNetworkEditOperation
[JsonPropertyName("before")]
public string Before { get; init; }

[JsonPropertyName("before_coord")]
public byte[] BeforeCoord { get; init; }

[JsonPropertyName("after")]
public string After { get; init; }

[JsonPropertyName("after_coord")]
public byte[] AfterCoord { get; init; }

[JsonPropertyName("type")]
public string Type { get; init; }

Expand All @@ -25,13 +31,17 @@ public RouteNetworkEditOperation(
long sequenceNumber,
Guid eventId,
string before,
byte[] beforeCoord,
string after,
byte[] afterCoord,
string type)
{
SequenceNumber = sequenceNumber;
EventId = eventId;
Before = before;
BeforeCoord = beforeCoord;
After = after;
AfterCoord = afterCoord;
Type = type;
}
}
Loading

0 comments on commit be21689

Please sign in to comment.