Download the PHP package angel-source-labs/laravel-spatial without Composer
On this page you can find all versions of the php package angel-source-labs/laravel-spatial. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download angel-source-labs/laravel-spatial
More information about angel-source-labs/laravel-spatial
Files in angel-source-labs/laravel-spatial
Package laravel-spatial
Short Description Spatial data types extension for Laravel.
License MIT
Informations about the package laravel-spatial
Spatial: GIS Spatial Extensions for Laravel
Laravel package to easily work with GIS data types in PostGIS, MySQL 5.7, and MySQL 8.
Supported Compatibiltiy
Laravel
This package is tested against the following Laravel versions:
- Laravel 6.x
- Laravel 7.x
- Laravel 8.x
- Laravel 9.x
- Laravel 10.x
- Laravel 11.x
Databases
This package is tested against the following Databases
Database | Platform | SRID Supported | Geography Type Supported |
---|---|---|---|
PostGIS 10.x - 14.x | Postgres 10 | Yes | Yes |
MySQL 5.7 | MySQL 5.7 | No | No |
MySQL 8.x | MySQL 8 | Yes | No |
Percona 5.7 | MySQL 5.7 | No | No |
Percona 8.x | MySQL 8 | Yes | No |
MariaDB 10.2 - 10.7 | MySQL 5.7 | No | No |
Future
Support for these databases may be available in a future release. This package has been designed to support these databases but the work is not complete.
- SQLServer
- SQLite
History and Motivation
We really like the grimzy/laravel-mysql-spatial Laravel Eloquent API and we wanted to also be able to use Postgis. (See issue 137). The goal of this package is to provide an API compatible with the grimzy/laravel-mysql-spatial package that also supports postgis and additional database drivers.
This package is a fork and substantial refactoring of grimzy/laravel-mysql-spatial
:
- refactored to use
laravel-expressions
to provide database compatibility across postgis, mysql 8, and mysql 5.7 - refactored to use
orchestra/testbench
in PHPUnit tests - PHPUnit tests updated to use PHPUnit 9.x versus PHPUnit 6.x
Historically, grimzy/laravel-mysql-spatial
was itself a fork of njbarrett/laravel-postgis
, which is now mstaack/laravel-postgis
. These laravel-postgis
pacakges provide access to postgis but do not provide the Laravel Eloquent Spatial analysis functions which were added by grimzy/laravel-mysql-spatial
.
- March 2015: phaza/laravel-postgres Peter Haza
- May 2016: njbarrett/laravel-postgis Nick Barrett
- March 2017: grimzy/laravel-mysql-spatial Joseph Estefane
- Feb 2022: (this package) Angel-Source-Labs/laravel-spatial Brion Finlay
- March 2020: mstaack/laravel-postgis Max Staack
Installation
Add the package using composer:
Installation
Laravel 8.x, 9.x, 10.x, 11.x
- Require this package directly by
composer require angel-source-labs/laravel-spatial
- Or add this package in your composer.json and run
composer update
Laravel 6.x, 7.x
Laravel 6.x, and 7.x require DBAL 2.x. Because DBAL is a require-dev
dependency of laravel, its version
constraint will not be resolved by composer when installing a child package. However, this is easy to solve by specifying DBAL 2.x as
an additional dependency.
Note that Laravel 6.x, 7.x, 8.x, and 9.x are EOL. See https://laravelversions.com/en. These versions will continue to be supported by this package for as long as it is reasonably easy, thanks to github actions performing the testing.
To install for Laravel 6.x, and 7.x:
- Require this package directly by
composer require angel-source-labs/laravel-spatial
- Require the dbal package directly by
composer require doctrine/dbal:^2.6
- Or add these packages in your composer.json and run
composer update
Quickstart
Create a migration
From the command line:
Then edit the migration you just created by adding at least one spatial data field.
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 spatial data field(s) created in the migration:
Saving a model
Or if your database fields were created with a specific SRID:
Note: When saving collection Geometries (
LineString
,Polygon
,MultiPoint
,MultiLineString
, andGeometryCollection
), only the top-most geometry should have an SRID set in the constructor.In the example above, when creating a
new Polygon()
, we only set the SRID on thePolygon
and use the default for theLineString
and thePoint
objects.
Retrieving a model
Geometry classes
Available Geometry classes
AngelSourceLabs\LaravelSpatial\Doctrine\Types | OpenGIS Class |
---|---|
Point($lat, $lng, $srid = 0) |
Point |
MultiPoint(Point[], $srid = 0) |
MultiPoint |
LineString(Point[], $srid = 0) |
LineString |
MultiLineString(LineString[], $srid = 0) |
MultiLineString |
Polygon(LineString[], $srid = 0) (exterior and interior boundaries) |
Polygon |
MultiPolygon(Polygon[], $srid = 0) |
MultiPolygon |
GeometryCollection(Geometry[], $srid = 0) |
GeometryCollection |
PlantUML Markup
Using Geometry classes
In order for your Eloquent Model to handle the Geometry classes, it must use the AngelSourceLabs\LaravelSpatial\Eloquent\SpatialTrait
trait and define a protected
property $spatialFields
as an array of spatial data type column names (example in Quickstart).
IteratorAggregate and ArrayAccess
The collection 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 spatial analysis functions differs in each database and database version.
Spatial function references:
Migrations
Columns
Available spatial type migration blueprints:
$table->geometry(string $column_name, int $srid = 0)
$table->point(string $column_name, int $srid = 0)
$table->lineString(string $column_name, int $srid = 0)
$table->polygon(string $column_name, int $srid = 0)
$table->multiPoint(string $column_name, int $srid = 0)
$table->multiLineString(string $column_name, int $srid = 0)
$table->multiPolygon(string $column_name, int $srid = 0)
$table->geometryCollection(string $column_name, int $srid = 0)
Spatial type references:
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 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
Start MySQL 5.7, MySQL8, Postgis Docker containers for tests.
run tests. Unit tests can be run without the database server docker containers.
Contributing
Recommendations and pull request are most welcome! Pull requests with tests are the best! There are still a lot of spatial functions to implement or creative ways to use spatial functions.
Credits
Originally inspired from grimzy/laravel-mysql-spatial and njbarrett's Laravel postgis package.
License
Spatial for Laravel is open-sourced software licensed under the MIT license.
All versions of laravel-spatial with dependencies
ext-pdo Version *
ext-json Version *
angel-source-labs/laravel-expressions Version ^1.0
doctrine/dbal Version ^2.6|^3.0
geo-io/wkb-parser Version ^1.0
jmikola/geojson Version ^1.0
laravel/framework Version >=6.0