33namespace Grimzy \LaravelMysqlSpatial \Eloquent ;
44
55use Grimzy \LaravelMysqlSpatial \Exceptions \SpatialFieldsNotDefinedException ;
6+ use Grimzy \LaravelMysqlSpatial \Exceptions \UnknownSpatialRelationFunction ;
67use Grimzy \LaravelMysqlSpatial \Types \Geometry ;
78use Grimzy \LaravelMysqlSpatial \Types \GeometryInterface ;
89use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
@@ -37,6 +38,17 @@ trait SpatialTrait
3738
3839 public $ geometries = [];
3940
41+ protected $ stRelations = [
42+ 'within ' ,
43+ 'crosses ' ,
44+ 'contains ' ,
45+ 'disjoint ' ,
46+ 'equals ' ,
47+ 'intersects ' ,
48+ 'overlaps ' ,
49+ 'touches ' ,
50+ ];
51+
4052 /**
4153 * Create a new Eloquent query builder for the model.
4254 *
@@ -49,12 +61,21 @@ public function newEloquentBuilder($query)
4961 return new Builder ($ query );
5062 }
5163
64+ protected function newBaseQueryBuilder ()
65+ {
66+ $ connection = $ this ->getConnection ();
67+
68+ return new BaseBuilder (
69+ $ connection , $ connection ->getQueryGrammar (), $ connection ->getPostProcessor ()
70+ );
71+ }
72+
5273 protected function performInsert (EloquentBuilder $ query , array $ options = [])
5374 {
5475 foreach ($ this ->attributes as $ key => $ value ) {
5576 if ($ value instanceof GeometryInterface) {
5677 $ this ->geometries [$ key ] = $ value ; //Preserve the geometry objects prior to the insert
57- $ this ->attributes [$ key ] = $ this -> getConnection ()-> raw ( sprintf ( " GeomFromText('%s') " , $ value-> toWKT ()) );
78+ $ this ->attributes [$ key ] = new SpatialExpression ( $ value );
5879 }
5980 }
6081
@@ -89,30 +110,59 @@ public function getSpatialFields()
89110 }
90111 }
91112
113+ public function isColumnAllowed ($ geometryColumn )
114+ {
115+ if (!in_array ($ geometryColumn , $ this ->getSpatialFields ())) {
116+ throw new SpatialFieldsNotDefinedException ();
117+ }
118+
119+ return true ;
120+ }
121+
92122 public function scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance , $ exclude_self = false )
93123 {
94- $ query ->whereRaw ("st_distance(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) <= {$ distance }" );
124+ $ this ->isColumnAllowed ($ geometryColumn );
125+
126+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) <= ? " , [
127+ $ geometry ->toWkt (),
128+ $ distance ,
129+ ]);
95130
96131 if ($ exclude_self ) {
97- $ query ->whereRaw ("st_distance(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) != 0 " );
132+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) != 0 " , [
133+ $ geometry ->toWkt (),
134+ ]);
98135 }
99136
100137 return $ query ;
101138 }
102139
103140 public function scopeDistanceValue ($ query , $ geometryColumn , $ geometry )
104141 {
142+ $ this ->isColumnAllowed ($ geometryColumn );
143+
105144 $ columns = $ query ->getQuery ()->columns ;
106145
107146 if (!$ columns ) {
108147 $ query ->select ('* ' );
109148 }
110- $ query ->selectRaw ("st_distance(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) as distance " );
149+
150+ $ query ->selectRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) as distance " , [
151+ $ geometry ->toWkt (),
152+ ]);
111153 }
112154
113155 public function scopeComparison ($ query , $ geometryColumn , $ geometry , $ relationship )
114156 {
115- $ query ->whereRaw ("st_ {$ relationship }(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) " );
157+ $ this ->isColumnAllowed ($ geometryColumn );
158+
159+ if (!in_array ($ relationship , $ this ->stRelations )) {
160+ throw new UnknownSpatialRelationFunction ($ relationship );
161+ }
162+
163+ $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?)) " , [
164+ $ geometry ->toWkt (),
165+ ]);
116166
117167 return $ query ;
118168 }
0 commit comments