|
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 | * |
@@ -89,61 +101,105 @@ public function getSpatialFields() |
89 | 101 | } |
90 | 102 | } |
91 | 103 |
|
| 104 | + public function isColumnAllowed($geometryColumn) |
| 105 | + { |
| 106 | + if (! in_array($geometryColumn, $this->getSpatialFields())) { |
| 107 | + throw new SpatialFieldsNotDefinedException(); |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
92 | 113 | public function scopeDistance($query, $geometryColumn, $geometry, $distance) |
93 | 114 | { |
94 | | - $query->whereRaw("st_distance(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) <= {$distance}"); |
| 115 | + $this->isColumnAllowed($geometryColumn); |
| 116 | + |
| 117 | + $query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?)) <= ?", [ |
| 118 | + $geometry->toWkt(), |
| 119 | + $distance |
| 120 | + ]); |
95 | 121 |
|
96 | 122 | return $query; |
97 | 123 | } |
98 | 124 |
|
99 | 125 | public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $distance) |
100 | 126 | { |
| 127 | + $this->isColumnAllowed($geometryColumn); |
| 128 | + |
101 | 129 | $query = $this->scopeDistance($query, $geometryColumn, $geometry, $distance); |
102 | 130 |
|
103 | | - $query->whereRaw("st_distance(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0"); |
| 131 | + $query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?)) != 0", [ |
| 132 | + $geometry->toWkt() |
| 133 | + ]); |
104 | 134 |
|
105 | 135 | return $query; |
106 | 136 | } |
107 | 137 |
|
108 | 138 | public function scopeDistanceValue($query, $geometryColumn, $geometry) |
109 | 139 | { |
| 140 | + $this->isColumnAllowed($geometryColumn); |
| 141 | + |
110 | 142 | $columns = $query->getQuery()->columns; |
111 | 143 |
|
112 | 144 | if (!$columns) { |
113 | 145 | $query->select('*'); |
114 | 146 | } |
115 | | - $query->selectRaw("st_distance(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) as distance"); |
| 147 | + |
| 148 | + $query->selectRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?)) as distance", [ |
| 149 | + $geometry->toWkt() |
| 150 | + ]); |
116 | 151 | } |
117 | 152 |
|
118 | 153 | public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distance) |
119 | 154 | { |
120 | | - $query->whereRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) <= {$distance}"); |
| 155 | + $this->isColumnAllowed($geometryColumn); |
| 156 | + |
| 157 | + $query->whereRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?)) <= ?", [ |
| 158 | + $geometry->toWkt(), |
| 159 | + $distance |
| 160 | + ]); |
121 | 161 |
|
122 | 162 | return $query; |
123 | 163 | } |
124 | 164 |
|
125 | 165 | public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geometry, $distance) |
126 | 166 | { |
| 167 | + $this->isColumnAllowed($geometryColumn); |
| 168 | + |
127 | 169 | $query = $this->scopeDistanceSphere($query, $geometryColumn, $geometry, $distance); |
128 | 170 |
|
129 | | - $query->whereRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0"); |
| 171 | + $query->whereRaw("st_distance_sphere($geometryColumn, ST_GeomFromText(?)) != 0", [ |
| 172 | + $geometry->toWkt() |
| 173 | + ]); |
130 | 174 |
|
131 | 175 | return $query; |
132 | 176 | } |
133 | 177 |
|
134 | 178 | public function scopeDistanceSphereValue($query, $geometryColumn, $geometry) |
135 | 179 | { |
| 180 | + $this->isColumnAllowed($geometryColumn); |
| 181 | + |
136 | 182 | $columns = $query->getQuery()->columns; |
137 | 183 |
|
138 | 184 | if (!$columns) { |
139 | 185 | $query->select('*'); |
140 | 186 | } |
141 | | - $query->selectRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) as distance"); |
| 187 | + $query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?)) as distance", [ |
| 188 | + $geometry->toWkt() |
| 189 | + ]); |
142 | 190 | } |
143 | 191 |
|
144 | 192 | public function scopeComparison($query, $geometryColumn, $geometry, $relationship) |
145 | 193 | { |
146 | | - $query->whereRaw("st_{$relationship}(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}'))"); |
| 194 | + $this->isColumnAllowed($geometryColumn); |
| 195 | + |
| 196 | + if (! in_array($relationship, $this->stRelations)) { |
| 197 | + throw new UnknownSpatialRelationFunction($relationship); |
| 198 | + } |
| 199 | + |
| 200 | + $query->whereRaw("st_{$relationship}(`$geometryColumn`, ST_GeomFromText(?))", [ |
| 201 | + $geometry->toWkt() |
| 202 | + ]); |
147 | 203 |
|
148 | 204 | return $query; |
149 | 205 | } |
|
0 commit comments