Skip to content

Commit 60ee8ad

Browse files
committed
Updated documentation 📖
1 parent 43b342f commit 60ee8ad

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

README.md

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

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+
1820
[TOC]
1921

2022
## Installation
@@ -42,8 +44,6 @@ For Laravel versions before 5.5 or if not using auto-discovery, register the ser
4244
],
4345
```
4446

45-
46-
4747
## Quickstart
4848

4949
### Create a migration
@@ -75,6 +75,8 @@ class CreatePlacesTable extends Migration {
7575
$table->string('name')->unique();
7676
// Add a Point spatial data field named location
7777
$table->point('location')->nullable();
78+
// Add a Polygon spatial data field named area
79+
$table->polygon('area')->nullable();
7880
$table->timestamps();
7981
});
8082
}
@@ -121,22 +123,40 @@ class Place extends Model
121123
use SpatialTrait;
122124

123125
protected $fillable = [
124-
'name',
126+
'name'
125127
];
126128

127129
protected $spatialFields = [
128130
'location',
131+
'area'
129132
];
130133
}
131134
```
132135

133136
### Saving a model
134137

135138
```php
139+
use Grimzy\LaravelMysqlSpatial\Types\Point;
140+
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
141+
136142
$place1 = new Place();
137143
$place1->name = 'Empire State Building';
138-
$place1->location = new Point(40.7484404, -73.9878441);
144+
145+
// saving a point
146+
$place1->location = new Point(40.7484404, -73.9878441); // (lat, lng)
139147
$place1->save();
148+
149+
// saving a polygon
150+
$place1->area = new Polygon([new LineString([
151+
new Point(40.74894149554006, -73.98615270853043),
152+
new Point(40.74848633046773, -73.98648262023926),
153+
new Point(40.747925497790725, -73.9851602911949),
154+
new Point(40.74837050671544, -73.98482501506805),
155+
new Point(40.74894149554006, -73.98615270853043)
156+
])]);
157+
158+
$place1->area = new Polygon();
159+
140160
```
141161

142162
### Retrieving a model
@@ -147,8 +167,6 @@ $lat = $place2->location->getLat(); // 40.7484404
147167
$lng = $place2->location->getLng(); // -73.9878441
148168
```
149169

150-
151-
152170
## Migrations
153171

154172
### Columns
@@ -177,7 +195,7 @@ Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/
177195

178196
> 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`.
179197
180-
Following the example in the [Quickstart](#user-content-create-a-migration); from the command line:
198+
For example, as a follow up to the [Quickstart](#user-content-create-a-migration); from the command line, generate a new migration:
181199

182200
```shell
183201
php artisan make:migration update_places_table
@@ -231,8 +249,6 @@ class UpdatePlacesTable extends Migration
231249
}
232250
```
233251

234-
235-
236252
## Geometry classes
237253

238254
### Available Geometry classes
@@ -247,6 +263,8 @@ class UpdatePlacesTable extends Migration
247263
| `MultiPolygon(Polygon[])` | [MultiPolygon](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multipolygon.html) |
248264
| `GeometryCollection(Geometry[])` | [GeometryCollection](https://dev.mysql.com/doc/refman/5.7/en/gis-class-geometrycollection.html) |
249265

266+
Check out the [Class diagram](https://user-images.githubusercontent.com/1837678/30788608-a5afd894-a16c-11e7-9a51-0a08b331d4c4.png).
267+
250268
### Using Geometry classes
251269

252270
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)).
@@ -267,7 +285,7 @@ for($polygon as $i => $linestring) {
267285

268286
#### Helpers
269287

270-
##### From/To Well Kown Text ([WKT](https://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html#gis-wkt-format))
288+
##### From/To Well Known Text ([WKT](https://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html#gis-wkt-format))
271289

272290
```php
273291
$polygon = Polygon::fromWKT('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))');
@@ -291,29 +309,26 @@ The Geometry classes implement [`JsonSerializable`](http://php.net/manual/en/cla
291309
```php
292310
$point = new Point(10, 20);
293311

294-
json_encode($point);
295-
// or
296-
$point->toJson();
297-
```
298-
299-
Returns:
300-
301-
```javascript
302-
{
303-
"type": "Feature",
304-
"properties": {},
305-
"geometry": {
306-
"type": "Point",
307-
"coordinates": [
308-
-73.9878441,
309-
40.7484404
310-
]
311-
}
312-
}
312+
json_encode($point); // or $point->toJson();
313+
314+
// {
315+
// "type": "Feature",
316+
// "properties": {},
317+
// "geometry": {
318+
// "type": "Point",
319+
// "coordinates": [
320+
// -73.9878441,
321+
// 40.7484404
322+
// ]
323+
// }
324+
// }
325+
326+
$locaction = Geometry::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
327+
$location instanceof Point::class; // true
328+
$location->getLat(); // 1.2
329+
$location->getLng()); // 3.4
313330
```
314331

315-
#####
316-
317332
## Scopes: Spatial analysis functions
318333

319334
Spatial analysis functions are implemented using [Eloquent Local Scopes](https://laravel.com/docs/5.4/eloquent#local-scopes).
@@ -336,8 +351,6 @@ Available scopes:
336351

337352
*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)).*
338353

339-
340-
341354
## Credits
342355

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

0 commit comments

Comments
 (0)