Download the PHP package bertim/laravel-mysql-spatial without Composer
On this page you can find all versions of the php package bertim/laravel-mysql-spatial. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bertim/laravel-mysql-spatial
More information about bertim/laravel-mysql-spatial
Files in bertim/laravel-mysql-spatial
Package laravel-mysql-spatial
Short Description MySQL spatial data types extension for Laravel.
License MIT
Informations about the package laravel-mysql-spatial
What's the different of the version original?
This project resolve the issue [#38] (https://github.com/grimzy/laravel-mysql-spatial/issues/38)
How to use?
Laravel MySQL Spatial extension
Laravel package to easily work with MySQL Spatial Data Types and MySQL Spatial Functions.
Please check the documentation for your MySQL version. MySQL's Extension for Spatial Data was added in MySQL 5.5 but many Spatial Functions were changed in 5.6 and 5.7.
Versions
1.x.x
: MySQL 5.6 (also supports MySQL 5.5 but not all spatial analysis functions)2.x.x
: MySQL 5.7 and 8.0
This package also works with MariaDB. Please refer to the MySQL/MariaDB Spatial Support Matrix for compatibility.
Installation
Add the package using composer:
For MySQL 5.6 and 5.5:
For Laravel versions before 5.5 or if not using auto-discovery, register the service provider in config/app.php
:
Quickstart
Create a migration
From the command line:
Then edit the migration you just created by adding at least one spatial data field. For Laravel versions prior to 5.5, you can use the Blueprint provided by this package (Grimzy\LaravelMysqlSpatial\Schema\Blueprint):
Run the migration:
Create a model
From the command line:
Then edit the model you just created. It must use the SpatialTrait
and define an array called $spatialFields
with the name of the MySQL Spatial Data field(s) created in the migration:
Saving a model
Retrieving a model
Geometry classes
Available Geometry classes
Grimzy\LaravelMysqlSpatial\Types | OpenGIS Class |
---|---|
Point($lat, $lng) |
Point |
MultiPoint(Point[]) |
MultiPoint |
LineString(Point[]) |
LineString |
MultiLineString(LineString[]) |
MultiLineString |
Polygon(LineString[]) (exterior and interior boundaries) |
Polygon |
MultiPolygon(Polygon[]) |
MultiPolygon |
GeometryCollection(Geometry[]) |
GeometryCollection |
Check out the Class diagram.
Using Geometry classes
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).
IteratorAggregate and ArrayAccess
The "composite" Geometries (LineString
, Polygon
, MultiPoint
, MultiLineString
, and GeometryCollection
) implement IteratorAggregate
and ArrayAccess
; making it easy to perform Iterator and Array operations. For example:
Helpers
From/To Well Known Text (WKT)
From/To String
From/To JSON (GeoJSON)
The Geometry classes implement JsonSerializable
and Illuminate\Contracts\Support\Jsonable
to help serialize into GeoJSON:
To deserialize a GeoJSON string into a Geometry class, you can use Geometry::fromJson($json_string)
:
Scopes: Spatial analysis functions
Spatial analysis functions are implemented using Eloquent Local Scopes.
Available scopes:
distance($geometryColumn, $geometry, $distance)
distanceExcludingSelf($geometryColumn, $geometry, $distance)
distanceSphere($geometryColumn, $geometry, $distance)
distanceSphereExcludingSelf($geometryColumn, $geometry, $distance)
comparison($geometryColumn, $geometry, $relationship)
within($geometryColumn, $polygon)
crosses($geometryColumn, $geometry)
contains($geometryColumn, $geometry)
disjoint($geometryColumn, $geometry)
equals($geometryColumn, $geometry)
intersects($geometryColumn, $geometry)
overlaps($geometryColumn, $geometry)
doesTouch($geometryColumn, $geometry)
orderBySpatial($geometryColumn, $geometry, $orderFunction, $direction = 'asc')
orderByDistance($geometryColumn, $geometry, $direction = 'asc')
orderByDistanceSphere($geometryColumn, $geometry, $direction = 'asc')
Note that behavior and availability of MySQL spatial analysis functions differs in each MySQL version (cf. documentation).
Migrations
For Laravel versions prior to 5.5, you can use the Blueprint provided with this package: Grimzy\LaravelMysqlSpatial\Schema\Blueprint
.
Columns
Available MySQL Spatial Types migration blueprints:
$table->geometry('column_name')
$table->point('column_name')
$table->lineString('column_name')
$table->polygon('column_name')
$table->multiPoint('column_name')
$table->multiLineString('column_name')
$table->multiPolygon('column_name')
$table->geometryCollection('column_name')
Spatial indexes
You can add or drop spatial indexes in your migrations with the spatialIndex
and dropSpatialIndex
blueprints.
$table->spatialIndex('column_name')
$table->dropSpatialIndex(['column_name'])
or$table->dropSpatialIndex('index_name')
Note about spatial indexes from the MySQL documentation:
For
MyISAM
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 theSPATIAL
keyword. Columns in spatial indexes must be declaredNOT NULL
.
Also please read this important note regarding Index Lengths in the Laravel 5.6 documentation.
For example, as a follow up to the Quickstart; from the command line, generate a new migration:
Then edit the migration file that you just created:
Tests
Integration tests require a running MySQL database. If you have Docker installed, you can start easily start one:
Contributing
Recommendations and pull request are most welcome! Pull requests with tests are the best! There are still a lot of MySQL spatial functions to implement or creative ways to use spatial functions.
Credits
Originally inspired from njbarrett's Laravel postgis package.
All versions of laravel-mysql-spatial with dependencies
ext-pdo Version *
ext-json Version *
illuminate/database Version ^5.2
geo-io/wkb-parser Version ^1.0
jmikola/geojson Version ^1.0