Skip to content

Commit

Permalink
Merge commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FObermaier committed May 15, 2024
2 parents cce95ef + ac2eee0 commit dc36f53
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/NetTopologySuite.IO.SqlServerBytes/SqlServerBytesWriter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Properties;

using Figure = NetTopologySuite.IO.Serialization.Figure;
using FigureAttribute = NetTopologySuite.IO.Serialization.FigureAttribute;
using Geography = NetTopologySuite.IO.Serialization.Geography;
Expand All @@ -22,7 +20,9 @@ public class SqlServerBytesWriter
{
private bool _emitZ = true;
private bool _emitM = true;

private Func<Geometry, bool> _geometryValidationFn;
private static readonly Func<Geometry, bool> DefaultGeometryValidationFn = (g) => g.IsValid;

/// <summary>
/// Gets or sets the desired <see cref="IO.ByteOrder"/>. Returns <see cref="IO.ByteOrder.LittleEndian"/> since
/// it's required. Setting does nothing.
Expand Down Expand Up @@ -87,6 +87,15 @@ public virtual Ordinates HandleOrdinates
/// </summary>
public virtual bool IsGeography { get; set; }

/// <summary>
/// Gets or sets a validator for a geometry. The result of this validator will be used to set the Valid flag of the Geography in SQL Server
/// </summary>
public Func<Geometry, bool> GeometryValidator
{
get => _geometryValidationFn ?? DefaultGeometryValidationFn;
set => _geometryValidationFn = value;
}

/// <summary>
/// Writes a binary representation of a given geometry.
/// </summary>
Expand Down Expand Up @@ -137,7 +146,7 @@ private Geography ToGeography(Geometry geometry)
var geography = new Geography
{
SRID = Math.Max(0, geometry.SRID),
IsValid = geometry.IsValid
IsValid = GeometryValidator(geometry)
};

while (geometries.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,81 @@ public void Types_still_unknown(string wkt)
() => reader.Read(wkt));
}

[Theory]
[InlineData(
"POINT EMPTY",
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF01")]
[InlineData(
"POINT (1 2)",
"000000000108000000000000F03F0000000000000040")]
[InlineData(
"POINT Z(1 2 3)",
"000000000109000000000000F03F00000000000000400000000000000840")]
[InlineData(
"LINESTRING EMPTY",
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF02")]
[InlineData(
"LINESTRING (0 0, 0 1)",
"000000000110000000000000000000000000000000000000000000000000000000000000F03F")]
[InlineData(
"LINESTRING Z(0 0 1, 0 1 2)",
"000000000111000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040")]
//[InlineData(
// "LINESTRING (0 0, 0 1 2)",
// "000000000115000000000000000000000000000000000000000000000000000000000000F03F000000000000F8FF0000000000000040")]
[InlineData(
"LINESTRING (0 0, 0 1, 0 2)",
"00000000010003000000000000000000000000000000000000000000000000000000000000000000F03F0000000000000000000000000000004001000000010000000001000000FFFFFFFF0000000002")]
[InlineData(
"LINESTRING Z(0 0 1, 0 1 2, 0 2 3)",
"00000000010103000000000000000000000000000000000000000000000000000000000000000000F03F00000000000000000000000000000040000000000000F03F0000000000000040000000000000084001000000010000000001000000FFFFFFFF0000000002")]
[InlineData(
"POLYGON EMPTY",
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF03")]
// 0xE61000000104000000000000000001000000FFFFFFFFFFFFFFFF03
[InlineData(
"POLYGON ((0 0, 0 1, 1 1, 0 0))",
"00000000010004000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000001000000020000000001000000FFFFFFFF0000000003")]
[InlineData(
"POLYGON ((0 0, 0 3, 3 3, 3 0, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))",
"0000000001000A0000000000000000000000000000000000000000000000000000000000000000000840000000000000084000000000000008400000000000000840000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000040000000000000004000000000000000400000000000000040000000000000F03F000000000000F03F000000000000F03F020000000200000000000500000001000000FFFFFFFF0000000003")]
[InlineData(
"POLYGON ((0 0, 0 3, 3 3, 3 0, 0 0), (0 0, 0 2, 2 2, 2 0, 0 0))",
"0000000001000A00000000000000000000000000000000000000000000000000000000000000000008400000000000000840000000000000084000000000000008400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000004000000000000000400000000000000040000000000000000000000000000000000000000000000000020000000200000000000500000001000000FFFFFFFF0000000003")]
[InlineData(
"GEOMETRYCOLLECTION EMPTY",
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF07")]
[InlineData(
"GEOMETRYCOLLECTION (POINT (0 0))",
"000000000100010000000000000000000000000000000000000001000000010000000002000000FFFFFFFF0000000007000000000000000001")]
[InlineData(
"GEOMETRYCOLLECTION (POINT (0 0), POINT (0 1))",
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F020000000100000000010100000003000000FFFFFFFF0000000007000000000000000001000000000100000001")]
[InlineData(
"GEOMETRYCOLLECTION (POINT (0 0), POINT EMPTY, POINT (0 1))",
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F020000000100000000010100000004000000FFFFFFFF000000000700000000000000000100000000FFFFFFFF01000000000100000001")]
[InlineData(
"GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (POINT (0 1)))",
"000000000100010000000000000000000000000000000000F03F01000000010000000003000000FFFFFFFF0000000007000000000000000007010000000000000001")]
[InlineData(
"GEOMETRYCOLLECTION (POINT (0 0), GEOMETRYCOLLECTION (POINT (0 1)))",
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F020000000100000000010100000004000000FFFFFFFF0000000007000000000000000001000000000100000007020000000100000001")]
[InlineData(
"MULTIPOINT ((0 0))",
"000000000100010000000000000000000000000000000000000001000000010000000002000000FFFFFFFF0000000004000000000000000001")]
[InlineData(
"MULTILINESTRING ((0 0, 0 1))",
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F01000000010000000002000000FFFFFFFF0000000005000000000000000002")]
[InlineData(
"MULTIPOLYGON (((0 0, 0 1, 1 1, 0 0)))",
"00000000010004000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000001000000020000000002000000FFFFFFFF0000000006000000000000000003")]
public void Write_AllInvalid(string wkt, string expected)
{
var geometry = new WKTReader() { IsOldNtsCoordinateSyntaxAllowed = false }.Read(wkt);

Assert.Equal(expected, Write(geometry, geometryValidator: _ => false));
}

[Fact, Description("Issue 31")]
public void Write_GC_with_MultiLineString_and_Polygon()
{
Expand All @@ -230,14 +305,20 @@ public void Write_GC_with_MultiLineString_and_Polygon()
private string Write(
Geometry geometry,
Ordinates handleOrdinates = Ordinates.XYZM,
bool isGeography = false)
bool isGeography = false,
Func<Geometry, bool> geometryValidator = default)
{
var writer = new SqlServerBytesWriter
{
HandleOrdinates = handleOrdinates,
IsGeography = isGeography
};

if (geometryValidator != default)
{
writer.GeometryValidator = geometryValidator;
}

return string.Concat(writer.Write(geometry).Select(b => b.ToString("X2")));
}
}
Expand Down

0 comments on commit dc36f53

Please sign in to comment.