Skip to content

Commit b78a50c

Browse files
committed
Implemented several Geometry Constructors methods
1 parent 07026f5 commit b78a50c

File tree

3 files changed

+307
-1
lines changed

3 files changed

+307
-1
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System.Linq;
2+
3+
using LinqToDB;
4+
using NUnit.Framework;
5+
6+
namespace LinqToDBPostGisNetTopologySuite.Tests
7+
{
8+
[TestFixture]
9+
class GeometryConstructorsTests : TestsBase
10+
{
11+
[SetUp]
12+
public void Setup()
13+
{
14+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
15+
{
16+
db.TestGeometries.Delete();
17+
}
18+
}
19+
20+
[Test]
21+
public void TestSTCollect()
22+
{
23+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
24+
{
25+
db.TestGeometries
26+
.Value(g => g.Id, 1)
27+
.Value(p => p.Geometry, () => GeometryConstructors.STCollect(
28+
GeometryInput.STGeomFromText("POINT(1 2)"),
29+
GeometryInput.STGeomFromText("POINT(-2 3)")))
30+
.Insert();
31+
32+
db.TestGeometries
33+
.Value(g => g.Id, 2)
34+
.Value(p => p.Geometry, () => GeometryConstructors.STCollect(
35+
GeometryInput.STGeomFromText("POINT(1 2)"),
36+
GeometryInput.STGeomFromText("LINESTRING(0 0, 0 1, 1 0, 1 1, 0 0)")))
37+
.Insert();
38+
39+
var collected1 = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STAsText()).Single();
40+
Assert.AreEqual("MULTIPOINT(1 2,-2 3)", collected1);
41+
42+
var collected2 = db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STAsText()).Single();
43+
Assert.AreEqual("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(0 0,0 1,1 0,1 1,0 0))", collected2);
44+
}
45+
}
46+
47+
[Test]
48+
public void TestSTLineFromMultiPoint()
49+
{
50+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
51+
{
52+
db.TestGeometries
53+
.Value(g => g.Id, 1)
54+
.Value(p => p.Geometry, () => GeometryConstructors.STLineFromMultiPoint(
55+
GeometryInput.STGeomFromText("MULTIPOINT(1 2 3, 4 5 6, 7 8 9)")))
56+
.Insert();
57+
58+
var line1 = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STAsEWKT()).Single();
59+
Assert.AreEqual("LINESTRING(1 2 3,4 5 6,7 8 9)", line1);
60+
}
61+
}
62+
63+
[Test]
64+
public void TestSTMakeEnvelope()
65+
{
66+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
67+
{
68+
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryConstructors.STMakeEnvelope(10, 10, 11, 11, SRID4326)).Insert();
69+
var envelope1 = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STAsEWKT()).Single();
70+
Assert.AreEqual("SRID=4326;POLYGON((10 10,10 11,11 11,11 10,10 10))", envelope1);
71+
72+
db.TestGeometries.Value(g => g.Id, 2).Value(p => p.Geometry, () => GeometryConstructors.STMakeEnvelope(10, 10, 11, 11)).Insert();
73+
var envelope2 = db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STAsEWKT()).Single();
74+
Assert.AreEqual("POLYGON((10 10,10 11,11 11,11 10,10 10))", envelope2);
75+
}
76+
}
77+
78+
79+
[Test]
80+
public void TestSTMakeLine()
81+
{
82+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
83+
{
84+
db.TestGeometries
85+
.Value(g => g.Id, 1)
86+
.Value(p => p.Geometry, () => GeometryConstructors.STMakeLine(
87+
GeometryInput.STGeomFromText("POINT(1 2)"),
88+
GeometryInput.STGeomFromText("POINT(3 4)")))
89+
.Insert();
90+
91+
db.TestGeometries
92+
.Value(g => g.Id, 2)
93+
.Value(p => p.Geometry, () => GeometryConstructors.STMakeLine(
94+
GeometryInput.STGeomFromText("LINESTRING(0 0, 1 1)"),
95+
GeometryInput.STGeomFromText("LINESTRING(2 2, 3 3)")))
96+
.Insert();
97+
98+
var line1 = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STAsText()).Single();
99+
Assert.AreEqual("LINESTRING(1 2,3 4)", line1);
100+
101+
var line2 = db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STAsText()).Single();
102+
Assert.AreEqual("LINESTRING(0 0,1 1,2 2,3 3)", line2);
103+
}
104+
}
105+
106+
[Test]
107+
public void TestSTMakePoint()
108+
{
109+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
110+
{
111+
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryConstructors.STMakePoint(-71.1043443253471, 42.3150676015829)).Insert();
112+
db.TestGeometries.Value(g => g.Id, 2).Value(p => p.Geometry, () => GeometryConstructors.STMakePoint(1, 2, 1.5)).Insert();
113+
db.TestGeometries.Value(g => g.Id, 3).Value(p => p.Geometry, () => GeometryConstructors.STMakePoint(10, 20, 30, -999)).Insert();
114+
115+
var point1 = db.TestGeometries.Where(g => g.Id == 1);
116+
Assert.AreEqual(-71.1043443253471, point1.Select(g => g.Geometry.STX()).Single(), 0.0000000000001);
117+
Assert.AreEqual(42.3150676015829, point1.Select(g => g.Geometry.STY()).Single(), 0.0000000000001);
118+
119+
var point2 = db.TestGeometries.Where(g => g.Id == 2);
120+
Assert.AreEqual(1, point2.Select(g => g.Geometry.STX()).Single(), 0.1);
121+
Assert.AreEqual(2, point2.Select(g => g.Geometry.STY()).Single(), 0.1);
122+
Assert.AreEqual(1.5, point2.Select(g => g.Geometry.STZ()).Single(), 0.1);
123+
124+
var point3 = db.TestGeometries.Where(g => g.Id == 3);
125+
Assert.AreEqual(10, point3.Select(g => g.Geometry.STX()).Single(), 0.1);
126+
Assert.AreEqual(20, point3.Select(g => g.Geometry.STY()).Single(), 0.1);
127+
Assert.AreEqual(30, point3.Select(g => g.Geometry.STZ()).Single(), 0.1);
128+
Assert.AreEqual(-999, point3.Select(g => g.Geometry.STM()).Single(), 0.1);
129+
}
130+
}
131+
132+
[Test]
133+
public void TestSTMakePointM()
134+
{
135+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
136+
{
137+
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryConstructors.STMakePointM(10, 20, -999)).Insert();
138+
139+
var point1 = db.TestGeometries.Where(g => g.Id == 1);
140+
Assert.AreEqual(10, point1.Select(g => g.Geometry.STX()).Single(), 0.1);
141+
Assert.AreEqual(20, point1.Select(g => g.Geometry.STY()).Single(), 0.1);
142+
Assert.AreEqual(-999, point1.Select(g => g.Geometry.STM()).Single(), 0.1);
143+
}
144+
}
145+
}
146+
}

LinqToDBPostGisNetTopologySuite/GeometryAccessors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static string STGeometryType(this NTSG geometry)
198198
}
199199

200200
/// <summary>
201-
/// Returns M coordinate of Point, or null if not available. Input must be a Point.
201+
/// Returns M coordinate (measure) of Point, or null if not available. Input must be a Point.
202202
/// </summary>
203203
/// <remarks>
204204
/// See https://postgis.net/docs/manual-3.0/ST_M.html
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System;
2+
3+
using LinqToDB;
4+
5+
using NTSG = NetTopologySuite.Geometries.Geometry;
6+
7+
namespace LinqToDBPostGisNetTopologySuite
8+
{
9+
/// <summary>
10+
/// Geometry Constructors
11+
/// </summary>
12+
/// <remarks>
13+
/// 8.3. Geometry Constructors https://postgis.net/docs/manual-3.0/reference.html#Geometry_Constructors
14+
/// </remarks>
15+
public static class GeometryConstructors
16+
{
17+
/// <summary>
18+
/// Collects geometries into a geometry collection. The result is either a Multi* or a GeometryCollection, depending on whether the input geometries have the same or different types (homogeneous or heterogeneous).
19+
/// </summary>
20+
/// <remarks>
21+
/// See https://postgis.net/docs/manual-3.0/ST_Collect.html
22+
/// </remarks>
23+
/// <param name="geometry">Input geometry 1</param>
24+
/// <param name="other">Input geometry 2</param>
25+
/// <returns>Collected geometry</returns>
26+
[Sql.Function("ST_Collect", ServerSideOnly = true)]
27+
public static NTSG STCollect(this NTSG geometry, NTSG other) // TODO: other versions
28+
{
29+
throw new InvalidOperationException();
30+
}
31+
32+
/// <summary>
33+
/// Creates LineString from MultiPoint geometry.
34+
/// </summary>
35+
/// <remarks>
36+
/// See https://postgis.net/docs/manual-3.0/ST_LineFromMultiPoint.html
37+
/// </remarks>
38+
/// <param name="geometry">Input geometry (MultiPoint)</param>
39+
/// <returns>Constructed geometry</returns>
40+
[Sql.Function("ST_LineFromMultiPoint", ServerSideOnly = true)]
41+
public static NTSG STLineFromMultiPoint(this NTSG geometry)
42+
{
43+
throw new InvalidOperationException();
44+
}
45+
46+
/// <summary>
47+
/// Creates a rectangular Polygon from the minimum and maximum values for X and Y.
48+
/// </summary>
49+
/// <remarks>
50+
/// See https://postgis.net/docs/manual-3.0/ST_MakeEnvelope.html
51+
/// </remarks>
52+
/// <param name="xmin">Minimum X coordinate</param>
53+
/// <param name="ymin">Minimum Y coordinate</param>
54+
/// <param name="xmax">Maximum X coordinate</param>
55+
/// <param name="ymax">Maximum Y coordinate</param>
56+
/// <returns>Constructed geometry</returns>
57+
[Sql.Function("ST_MakeEnvelope", ServerSideOnly = true)]
58+
public static NTSG STMakeEnvelope(double xmin, double ymin, double xmax, double ymax)
59+
{
60+
throw new InvalidOperationException();
61+
}
62+
63+
/// <summary>
64+
/// Creates a rectangular Polygon from the minimum and maximum values for X and Y.
65+
/// </summary>
66+
/// <remarks>
67+
/// See https://postgis.net/docs/manual-3.0/ST_MakeEnvelope.html
68+
/// </remarks>
69+
/// <param name="xmin">Minimum X coordinate</param>
70+
/// <param name="ymin">Minimum Y coordinate</param>
71+
/// <param name="xmax">Maximum X coordinate</param>
72+
/// <param name="ymax">Maximum Y coordinate</param>
73+
/// <param name="srid">Spatial Reference System Identifier</param>
74+
/// <returns>Constructed geometry</returns>
75+
[Sql.Function("ST_MakeEnvelope", ServerSideOnly = true)]
76+
public static NTSG STMakeEnvelope(double xmin, double ymin, double xmax, double ymax, int srid)
77+
{
78+
throw new InvalidOperationException();
79+
}
80+
81+
/// <summary>
82+
/// Creates a LineString containing the points of given geometries (Point, MultiPoint, or LineString).
83+
/// </summary>
84+
/// <remarks>
85+
/// See https://postgis.net/docs/manual-3.0/ST_MakeLine.html
86+
/// </remarks>
87+
/// <param name="geometry">Input geometry 1</param>
88+
/// <param name="other">Input geometry 2</param>
89+
/// <returns>Constructed geometry</returns>
90+
[Sql.Function("ST_MakeLine", ServerSideOnly = true)]
91+
public static NTSG STMakeLine(this NTSG geometry, NTSG other) // TODO: other versions
92+
{
93+
throw new InvalidOperationException();
94+
}
95+
96+
/// <summary>
97+
/// Constructs 2D Point geometry.
98+
/// </summary>
99+
/// <remarks>
100+
/// See https://postgis.net/docs/manual-3.0/ST_MakePoint.html
101+
/// </remarks>
102+
/// <param name="x">X coordinate</param>
103+
/// <param name="y">Y coordinate</param>
104+
/// <returns>Geometry</returns>
105+
[Sql.Function("ST_MakePoint", ServerSideOnly = true)]
106+
public static NTSG STMakePoint(double x, double y)
107+
{
108+
throw new InvalidOperationException();
109+
}
110+
111+
/// <summary>
112+
/// Constructs 3D Z Point geometry.
113+
/// </summary>
114+
/// <remarks>
115+
/// See https://postgis.net/docs/manual-3.0/ST_MakePoint.html
116+
/// </remarks>
117+
/// <param name="x">X coordinate</param>
118+
/// <param name="y">Y coordinate</param>
119+
/// <param name="z">Z coordinate</param>
120+
/// <returns>Geometry</returns>
121+
[Sql.Function("ST_MakePoint", ServerSideOnly = true)]
122+
public static NTSG STMakePoint(double x, double y, double z)
123+
{
124+
throw new InvalidOperationException();
125+
}
126+
127+
/// <summary>
128+
/// Constructs 4D ZM Point geometry.
129+
/// </summary>
130+
/// <remarks>
131+
/// See https://postgis.net/docs/manual-3.0/ST_MakePoint.html
132+
/// </remarks>
133+
/// <param name="x">X coordinate</param>
134+
/// <param name="y">Y coordinate</param>
135+
/// <param name="z">Z coordinate</param>
136+
/// <param name="m">M coordinate (measure)</param>
137+
/// <returns>Geometry</returns>
138+
[Sql.Function("ST_MakePoint", ServerSideOnly = true)]
139+
public static NTSG STMakePoint(double x, double y, double z, double m)
140+
{
141+
throw new InvalidOperationException();
142+
}
143+
144+
/// <summary>
145+
/// Constructs Point geometry with X, Y and M coordinates.
146+
/// </summary>
147+
/// <remarks>
148+
/// See https://postgis.net/docs/manual-3.0/ST_MakePointM.html
149+
/// </remarks>
150+
/// <param name="x">X coordinate</param>
151+
/// <param name="y">Y coordinate</param>
152+
/// <param name="m">M coordinate (measure)</param>
153+
/// <returns>Geometry</returns>
154+
[Sql.Function("ST_MakePointM", ServerSideOnly = true)]
155+
public static NTSG STMakePointM(double x, double y, double m)
156+
{
157+
throw new InvalidOperationException();
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)