Skip to content

Commit f95315a

Browse files
committed
Added STSnapToGrid, STSnap Geometry Editors methods with tests
1 parent b1ba55c commit f95315a

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

LinqToDBPostGisNetTopologySuite.Tests/GeometryEditorsTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ public void TestSTLineMerge()
548548
Assert.AreEqual(5, mergedLine.GetCoordinateN(2).X);
549549
Assert.AreEqual(4, mergedLine.GetCoordinateN(2).Y);
550550
Assert.AreEqual(double.NaN, mergedLine.GetCoordinateN(2).M);
551-
552551
}
553552
}
554553

@@ -611,6 +610,55 @@ public void TestSTSetPoint()
611610
}
612611
}
613612

613+
[Test]
614+
public void TestSTSnapToGrid()
615+
{
616+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
617+
{
618+
Assert.AreEqual(
619+
"LINESTRING(1.112 2.123,4.111 3.237)",
620+
db.Select(() => GeometryInput.STGeomFromText("LINESTRING(1.1115678 2.123, 4.111111 3.2374897, 4.11112 3.23748667)")
621+
.STSnapToGrid(0.001)
622+
.STAsText()));
623+
624+
Assert.AreEqual(
625+
"LINESTRING(-1.08 2.12 2.3 1.1144,4.12 3.22 3.1 1.1144,-1.08 2.12 2.3 1.1144)",
626+
db.Select(() => GeometryInput.STGeomFromEWKT("LINESTRING(-1.1115678 2.123 2.3456 1.11111, 4.111111 3.2374897 3.1234 1.1111, -1.11111112 2.123 2.3456 1.1111112)")
627+
.STSnapToGrid(GeometryInput.STGeomFromEWKT("POINT(1.12 2.22 3.2 4.4444)"), 0.1, 0.1, 0.1, 0.01)
628+
.STAsEWKT()));
629+
630+
Assert.AreEqual(
631+
"LINESTRING(-1.11 2.12 3 2.3456,4.11 3.24 3.1234 1.1111)",
632+
db.Select(() => GeometryInput.STGeomFromEWKT("LINESTRING(-1.1115678 2.123 3 2.3456, 4.111111 3.2374897 3.1234 1.1111)")
633+
.STSnapToGrid(0.01)
634+
.STAsEWKT()));
635+
}
636+
}
637+
638+
[Test]
639+
public void TestSTSnap()
640+
{
641+
const string LinestringWkt = "LINESTRING (5 107, 54 84, 101 100)";
642+
const string MultiPolygonWkt = "MULTIPOLYGON(((26 125, 26 200, 126 200, 126 125, 26 125), (51 150, 101 150, 76 175, 51 150 )), ((151 100, 151 200, 176 175, 151 100 )))";
643+
644+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
645+
{
646+
db.TestGeometries
647+
.Value(g => g.Id, 1)
648+
.Value(g => g.Geometry, () => GeometryInput.STGeomFromText(MultiPolygonWkt))
649+
.Insert();
650+
651+
var line = db.Select(() => GeometryInput.STGeomFromText(LinestringWkt));
652+
653+
var snapped = db.TestGeometries
654+
.Where(g => g.Id == 1)
655+
.Select(g => g.Geometry.STSnap(line, g.Geometry.STDistance(line).Value * 1.01).STAsText())
656+
.Single();
657+
658+
Assert.AreEqual(snapped, "MULTIPOLYGON(((26 125,26 200,126 200,126 125,101 100,26 125),(51 150,101 150,76 175,51 150)),((151 100,151 200,176 175,151 100)))");
659+
}
660+
}
661+
614662
[Test]
615663
public void TestSTSwapOrdinates()
616664
{

LinqToDBPostGisNetTopologySuite/GeometryEditors.cs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,90 @@ public static NTSG STSetPoint(this NTSG geometry, int index, NTSG point)
301301
throw new InvalidOperationException();
302302
}
303303

304+
/// <summary>
305+
/// Snaps all points of input <paramref name="geometry"/> to the grid defined by its origin and cell size.
306+
/// </summary>
307+
/// <remarks>
308+
/// See https://postgis.net/docs/manual-3.0/ST_SnapToGrid.html
309+
/// </remarks>
310+
/// <param name="geometry">Input geometry.</param>
311+
/// <param name="originX">X coordinate of grid origin.</param>
312+
/// <param name="originY">Y coordinate of grid origin.</param>
313+
/// <param name="sizeX">Grid cell size by X axis.</param>
314+
/// <param name="sizeY">Grid cell size by Y axis.</param>
315+
/// <returns>Geometry with all points snapped to grid.</returns>
316+
[Sql.Function("ST_SnapToGrid", ServerSideOnly = true)]
317+
public static NTSG STSnapToGrid(this NTSG geometry, double originX, double originY, double sizeX, double sizeY)
318+
{
319+
throw new InvalidOperationException();
320+
}
321+
322+
/// <summary>
323+
/// Snaps all points of input <paramref name="geometry"/> to the grid defined by its cell size.
324+
/// </summary>
325+
/// <remarks>
326+
/// See https://postgis.net/docs/manual-3.0/ST_SnapToGrid.html
327+
/// </remarks>
328+
/// <param name="geometry">Input geometry.</param>
329+
/// <param name="sizeX">Grid cell size by X axis.</param>
330+
/// <param name="sizeY">Grid cell size by Y axis.</param>
331+
/// <returns>Geometry with all points snapped to grid.</returns>
332+
[Sql.Function("ST_SnapToGrid", ServerSideOnly = true)]
333+
public static NTSG STSnapToGrid(this NTSG geometry, double sizeX, double sizeY)
334+
{
335+
throw new InvalidOperationException();
336+
}
337+
338+
/// <summary>
339+
/// Snaps all points of input <paramref name="geometry"/> to the grid defined by its cell size.
340+
/// </summary>
341+
/// <remarks>
342+
/// See https://postgis.net/docs/manual-3.0/ST_SnapToGrid.html
343+
/// </remarks>
344+
/// <param name="geometry">Input geometry.</param>
345+
/// <param name="size">Grid cell size.</param>
346+
/// <returns>Geometry with all points snapped to grid.</returns>
347+
[Sql.Function("ST_SnapToGrid", ServerSideOnly = true)]
348+
public static NTSG STSnapToGrid(this NTSG geometry, double size)
349+
{
350+
throw new InvalidOperationException();
351+
}
352+
353+
/// <summary>
354+
/// Snaps all points of input <paramref name="geometry"/> to the grid defined by its origin point and cell sizes.
355+
/// </summary>
356+
/// <remarks>
357+
/// See https://postgis.net/docs/manual-3.0/ST_SnapToGrid.html
358+
/// </remarks>
359+
/// <param name="geometry">Input geometry.</param>
360+
/// <param name="origin">Grid origin (Point).</param>
361+
/// <param name="sizeX">Grid cell size by X axis.</param>
362+
/// <param name="sizeY">Grid cell size by Y axis.</param>
363+
/// <param name="sizeZ">Grid cell size by Z axis.</param>
364+
/// <param name="sizeM">Grid cell size by M axis.</param>
365+
/// <returns>Geometry with all points snapped to grid.</returns>
366+
[Sql.Function("ST_SnapToGrid", ServerSideOnly = true)]
367+
public static NTSG STSnapToGrid(this NTSG geometry, NTSG origin, double sizeX, double sizeY, double sizeZ, double sizeM)
368+
{
369+
throw new InvalidOperationException();
370+
}
371+
372+
/// <summary>
373+
/// Snaps the vertices and segments of input <paramref name="geometry"/> to <paramref name="reference"/> geometry vertices.
374+
/// </summary>
375+
/// <remarks>
376+
/// See https://postgis.net/docs/manual-3.0/ST_Snap.html
377+
/// </remarks>
378+
/// <param name="geometry">Input geometry.</param>
379+
/// <param name="reference">Reference geometry.</param>
380+
/// <param name="tolerance">Snap distance tolerance.</param>
381+
/// <returns>Geometry with the vertices snapped.</returns>
382+
[Sql.Function("ST_Snap", ServerSideOnly = true)]
383+
public static NTSG STSnap(this NTSG geometry, NTSG reference, double tolerance)
384+
{
385+
throw new InvalidOperationException();
386+
}
387+
304388
/// <summary>
305389
/// Returns a version of input <paramref name="geometry"/> with given ordinates swapped.
306390
/// </summary>
@@ -309,7 +393,7 @@ public static NTSG STSetPoint(this NTSG geometry, int index, NTSG point)
309393
/// </remarks>
310394
/// <param name="geometry">Input geometry.</param>
311395
/// <param name="ordinates">Two-characters string naming the ordinates to swap. Valid names are: x,y,z and m.</param>
312-
/// <returns>Geometry with point removed</returns>
396+
/// <returns>Geometry with given ordinates swapped.</returns>
313397
[Sql.Function("ST_SwapOrdinates", ServerSideOnly = true)]
314398
public static NTSG STSwapOrdinates(this NTSG geometry, string ordinates)
315399
{
@@ -324,7 +408,7 @@ public static NTSG STSwapOrdinates(this NTSG geometry, string ordinates)
324408
/// </remarks>
325409
/// <param name="geometry">Input geometry.</param>
326410
/// <param name="ordinates">Two-characters string naming the ordinates to swap. Valid names are: x,y,z and m.</param>
327-
/// <returns>Geometry with point removed</returns>
411+
/// <returns>Geometry with given ordinates swapped.</returns>
328412
[Sql.Function("ST_SwapOrdinates", ServerSideOnly = true)]
329413
public static NTSG STSwapOrdinates(string geometry, string ordinates)
330414
{

0 commit comments

Comments
 (0)