You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints:
150
158
151
-
- geometry
152
-
- point
153
-
- lineString
154
-
- polygon
155
-
- multiPoint
156
-
- multiLineString
157
-
- multiPolygon
158
-
- geometryCollection
159
+
-
160
+
`$table->geometry('column_name');`
159
161
160
-
### Spatial index
162
+
-`$table->point('column_name');`
163
+
-`$table->lineString('column_name');`
164
+
-`$table->polygon('column_name');`
165
+
-`$table->multiPoint('column_name');`
166
+
-`$table->multiLineString('column_name');`
167
+
-`$table->multiPolygon('column_name');`
168
+
-`$table->geometryCollection('column_name');`
169
+
170
+
### Spatial indexes
161
171
162
172
You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints.
163
173
174
+
-`$table->spatialIndex('column_name');`
175
+
-`$table->dropSpatialIndex(['column_name']);` or `$table->dropSpatialIndex('index_name')`
176
+
164
177
Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):
165
178
166
179
> For [`MyISAM`](https://dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html) and (as of MySQL 5.7.5) `InnoDB` tables, MySQL can create spatial indexes using syntax similar to that for creating regular indexes, but using the `SPATIAL` keyword. Columns in spatial indexes must be declared `NOT NULL`.
167
180
168
-
From the command line:
181
+
Following the example in the [Quickstart](#user-content-create-a-migration); from the command line:
169
182
170
183
```shell
171
184
php artisan make:migration update_places_table
172
185
```
173
186
174
-
Then edit the migration you just created:
187
+
Then edit the migration file that you just created:
175
188
176
189
```php
177
190
use Illuminate\Database\Migrations\Migration;
@@ -218,17 +231,89 @@ class UpdatePlacesTable extends Migration
218
231
}
219
232
}
220
233
```
221
-
## Models
222
234
223
-
Available geometry classes:
224
235
225
-
-`Point($lat, $lng)`
226
-
-`MultiPoint(Point[])`
227
-
-`LineString(Point[])`
228
-
-`MultiLineString(LineString[])`
229
-
-`Polygon(LineString[])`
230
-
-`MultiPolygon(Polygon[])`
231
-
-`GeometryCollection(Geometry[])`*(a collection of spatial models)*
236
+
237
+
## Geometry classes
238
+
239
+
### Available Geometry classes
240
+
241
+
| Grimzy\LaravelMysqlSpatial\Types | OpenGIS Class |
|`Polygon(LineString[])`*([exterior and interior boundaries](https://dev.mysql.com/doc/refman/5.7/en/gis-class-polygon.html))*|[Polygon](https://dev.mysql.com/doc/refman/5.7/en/gis-class-polygon.html)|
In order for your Eloquent Model to handle the Geometry classes, it must use the `Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait` trait and define a `protected` property `$spatialFields` as an array of MySQL Spatial Data Type column names (example in [Quickstart](#user-content-create-a-model)).
254
+
255
+
#### IteratorAggregate and ArrayAccess
256
+
257
+
The "composite" Geometries (`LineString`, `Polygon`, `MultiPoint`, `MultiLineString`, and `GeometryCollection`) implement [`IteratorAggregate`](http://php.net/manual/en/class.iteratoraggregate.php) and [`ArrayAccess`](http://php.net/manual/en/class.arrayaccess.php); making it easy to perform Iterator and Array operations. For example:
258
+
259
+
```php
260
+
$polygon = $multipolygon[10]; // ArrayAccess
261
+
262
+
// IteratorAggregate
263
+
for($polygon as $i => $linestring) {
264
+
echo (string) $linestring;
265
+
}
266
+
267
+
```
268
+
269
+
#### Helpers
270
+
271
+
##### From/To Well Kown Text ([WKT](https://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html#gis-wkt-format))
The Geometry classes implement [`JsonSerializable`](http://php.net/manual/en/class.jsonserializable.php) and `Illuminate\Contracts\Support\Jsonable` and with the help of [jmikola/geojson](https://github.com/jmikola/geojson), we can easily serialize/deserialize GeoJSON:
291
+
292
+
```php
293
+
$point = new Point(10, 20);
294
+
295
+
json_encode($point);
296
+
// or
297
+
$point->toJson();
298
+
```
299
+
300
+
Returns:
301
+
302
+
```javascript
303
+
{
304
+
"type":"Feature",
305
+
"properties": {},
306
+
"geometry": {
307
+
"type":"Point",
308
+
"coordinates": [
309
+
-73.9878441,
310
+
40.7484404
311
+
]
312
+
}
313
+
}
314
+
```
315
+
316
+
#####
232
317
233
318
## Scopes: Spatial analysis functions
234
319
@@ -252,6 +337,8 @@ Available scopes:
252
337
253
338
*Note that behavior and availability of MySQL spatial analysis functions differs in each MySQL version (cf. [documentation](https://dev.mysql.com/doc/refman/5.7/en/spatial-function-reference.html)).*
254
339
340
+
341
+
255
342
## Credits
256
343
257
344
Originally inspired from [njbarrett's Laravel postgis package](https://github.com/njbarrett/laravel-postgis).
0 commit comments