在构建现代 Web 应用时,地理空间数据处理变得越来越常见。无论是开发一个外卖配送系统、一个社交签到应用,还是一个房产租赁平台,都需要有效地存储和查询地理位置信息。然而,如果你仅仅使用
float
想象一下,你有一个
stores
latitude
longitude
<pre class="brush:php;toolbar:false;">SELECT *,
(6371 * acos(
cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) +
sin(radians(?)) * sin(radians(latitude))
)) AS distance
FROM stores
ORDER BY distance
LIMIT 10;这段 SQL 不仅复杂,而且随着数据量的增长,性能会迅速下降。更不用说处理多边形区域(Geofencing)等更高级的地理空间操作了。
mstaack/laravel-postgis
为了解决上述问题,我们需要引入强大的地理空间数据库扩展——PostGIS。PostGIS 为 PostgreSQL 数据库提供了存储和查询地理空间对象(如点、线、多边形)的能力,并提供了大量高效的地理空间函数。
然而,如何将 PostGIS 的能力无缝集成到 Laravel 的 Eloquent 模型中呢?这就是
mstaack/laravel-postgis
【重要提示】 请注意,
mstaack/laravel-postgis
clickbar/laravel-magellan
mstaack/laravel-postgis
首先,通过 Composer 安装
mstaack/laravel-postgis
<pre class="brush:php;toolbar:false;">composer require mstaack/laravel-postgis
对于 Laravel 5.5 及更高版本,包会自动发现。如果你使用的是更早的 Laravel 版本,你需要在
config/app.php
MStaack\LaravelPostgis\DatabaseServiceProvider
在你的 PostgreSQL 数据库中启用 PostGIS 扩展是使用此包的前提。你可以通过 Laravel 迁移来完成:
<pre class="brush:php;toolbar:false;">php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="migrations"
<pre class="brush:php;toolbar:false;">php artisan migrate
这会创建一个迁移文件,负责在你的数据库中启用 PostGIS 扩展。
如果你更喜欢手动操作,也可以直接在 SQL 客户端中运行:
<pre class="brush:php;toolbar:false;">CREATE EXTENSION postgis;
现在,你可以在 Laravel 迁移文件中定义地理空间字段了。这个包提供了一个扩展的
Blueprint
创建一个新的迁移:
<pre class="brush:php;toolbar:false;">php artisan make:model Location -m
编辑生成的迁移文件,使用
MStaack\LaravelPostgis\Schema\Blueprint
<pre class="brush:php;toolbar:false;"><?php
use Illuminate\Database\Migrations\Migration;
use MStaack\LaravelPostgis\Schema\Blueprint; // 注意这里的命名空间
use Illuminate\Support\Facades\Schema;
class CreateLocationsTable extends Migration
{
public function up()
{
Schema::create('locations', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('address')->unique();
$table->point('location'); // GEOGRAPHY POINT 列,默认 SRID 4326
$table->point('store_entrance', 'GEOMETRY', 27700); // GEOMETRY POINT 列,自定义 SRID 27700
$table->polygon('delivery_area'); // GEOGRAPHY POLYGON 列
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('locations');
}
}这里我们定义了
point
polygon
GEOGRAPHY
GEOMETRY
为了让 Eloquent 模型能够识别并操作这些地理空间字段,你需要:
PostgisTrait
$postgisFields
$postgisTypes
geomtype
srid
编辑
app/Models/Location.php
<pre class="brush:php;toolbar:false;"><?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use MStaack\LaravelPostgis\Eloquent\PostgisTrait;
use MStaack\LaravelPostgis\Geometries\Point;
use MStaack\LaravelPostgis\Geometries\Polygon;
use MStaack\LaravelPostgis\Geometries\LineString; // 如果需要
class Location extends Model
{
use PostgisTrait;
protected $fillable = [
'name',
'address',
];
// 声明哪些字段是 PostGIS 几何类型
protected $postgisFields = [
'location',
'store_entrance',
'delivery_area',
];
// 为每个 PostGIS 字段指定其几何类型和 SRID
protected $postgisTypes = [
'location' => [
'geomtype' => 'geography',
'srid' => 4326
],
'store_entrance' => [
'geomtype' => 'geometry',
'srid' => 27700 // 自定义 SRID
],
'delivery_area' => [
'geomtype' => 'geography',
'srid' => 4326
]
];
// ... 其他模型方法
}现在,你可以像操作普通 Eloquent 属性一样操作地理空间字段了:
<pre class="brush:php;toolbar:false;">use App\Models\Location;
use MStaack\LaravelPostgis\Geometries\Point;
use MStaack\LaravelPostgis\Geometries\Polygon;
use MStaack\LaravelPostgis\Geometries\LineString;
// 创建一个点对象 (纬度, 经度)
$point = new Point(34.052235, -118.243683); // 洛杉矶市中心
// 创建一个多边形对象 (由一个或多个 LineString 组成)
$linestring = new LineString([
new Point(0, 0),
new Point(0, 1),
new Point(1, 1),
new Point(1, 0),
new Point(0, 0) // 闭合多边形
]);
$polygon = new Polygon([$linestring]);
// 保存数据
$store = new Location();
$store->name = '我的商店';
$store->address = '某某街某某号';
$store->location = $point;
$store->store_entrance = new Point(34.052230, -118.243670); // 另一个点
$store->delivery_area = $polygon;
$store->save();
// 读取数据
$retrievedStore = Location::first();
dump($retrievedStore->location); // 返回 MStaack\LaravelPostgis\Geometries\Point 实例
dump($retrievedStore->location instanceof Point); // true
// 你可以直接访问点的坐标
echo "纬度: " . $retrievedStore->location->getLat();
echo "经度: " . $retrievedStore->location->getLng();point
polygon
mstaack/laravel-postgis
DB::raw()
通过
mstaack/laravel-postgis
虽然
mstaack/laravel-postgis
如果你正在开始一个新的项目,并且需要集成 PostGIS,我强烈建议你直接考虑使用其推荐的替代品:clickbar/laravel-magellan
mstaack/laravel-postgis
以上就是如何解决Laravel地理空间数据处理的复杂性?mstaack/laravel-postgis助你轻松驾驭!的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号