Skip to content

Commit dc36f53

Browse files
committed
Merge commit
2 parents cce95ef + ac2eee0 commit dc36f53

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

src/NetTopologySuite.IO.SqlServerBytes/SqlServerBytesWriter.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System;
1+
using NetTopologySuite.Geometries;
2+
using NetTopologySuite.IO.Properties;
3+
using System;
24
using System.Collections.Generic;
35
using System.IO;
4-
using System.Linq;
5-
using NetTopologySuite.Geometries;
6-
using NetTopologySuite.IO.Properties;
7-
86
using Figure = NetTopologySuite.IO.Serialization.Figure;
97
using FigureAttribute = NetTopologySuite.IO.Serialization.FigureAttribute;
108
using Geography = NetTopologySuite.IO.Serialization.Geography;
@@ -22,7 +20,9 @@ public class SqlServerBytesWriter
2220
{
2321
private bool _emitZ = true;
2422
private bool _emitM = true;
25-
23+
private Func<Geometry, bool> _geometryValidationFn;
24+
private static readonly Func<Geometry, bool> DefaultGeometryValidationFn = (g) => g.IsValid;
25+
2626
/// <summary>
2727
/// Gets or sets the desired <see cref="IO.ByteOrder"/>. Returns <see cref="IO.ByteOrder.LittleEndian"/> since
2828
/// it's required. Setting does nothing.
@@ -87,6 +87,15 @@ public virtual Ordinates HandleOrdinates
8787
/// </summary>
8888
public virtual bool IsGeography { get; set; }
8989

90+
/// <summary>
91+
/// 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
92+
/// </summary>
93+
public Func<Geometry, bool> GeometryValidator
94+
{
95+
get => _geometryValidationFn ?? DefaultGeometryValidationFn;
96+
set => _geometryValidationFn = value;
97+
}
98+
9099
/// <summary>
91100
/// Writes a binary representation of a given geometry.
92101
/// </summary>
@@ -137,7 +146,7 @@ private Geography ToGeography(Geometry geometry)
137146
var geography = new Geography
138147
{
139148
SRID = Math.Max(0, geometry.SRID),
140-
IsValid = geometry.IsValid
149+
IsValid = GeometryValidator(geometry)
141150
};
142151

143152
while (geometries.Count > 0)

test/NetTopologySuite.IO.SqlServerBytes.Test/SqlServerBytesWriterTest.cs

+82-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,81 @@ public void Types_still_unknown(string wkt)
210210
() => reader.Read(wkt));
211211
}
212212

213+
[Theory]
214+
[InlineData(
215+
"POINT EMPTY",
216+
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF01")]
217+
[InlineData(
218+
"POINT (1 2)",
219+
"000000000108000000000000F03F0000000000000040")]
220+
[InlineData(
221+
"POINT Z(1 2 3)",
222+
"000000000109000000000000F03F00000000000000400000000000000840")]
223+
[InlineData(
224+
"LINESTRING EMPTY",
225+
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF02")]
226+
[InlineData(
227+
"LINESTRING (0 0, 0 1)",
228+
"000000000110000000000000000000000000000000000000000000000000000000000000F03F")]
229+
[InlineData(
230+
"LINESTRING Z(0 0 1, 0 1 2)",
231+
"000000000111000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040")]
232+
//[InlineData(
233+
// "LINESTRING (0 0, 0 1 2)",
234+
// "000000000115000000000000000000000000000000000000000000000000000000000000F03F000000000000F8FF0000000000000040")]
235+
[InlineData(
236+
"LINESTRING (0 0, 0 1, 0 2)",
237+
"00000000010003000000000000000000000000000000000000000000000000000000000000000000F03F0000000000000000000000000000004001000000010000000001000000FFFFFFFF0000000002")]
238+
[InlineData(
239+
"LINESTRING Z(0 0 1, 0 1 2, 0 2 3)",
240+
"00000000010103000000000000000000000000000000000000000000000000000000000000000000F03F00000000000000000000000000000040000000000000F03F0000000000000040000000000000084001000000010000000001000000FFFFFFFF0000000002")]
241+
[InlineData(
242+
"POLYGON EMPTY",
243+
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF03")]
244+
// 0xE61000000104000000000000000001000000FFFFFFFFFFFFFFFF03
245+
[InlineData(
246+
"POLYGON ((0 0, 0 1, 1 1, 0 0))",
247+
"00000000010004000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000001000000020000000001000000FFFFFFFF0000000003")]
248+
[InlineData(
249+
"POLYGON ((0 0, 0 3, 3 3, 3 0, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))",
250+
"0000000001000A0000000000000000000000000000000000000000000000000000000000000000000840000000000000084000000000000008400000000000000840000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000040000000000000004000000000000000400000000000000040000000000000F03F000000000000F03F000000000000F03F020000000200000000000500000001000000FFFFFFFF0000000003")]
251+
[InlineData(
252+
"POLYGON ((0 0, 0 3, 3 3, 3 0, 0 0), (0 0, 0 2, 2 2, 2 0, 0 0))",
253+
"0000000001000A00000000000000000000000000000000000000000000000000000000000000000008400000000000000840000000000000084000000000000008400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000004000000000000000400000000000000040000000000000000000000000000000000000000000000000020000000200000000000500000001000000FFFFFFFF0000000003")]
254+
[InlineData(
255+
"GEOMETRYCOLLECTION EMPTY",
256+
"000000000100000000000000000001000000FFFFFFFFFFFFFFFF07")]
257+
[InlineData(
258+
"GEOMETRYCOLLECTION (POINT (0 0))",
259+
"000000000100010000000000000000000000000000000000000001000000010000000002000000FFFFFFFF0000000007000000000000000001")]
260+
[InlineData(
261+
"GEOMETRYCOLLECTION (POINT (0 0), POINT (0 1))",
262+
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F020000000100000000010100000003000000FFFFFFFF0000000007000000000000000001000000000100000001")]
263+
[InlineData(
264+
"GEOMETRYCOLLECTION (POINT (0 0), POINT EMPTY, POINT (0 1))",
265+
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F020000000100000000010100000004000000FFFFFFFF000000000700000000000000000100000000FFFFFFFF01000000000100000001")]
266+
[InlineData(
267+
"GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (POINT (0 1)))",
268+
"000000000100010000000000000000000000000000000000F03F01000000010000000003000000FFFFFFFF0000000007000000000000000007010000000000000001")]
269+
[InlineData(
270+
"GEOMETRYCOLLECTION (POINT (0 0), GEOMETRYCOLLECTION (POINT (0 1)))",
271+
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F020000000100000000010100000004000000FFFFFFFF0000000007000000000000000001000000000100000007020000000100000001")]
272+
[InlineData(
273+
"MULTIPOINT ((0 0))",
274+
"000000000100010000000000000000000000000000000000000001000000010000000002000000FFFFFFFF0000000004000000000000000001")]
275+
[InlineData(
276+
"MULTILINESTRING ((0 0, 0 1))",
277+
"00000000010002000000000000000000000000000000000000000000000000000000000000000000F03F01000000010000000002000000FFFFFFFF0000000005000000000000000002")]
278+
[InlineData(
279+
"MULTIPOLYGON (((0 0, 0 1, 1 1, 0 0)))",
280+
"00000000010004000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000001000000020000000002000000FFFFFFFF0000000006000000000000000003")]
281+
public void Write_AllInvalid(string wkt, string expected)
282+
{
283+
var geometry = new WKTReader() { IsOldNtsCoordinateSyntaxAllowed = false }.Read(wkt);
284+
285+
Assert.Equal(expected, Write(geometry, geometryValidator: _ => false));
286+
}
287+
213288
[Fact, Description("Issue 31")]
214289
public void Write_GC_with_MultiLineString_and_Polygon()
215290
{
@@ -230,14 +305,20 @@ public void Write_GC_with_MultiLineString_and_Polygon()
230305
private string Write(
231306
Geometry geometry,
232307
Ordinates handleOrdinates = Ordinates.XYZM,
233-
bool isGeography = false)
308+
bool isGeography = false,
309+
Func<Geometry, bool> geometryValidator = default)
234310
{
235311
var writer = new SqlServerBytesWriter
236312
{
237313
HandleOrdinates = handleOrdinates,
238314
IsGeography = isGeography
239315
};
240316

317+
if (geometryValidator != default)
318+
{
319+
writer.GeometryValidator = geometryValidator;
320+
}
321+
241322
return string.Concat(writer.Write(geometry).Select(b => b.ToString("X2")));
242323
}
243324
}

0 commit comments

Comments
 (0)