coresmart/laravel-spatial
Composer 安装命令:
composer require coresmart/laravel-spatial
包简介
Laravel package to work with geospatial data types and functions.
README 文档
README
Forked from tarfin-labs/laravel-spatial for laravel 9 support
This is a Laravel package to work with geospatial data types and functions.
It supports only MySQL Spatial Data Types and Functions, other RDBMS is on the roadmap.
Supported data types:
Point
Available Scopes:
withinDistanceTo($column, $coordinates, $distance)selectDistanceTo($column, $coordinates)orderByDistanceTo($column, $coordinates, 'asc')
Installation
You can install the package via composer:
composer require tarfin-labs/laravel-spatial
Usage
Generate a new model with a migration file:
php artisan make:model Address --migration
1- Migrations:
To add a spatial data field, you need to extend the migration from TarfinLabs\LaravelSpatial\Migrations\SpatialMigration.
It is a simple abstract class that adds point spatial data type to Doctrine mapped types in the constructor.
use TarfinLabs\LaravelSpatial\Migrations\SpatialMigration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends SpatialMigration { public function up(): void { Schema::create('addresses', function (Blueprint $table) { $table->point('location'); }) } }
The migration above creates an addresses table with a location spatial column.
Spatial columns with no SRID attribute are not SRID-restricted and accept values with any SRID. However, the optimizer cannot use SPATIAL indexes on them until the column definition is modified to include an SRID attribute, which may require that the column contents first be modified so that all values have the same SRID.
So you should give an SRID attribute to use spatial indexes in the migrations:
Schema::create('addresses', function (Blueprint $table) { $table->point(column: 'location', srid: 4326); $table->spatialIndex('location'); })
2- Models:
Fill the $fillable, $casts arrays in the model:
use Illuminate\Database\Eloquent\Model; use TarfinLabs\LaravelSpatial\Casts\LocationCast; use TarfinLabs\LaravelSpatial\Traits\HasSpatial; class Address extends Model { use HasSpatial; protected $fillable = [ 'id', 'name', 'address', 'location', ]; protected array $casts = [ 'location' => LocationCast::class ]; }
3- Spatial Data Types:
Point:
Point represents the coordinates of a location and contains latitude, longitude, and srid properties.
At this point, it is crucial to understand what SRID is. Each spatial instance has a spatial reference identifier (SRID). The SRID corresponds to a spatial reference system based on the specific ellipsoid used for either flat-earth mapping or round-earth mapping. A spatial column can contain objects with different SRIDs.
For details about SRID you can follow the link: https://en.wikipedia.org/wiki/Spatial_reference_system
- Default value of
latitude,longitudeparameters is0.0. - Default value of
sridparameter is0.
use TarfinLabs\LaravelSpatial\Types\Point; $location = new Point(lat: 28.123456, lng: 39.123456, srid: 4326); $location->getLat(); // 28.123456 $location->getLng(); // 39.123456 $locatipn->getSrid(); // 4326
You can override the default SRID via the laravel-spatial config file. To do that, you should publish the config file using vendor:publish artisan command:
php artisan vendor:publish --provider="TarfinLabs\LaravelSpatial\LaravelSpatialServiceProvider"
After that, you can change the value of default_srid in config/laravel-spatial.php
return [ 'default_srid' => 4326, ];
4- Scopes:
withinDistanceTo()
You can use the withinDistanceTo() scope to filter locations by given distance:
To filter addresses within the range of 10 km from the given coordinate:
use TarfinLabs\LaravelSpatial\Types\Point; use App\Models\Address; Address::query() ->withinDistanceTo('location', new Point(lat: 25.45634, lng: 35.54331), 10000) ->get();
selectDistanceTo()
You can get the distance between two points by using selectDistanceTo() scope. The distance will be in meters:
use TarfinLabs\LaravelSpatial\Types\Point; use App\Models\Address; Address::query() ->selectDistanceTo('location', new Point(lat: 25.45634, lng: 35.54331)) ->get();
orderByDistanceTo()
You can order your models by distance to given coordinates:
use TarfinLabs\LaravelSpatial\Types\Point; use App\Models\Address; // ASC Address::query() ->orderByDistanceTo('location', new Point(lat: 25.45634, lng: 35.54331)) ->get(); // DESC Address::query() ->orderByDistanceTo('location', new Point(lat: 25.45634, lng: 35.54331), 'desc') ->get();
Get latitude and longitude of the location:
use App\Models\Address; $address = Address::find(1); $address->location; // TarfinLabs\LaravelSpatial\Types\Point $address->location->getLat(); $address->location->getLng();
Create a new address with location:
use App\Models\Address; Address::create([ 'name' => 'Bag End', 'address' => '1 Bagshot Row, Hobbiton, Shire', 'location' => new Point(lat: 25.45634, lng: 35.54331), ]);
Testing
composer test
Road Map
- MultiPoint
- LineString
- MultiLineString
- Polygon
- MultiPolygon
- GeometryCollection
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email development@tarfin.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
coresmart/laravel-spatial 适用场景与选型建议
coresmart/laravel-spatial 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tarfin-labs」 「laravel-spatial」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 coresmart/laravel-spatial 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coresmart/laravel-spatial 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 coresmart/laravel-spatial 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel logger for AWS Clouldwatch Log service.
Laravel package to work with geospatial data types and functions.
zbar-php is a php package that provides an interface to the zbar bar-code reading library.
netgsm channel for laravel
Makes pdf processing easy.
Key value config management for Laravel
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-28