From d882d3a43b2e32abe30376f1e343d8e949650529 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Mon, 30 Sep 2024 09:21:54 +0200 Subject: [PATCH] Prefix JSON geometry representation with SRID Fixes #3236 --- .../NpgsqlJsonGeometryWktReaderWriter.cs | 13 ++- .../JsonTypesNpgsqlTest.cs | 98 +++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/EFCore.PG.NTS/Storage/Internal/NpgsqlJsonGeometryWktReaderWriter.cs b/src/EFCore.PG.NTS/Storage/Internal/NpgsqlJsonGeometryWktReaderWriter.cs index 3b5920e27..4685aa0ce 100644 --- a/src/EFCore.PG.NTS/Storage/Internal/NpgsqlJsonGeometryWktReaderWriter.cs +++ b/src/EFCore.PG.NTS/Storage/Internal/NpgsqlJsonGeometryWktReaderWriter.cs @@ -29,7 +29,18 @@ public override Geometry FromJsonTyped(ref Utf8JsonReaderManager manager, object /// public override void ToJsonTyped(Utf8JsonWriter writer, Geometry value) - => writer.WriteStringValue(value.ToText()); + { + var wkt = value.ToText(); + + // If the SRID is defined, prefix the WKT with it (SRID=4326;POINT(-44.3 60.1)) + // Although this is a PostgreSQL extension, NetTopologySuite supports it (see #3236) + if (value.SRID > 0) + { + wkt = $"SRID={value.SRID};{wkt}"; + } + + writer.WriteStringValue(wkt); + } /// public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty); diff --git a/test/EFCore.PG.FunctionalTests/JsonTypesNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/JsonTypesNpgsqlTest.cs index 575433a57..1f15efa71 100644 --- a/test/EFCore.PG.FunctionalTests/JsonTypesNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/JsonTypesNpgsqlTest.cs @@ -3,6 +3,8 @@ using System.Collections; using System.Globalization; using System.Numerics; +using NetTopologySuite; +using NetTopologySuite.Geometries; using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure; using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities; using Xunit.Sdk; @@ -463,6 +465,102 @@ public virtual Task Can_read_write_LogSequenceNumber_JSON_values(ulong value, st new NpgsqlLogSequenceNumber(value), json); + [ConditionalFact] + public override async Task Can_read_write_point() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(PointType.Point), + factory.CreatePoint(new Coordinate(2, 4)), + """{"Prop":"SRID=4326;POINT (2 4)"}"""); + } + + [ConditionalFact] + public virtual async Task Can_read_write_point_without_SRID() + => await Can_read_and_write_JSON_value( + nameof(PointType.Point), + new Point(2, 4), + """{"Prop":"POINT (2 4)"}"""); + + [ConditionalFact] + public override async Task Can_read_write_point_with_M() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(PointMType.PointM), + factory.CreatePoint(new CoordinateM(2, 4, 6)), + """{"Prop":"SRID=4326;POINT (2 4)"}"""); + } + + public override async Task Can_read_write_point_with_Z() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(PointZType.PointZ), + factory.CreatePoint(new CoordinateZ(2, 4, 6)), + """{"Prop":"SRID=4326;POINT Z(2 4 6)"}"""); + } + + public override async Task Can_read_write_point_with_Z_and_M() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(PointZMType.PointZM), + factory.CreatePoint(new CoordinateZM(1, 2, 3, 4)), + """{"Prop":"SRID=4326;POINT Z(1 2 3)"}"""); + } + + [ConditionalFact] + public override async Task Can_read_write_line_string() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(LineStringType.LineString), + factory.CreateLineString([new Coordinate(0, 0), new Coordinate(1, 0)]), + """{"Prop":"SRID=4326;LINESTRING (0 0, 1 0)"}"""); + } + + public override async Task Can_read_write_multi_line_string() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(MultiLineStringType.MultiLineString), + factory.CreateMultiLineString( + [ + factory.CreateLineString( + [new Coordinate(0, 0), new Coordinate(0, 1)]), + factory.CreateLineString( + [new Coordinate(1, 0), new Coordinate(1, 1)]) + ]), + """{"Prop":"SRID=4326;MULTILINESTRING ((0 0, 0 1), (1 0, 1 1))"}"""); + } + + public override async Task Can_read_write_polygon() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(PolygonType.Polygon), + factory.CreatePolygon([new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(0, 1), new Coordinate(0, 0)]), + """{"Prop":"SRID=4326;POLYGON ((0 0, 1 0, 0 1, 0 0))"}"""); + } + + public override async Task Can_read_write_polygon_typed_as_geometry() + { + var factory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); + + await Can_read_and_write_JSON_value( + nameof(GeometryType.Geometry), + factory.CreatePolygon([new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(0, 1), new Coordinate(0, 0)]), + """{"Prop":"SRID=4326;POLYGON ((0 0, 1 0, 0 1, 0 0))"}"""); + } + protected class LogSequenceNumberType { public NpgsqlLogSequenceNumber LogSequenceNumber { get; set; }