3
3
namespace Grimzy \LaravelMysqlSpatial \Eloquent ;
4
4
5
5
use Grimzy \LaravelMysqlSpatial \Exceptions \SpatialFieldsNotDefinedException ;
6
+ use Grimzy \LaravelMysqlSpatial \Exceptions \UnknownSpatialRelationFunction ;
6
7
use Grimzy \LaravelMysqlSpatial \Types \Geometry ;
7
8
use Grimzy \LaravelMysqlSpatial \Types \GeometryInterface ;
8
9
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
@@ -37,6 +38,17 @@ trait SpatialTrait
37
38
38
39
public $ geometries = [];
39
40
41
+ protected $ stRelations = [
42
+ 'within ' ,
43
+ 'crosses ' ,
44
+ 'contains ' ,
45
+ 'disjoint ' ,
46
+ 'equals ' ,
47
+ 'intersects ' ,
48
+ 'overlaps ' ,
49
+ 'touches ' ,
50
+ ];
51
+
40
52
/**
41
53
* Create a new Eloquent query builder for the model.
42
54
*
@@ -49,12 +61,21 @@ public function newEloquentBuilder($query)
49
61
return new Builder ($ query );
50
62
}
51
63
64
+ protected function newBaseQueryBuilder ()
65
+ {
66
+ $ connection = $ this ->getConnection ();
67
+
68
+ return new BaseBuilder (
69
+ $ connection , $ connection ->getQueryGrammar (), $ connection ->getPostProcessor ()
70
+ );
71
+ }
72
+
52
73
protected function performInsert (EloquentBuilder $ query , array $ options = [])
53
74
{
54
75
foreach ($ this ->attributes as $ key => $ value ) {
55
76
if ($ value instanceof GeometryInterface) {
56
77
$ 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 );
58
79
}
59
80
}
60
81
@@ -89,30 +110,59 @@ public function getSpatialFields()
89
110
}
90
111
}
91
112
113
+ public function isColumnAllowed ($ geometryColumn )
114
+ {
115
+ if (!in_array ($ geometryColumn , $ this ->getSpatialFields ())) {
116
+ throw new SpatialFieldsNotDefinedException ();
117
+ }
118
+
119
+ return true ;
120
+ }
121
+
92
122
public function scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance , $ exclude_self = false )
93
123
{
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
+ ]);
95
130
96
131
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
+ ]);
98
135
}
99
136
100
137
return $ query ;
101
138
}
102
139
103
140
public function scopeDistanceValue ($ query , $ geometryColumn , $ geometry )
104
141
{
142
+ $ this ->isColumnAllowed ($ geometryColumn );
143
+
105
144
$ columns = $ query ->getQuery ()->columns ;
106
145
107
146
if (!$ columns ) {
108
147
$ query ->select ('* ' );
109
148
}
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
+ ]);
111
153
}
112
154
113
155
public function scopeComparison ($ query , $ geometryColumn , $ geometry , $ relationship )
114
156
{
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
+ ]);
116
166
117
167
return $ query ;
118
168
}
0 commit comments