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
Copy file name to clipboardExpand all lines: README.md
+129-23
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,8 @@ Please check the documentation for your MySQL version. MySQL's Extension for Spa
15
15
-`1.x.x`: MySQL 5.6 (also supports MySQL 5.5 but not all spatial analysis functions)
16
16
-`2.x.x`: MySQL 5.7 and 8.0
17
17
18
+
This package also works with MariaDB. Please refer to the [MySQL/MariaDB Spatial Support Matrix](https://mariadb.com/kb/en/library/mysqlmariadb-spatial-support-matrix/) for compatibility.
19
+
18
20
## Installation
19
21
20
22
Add the package using composer:
@@ -71,6 +73,8 @@ class CreatePlacesTable extends Migration {
71
73
$table->string('name')->unique();
72
74
// Add a Point spatial data field named location
73
75
$table->point('location')->nullable();
76
+
// Add a Polygon spatial data field named area
77
+
$table->polygon('area')->nullable();
74
78
$table->timestamps();
75
79
});
76
80
}
@@ -117,22 +121,41 @@ class Place extends Model
117
121
use SpatialTrait;
118
122
119
123
protected $fillable = [
120
-
'name',
124
+
'name'
121
125
];
122
126
123
127
protected $spatialFields = [
124
128
'location',
129
+
'area'
125
130
];
126
131
}
127
132
```
128
133
129
134
### Saving a model
130
135
131
136
```php
137
+
use Grimzy\LaravelMysqlSpatial\Types\Point;
138
+
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
139
+
132
140
$place1 = new Place();
133
141
$place1->name = 'Empire State Building';
134
-
$place1->location = new Point(40.7484404, -73.9878441);
142
+
143
+
// saving a point
144
+
$place1->location = new Point(40.7484404, -73.9878441); // (lat, lng)
Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints:
149
174
150
-
- geometry
151
-
- point
152
-
- lineString
153
-
- polygon
154
-
- multiPoint
155
-
- multiLineString
156
-
- multiPolygon
157
-
- geometryCollection
175
+
-
176
+
`$table->geometry('column_name');`
177
+
178
+
-`$table->point('column_name');`
179
+
-`$table->lineString('column_name');`
180
+
-`$table->polygon('column_name');`
181
+
-`$table->multiPoint('column_name');`
182
+
-`$table->multiLineString('column_name');`
183
+
-`$table->multiPolygon('column_name');`
184
+
-`$table->geometryCollection('column_name');`
158
185
159
-
### Spatial index
186
+
### Spatial indexes
160
187
161
188
You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints.
162
189
190
+
-`$table->spatialIndex('column_name');`
191
+
-`$table->dropSpatialIndex(['column_name']);` or `$table->dropSpatialIndex('index_name')`
192
+
163
193
Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):
164
194
165
195
> 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`.
166
196
167
-
From the command line:
197
+
Also please read this [**important note**](https://laravel.com/docs/5.5/migrations#indexes) regarding Index Lengths in the Laravel 5.6 documentation.
198
+
199
+
For example, as a follow up to the [Quickstart](#user-content-create-a-migration); from the command line, generate a new migration:
168
200
169
201
```shell
170
202
php artisan make:migration update_places_table
171
203
```
172
204
173
-
Then edit the migration you just created:
205
+
Then edit the migration file that you just created:
174
206
175
207
```php
176
208
use Illuminate\Database\Migrations\Migration;
@@ -217,17 +249,91 @@ class UpdatePlacesTable extends Migration
217
249
}
218
250
}
219
251
```
220
-
## Models
221
252
222
-
Available geometry classes:
253
+
## Geometry classes
254
+
255
+
### Available Geometry classes
256
+
257
+
| 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)|
Check out the [Class diagram](https://user-images.githubusercontent.com/1837678/30788608-a5afd894-a16c-11e7-9a51-0a08b331d4c4.png).
268
+
269
+
### Using Geometry classes
270
+
271
+
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)).
272
+
273
+
#### IteratorAggregate and ArrayAccess
274
+
275
+
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:
276
+
277
+
```php
278
+
$polygon = $multipolygon[10]; // ArrayAccess
279
+
280
+
// IteratorAggregate
281
+
for($polygon as $i => $linestring) {
282
+
echo (string) $linestring;
283
+
}
284
+
285
+
```
286
+
287
+
#### Helpers
288
+
289
+
##### From/To Well Known 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` to help serialize into GeoJSON:
223
310
224
-
-`Point($lat, $lng)`
225
-
-`MultiPoint(Point[])`
226
-
-`LineString(Point[])`
227
-
-`MultiLineString(LineString[])`
228
-
-`Polygon(LineString[])`
229
-
-`MultiPolygon(Polygon[])`
230
-
-`GeometryCollection(Geometry[])`*(a collection of spatial models)*
311
+
```php
312
+
$point = new Point(10, 20);
313
+
314
+
json_encode($point); // or $point->toJson();
315
+
316
+
// {
317
+
// "type": "Feature",
318
+
// "properties": {},
319
+
// "geometry": {
320
+
// "type": "Point",
321
+
// "coordinates": [
322
+
// -73.9878441,
323
+
// 40.7484404
324
+
// ]
325
+
// }
326
+
// }
327
+
```
328
+
329
+
To deserialize a GeoJSON string into a Geometry class, you can use `Geometry::fromJson($json_string)` :
0 commit comments