Skip to content

Commit 406d5d3

Browse files
committed
Updated README 📖
1 parent e90ed9e commit 406d5d3

File tree

1 file changed

+108
-21
lines changed

1 file changed

+108
-21
lines changed

README.md

Lines changed: 108 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Please check the documentation for your MySQL version. MySQL's Extension for Spa
1616
- `1.x.x`: MySQL 5.6 (also supports MySQL 5.5 but not all spatial analysis functions)
1717
- `2.x.x`: MySQL 5.7 and 8.0
1818

19+
[TOC]
20+
1921
## Installation
2022

2123
Add the package using composer:
@@ -41,6 +43,8 @@ For Laravel versions before 5.5 or if not using auto-discovery, register the ser
4143
],
4244
```
4345

46+
47+
4448
## Quickstart
4549

4650
### Create a migration
@@ -144,34 +148,43 @@ $lat = $place2->location->getLat(); // 40.7484404
144148
$lng = $place2->location->getLng(); // -73.9878441
145149
```
146150

147-
## Migration
151+
152+
153+
## Migrations
154+
155+
### Columns
148156

149157
Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints:
150158

151-
- geometry
152-
- point
153-
- lineString
154-
- polygon
155-
- multiPoint
156-
- multiLineString
157-
- multiPolygon
158-
- geometryCollection
159+
-
160+
`$table->geometry('column_name');`
159161

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
161171

162172
You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints.
163173

174+
- `$table->spatialIndex('column_name');`
175+
- `$table->dropSpatialIndex(['column_name']);` or `$table->dropSpatialIndex('index_name')`
176+
164177
Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):
165178

166179
> 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`.
167180
168-
From the command line:
181+
Following the example in the [Quickstart](#user-content-create-a-migration); from the command line:
169182

170183
```shell
171184
php artisan make:migration update_places_table
172185
```
173186

174-
Then edit the migration you just created:
187+
Then edit the migration file that you just created:
175188

176189
```php
177190
use Illuminate\Database\Migrations\Migration;
@@ -218,17 +231,89 @@ class UpdatePlacesTable extends Migration
218231
}
219232
}
220233
```
221-
## Models
222234

223-
Available geometry classes:
224235

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 |
242+
| ---------------------------------------- | ---------------------------------------- |
243+
| `Point($lat, $lng)` | [Point](https://dev.mysql.com/doc/refman/5.7/en/gis-class-point.html) |
244+
| `MultiPoint(Point[])` | [MultiPoint](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multipoint.html) |
245+
| `LineString(Point[])` | [LineString](https://dev.mysql.com/doc/refman/5.7/en/gis-class-linestring.html) |
246+
| `MultiLineString(LineString[])` | [MultiLineString](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multilinestring.html) |
247+
| `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) |
248+
| `MultiPolygon(Polygon[])` | [MultiPolygon](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multipolygon.html) |
249+
| `GeometryCollection(Geometry[])` | [GeometryCollection](https://dev.mysql.com/doc/refman/5.7/en/gis-class-geometrycollection.html) |
250+
251+
### Using Geometry classes
252+
253+
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))
272+
273+
```php
274+
$polygon = Polygon::fromWKT('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))');
275+
276+
$polygon->toWKT(); // POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
277+
```
278+
279+
##### From/To String
280+
281+
```php
282+
// fromString($wkt)
283+
$polygon = Polygon::fromString('(0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)');
284+
285+
(string)$polygon; // (0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)
286+
```
287+
288+
##### From/To JSON ([GeoJSON](http://geojson.org/))
289+
290+
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+
#####
232317

233318
## Scopes: Spatial analysis functions
234319

@@ -252,6 +337,8 @@ Available scopes:
252337

253338
*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)).*
254339

340+
341+
255342
## Credits
256343

257344
Originally inspired from [njbarrett's Laravel postgis package](https://github.com/njbarrett/laravel-postgis).

0 commit comments

Comments
 (0)