Skip to content

Commit ab287f3

Browse files
Added ST_Force methods of GeometryEditors with tests
1 parent 69d173b commit ab287f3

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
using System;
2+
using System.Linq;
3+
4+
using LinqToDB;
5+
using NUnit.Framework;
6+
7+
using NTSG = NetTopologySuite.Geometries;
8+
9+
namespace LinqToDBPostGisNetTopologySuite.Tests
10+
{
11+
[TestFixture]
12+
class GeometryEditorsTests : TestsBase
13+
{
14+
[SetUp]
15+
public void Setup()
16+
{
17+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
18+
{
19+
db.TestGeometries.Delete();
20+
}
21+
}
22+
23+
[Test]
24+
public void TestSTForce2D()
25+
{
26+
const string wkt = "LINESTRING(25 50 1, 100 125 1, 150 190 1)";
27+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
28+
{
29+
db.TestGeometries
30+
.Value(g => g.Id, 1)
31+
.Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(wkt))
32+
.Insert();
33+
34+
var result = db.TestGeometries
35+
.Where(g => g.Id == 1)
36+
.Select(g => g.Geometry.STForce2D())
37+
.Single() as NTSG.LineString;
38+
39+
Assert.AreEqual(2, result.CoordinateSequence.Dimension);
40+
Assert.AreEqual(25, result.GetCoordinateN(0).X);
41+
Assert.AreEqual(50, result.GetCoordinateN(0).Y);
42+
Assert.AreEqual(100, result.GetCoordinateN(1).X);
43+
Assert.AreEqual(125, result.GetCoordinateN(1).Y);
44+
Assert.AreEqual(150, result.GetCoordinateN(2).X);
45+
Assert.AreEqual(190, result.GetCoordinateN(2).Y);
46+
}
47+
}
48+
49+
[Test]
50+
public void TestSTForce3D()
51+
{
52+
const string wkt = "LINESTRING(25 50, 100 125, 150 190)";
53+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
54+
{
55+
db.TestGeometries
56+
.Value(g => g.Id, 1)
57+
.Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(wkt))
58+
.Insert();
59+
60+
var result = db.TestGeometries
61+
.Where(g => g.Id == 1)
62+
.Select(g => g.Geometry.STForce3D())
63+
.Single() as NTSG.LineString;
64+
65+
Assert.AreEqual(3, result.CoordinateSequence.Dimension);
66+
67+
var nonExistM = result.GetCoordinateN(0).M;
68+
var existZ = result.GetCoordinateN(0).Z;
69+
70+
Assert.AreEqual(false, Double.IsNaN(existZ));
71+
Assert.AreEqual(true, Double.IsNaN(nonExistM));
72+
73+
Assert.AreEqual(25, result.GetCoordinateN(0).X);
74+
Assert.AreEqual(50, result.GetCoordinateN(0).Y);
75+
Assert.AreEqual(0, result.GetCoordinateN(0).Z);
76+
Assert.AreEqual(100, result.GetCoordinateN(1).X);
77+
Assert.AreEqual(125, result.GetCoordinateN(1).Y);
78+
Assert.AreEqual(0, result.GetCoordinateN(1).Z);
79+
Assert.AreEqual(150, result.GetCoordinateN(2).X);
80+
Assert.AreEqual(190, result.GetCoordinateN(2).Y);
81+
Assert.AreEqual(0, result.GetCoordinateN(2).Z);
82+
}
83+
}
84+
85+
[Test]
86+
public void TestSTForce3DZ()
87+
{
88+
const string wkt = "LINESTRING(25 50, 100 125, 150 190)";
89+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
90+
{
91+
db.TestGeometries
92+
.Value(g => g.Id, 1)
93+
.Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(wkt))
94+
.Insert();
95+
96+
var result = db.TestGeometries
97+
.Where(g => g.Id == 1)
98+
.Select(g => g.Geometry.STForce3DZ())
99+
.Single() as NTSG.LineString;
100+
101+
Assert.AreEqual(3, result.CoordinateSequence.Dimension);
102+
var nonExistM = result.GetCoordinateN(0).M;
103+
var existZ = result.GetCoordinateN(0).Z;
104+
105+
Assert.AreEqual(false, Double.IsNaN(existZ));
106+
Assert.AreEqual(true, Double.IsNaN(nonExistM));
107+
108+
Assert.AreEqual(25, result.GetCoordinateN(0).X);
109+
Assert.AreEqual(50, result.GetCoordinateN(0).Y);
110+
Assert.AreEqual(0, result.GetCoordinateN(0).Z);
111+
Assert.AreEqual(100, result.GetCoordinateN(1).X);
112+
Assert.AreEqual(125, result.GetCoordinateN(1).Y);
113+
Assert.AreEqual(0, result.GetCoordinateN(1).Z);
114+
Assert.AreEqual(150, result.GetCoordinateN(2).X);
115+
Assert.AreEqual(190, result.GetCoordinateN(2).Y);
116+
Assert.AreEqual(0, result.GetCoordinateN(2).Z);
117+
}
118+
}
119+
120+
[Test]
121+
public void TestSTForce3DM()
122+
{
123+
const string wkt = "LINESTRING(25 50, 100 125, 150 190)";
124+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
125+
{
126+
db.TestGeometries
127+
.Value(g => g.Id, 1)
128+
.Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(wkt))
129+
.Insert();
130+
131+
var result = db.TestGeometries
132+
.Where(g => g.Id == 1)
133+
.Select(g => g.Geometry.STForce3DM())
134+
.Single() as NTSG.LineString;
135+
136+
Assert.AreEqual(3, result.CoordinateSequence.Dimension);
137+
var existM = result.GetCoordinateN(0).M;
138+
var nonExistZ = result.GetCoordinateN(0).Z;
139+
140+
Assert.AreEqual(false, Double.IsNaN(existM));
141+
Assert.AreEqual(true, Double.IsNaN(nonExistZ));
142+
143+
Assert.AreEqual(25, result.GetCoordinateN(0).X);
144+
Assert.AreEqual(50, result.GetCoordinateN(0).Y);
145+
Assert.AreEqual(0, result.GetCoordinateN(0).M);
146+
Assert.AreEqual(100, result.GetCoordinateN(1).X);
147+
Assert.AreEqual(125, result.GetCoordinateN(1).Y);
148+
Assert.AreEqual(0, result.GetCoordinateN(1).M);
149+
Assert.AreEqual(150, result.GetCoordinateN(2).X);
150+
Assert.AreEqual(190, result.GetCoordinateN(2).Y);
151+
Assert.AreEqual(0, result.GetCoordinateN(2).M);
152+
}
153+
}
154+
155+
[Test]
156+
public void TestSTForce4D()
157+
{
158+
const string wkt = "LINESTRING(25 50, 100 125, 150 190)";
159+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
160+
{
161+
db.TestGeometries
162+
.Value(g => g.Id, 1)
163+
.Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(wkt))
164+
.Insert();
165+
166+
var result = db.TestGeometries
167+
.Where(g => g.Id == 1)
168+
.Select(g => g.Geometry.STForce4D())
169+
.Single() as NTSG.LineString;
170+
171+
Assert.AreEqual(4, result.CoordinateSequence.Dimension);
172+
var existM = result.GetCoordinateN(0).M;
173+
var existZ = result.GetCoordinateN(0).Z;
174+
Assert.AreEqual(false, Double.IsNaN(existM));
175+
Assert.AreEqual(false, Double.IsNaN(existZ));
176+
177+
Assert.AreEqual(25, result.GetCoordinateN(0).X);
178+
Assert.AreEqual(50, result.GetCoordinateN(0).Y);
179+
Assert.AreEqual(0, result.GetCoordinateN(0).M);
180+
Assert.AreEqual(100, result.GetCoordinateN(1).X);
181+
Assert.AreEqual(125, result.GetCoordinateN(1).Y);
182+
Assert.AreEqual(0, result.GetCoordinateN(1).M);
183+
Assert.AreEqual(150, result.GetCoordinateN(2).X);
184+
Assert.AreEqual(190, result.GetCoordinateN(2).Y);
185+
Assert.AreEqual(0, result.GetCoordinateN(2).M);
186+
}
187+
}
188+
}
189+
}

LinqToDBPostGisNetTopologySuite/GeometryEditors.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,76 @@ namespace LinqToDBPostGisNetTopologySuite
1414
/// </remarks>
1515
public static class GeometryEditors
1616
{
17+
/// <summary>
18+
/// Forces the geometries into a "2-dimensional mode" so that all output representations will only have the X and Y coordinates
19+
/// </summary>
20+
/// <remarks>
21+
/// See https://postgis.net/docs/manual-3.0/ST_Force2D.html
22+
/// </remarks>
23+
/// <param name="geometry">Input geometry</param>
24+
/// <returns>Geometry in XY mode</returns>
25+
[Sql.Function("ST_Force2D", ServerSideOnly = true)]
26+
public static NTSG STForce2D(this NTSG geometry)
27+
{
28+
throw new InvalidOperationException();
29+
}
30+
31+
/// <summary>
32+
/// Forces the geometries into XYZ mode. This is an alias for ST_Force_3DZ. If a geometry has no Z component, then a 0 Z coordinate is tacked on
33+
/// </summary>
34+
/// <remarks>
35+
/// See https://postgis.net/docs/manual-3.0/ST_Force_3D.html
36+
/// </remarks>
37+
/// <param name="geometry">Input geometry</param>
38+
/// <returns>Geometry in XYZ mode</returns>
39+
[Sql.Function("ST_Force3D", ServerSideOnly = true)]
40+
public static NTSG STForce3D(this NTSG geometry)
41+
{
42+
throw new InvalidOperationException();
43+
}
44+
45+
/// <summary>
46+
/// Forces the geometries into XYZ mode. This is a synonym for ST_Force3DZ. If a geometry has no Z component, then a 0 Z coordinate is tacked on
47+
/// </summary>
48+
/// <remarks>
49+
/// See https://postgis.net/docs/manual-3.0/ST_Force_3DZ.html
50+
/// </remarks>
51+
/// <param name="geometry">Input geometry</param>
52+
/// <returns>Geometry in XYZ mode</returns>
53+
[Sql.Function("ST_Force3DZ", ServerSideOnly = true)]
54+
public static NTSG STForce3DZ(this NTSG geometry)
55+
{
56+
throw new InvalidOperationException();
57+
}
58+
59+
/// <summary>
60+
/// Forces the geometries into XYM mode. If a geometry has no M component, then a 0 M coordinate is tacked on. If it has a Z component, then Z is removed
61+
/// </summary>
62+
/// <remarks>
63+
/// See https://postgis.net/docs/manual-3.0/ST_Force_3DM.html
64+
/// </remarks>
65+
/// <param name="geometry">Input geometry</param>
66+
/// <returns>Geometry in XYM mode</returns>
67+
[Sql.Function("ST_Force3DM", ServerSideOnly = true)]
68+
public static NTSG STForce3DM(this NTSG geometry)
69+
{
70+
throw new InvalidOperationException();
71+
}
72+
73+
/// <summary>
74+
/// Forces the geometries into XYZM mode. 0 is tacked on for missing Z and M dimensions.
75+
/// </summary>
76+
/// <remarks>
77+
/// See https://postgis.net/docs/manual-3.0/ST_Force_4D.html
78+
/// </remarks>
79+
/// <param name="geometry">Input geometry</param>
80+
/// <returns>Geometry in XYZM mode</returns>
81+
[Sql.Function("ST_Force4D", ServerSideOnly = true)]
82+
public static NTSG STForce4D(this NTSG geometry)
83+
{
84+
throw new InvalidOperationException();
85+
}
86+
1787
/// <summary>
1888
/// Returns geometry in its normalized/canonical form.
1989
/// </summary>

0 commit comments

Comments
 (0)