Skip to content

Commit 8f8de7b

Browse files
committed
Added some Geometry Processing methods with tests
1 parent dd49e58 commit 8f8de7b

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed

LinqToDBPostGisNetTopologySuite.Tests/GeometryProcessingTests.cs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ public void TestSTFlipCoordinates()
144144
}
145145
}
146146

147-
148147
[Test]
149148
public void TestSTGeneratePoints()
150149
{
@@ -159,6 +158,81 @@ public void TestSTGeneratePoints()
159158
}
160159
}
161160

161+
[Test]
162+
public void TestSTGeometricMedian()
163+
{
164+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
165+
{
166+
const string wkt = "MULTIPOINT((0 0), (1 1), (2 2), (200 200))";
167+
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryInput.STGeomFromText(wkt)).Insert();
168+
169+
var median = db.TestGeometries
170+
.Where(g => g.Id == 1)
171+
.Select(g => g.Geometry.STGeometricMedian())
172+
.Single() as NTSG.Point;
173+
174+
Assert.AreEqual(1.9761550281255, median.X, 1.0E-8);
175+
Assert.AreEqual(1.9761550281255, median.Y, 1.0E-8);
176+
}
177+
}
178+
179+
[Test]
180+
public void TestSTLineToCurve()
181+
{
182+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
183+
{
184+
const string wkt = "POINT(1 3)";
185+
db.TestGeometries
186+
.Value(g => g.Id, 1)
187+
.Value(p => p.Geometry, () =>
188+
GeometryInput.STGeomFromText(wkt).STBuffer(3.0))
189+
.Insert();
190+
191+
// NTS error: 'Geometry type not recognized. GeometryCode: 10'
192+
var curvePolygon = db.TestGeometries
193+
.Where(g => g.Id == 1)
194+
.Select(g => g.Geometry.STLineToCurve().STAsText())
195+
.Single();
196+
197+
Assert.AreEqual("CURVEPOLYGON(CIRCULARSTRING(4 3,-2 2.99999999999999,4 3))", curvePolygon);
198+
}
199+
}
200+
201+
[Test]
202+
public void TestSTMakeValid()
203+
{
204+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
205+
{
206+
const string wkt1 = "LINESTRING(0 0,1 1)";
207+
db.TestGeometries
208+
.Value(g => g.Id, 1)
209+
.Value(p => p.Geometry, () => GeometryInput.STGeomFromText(wkt1))
210+
.Insert();
211+
212+
const string wkt2 = "POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))";
213+
db.TestGeometries
214+
.Value(g => g.Id, 2)
215+
.Value(p => p.Geometry, () => GeometryInput.STGeomFromText(wkt2))
216+
.Insert();
217+
218+
var result1 = db.TestGeometries
219+
.Where(g => g.Id == 1)
220+
.Select(g => g.Geometry.STMakeValid().STAsText())
221+
.Single();
222+
223+
var result2 = db.TestGeometries
224+
.Where(g => g.Id == 2)
225+
.Select(g => g.Geometry.STMakeValid().STAsText())
226+
.Single();
227+
228+
// Already-valid geometries are returned without further intervention:
229+
Assert.AreEqual(wkt1, result1);
230+
231+
// Single polygons may become multi-geometries in case of self-intersections:
232+
Assert.AreEqual("MULTILINESTRING((0 0,1 1),(1 1,1 2))", result2);
233+
}
234+
}
235+
162236
[Test]
163237
public void TestSTUnion()
164238
{
@@ -177,4 +251,4 @@ public void TestSTUnion()
177251
}
178252
}
179253
}
180-
}
254+
}

LinqToDBPostGisNetTopologySuite/GeometryProcessing.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,36 @@ public static NTSG STGeneratePoints(this NTSG geometry, int numberOfPoints, int
246246
throw new InvalidOperationException();
247247
}
248248

249+
/// <summary>
250+
/// Returns approximate geometric median of input MultiPoint geometry using the Weiszfeld algorithm.
251+
/// </summary>
252+
/// <remarks>
253+
/// See https://postgis.net/docs/manual-3.0/ST_GeometricMedian.html
254+
/// </remarks>
255+
/// <param name="geometry">Input geometry (MultiPoint)</param>
256+
/// <param name="tolerance">Tolerance value</param>
257+
/// <param name="maxIterations">Maximum number of iterations</param>
258+
/// <param name="failIfNotConverged">Fail if not converged after maxIterations</param>
259+
/// <returns>Geometric median</returns>
260+
[Sql.Function("ST_GeometricMedian", ServerSideOnly = true)]
261+
public static NTSG STGeometricMedian(this NTSG geometry, double tolerance, int maxIterations, bool failIfNotConverged)
262+
{
263+
throw new InvalidOperationException();
264+
}
249265

266+
/// <summary>
267+
/// Returns approximate geometric median of input MultiPoint geometry using the Weiszfeld algorithm.
268+
/// </summary>
269+
/// <remarks>
270+
/// See https://postgis.net/docs/manual-3.0/ST_GeometricMedian.html
271+
/// </remarks>
272+
/// <param name="geometry">Input geometry (MultiPoint)</param>
273+
/// <returns>Geometric median</returns>
274+
[Sql.Function("ST_GeometricMedian", ServerSideOnly = true)]
275+
public static NTSG STGeometricMedian(this NTSG geometry)
276+
{
277+
throw new InvalidOperationException();
278+
}
250279

251280
/// <summary>
252281
/// Returns geometry that represents the point set intersection of given geometries.
@@ -263,6 +292,36 @@ public static NTSG STIntersection(this NTSG geometry, NTSG other)
263292
throw new InvalidOperationException();
264293
}
265294

295+
/// <summary>
296+
/// Converts plain input LineString/Polygon to CircularString/CurvePolygon.
297+
/// </summary>
298+
/// <remarks>
299+
/// See https://postgis.net/docs/manual-3.0/ST_LineToCurve.html
300+
/// </remarks>
301+
/// <param name="geometry">Input geometry (LineString/Polygon)</param>
302+
/// <returns>Curved equivalent of input geometry</returns>
303+
[Sql.Function("ST_LineToCurve", ServerSideOnly = true)]
304+
public static NTSG STLineToCurve(this NTSG geometry)
305+
{
306+
throw new InvalidOperationException();
307+
}
308+
309+
/// <summary>
310+
/// Attempts to create a valid representation of input invalid geometry without losing any of input vertices.
311+
/// </summary>
312+
/// <remarks>
313+
/// See https://postgis.net/docs/manual-3.0/ST_MakeValid.html
314+
/// </remarks>
315+
/// <param name="geometry">Input geometry</param>
316+
/// <returns>Fixed valid geometry</returns>
317+
[Sql.Function("ST_MakeValid", ServerSideOnly = true)]
318+
public static NTSG STMakeValid(this NTSG geometry)
319+
{
320+
throw new InvalidOperationException();
321+
}
322+
323+
// TODO: ST_MemUnion(geometry set)
324+
266325
/// <summary>
267326
/// Returns POINT guaranteed to intersect a surface.
268327
/// </summary>
@@ -293,7 +352,7 @@ public static NTSG STSymDifference(this NTSG geometry, NTSG other)
293352
}
294353

295354
/// <summary>
296-
/// Returns union of given geometries.
355+
/// Returns union of input geometries.
297356
/// </summary>
298357
/// <remarks>
299358
/// See https://postgis.net/docs/manual-3.0/ST_Union.html
@@ -307,4 +366,4 @@ public static NTSG STUnion(this NTSG geometry, NTSG other) // TODO: other varian
307366
throw new InvalidOperationException();
308367
}
309368
}
310-
}
369+
}

0 commit comments

Comments
 (0)