Skip to content

Commit 168b934

Browse files
committed
Implemented most Geometry Constructors methods with tests
1 parent ab287f3 commit 168b934

File tree

4 files changed

+155
-7
lines changed

4 files changed

+155
-7
lines changed

LinqToDBPostGisNetTopologySuite.Tests/GeometryConstructorsTests.cs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,99 @@ public void TestSTMakePointM()
134134
{
135135
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
136136
{
137-
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryConstructors.STMakePointM(10, 20, -999)).Insert();
137+
db.TestGeometries
138+
.Value(g => g.Id, 1)
139+
.Value(p => p.Geometry, () => GeometryConstructors.STMakePointM(10, 20, -999))
140+
.Insert();
138141

139142
var point1 = db.TestGeometries.Where(g => g.Id == 1);
140143
Assert.AreEqual(10, point1.Select(g => g.Geometry.STX()).Single(), 0.1);
141144
Assert.AreEqual(20, point1.Select(g => g.Geometry.STY()).Single(), 0.1);
142145
Assert.AreEqual(-999, point1.Select(g => g.Geometry.STM()).Single(), 0.1);
143146
}
144147
}
148+
149+
[Test]
150+
public void TestSTMakePolygon()
151+
{
152+
const string Wkt1 = "LINESTRING(75.15 29.53 1,77 29 1,77.6 29.5 1, 75.15 29.53 1)";
153+
const string Wkt2 = "LINESTRINGM(75.15 29.53 1,77 29 1,77.6 29.5 2, 75.15 29.53 2)";
154+
155+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
156+
{
157+
db.TestGeometries
158+
.Value(g => g.Id, 1)
159+
.Value(p => p.Geometry, () => GeometryConstructors.STMakePolygon(GeometryInput.STGeomFromText(Wkt1)))
160+
.Insert();
161+
162+
db.TestGeometries
163+
.Value(g => g.Id, 2)
164+
.Value(p => p.Geometry, () => GeometryConstructors.STMakePolygon(GeometryInput.STGeomFromText(Wkt2)))
165+
.Insert();
166+
167+
var result1 = db.TestGeometries.Where(g => g.Id == 1);
168+
Assert.AreEqual("POLYGON((75.15 29.53 1,77 29 1,77.6 29.5 1,75.15 29.53 1))",
169+
result1.Select(g => g.Geometry.STAsEWKT()).Single());
170+
171+
var result2 = db.TestGeometries.Where(g => g.Id == 2);
172+
Assert.AreEqual("POLYGONM((75.15 29.53 1,77 29 1,77.6 29.5 2,75.15 29.53 2))",
173+
result2.Select(g => g.Geometry.STAsEWKT()).Single());
174+
}
175+
}
176+
177+
[Test]
178+
public void TestSTPolygon()
179+
{
180+
const string Wkt1 = "LINESTRING(75 29, 77 29, 77 29, 75 29)";
181+
const string Wkt2 = "LINESTRING(75 29 1, 77 29 2, 77 29 3, 75 29 1)";
182+
183+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
184+
{
185+
db.TestGeometries
186+
.Value(g => g.Id, 1)
187+
.Value(p => p.Geometry, () => GeometryConstructors.STPolygon(GeometryInput.STGeomFromText(Wkt1), SRID4326))
188+
.Insert();
189+
190+
db.TestGeometries
191+
.Value(g => g.Id, 2)
192+
.Value(p => p.Geometry, () => GeometryConstructors.STPolygon(GeometryInput.STGeomFromText(Wkt2), SRID4326))
193+
.Insert();
194+
195+
var result1 = db.TestGeometries.Where(g => g.Id == 1);
196+
Assert.AreEqual("POLYGON((75 29,77 29,77 29,75 29))",
197+
result1.Select(g => g.Geometry.STAsText()).Single());
198+
199+
var result2 = db.TestGeometries.Where(g => g.Id == 2);
200+
Assert.AreEqual("SRID=4326;POLYGON((75 29 1,77 29 2,77 29 3,75 29 1))",
201+
result2.Select(g => g.Geometry.STAsEWKT()).Single());
202+
}
203+
}
204+
205+
[Test]
206+
public void TestSTTileEnvelope()
207+
{
208+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
209+
{
210+
db.TestGeometries
211+
.Value(g => g.Id, 1)
212+
.Value(p => p.Geometry, () => GeometryConstructors.STTileEnvelope(2, 1, 1))
213+
.Insert();
214+
215+
db.TestGeometries
216+
.Value(g => g.Id, 2)
217+
.Value(p => p.Geometry, () => GeometryConstructors.STTileEnvelope(3, 1, 1, GeometryConstructors.STMakeEnvelope(-180, -90, 180, 90, SRID4326)))
218+
.Insert();
219+
220+
var result1 = db.TestGeometries.Where(g => g.Id == 1);
221+
Assert.AreEqual("POLYGON((-10018754.1713945 0,-10018754.1713945 10018754.1713945,0 10018754.1713945,0 0,-10018754.1713945 0))",
222+
result1.Select(g => g.Geometry.STAsText()).Single());
223+
Assert.AreEqual(3857,
224+
result1.Select(g => g.Geometry.STSrId()).Single());
225+
226+
var result2 = db.TestGeometries.Where(g => g.Id == 2);
227+
Assert.AreEqual("POLYGON((-135 45,-135 67.5,-90 67.5,-90 45,-135 45))",
228+
result2.Select(g => g.Geometry.STAsText()).Single());
229+
}
230+
}
145231
}
146-
}
232+
}

LinqToDBPostGisNetTopologySuite/GeometryConstructors.cs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static NTSG STMakeEnvelope(double xmin, double ymin, double xmax, double
7070
/// <param name="ymin">Minimum Y coordinate</param>
7171
/// <param name="xmax">Maximum X coordinate</param>
7272
/// <param name="ymax">Maximum Y coordinate</param>
73-
/// <param name="srid">Spatial Reference System Identifier</param>
73+
/// <param name="srid">Spatial Reference System Identifier (SRID)</param>
7474
/// <returns>Constructed geometry</returns>
7575
[Sql.Function("ST_MakeEnvelope", ServerSideOnly = true)]
7676
public static NTSG STMakeEnvelope(double xmin, double ymin, double xmax, double ymax, int srid)
@@ -157,6 +157,20 @@ public static NTSG STMakePointM(double x, double y, double m)
157157
throw new InvalidOperationException();
158158
}
159159

160+
/// <summary>
161+
/// Constructs Polygon geometry formed by given shell.
162+
/// </summary>
163+
/// <remarks>
164+
/// See https://postgis.net/docs/manual-3.0/ST_MakePolygon.html
165+
/// </remarks>
166+
/// <param name="geometry">Input geometry (closed LineString)</param>
167+
/// <returns>Geometry</returns>
168+
[Sql.Function("ST_MakePolygon", ServerSideOnly = true)]
169+
public static NTSG STMakePolygon(this NTSG geometry) // TODO: with interiorlinestrings
170+
{
171+
throw new InvalidOperationException();
172+
}
173+
160174
/// <summary>
161175
/// Constructs 2D Point geometry.
162176
/// </summary>
@@ -165,11 +179,59 @@ public static NTSG STMakePointM(double x, double y, double m)
165179
/// </remarks>
166180
/// <param name="x">X coordinate</param>
167181
/// <param name="y">Y coordinate</param>
168-
/// <returns>Geometry (POINT)</returns>
182+
/// <returns>Geometry (Point)</returns>
169183
[Sql.Function("ST_Point", ServerSideOnly = true)]
170184
public static NTSG STPoint(double x, double y)
171185
{
172186
throw new InvalidOperationException();
173187
}
188+
189+
/// <summary>
190+
/// Constructs Polygon geometry from given LineString.
191+
/// </summary>
192+
/// <remarks>
193+
/// See https://postgis.net/docs/manual-3.0/ST_Polygon.html
194+
/// </remarks>
195+
/// <param name="geometry">Input geometry (closed LineString)</param>
196+
/// <param name="srid">Spatial Reference System Identifier (SRID)</param>
197+
/// <returns>Geometry</returns>
198+
[Sql.Function("ST_Polygon", ServerSideOnly = true)]
199+
public static NTSG STPolygon(this NTSG geometry, int srid)
200+
{
201+
throw new InvalidOperationException();
202+
}
203+
204+
/// <summary>
205+
/// Constructs rectangular Polygon in Web Mercator (SRID:3857) using XYZ tile system.
206+
/// </summary>
207+
/// <remarks>
208+
/// See https://postgis.net/docs/manual-3.0/ST_TileEnvelope.html
209+
/// </remarks>
210+
/// <param name="tileZoom">Tile Z</param>
211+
/// <param name="tileX">Tile X</param>
212+
/// <param name="tileY">Tile Y</param>
213+
/// <returns>Geometry (Polygon)</returns>
214+
[Sql.Function("ST_TileEnvelope", ServerSideOnly = true)]
215+
public static NTSG STTileEnvelope(int tileZoom, int tileX, int tileY)
216+
{
217+
throw new InvalidOperationException();
218+
}
219+
220+
/// <summary>
221+
/// Constructs rectangular Polygon in Web Mercator (SRID:3857) using XYZ tile system.
222+
/// </summary>
223+
/// <remarks>
224+
/// See https://postgis.net/docs/manual-3.0/ST_TileEnvelope.html
225+
/// </remarks>
226+
/// <param name="tileZoom">Tile Z</param>
227+
/// <param name="tileX">Tile X</param>
228+
/// <param name="tileY">Tile Y</param>
229+
/// <param name="bounds">Bounds</param>
230+
/// <returns>Geometry (Polygon)</returns>
231+
[Sql.Function("ST_TileEnvelope", ServerSideOnly = true)]
232+
public static NTSG STTileEnvelope(int tileZoom, int tileX, int tileY, NTSG bounds)
233+
{
234+
throw new InvalidOperationException();
235+
}
174236
}
175-
}
237+
}

LinqToDBPostGisNetTopologySuite/LinqToDBPostGisNetTopologySuite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.0.4</Version>
5+
<Version>0.0.5</Version>
66
<RepositoryUrl>https://github.com/apdevelop/linq2db-postgis-extensions</RepositoryUrl>
77
<Authors>apdevelop</Authors>
88
<Company>apdevelop</Company>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Depends on [linq2db](https://github.com/linq2db/linq2db), [Npgsql](https://githu
9494
### TODOs
9595
* Implement full set of PostGIS methods.
9696
* Add support for PostGIS `geography` data type.
97-
* Test on various versions of PostgreSQL/PostGIS and platforms.
97+
* Test on various versions of PostgreSQL/PostGIS and platforms (including .NET 5).
9898

9999
### References
100100
* [PostGIS Reference](https://postgis.net/docs/manual-3.0/reference.html)

0 commit comments

Comments
 (0)