Skip to content

Commit 57ed46e

Browse files
committed
rewrite point2d type to be a geometry string with a type
1 parent 5a2e633 commit 57ed46e

File tree

14 files changed

+102
-146
lines changed

14 files changed

+102
-146
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CIM.PhysicalNetworkModel
2+
{
3+
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://daxgrid.net/PhysicalNetworkModel_0_1")]
4+
public enum GeometryType
5+
{
6+
Point,
7+
LineString
8+
}
9+
}

src/CIM.Core/DAX.CIM.PhysicalNetworkModel/Equipment/LocationExt.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,31 @@
66
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://daxgrid.net/PhysicalNetworkModel_0_1")]
77
public partial class LocationExt : Location
88
{
9-
private Point2D[] coordinatesField;
9+
private GeometryType geometryType;
1010

11-
public Point2D[] coordinates
11+
private string geometry;
12+
13+
public GeometryType GeometryType
14+
{
15+
get
16+
{
17+
return this.geometryType;
18+
}
19+
set
20+
{
21+
this.geometryType = value;
22+
}
23+
}
24+
25+
public string Geometry
1226
{
1327
get
1428
{
15-
return this.coordinatesField;
29+
return this.geometry;
1630
}
1731
set
1832
{
19-
this.coordinatesField = value;
33+
this.geometry = value;
2034
}
2135
}
2236
}

src/CIM.Core/DAX.CIM.PhysicalNetworkModel/Equipment/Point2D.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/CIM.Cson/Converters/Point2DConverter.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/CIM.Mapper/DAX.IO.CIM/Serialization/CIM100/CIM100Serializer.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using DAX.IO.CIM.Processing;
33
using DAX.Util;
44
using System.Globalization;
5+
using System.Text;
56

67
namespace DAX.IO.CIM.Serialization.CIM100
78
{
@@ -2099,20 +2100,29 @@ private Location CreateLocation(double[] coords, CIMIdentifiedObject cimObj)
20992100
loc = new LocationExt() { mRID = GUIDHelper.CreateDerivedGuid(cimObj.mRID, 999, true).ToString() };
21002101
loc.CoordinateSystem = new LocationCoordinateSystem() { @ref = _coordSys.mRID };
21012102

2102-
if (coords != null)
2103+
if (coords is not null)
21032104
{
2104-
List<Point2D> points = new List<Point2D>();
2105-
2106-
for (int i = 0; i < coords.Length; i += 2)
2107-
{
2108-
double x = ((double)coords[i]);
2109-
double y = ((double)coords[i + 1]);
2105+
var stringBuilder = new StringBuilder();
21102106

2111-
points.Add(new Point2D(x, y));
2107+
stringBuilder.AppendLine("[");
21122108

2109+
// LineString
2110+
if (coords.Length > 2)
2111+
{
2112+
((LocationExt)loc).GeometryType = GeometryType.LineString;
2113+
((LocationExt)loc).Geometry = $"[{String.Join(",", coords.Chunk(2).Select(x => $"[{x[0]}, {x[1]}]"))}]";
2114+
}
2115+
// Point
2116+
else if (coords.Length == 2)
2117+
{
2118+
((LocationExt)loc).GeometryType = GeometryType.Point;
2119+
((LocationExt)loc).Geometry = $"[{coords[0]}, {coords[1]}]";
2120+
}
2121+
// Not handled geometry type
2122+
else
2123+
{
2124+
throw new InvalidOperationException($"Could not handle geometry with length '{coords.Length}'.");
21132125
}
2114-
2115-
((LocationExt)loc).coordinates = points.ToArray();
21162126
}
21172127

21182128
return loc;

src/CIM.PostgresImporter.CLI/DynamicSchema.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ private int GetSortNumber()
5050
}
5151
}
5252

53-
#pragma warning disable CA1812 // Type class for utility useage.
54-
internal sealed record Point2D
55-
{
56-
public double X { get; init; }
57-
public double Y { get; init; }
58-
}
59-
#pragma warning restore CA1812
60-
6153
#pragma warning disable CA1812 // Type class for utility useage.
6254
internal sealed class CompositeObject { }
6355
#pragma warning restore CA1812
@@ -253,17 +245,6 @@ private static Type ConvertJsonType(JsonElement jsonElement)
253245
case JsonValueKind.False:
254246
return typeof(bool);
255247
default:
256-
if (jsonElement.TryGetProperty("$type", out var elementType))
257-
{
258-
// Point2D is a special type.
259-
// In the future it might be simplified to an array of arrays with doubles in side.
260-
// Until then it's handled in this way.
261-
if (elementType.GetString() == "Point2D")
262-
{
263-
return typeof(Point2D);
264-
}
265-
}
266-
267248
return typeof(CompositeObject);
268249
}
269250

src/CIM.PostgresImporter.CLI/PostgresImport.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,27 @@ await manyToManyBinaryPostgresWriter
169169
{
170170
parameter = propertyValue.Value.GetRawText();
171171
}
172-
else if (propertyType == typeof(ICollection<Point2D>))
172+
else if (schemaProperty.Key.Equals("Geometry", StringComparison.OrdinalIgnoreCase))
173173
{
174-
var points = propertyValue.Value.Deserialize<ICollection<Point2D>>()?.ToArray()
175-
?? throw new InvalidOperationException("Could not deserialize point array.");
174+
var geometryType = properties.First(x => x.Key == "GeometryType").Value.GetString()!;
176175

177176
Geometry? geometry;
178-
if (points.Length == 1)
177+
178+
var coordinateJson = propertyValue.Value.GetString()!;
179+
180+
if (geometryType.Equals("Point", StringComparison.OrdinalIgnoreCase))
181+
{
182+
var coordinates = JsonSerializer.Deserialize<double[]>(coordinateJson)!;
183+
geometry = new Point(coordinates[0], coordinates[1]);
184+
}
185+
else if (geometryType.Equals("LineString", StringComparison.OrdinalIgnoreCase))
179186
{
180-
geometry = new Point(points[0].X, points[0].Y);
187+
var coordinates = JsonSerializer.Deserialize<double[][]>(coordinateJson)!;
188+
geometry = new LineString(coordinates.Select(x => new Coordinate(x[0], x[1])).ToArray());
181189
}
182190
else
183191
{
184-
geometry = new LineString(points.Select(x => new Coordinate(x.X, x.Y)).ToArray());
192+
throw new InvalidOperationException($"Could not handle: '{geometryType}'.");
185193
}
186194

187195
geometry.SRID = srid;
@@ -195,7 +203,8 @@ await manyToManyBinaryPostgresWriter
195203
await postgresqlBinaryWriter
196204
.WriteAsync(
197205
parameter,
198-
ConvertInternalTypeToPostgresqlType(propertyType))
206+
schemaProperty.Key.Equals("Geometry", StringComparison.OrdinalIgnoreCase)
207+
? NpgsqlDbType.Geometry : ConvertInternalTypeToPostgresqlType(propertyType))
199208
.ConfigureAwait(false);
200209
}
201210
}
@@ -219,7 +228,6 @@ await postgresqlBinaryWriter
219228
{ typeof(Guid), NpgsqlDbType.Uuid },
220229
{ typeof(string), NpgsqlDbType.Text },
221230
{ typeof(bool), NpgsqlDbType.Boolean },
222-
{ typeof(ICollection<Point2D>), NpgsqlDbType.Geometry },
223231
{ typeof(ICollection<Guid>), NpgsqlDbType.Array | NpgsqlDbType.Uuid },
224232
{ typeof(ICollection<string>), NpgsqlDbType.Array | NpgsqlDbType.Text },
225233
{ typeof(CompositeObject), NpgsqlDbType.Jsonb }

src/CIM.PostgresImporter.CLI/PostgresSqlBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private static string Build(SchemaType schemaType, string schemaName, bool addIf
1515
var primaryKeys = string.Join(",", schemaType.Properties.Where(x => x.IsPrimaryKey).Select(x => CustomTableAndColumnNameConverter(x.Name)));
1616

1717
var ifExists = addIfNotExists ? "IF NOT EXISTS" : "";
18-
var columns = string.Join(",\n ", schemaType.Properties.Where(x => !x.ManyToManyAttribute).Select(x => $"\"{CustomTableAndColumnNameConverter(x.Name)}\" {ConvertInternalTypeToPostgresqlType(x.Type)}"));
18+
var columns = string.Join(",\n ", schemaType.Properties.Where(x => !x.ManyToManyAttribute).Select(x => $"\"{CustomTableAndColumnNameConverter(x.Name)}\" {ConvertInternalTypeToPostgresqlType(x.Type, x.Name)}"));
1919
return @$"
2020
CREATE TABLE {ifExists} ""{schemaName}"".""{CustomTableAndColumnNameConverter(schemaType.Name)}"" (
2121
{columns},
@@ -38,9 +38,13 @@ public static string CustomTableAndColumnNameConverter(string x)
3838
#pragma warning restore CA1308
3939
}
4040

41-
private static string ConvertInternalTypeToPostgresqlType(Type type)
41+
private static string ConvertInternalTypeToPostgresqlType(Type type, string propertyName)
4242
{
43-
if (type == typeof(int))
43+
if (propertyName.Equals("geometry", StringComparison.OrdinalIgnoreCase))
44+
{
45+
return "GEOMETRY";
46+
}
47+
else if (type == typeof(int))
4448
{
4549
return "INTEGER";
4650
}
@@ -60,10 +64,6 @@ private static string ConvertInternalTypeToPostgresqlType(Type type)
6064
{
6165
return "BOOLEAN";
6266
}
63-
else if (type == typeof(ICollection<Point2D>))
64-
{
65-
return $"GEOMETRY";
66-
}
6767
else if (type == typeof(ICollection<Guid>))
6868
{
6969
return $"UUID[]";

src/CIM.PostgresImporter.CLI/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ await CimReader
220220
logger.LogInformation($"Total CIM objects imported: '{totalInsertCountByType.Sum(x => x.Value)}'.");
221221
foreach (var insertionCountPair in totalInsertCountByType)
222222
{
223-
logger.LogInformation($"Inserted a total of '{insertionCountPair.Value}' of type '{insertionCountPair.Key}'.");
223+
// This is done to handle relationship types, so they're not written out, since the count will always be 0.
224+
if (insertionCountPair.Value > 0)
225+
{
226+
logger.LogInformation($"Inserted a total of '{insertionCountPair.Value}' of type '{insertionCountPair.Key}'.");
227+
}
224228
}
225229
}
226230
}

src/CIM.PowerFactoryExporter/CGMES/DL_Writer.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using Newtonsoft.Json;
89

910
namespace CIM.PowerFactoryExporter
1011
{
@@ -62,16 +63,18 @@ public void AddLocation(Guid psrId, PhysicalNetworkModel.LocationExt loc)
6263
{
6364
Guid locationId = AddLocation(psrId, Guid.Parse(loc.mRID));
6465

65-
if (loc.coordinates.Length == 1)
66+
if (loc.GeometryType == PhysicalNetworkModel.GeometryType.Point)
6667
{
67-
AddPositionPoint(locationId, 0, loc.coordinates[0].X, loc.coordinates[0].Y);
68+
var point = JsonConvert.DeserializeObject<double[]>(loc.Geometry);
69+
AddPositionPoint(locationId, 0, point[0], point[1]);
6870
}
6971
else
7072
{
7173
int seqNo = 1;
72-
foreach (var coord in loc.coordinates)
74+
var points = JsonConvert.DeserializeObject<double[][]>(loc.Geometry);
75+
foreach (var point in points)
7376
{
74-
AddPositionPoint(locationId, seqNo, coord.X, coord.Y);
77+
AddPositionPoint(locationId, seqNo, point[0], point[1]);
7578
seqNo++;
7679
}
7780
}

0 commit comments

Comments
 (0)