1+ using System . Linq ;
2+
3+ using LinqToDB ;
4+ using NUnit . Framework ;
5+ using NTSG = NetTopologySuite . Geometries ;
6+
7+ namespace LinqToDBPostGisNetTopologySuite . Tests
8+ {
9+ [ TestFixture ]
10+ class LinearReferencingTests : TestsBase
11+ {
12+ [ SetUp ]
13+ public void Setup ( )
14+ {
15+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
16+ {
17+ db . TestGeometries . Delete ( ) ;
18+ }
19+ }
20+
21+ [ Test ]
22+ public void TestSTLineInterpolatePoint ( )
23+ {
24+ const string ewkt = "LINESTRING(25 50, 100 125, 150 190)" ;
25+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
26+ {
27+ db . TestGeometries
28+ . Value ( g => g . Id , 1 )
29+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
30+ . Insert ( ) ;
31+
32+ var result = db . TestGeometries
33+ . Where ( g => g . Id == 1 )
34+ . Select ( g => g . Geometry . STLineInterpolatePoint ( 0.20 ) )
35+ . Single ( ) as NTSG . Point ;
36+
37+ Assert . AreEqual ( 51.5974135047432 , result . X , 1.0E-8 ) ;
38+ Assert . AreEqual ( 76.5974135047432 , result . Y , 1.0E-8 ) ;
39+
40+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLineInterpolatePoint ( null , 0.0 ) ) ) ;
41+ }
42+ }
43+
44+ [ Test ]
45+ public void TestST3DLineInterpolatePoint ( )
46+ {
47+ const string ewkt = "LINESTRING(25 50 70, 100 125 90, 150 190 200)" ;
48+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
49+ {
50+ db . TestGeometries
51+ . Value ( g => g . Id , 1 )
52+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
53+ . Insert ( ) ;
54+
55+ var result = db . TestGeometries
56+ . Where ( g => g . Id == 1 )
57+ . Select ( g => g . Geometry . ST3DLineInterpolatePoint ( 0.20 ) )
58+ . Single ( ) as NTSG . Point ;
59+
60+ Assert . AreEqual ( 59.0675892910822 , result . X , 1.0E-8 ) ;
61+ Assert . AreEqual ( 84.0675892910822 , result . Y , 1.0E-8 ) ;
62+ Assert . AreEqual ( 79.0846904776219 , result . Z , 1.0E-8 ) ;
63+
64+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . ST3DLineInterpolatePoint ( null , 0 ) ) ) ;
65+ }
66+ }
67+
68+ [ Test ]
69+ public void TestSTLineInterpolatePoints ( )
70+ {
71+ const string ewkt = "LINESTRING(25 50, 100 125, 150 190)" ;
72+ var expected = new double [ ] [ ]
73+ {
74+ new double [ ] { 51.5974135047432 , 76.5974135047432 } ,
75+ new double [ ] { 78.1948270094864 , 103.194827009486 } ,
76+ new double [ ] { 104.132163186446 , 130.37181214238 } ,
77+ new double [ ] { 127.066081593223 , 160.18590607119 } ,
78+ new double [ ] { 150.0 , 190.0 } ,
79+ } ;
80+
81+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
82+ {
83+ db . TestGeometries
84+ . Value ( g => g . Id , 1 )
85+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
86+ . Insert ( ) ;
87+
88+ var result = db . TestGeometries
89+ . Where ( g => g . Id == 1 )
90+ . Select ( g => g . Geometry . STLineInterpolatePoints ( 0.20 , true ) )
91+ . Single ( ) as NTSG . MultiPoint ;
92+
93+ Assert . AreEqual ( expected . Length , result . NumPoints ) ;
94+ for ( int i = 0 ; i < expected . Length ; i ++ )
95+ {
96+ var exp = expected [ i ] ;
97+ var res = result . Geometries [ i ] as NTSG . Point ;
98+
99+ Assert . AreEqual ( exp [ 0 ] , res . X , 1.0E-8 ) ;
100+ Assert . AreEqual ( exp [ 1 ] , res . Y , 1.0E-8 ) ;
101+ }
102+
103+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLineInterpolatePoints ( null , 0 , true ) ) ) ;
104+ }
105+ }
106+
107+ [ Test ]
108+ public void TestSTLineLocatePoint ( )
109+ {
110+ const string ewkt = "LINESTRING(1 2, 4 5, 6 7)" ;
111+ var givenPointText = "POINT(4 3)" ;
112+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
113+ {
114+ db . TestGeometries
115+ . Value ( g => g . Id , 1 )
116+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
117+ . Insert ( ) ;
118+
119+ var result = db . TestGeometries
120+ . Where ( g => g . Id == 1 )
121+ . Select ( g => g . Geometry . STLineLocatePoint ( GeometryInput . STGeomFromEWKT ( givenPointText ) ) )
122+ . Single ( ) as double ? ;
123+
124+ Assert . AreEqual ( 0.4 , result . Value , 1.0E-8 ) ;
125+
126+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLineLocatePoint ( null , null ) ) ) ;
127+ }
128+ }
129+
130+ [ Test ]
131+ public void TestSTLineSubstring ( )
132+ {
133+ const string ewkt = "LINESTRING(25 50, 100 125, 150 190)" ;
134+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
135+ {
136+ db . TestGeometries
137+ . Value ( g => g . Id , 1 )
138+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
139+ . Insert ( ) ;
140+
141+ var result = db . TestGeometries
142+ . Where ( g => g . Id == 1 )
143+ . Select ( g => g . Geometry . STLineSubstring ( 0.333 , 0.666 ) )
144+ . Single ( ) as NTSG . LineString ;
145+
146+ Assert . AreEqual ( 3.0 , result . Coordinates . Length , 1.0E-8 ) ;
147+
148+ Assert . AreEqual ( 69.28469348539744 , result . GetCoordinateN ( 0 ) . X , 1.0E-8 ) ;
149+ Assert . AreEqual ( 94.28469348539744 , result . GetCoordinateN ( 0 ) . Y , 1.0E-8 ) ;
150+
151+ Assert . AreEqual ( 100.0 , result . GetCoordinateN ( 1 ) . X , 1.0E-8 ) ;
152+ Assert . AreEqual ( 125.0 , result . GetCoordinateN ( 1 ) . Y , 1.0E-8 ) ;
153+
154+ Assert . AreEqual ( 111.70035626068274 , result . GetCoordinateN ( 2 ) . X , 1.0E-8 ) ;
155+ Assert . AreEqual ( 140.21046313888758 , result . GetCoordinateN ( 2 ) . Y , 1.0E-8 ) ;
156+
157+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLineSubstring ( null , 0 , 0 ) ) ) ;
158+ }
159+ }
160+
161+ [ Test ]
162+ public void TestSTLocateAlong ( )
163+ {
164+ const string ewkt = "MULTILINESTRINGM((1 2 3, 3 4 2, 9 4 3),(1 2 3, 5 4 5))" ;
165+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
166+ {
167+ db . TestGeometries
168+ . Value ( g => g . Id , 1 )
169+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
170+ . Insert ( ) ;
171+
172+ var result = db . TestGeometries
173+ . Where ( g => g . Id == 1 )
174+ . Select ( g => g . Geometry . STLocateAlong ( 3 , 0.0 ) )
175+ . Single ( ) as NTSG . MultiPoint ;
176+
177+ Assert . AreEqual ( 3 , result . NumPoints ) ;
178+
179+ Assert . AreEqual ( 1.0 , result . Coordinates [ 0 ] . X , 1.0E-8 ) ;
180+ Assert . AreEqual ( 2.0 , result . Coordinates [ 0 ] . Y , 1.0E-8 ) ;
181+ Assert . AreEqual ( 3.0 , result . Coordinates [ 0 ] . M , 1.0E-8 ) ;
182+
183+ Assert . AreEqual ( 9.0 , result . Coordinates [ 1 ] . X , 1.0E-8 ) ;
184+ Assert . AreEqual ( 4.0 , result . Coordinates [ 1 ] . Y , 1.0E-8 ) ;
185+ Assert . AreEqual ( 3.0 , result . Coordinates [ 1 ] . M , 1.0E-8 ) ;
186+
187+ Assert . AreEqual ( 1.0 , result . Coordinates [ 2 ] . X , 1.0E-8 ) ;
188+ Assert . AreEqual ( 2.0 , result . Coordinates [ 2 ] . Y , 1.0E-8 ) ;
189+ Assert . AreEqual ( 3.0 , result . Coordinates [ 2 ] . M , 1.0E-8 ) ;
190+
191+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLocateAlong ( null , 0 , 0 ) ) ) ;
192+ }
193+ }
194+
195+ [ Test ]
196+ public void TestSTLocateBetween ( )
197+ {
198+ const string ewkt = "MULTILINESTRING M ((1 2 3, 3 4 2, 9 4 3),(1 2 3, 5 4 5))" ;
199+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
200+ {
201+ db . TestGeometries
202+ . Value ( g => g . Id , 1 )
203+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
204+ . Insert ( ) ;
205+
206+ var result = db . TestGeometries
207+ . Where ( g => g . Id == 1 )
208+ . Select ( g => g . Geometry . STLocateBetween ( 1.5 , 3.0 , 0.0 ) )
209+ . Single ( ) as NTSG . GeometryCollection ;
210+
211+
212+ Assert . AreEqual ( 2 , result . NumGeometries ) ;
213+
214+ var geom1 = result . Geometries [ 0 ] ;
215+ var geom2 = result . Geometries [ 1 ] ;
216+
217+ Assert . IsInstanceOf < NTSG . LineString > ( geom1 ) ;
218+ Assert . IsInstanceOf < NTSG . Point > ( geom2 ) ;
219+
220+ var resLineString = geom1 as NTSG . LineString ;
221+ var resPoint = geom2 as NTSG . Point ;
222+
223+ Assert . AreEqual ( 1.0 , resLineString . Coordinates [ 0 ] . X , 1.0E-8 ) ;
224+ Assert . AreEqual ( 2.0 , resLineString . Coordinates [ 0 ] . Y , 1.0E-8 ) ;
225+ Assert . AreEqual ( 3.0 , resLineString . Coordinates [ 0 ] . M , 1.0E-8 ) ;
226+
227+ Assert . AreEqual ( 3.0 , resLineString . Coordinates [ 1 ] . X , 1.0E-8 ) ;
228+ Assert . AreEqual ( 4.0 , resLineString . Coordinates [ 1 ] . Y , 1.0E-8 ) ;
229+ Assert . AreEqual ( 2.0 , resLineString . Coordinates [ 1 ] . M , 1.0E-8 ) ;
230+
231+ Assert . AreEqual ( 9.0 , resLineString . Coordinates [ 2 ] . X , 1.0E-8 ) ;
232+ Assert . AreEqual ( 4.0 , resLineString . Coordinates [ 2 ] . Y , 1.0E-8 ) ;
233+ Assert . AreEqual ( 3.0 , resLineString . Coordinates [ 2 ] . M , 1.0E-8 ) ;
234+
235+ Assert . AreEqual ( 1.0 , resPoint . X , 1.0E-8 ) ;
236+ Assert . AreEqual ( 2.0 , resPoint . Y , 1.0E-8 ) ;
237+ Assert . AreEqual ( 3.0 , resPoint . M , 1.0E-8 ) ;
238+
239+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLocateBetween ( null , 0 , 0 , 0 ) ) ) ;
240+ }
241+ }
242+
243+ [ Test ]
244+ public void TestSTLocateBetweenElevations ( )
245+ {
246+ const string ewkt = "LINESTRING(1 2 3, 4 5 6)" ;
247+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
248+ {
249+ db . TestGeometries
250+ . Value ( g => g . Id , 1 )
251+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
252+ . Insert ( ) ;
253+
254+ var result = db . TestGeometries
255+ . Where ( g => g . Id == 1 )
256+ . Select ( g => g . Geometry . STLocateBetweenElevations ( 2.0 , 4.0 ) )
257+ . Single ( ) as NTSG . MultiLineString ;
258+
259+ Assert . AreEqual ( 1 , result . NumGeometries ) ;
260+
261+ Assert . AreEqual ( 1.0 , result . Coordinates [ 0 ] . X , 1.0E-8 ) ;
262+ Assert . AreEqual ( 2.0 , result . Coordinates [ 0 ] . Y , 1.0E-8 ) ;
263+ Assert . AreEqual ( 3.0 , result . Coordinates [ 0 ] . Z , 1.0E-8 ) ;
264+
265+ Assert . AreEqual ( 2.0 , result . Coordinates [ 1 ] . X , 1.0E-8 ) ;
266+ Assert . AreEqual ( 3.0 , result . Coordinates [ 1 ] . Y , 1.0E-8 ) ;
267+ Assert . AreEqual ( 4.0 , result . Coordinates [ 1 ] . Z , 1.0E-8 ) ;
268+
269+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STLocateBetweenElevations ( null , 0 , 0 ) ) ) ;
270+ }
271+ }
272+
273+ [ Test ]
274+ public void TestSTInterpolatePoint ( )
275+ {
276+ const string ewkt = "LINESTRING M (0 0 0, 10 0 20)" ;
277+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
278+ {
279+ db . TestGeometries
280+ . Value ( g => g . Id , 1 )
281+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
282+ . Insert ( ) ;
283+
284+ var result = db . TestGeometries
285+ . Where ( g => g . Id == 1 )
286+ . Select ( g => g . Geometry . STInterpolatePoint ( GeometryInput . STGeomFromEWKT ( "POINT(5 5)" ) ) )
287+ . Single ( ) as double ? ;
288+
289+ Assert . AreEqual ( 10.0 , result . Value , 1.0E-8 ) ;
290+
291+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STInterpolatePoint ( null , null ) ) ) ;
292+ }
293+ }
294+
295+ [ Test ]
296+ public void TestSTAddMeasure ( )
297+ {
298+ const string ewkt = "LINESTRING(1 0, 2 0, 4 0)" ;
299+ using ( var db = new PostGisTestDataConnection ( TestDatabaseConnectionString ) )
300+ {
301+ db . TestGeometries
302+ . Value ( g => g . Id , 1 )
303+ . Value ( g => g . Geometry , ( ) => GeometryInput . STGeomFromEWKT ( ewkt ) )
304+ . Insert ( ) ;
305+
306+ var result = db . TestGeometries
307+ . Where ( g => g . Id == 1 )
308+ . Select ( g => g . Geometry . STAddMeasure ( 1.0 , 4.0 ) )
309+ . Single ( ) as NTSG . LineString ;
310+
311+
312+ Assert . AreEqual ( 1.0 , result . Coordinates [ 0 ] . X , 1.0E-8 ) ;
313+ Assert . AreEqual ( 0.0 , result . Coordinates [ 0 ] . Y , 1.0E-8 ) ;
314+ Assert . AreEqual ( 1.0 , result . Coordinates [ 0 ] . M , 1.0E-8 ) ;
315+
316+ Assert . AreEqual ( 2.0 , result . Coordinates [ 1 ] . X , 1.0E-8 ) ;
317+ Assert . AreEqual ( 0.0 , result . Coordinates [ 1 ] . Y , 1.0E-8 ) ;
318+ Assert . AreEqual ( 2.0 , result . Coordinates [ 1 ] . M , 1.0E-8 ) ;
319+
320+ Assert . AreEqual ( 4.0 , result . Coordinates [ 2 ] . X , 1.0E-8 ) ;
321+ Assert . AreEqual ( 0.0 , result . Coordinates [ 2 ] . Y , 1.0E-8 ) ;
322+ Assert . AreEqual ( 4.0 , result . Coordinates [ 2 ] . M , 1.0E-8 ) ;
323+
324+ Assert . IsNull ( db . Select ( ( ) => LinearReferencing . STAddMeasure ( null , 0 , 0 ) ) ) ;
325+ }
326+ }
327+ }
328+ }
0 commit comments