11<?php
22use Grimzy \LaravelSpatial \SpatialServiceProvider ;
3+ use Grimzy \LaravelSpatial \Types \GeometryCollection ;
4+ use Grimzy \LaravelSpatial \Types \LineString ;
5+ use Grimzy \LaravelSpatial \Types \MultiPoint ;
6+ use Grimzy \LaravelSpatial \Types \MultiPolygon ;
37use Grimzy \LaravelSpatial \Types \Point ;
8+ use Grimzy \LaravelSpatial \Types \Polygon ;
49use Illuminate \Filesystem \Filesystem ;
510use Illuminate \Foundation \Testing \TestCase ;
611
@@ -19,7 +24,7 @@ public function createApplication()
1924 $ app ->make ('Illuminate\Contracts\Console\Kernel ' )->bootstrap ();
2025
2126 $ app ['config ' ]->set ('database.default ' , 'mysql ' );
22- $ app ['config ' ]->set ('database.connections.mysql.host ' , env ('DB_HOST ' , '127.0 .0.1 ' ));
27+ $ app ['config ' ]->set ('database.connections.mysql.host ' , env ('DB_HOST ' , '172.17 .0.2 ' )); // TODO: do not commit ip change
2328 $ app ['config ' ]->set ('database.connections.mysql.database ' , 'test ' );
2429 $ app ['config ' ]->set ('database.connections.mysql.username ' , 'root ' );
2530 $ app ['config ' ]->set ('database.connections.mysql.password ' , '' );
@@ -75,12 +80,79 @@ private function onMigrations(\Closure $closure, $reverse_sort = false)
7580 }
7681 }
7782
78- // TODO: Test with a model missing $spatialFields to expect SpatialFieldNotDefinedException
83+ public function testSpatialFieldsNotDefinedException () {
84+ $ geo = new NoSpatialFieldsModel ();
85+ $ geo ->geometry = new Point (1 , 2 );
86+ $ geo ->save ();
87+
88+ $ this ->setExpectedException (\Grimzy \LaravelSpatial \Exceptions \SpatialFieldsNotDefinedException::class);
89+ NoSpatialFieldsModel::all ();
90+
91+ }
92+
93+ public function testInsertPoint ()
94+ {
95+ $ geo = new GeometryModel ();
96+ $ geo ->location = new Point (1 , 2 );
97+ $ geo ->save ();
98+ $ this ->assertDatabaseHas ('geometry ' , ['id ' => $ geo ->id ]);
99+ }
100+
101+ public function testInsertLineString ()
102+ {
103+ $ geo = new GeometryModel ();
79104
80- public function testInsert ()
105+ $ geo ->location = new Point (1 , 2 );
106+ $ geo ->line = new LineString ([new Point (1 , 1 ), new Point (2 , 2 )]);
107+ $ geo ->save ();
108+ $ this ->assertDatabaseHas ('geometry ' , ['id ' => $ geo ->id ]);
109+ }
110+
111+ public function testInsertPolygon ()
81112 {
82113 $ geo = new GeometryModel ();
114+
83115 $ geo ->location = new Point (1 , 2 );
116+ $ geo ->shape = Polygon::fromWKT ("POLYGON((0 10,10 10,10 0,0 0,0 10)) " );
117+ $ geo ->save ();
118+ $ this ->assertDatabaseHas ('geometry ' , ['id ' => $ geo ->id ]);
119+ }
120+
121+ public function testInsertMultiPoint ()
122+ {
123+ $ geo = new GeometryModel ();
124+
125+ $ geo ->location = new Point (1 , 2 );
126+ $ geo ->multi_locations = new MultiPoint ([new Point (1 , 1 ), new Point (2 , 2 )]);
127+ $ geo ->save ();
128+ $ this ->assertDatabaseHas ('geometry ' , ['id ' => $ geo ->id ]);
129+ }
130+
131+ public function testInsertMultiPolygon ()
132+ {
133+ $ geo = new GeometryModel ();
134+
135+ $ geo ->location = new Point (1 , 2 );
136+
137+ $ geo ->multi_shapes = new MultiPolygon ([
138+ Polygon::fromWKT ("POLYGON((0 10,10 10,10 0,0 0,0 10)) " ),
139+ Polygon::fromWKT ("POLYGON((0 0,0 5,5 5,5 0,0 0)) " )
140+ ]);
141+ $ geo ->save ();
142+ $ this ->assertDatabaseHas ('geometry ' , ['id ' => $ geo ->id ]);
143+ }
144+
145+ public function testInsertGeometryCollection ()
146+ {
147+ $ geo = new GeometryModel ();
148+
149+ $ geo ->location = new Point (1 , 2 );
150+
151+ $ geo ->multi_geometries = new GeometryCollection ([
152+ Polygon::fromWKT ("POLYGON((0 10,10 10,10 0,0 0,0 10)) " ),
153+ Polygon::fromWKT ("POLYGON((0 0,0 5,5 5,5 0,0 0)) " ),
154+ new Point (0 , 0 )
155+ ]);
84156 $ geo ->save ();
85157 $ this ->assertDatabaseHas ('geometry ' , ['id ' => $ geo ->id ]);
86158 }
@@ -107,35 +179,133 @@ public function testUpdate()
107179 }
108180
109181 public function testDistance ()
182+ {
183+ $ loc1 = new GeometryModel ();
184+ $ loc1 ->location = new Point (1 , 1 );
185+ $ loc1 ->save ();
186+
187+ $ loc2 = new GeometryModel ();
188+ $ loc2 ->location = new Point (2 , 2 ); // Distance from loc1: 1.4142135623731
189+ $ loc2 ->save ();
190+
191+ $ loc3 = new GeometryModel ();
192+ $ loc3 ->location = new Point (3 , 3 ); // Distance from loc1: 2.8284271247462
193+ $ loc3 ->save ();
194+
195+ $ a = GeometryModel::distance (2 , $ loc1 ->location , 'location ' )->get ();
196+ $ this ->assertCount (2 , $ a );
197+ $ this ->assertTrue ($ a ->contains ($ loc1 ));
198+ $ this ->assertTrue ($ a ->contains ($ loc2 ));
199+ $ this ->assertFalse ($ a ->contains ($ loc3 ));
200+
201+ // Excluding self
202+ $ b = GeometryModel::distance (2 , $ loc1 ->location , 'location ' , true )->get ();
203+ $ this ->assertCount (1 , $ b );
204+ $ this ->assertFalse ($ b ->contains ($ loc1 ));
205+ $ this ->assertTrue ($ b ->contains ($ loc2 ));
206+ $ this ->assertFalse ($ b ->contains ($ loc3 ));
207+
208+ $ c = GeometryModel::distance (1 , $ loc1 ->location , 'location ' )->get ();
209+ $ this ->assertCount (1 , $ c );
210+ $ this ->assertTrue ($ c ->contains ($ loc1 ));
211+ $ this ->assertFalse ($ c ->contains ($ loc2 ));
212+ $ this ->assertFalse ($ c ->contains ($ loc3 ));
213+ }
214+
215+ public function testDistanceSphere ()
110216 {
111217 $ loc1 = new GeometryModel ();
112218 $ loc1 ->location = new Point (40.767864 , -73.971732 );
113219 $ loc1 ->save ();
114220
115221 $ loc2 = new GeometryModel ();
116- $ loc2 ->location = new Point (40.767664 , -73.971271 ); // Distance from loc1: 44.7414064845878
222+ $ loc2 ->location = new Point (40.767664 , -73.971271 ); // Distance from loc1: 44.741406484588
117223 $ loc2 ->save ();
118224
119225 $ loc3 = new GeometryModel ();
120- $ loc3 ->location = new Point (40.761434 , -73.977619 );
226+ $ loc3 ->location = new Point (40.761434 , -73.977619 ); // Distance from loc1: 870.06424066202
121227 $ loc3 ->save ();
122228
123- $ a = GeometryModel::distance ( 45 , $ loc1 ->location , 'location ' )->get ();
229+ $ a = GeometryModel::distanceSphere ( 200 , $ loc1 ->location , 'location ' )->get ();
124230 $ this ->assertCount (2 , $ a );
125231 $ this ->assertTrue ($ a ->contains ($ loc1 ));
126232 $ this ->assertTrue ($ a ->contains ($ loc2 ));
127233 $ this ->assertFalse ($ a ->contains ($ loc3 ));
128234
129- $ b = GeometryModel::distance (45 , $ loc1 ->location , 'location ' , true )->get ();
235+ // Excluding self
236+ $ b = GeometryModel::distanceSphere (200 , $ loc1 ->location , 'location ' , true )->get ();
130237 $ this ->assertCount (1 , $ b );
131238 $ this ->assertFalse ($ b ->contains ($ loc1 ));
132239 $ this ->assertTrue ($ b ->contains ($ loc2 ));
133240 $ this ->assertFalse ($ b ->contains ($ loc3 ));
134241
135- $ c = GeometryModel::distance (44.741406484587 , $ loc1 ->location , 'location ' )->get ();
242+ $ c = GeometryModel::distanceSphere (44.741406484587 , $ loc1 ->location , 'location ' )->get ();
136243 $ this ->assertCount (1 , $ c );
137244 $ this ->assertTrue ($ c ->contains ($ loc1 ));
138245 $ this ->assertFalse ($ c ->contains ($ loc2 ));
139246 $ this ->assertFalse ($ c ->contains ($ loc3 ));
140247 }
248+
249+ public function testDistanceValue ()
250+ {
251+ $ loc1 = new GeometryModel ();
252+ $ loc1 ->location = new Point (1 , 1 );
253+ $ loc1 ->save ();
254+
255+ $ loc2 = new GeometryModel ();
256+ $ loc2 ->location = new Point (2 , 2 ); // Distance from loc1: 1.4142135623731
257+ $ loc2 ->save ();
258+
259+ $ a = GeometryModel::distanceValue ($ loc1 ->location , 'location ' )->get ();
260+ $ this ->assertCount (2 , $ a );
261+ $ this ->assertEquals (0 , $ a [0 ]->distance );
262+ $ this ->assertEquals (1.4142135623 , $ a [1 ]->distance ); // PHP floats' 11th+ digits don't matter
263+ }
264+
265+ public function testDistanceSphereValue () {
266+ $ loc1 = new GeometryModel ();
267+ $ loc1 ->location = new Point (40.767864 , -73.971732 );
268+ $ loc1 ->save ();
269+
270+ $ loc2 = new GeometryModel ();
271+ $ loc2 ->location = new Point (40.767664 , -73.971271 ); // Distance from loc1: 44.741406484588
272+ $ loc2 ->save ();
273+
274+ $ a = GeometryModel::distanceSphereValue ($ loc1 ->location , 'location ' )->get ();
275+ $ this ->assertCount (2 , $ a );
276+ $ this ->assertEquals (0 , $ a [0 ]->distance );
277+ $ this ->assertEquals (44.7414064845 , $ a [1 ]->distance ); // PHP floats' 11th+ digits don't matter
278+ }
279+
280+ public function testBounding () {
281+ $ point = new Point (0 , 0 );
282+
283+ $ linestring1 = \Grimzy \LaravelSpatial \Types \LineString::fromWkt ("LINESTRING(1 1, 2 2) " );
284+ $ linestring2 = \Grimzy \LaravelSpatial \Types \LineString::fromWkt ("LINESTRING(20 20, 24 24) " );
285+ $ linestring3 = \Grimzy \LaravelSpatial \Types \LineString::fromWkt ("LINESTRING(0 10, 10 10) " );
286+
287+ $ geo1 = new GeometryModel ();
288+ $ geo1 ->location = $ point ;
289+ $ geo1 ->line = $ linestring1 ;
290+ $ geo1 ->save ();
291+
292+ $ geo2 = new GeometryModel ();
293+ $ geo2 ->location = $ point ;
294+ $ geo2 ->line = $ linestring2 ;
295+ $ geo2 ->save ();
296+
297+ $ geo3 = new GeometryModel ();
298+ $ geo3 ->location = $ point ;
299+ $ geo3 ->line = $ linestring3 ;
300+ $ geo3 ->save ();
301+
302+ $ polygon = Polygon::fromWKT ("POLYGON((0 10,10 10,10 0,0 0,0 10)) " );
303+
304+ $ result = GeometryModel::Bounding ($ polygon , 'line ' )->get ();
305+ $ this ->assertCount (2 , $ result );
306+ $ this ->assertTrue ($ result ->contains ($ geo1 ));
307+ $ this ->assertFalse ($ result ->contains ($ geo2 ));
308+ $ this ->assertTrue ($ result ->contains ($ geo3 ));
309+
310+ }
141311}
0 commit comments