Skip to content

Commit 7f4ca94

Browse files
committed
Merge branch 'documentation' into from-geojson
2 parents 3d49e42 + 2a701a9 commit 7f4ca94

File tree

1 file changed

+129
-23
lines changed

1 file changed

+129
-23
lines changed

README.md

+129-23
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
## Installation
1921

2022
Add the package using composer:
@@ -71,6 +73,8 @@ class CreatePlacesTable extends Migration {
7173
$table->string('name')->unique();
7274
// Add a Point spatial data field named location
7375
$table->point('location')->nullable();
76+
// Add a Polygon spatial data field named area
77+
$table->polygon('area')->nullable();
7478
$table->timestamps();
7579
});
7680
}
@@ -117,22 +121,41 @@ class Place extends Model
117121
use SpatialTrait;
118122

119123
protected $fillable = [
120-
'name',
124+
'name'
121125
];
122126

123127
protected $spatialFields = [
124128
'location',
129+
'area'
125130
];
126131
}
127132
```
128133

129134
### Saving a model
130135

131136
```php
137+
use Grimzy\LaravelMysqlSpatial\Types\Point;
138+
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
139+
132140
$place1 = new Place();
133141
$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)
135145
$place1->save();
146+
147+
// saving a polygon
148+
$place1->area = new Polygon([new LineString([
149+
new Point(40.74894149554006, -73.98615270853043),
150+
new Point(40.74848633046773, -73.98648262023926),
151+
new Point(40.747925497790725, -73.9851602911949),
152+
new Point(40.74837050671544, -73.98482501506805),
153+
new Point(40.74894149554006, -73.98615270853043)
154+
])]);
155+
$place1->save();
156+
157+
$place1->area = new Polygon();
158+
136159
```
137160

138161
### Retrieving a model
@@ -143,34 +166,43 @@ $lat = $place2->location->getLat(); // 40.7484404
143166
$lng = $place2->location->getLng(); // -73.9878441
144167
```
145168

146-
## Migration
169+
## Migrations
170+
171+
### Columns
147172

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

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');`
158185

159-
### Spatial index
186+
### Spatial indexes
160187

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

190+
- `$table->spatialIndex('column_name');`
191+
- `$table->dropSpatialIndex(['column_name']);` or `$table->dropSpatialIndex('index_name')`
192+
163193
Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):
164194

165195
> 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`.
166196
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:
168200

169201
```shell
170202
php artisan make:migration update_places_table
171203
```
172204

173-
Then edit the migration you just created:
205+
Then edit the migration file that you just created:
174206

175207
```php
176208
use Illuminate\Database\Migrations\Migration;
@@ -217,17 +249,91 @@ class UpdatePlacesTable extends Migration
217249
}
218250
}
219251
```
220-
## Models
221252

222-
Available geometry classes:
253+
## Geometry classes
254+
255+
### Available Geometry classes
256+
257+
| Grimzy\LaravelMysqlSpatial\Types | OpenGIS Class |
258+
| ---------------------------------------- | ---------------------------------------- |
259+
| `Point($lat, $lng)` | [Point](https://dev.mysql.com/doc/refman/5.7/en/gis-class-point.html) |
260+
| `MultiPoint(Point[])` | [MultiPoint](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multipoint.html) |
261+
| `LineString(Point[])` | [LineString](https://dev.mysql.com/doc/refman/5.7/en/gis-class-linestring.html) |
262+
| `MultiLineString(LineString[])` | [MultiLineString](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multilinestring.html) |
263+
| `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) |
264+
| `MultiPolygon(Polygon[])` | [MultiPolygon](https://dev.mysql.com/doc/refman/5.7/en/gis-class-multipolygon.html) |
265+
| `GeometryCollection(Geometry[])` | [GeometryCollection](https://dev.mysql.com/doc/refman/5.7/en/gis-class-geometrycollection.html) |
266+
267+
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))
290+
291+
```php
292+
// fromWKT($wkt)
293+
$polygon = Polygon::fromWKT('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))');
294+
295+
$polygon->toWKT(); // POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
296+
```
297+
298+
##### From/To String
299+
300+
```php
301+
// fromString($wkt)
302+
$polygon = Polygon::fromString('(0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)');
303+
304+
(string)$polygon; // (0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)
305+
```
306+
307+
##### From/To JSON ([GeoJSON](http://geojson.org/))
308+
309+
The Geometry classes implement [`JsonSerializable`](http://php.net/manual/en/class.jsonserializable.php) and `Illuminate\Contracts\Support\Jsonable` to help serialize into GeoJSON:
223310

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)` :
330+
331+
```php
332+
$locaction = Geometry::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
333+
$location instanceof Point::class; // true
334+
$location->getLat(); // 1.2
335+
$location->getLng()); // 3.4
336+
```
231337

232338
## Scopes: Spatial analysis functions
233339

0 commit comments

Comments
 (0)