Skip to content

Commit c1590de

Browse files
committed
Implemented most Geometry Input methods from Other Formats
1 parent 329b8a0 commit c1590de

File tree

2 files changed

+331
-1
lines changed

2 files changed

+331
-1
lines changed

LinqToDBPostGisNetTopologySuite.Tests/GeometryInputTests.cs

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23

34
using LinqToDB;
45
using NUnit.Framework;
@@ -45,5 +46,135 @@ public void TestSTGeomFromText()
4546
Assert.IsNull(srid3);
4647
}
4748
}
49+
50+
[Test]
51+
public void TestSTGeomFromGeoHash()
52+
{
53+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
54+
{
55+
const string geoHash = "9qqj7nmxncgyy4d0dbxqz0";
56+
57+
var wkt1 = db.Select(() => GeometryInput.STGeomFromGeoHash(geoHash).STAsText());
58+
Assert.AreEqual("POLYGON((-115.172816 36.114646,-115.172816 36.114646,-115.172816 36.114646,-115.172816 36.114646,-115.172816 36.114646))", wkt1);
59+
60+
var wkt2 = db.Select(() => GeometryInput.STGeomFromGeoHash(geoHash, 4).STAsText());
61+
Assert.AreEqual("POLYGON((-115.3125 36.03515625,-115.3125 36.2109375,-114.9609375 36.2109375,-114.9609375 36.03515625,-115.3125 36.03515625))", wkt2);
62+
63+
var wkt3 = db.Select(() => GeometryInput.STGeomFromGeoHash(geoHash, 10).STAsText());
64+
Assert.AreEqual("POLYGON((-115.17282128334 36.1146408319473,-115.17282128334 36.1146461963654,-115.172810554504 36.1146461963654,-115.172810554504 36.1146408319473,-115.17282128334 36.1146408319473))", wkt3);
65+
66+
Assert.IsNull(db.Select(() => GeometryInput.STGeomFromGeoHash(null)));
67+
}
68+
}
69+
70+
[Test]
71+
public void TestSTGeomFromGML()
72+
{
73+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
74+
{
75+
var ewkt1 = db.Select(() => GeometryInput.STGeomFromGML("<gml:LineString srsName=\"EPSG:4269\"><gml:coordinates>-71.16028,42.258729 -71.160837,42.259112 -71.161143,42.25932</gml:coordinates></gml:LineString>").STAsEWKT());
76+
Assert.AreEqual("SRID=4269;LINESTRING(-71.16028 42.258729,-71.160837 42.259112,-71.161143 42.25932)", ewkt1);
77+
78+
Assert.IsNull(db.Select(() => GeometryInput.STGeomFromGML(null)));
79+
}
80+
}
81+
82+
[Test]
83+
public void TestSTGeomFromGeoJSON()
84+
{
85+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
86+
{
87+
var wkt1 = db.Select(() => GeometryInput.STGeomFromGeoJSON("{\"type\":\"Point\",\"coordinates\":[-48.23456,20.12345]}").STAsText());
88+
Assert.AreEqual("POINT(-48.23456 20.12345)", wkt1);
89+
90+
var wkt2 = db.Select(() => GeometryInput.STGeomFromGeoJSON("{\"type\":\"LineString\",\"coordinates\":[[1,2,3],[4,5,6],[7,8,9]]}").STAsText());
91+
Assert.AreEqual("LINESTRING Z (1 2 3,4 5 6,7 8 9)", wkt2);
92+
93+
var wkt3 = db.Select(() => GeometryInput.STGeomFromGeoJSON(null).STAsText());
94+
Assert.IsNull(wkt3);
95+
}
96+
}
97+
98+
[Test]
99+
public void TestSTGeomFromKML()
100+
{
101+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
102+
{
103+
var ewkt1 = db.Select(() => GeometryInput.STGeomFromKML("<LineString><coordinates>-71.1663, 42.2614 -71.1667, 42.2616</coordinates></LineString>").STAsEWKT());
104+
Assert.AreEqual("SRID=4326;LINESTRING(-71.1663 42.2614,-71.1667 42.2616)", ewkt1);
105+
106+
var ewkt2 = db.Select(() => GeometryInput.STGeomFromKML(null).STAsEWKT());
107+
Assert.IsNull(ewkt2);
108+
}
109+
}
110+
111+
[Test]
112+
public void TestSTGeomFromTWKB()
113+
{
114+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
115+
{
116+
var ewkt1 = db.Select(() => GeometryInput.STGeomFromTWKB(new byte[] { 0x62, 0x00, 0x02, 0xf7, 0xf4, 0x0d, 0xbc, 0xe4, 0x04, 0x01, 0x05 }).STAsEWKT());
117+
Assert.AreEqual("LINESTRING(-113.98 39.198,-113.981 39.195)", ewkt1);
118+
119+
var ewkt2 = db.Select(() => GeometryInput.STGeomFromTWKB(null).STAsEWKT());
120+
Assert.IsNull(ewkt2);
121+
}
122+
}
123+
124+
[Test]
125+
public void TestSTLineFromEncodedPolyline()
126+
{
127+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
128+
{
129+
const string ep1 = "_p~iF~ps|U_ulLnnqC_mqNvxq`@";
130+
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryInput.STLineFromEncodedPolyline(ep1)).Insert();
131+
db.TestGeometries.Value(g => g.Id, 2).Value(p => p.Geometry, () => GeometryInput.STLineFromEncodedPolyline(ep1, 6)).Insert();
132+
133+
var ewkt1 = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STAsEWKT()).Single();
134+
Assert.AreEqual("SRID=4326;LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)", ewkt1);
135+
136+
var ewkt2 = db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STAsEWKT()).Single();
137+
Assert.AreEqual("SRID=4326;LINESTRING(-12.02 3.85,-12.095 4.07,-12.6453 4.3252)", ewkt2);
138+
139+
db.TestGeometries.Value(g => g.Id, 3).Value(p => p.Geometry, () => GeometryInput.STLineFromEncodedPolyline(String.Empty)).Insert();
140+
db.TestGeometries.Value(g => g.Id, 4).Value(p => p.Geometry, () => GeometryInput.STLineFromEncodedPolyline(null)).Insert();
141+
142+
var ewkt3 = db.TestGeometries.Where(g => g.Id == 3).Select(g => g.Geometry.STAsEWKT()).Single();
143+
Assert.AreEqual("SRID=4326;LINESTRING EMPTY", ewkt3);
144+
145+
var ewkt4 = db.TestGeometries.Where(g => g.Id == 4).Select(g => g.Geometry.STAsEWKT()).Single();
146+
Assert.IsNull(ewkt4);
147+
}
148+
}
149+
150+
[Test]
151+
public void TestSTPointFromGeoHash()
152+
{
153+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
154+
{
155+
const string geohash1 = "9qqj7nmxncgyy4d0dbxqz0";
156+
db.TestGeometries.Value(g => g.Id, 1).Value(p => p.Geometry, () => GeometryInput.STPointFromGeoHash(geohash1)).Insert();
157+
db.TestGeometries.Value(g => g.Id, 2).Value(p => p.Geometry, () => GeometryInput.STPointFromGeoHash(geohash1, 4)).Insert();
158+
db.TestGeometries.Value(g => g.Id, 3).Value(p => p.Geometry, () => GeometryInput.STPointFromGeoHash(geohash1, 10)).Insert();
159+
160+
var wkt1 = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STAsText()).Single();
161+
Assert.AreEqual("POINT(-115.172816 36.114646)", wkt1);
162+
163+
var wkt2 = db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STAsText()).Single();
164+
Assert.AreEqual("POINT(-115.13671875 36.123046875)", wkt2);
165+
166+
var wkt3 = db.TestGeometries.Where(g => g.Id == 3).Select(g => g.Geometry.STAsText()).Single();
167+
Assert.AreEqual("POINT(-115.172815918922 36.1146435141563)", wkt3);
168+
169+
db.TestGeometries.Value(g => g.Id, 4).Value(p => p.Geometry, () => GeometryInput.STPointFromGeoHash(String.Empty)).Insert();
170+
db.TestGeometries.Value(g => g.Id, 5).Value(p => p.Geometry, () => GeometryInput.STPointFromGeoHash(null)).Insert();
171+
172+
var wkt4 = db.TestGeometries.Where(g => g.Id == 4).Select(g => g.Geometry.STAsText()).Single();
173+
Assert.AreEqual("POINT(0 0)", wkt4);
174+
175+
var wkt5 = db.TestGeometries.Where(g => g.Id == 5).Select(g => g.Geometry.STAsText()).Single();
176+
Assert.IsNull(wkt5);
177+
}
178+
}
48179
}
49180
}

LinqToDBPostGisNetTopologySuite/GeometryInput.cs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace LinqToDBPostGisNetTopologySuite
1414
/// </remarks>
1515
public static class GeometryInput
1616
{
17+
// TODO: Complete 8.8.1. Well-Known Text (WKT)
18+
1719
/// <summary>
1820
/// Constructs geometry from the Extended Well-Known Text (EWKT) representation.
1921
/// </summary>
@@ -56,5 +58,202 @@ public static NTSG STGeomFromText(string wkt, int srid)
5658
{
5759
throw new InvalidOperationException();
5860
}
61+
62+
// TODO: Implement 8.8.2. Well-Known Binary (WKB)
63+
64+
#region 8.8.3. Other Formats
65+
66+
// TODO: ST_Box2dFromGeoHash return type NTS mapping?
67+
68+
/// <summary>
69+
/// Constructs Polygon geometry from given GeoHash string.
70+
/// </summary>
71+
/// <remarks>
72+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromGeoHash.html
73+
/// </remarks>
74+
/// <param name="geoHash">GeoHash string</param>
75+
/// <returns>Polygon representing the GeoHash bounds</returns>
76+
[Sql.Function("ST_GeomFromGeoHash", ServerSideOnly = true)]
77+
public static NTSG STGeomFromGeoHash(string geoHash)
78+
{
79+
throw new InvalidOperationException();
80+
}
81+
82+
/// <summary>
83+
/// Constructs Polygon geometry from given GeoHash string.
84+
/// </summary>
85+
/// <remarks>
86+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromGeoHash.html
87+
/// </remarks>
88+
/// <param name="geoHash">GeoHash string</param>
89+
/// <param name="precision">Number of characters to use from GeoHash</param>
90+
/// <returns>Polygon representing the GeoHash bounds</returns>
91+
[Sql.Function("ST_GeomFromGeoHash", ServerSideOnly = true)]
92+
public static NTSG STGeomFromGeoHash(string geoHash, int precision)
93+
{
94+
throw new InvalidOperationException();
95+
}
96+
97+
/// <summary>
98+
/// Constructs geometry from OGC GML representation.
99+
/// </summary>
100+
/// <remarks>
101+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromGML.html
102+
/// </remarks>
103+
/// <param name="gml">GML string</param>
104+
/// <returns>Geometry</returns>
105+
[Sql.Function("ST_GeomFromGML", ServerSideOnly = true)]
106+
public static NTSG STGeomFromGML(string gml)
107+
{
108+
throw new InvalidOperationException();
109+
}
110+
111+
/// <summary>
112+
/// Constructs geometry from OGC GML representation with given SRID.
113+
/// </summary>
114+
/// <remarks>
115+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromGML.html
116+
/// </remarks>
117+
/// <param name="gml">GML string</param>
118+
/// <param name="srid">Spatial Reference System Identifier for geometry</param>
119+
/// <returns>Geometry</returns>
120+
[Sql.Function("ST_GeomFromGML", ServerSideOnly = true)]
121+
public static NTSG STGeomFromGML(string gml, int srid)
122+
{
123+
throw new InvalidOperationException();
124+
}
125+
126+
/// <summary>
127+
/// Constructs geometry from GeoJSON representation.
128+
/// </summary>
129+
/// <remarks>
130+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromGeoJSON.html
131+
/// </remarks>
132+
/// <param name="geoJson">GeoJSON string</param>
133+
/// <returns>Geometry</returns>
134+
[Sql.Function("ST_GeomFromGeoJSON", ServerSideOnly = true)]
135+
public static NTSG STGeomFromGeoJSON(string geoJson)
136+
{
137+
throw new InvalidOperationException();
138+
}
139+
140+
// TODO: ? json / jsonb ?
141+
142+
/// <summary>
143+
/// Constructs geometry from OGC KML representation.
144+
/// </summary>
145+
/// <remarks>
146+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromKML.html
147+
/// </remarks>
148+
/// <param name="kml">KML string</param>
149+
/// <returns>Geometry</returns>
150+
[Sql.Function("ST_GeomFromKML", ServerSideOnly = true)]
151+
public static NTSG STGeomFromKML(string kml)
152+
{
153+
throw new InvalidOperationException();
154+
}
155+
156+
/// <summary>
157+
/// Constructs geometry from Tiny Well-Known Binary (TWKB) representation.
158+
/// </summary>
159+
/// <remarks>
160+
/// See https://postgis.net/docs/manual-3.0/ST_GeomFromTWKB.html
161+
/// </remarks>
162+
/// <param name="twkb">Tiny Well-Known Binary (TWKB)</param>
163+
/// <returns>Geometry</returns>
164+
[Sql.Function("ST_GeomFromTWKB", ServerSideOnly = true)]
165+
public static NTSG STGeomFromTWKB(byte[] twkb)
166+
{
167+
throw new InvalidOperationException();
168+
}
169+
170+
/// <summary>
171+
/// Constructs geometry from OGC GML representation (Alias for STGeomFromGML).
172+
/// </summary>
173+
/// <remarks>
174+
/// See https://postgis.net/docs/manual-3.0/ST_GMLToSQL.html
175+
/// </remarks>
176+
/// <param name="gml">GML string</param>
177+
/// <returns>Geometry</returns>
178+
[Sql.Function("ST_GMLToSQL", ServerSideOnly = true)]
179+
public static NTSG STGMLToSQL(string gml)
180+
{
181+
throw new InvalidOperationException();
182+
}
183+
184+
/// <summary>
185+
/// Constructs geometry from OGC GML representation with given SRID (Alias for STGeomFromGML).
186+
/// </summary>
187+
/// <remarks>
188+
/// See https://postgis.net/docs/manual-3.0/ST_GMLToSQL.html
189+
/// </remarks>
190+
/// <param name="gml">GML string</param>
191+
/// <param name="srid">Spatial Reference System Identifier for geometry</param>
192+
/// <returns>Geometry</returns>
193+
[Sql.Function("ST_GMLToSQL", ServerSideOnly = true)]
194+
public static NTSG STGMLToSQL(string gml, int srid)
195+
{
196+
throw new InvalidOperationException();
197+
}
198+
199+
/// <summary>
200+
/// Constructs LineString geometry from given Encoded Polyline string.
201+
/// </summary>
202+
/// <remarks>
203+
/// See https://postgis.net/docs/manual-3.0/ST_LineFromEncodedPolyline.html
204+
/// </remarks>
205+
/// <param name=" polyline">Encoded Polyline string</param>
206+
/// <returns>LineString geometry</returns>
207+
[Sql.Function("ST_LineFromEncodedPolyline", ServerSideOnly = true)]
208+
public static NTSG STLineFromEncodedPolyline(string polyline)
209+
{
210+
throw new InvalidOperationException();
211+
}
212+
213+
/// <summary>
214+
/// Constructs LineString geometry from given Encoded Polyline string.
215+
/// </summary>
216+
/// <remarks>
217+
/// See https://postgis.net/docs/manual-3.0/ST_LineFromEncodedPolyline.html
218+
/// </remarks>
219+
/// <param name=" polyline">Encoded Polyline string</param>
220+
/// <param name="precision">Number of characters to use from Encoded Polyline string</param>
221+
/// <returns>LineString geometry</returns>
222+
[Sql.Function("ST_LineFromEncodedPolyline", ServerSideOnly = true)]
223+
public static NTSG STLineFromEncodedPolyline(string polyline, int precision)
224+
{
225+
throw new InvalidOperationException();
226+
}
227+
228+
/// <summary>
229+
/// Constructs Point geometry from given GeoHash string.
230+
/// </summary>
231+
/// <remarks>
232+
/// See https://postgis.net/docs/manual-3.0/ST_PointFromGeoHash.html
233+
/// </remarks>
234+
/// <param name="geoHash">GeoHash string</param>
235+
/// <returns>Geometry (center point of GeoHash)</returns>
236+
[Sql.Function("ST_PointFromGeoHash", ServerSideOnly = true)]
237+
public static NTSG STPointFromGeoHash(string geoHash)
238+
{
239+
throw new InvalidOperationException();
240+
}
241+
242+
/// <summary>
243+
/// Constructs Point geometry from given GeoHash string.
244+
/// </summary>
245+
/// <remarks>
246+
/// See https://postgis.net/docs/manual-3.0/ST_PointFromGeoHash.html
247+
/// </remarks>
248+
/// <param name="geoHash">GeoHash string</param>
249+
/// <param name="precision">Number of characters to use from GeoHash</param>
250+
/// <returns>Geometry (center point of GeoHash)</returns>
251+
[Sql.Function("ST_PointFromGeoHash", ServerSideOnly = true)]
252+
public static NTSG STPointFromGeoHash(string geoHash, int precision)
253+
{
254+
throw new InvalidOperationException();
255+
}
256+
257+
#endregion
59258
}
60259
}

0 commit comments

Comments
 (0)