Laravel-PostGIS 使用教程

Laravel-PostGIS 使用教程

laravel-postgisPostgis extensions for laravel. Aims to make it easy to work with geometries from laravel models.项目地址:https://gitcode.com/gh_mirrors/la/laravel-postgis

项目介绍

Laravel-PostGIS 是一个为 Laravel 框架提供 PostGIS 扩展的项目。它旨在简化从 Laravel 模型中处理几何图形的工作。该项目受到了 mstaack/laravel-postgis 的启发,但已经发展成为功能更加丰富的工具。Laravel-PostGIS 提供了对 GeoJSON、WKB 和 WKT 的解析器/生成器,支持所有 PostGIS 数据类型,并避免了使用原始 SQL 来访问 PostGIS 函数。此外,它还扩展了 Schema、Query Builder 和 Postgres Grammar,以便轻松访问 PostGIS 数据库函数,如 ST_EXTENT。

项目快速启动

安装

首先,通过 Composer 安装 Laravel-PostGIS:

composer require mstaack/laravel-postgis

配置

config/app.php 中注册服务提供者:

'providers' => [
    // 其他服务提供者
    MStaack\LaravelPostgis\DatabaseServiceProvider::class,
],

启用 PostGIS

在数据库中启用 PostGIS。可以通过 Laravel 迁移或手动通过 SQL 启用。

通过 Laravel 迁移启用

发布迁移文件并运行迁移:

php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="migrations"
php artisan migrate
手动启用

使用 SQL 客户端连接到数据库并运行以下命令:

CREATE EXTENSION postgis;

验证 PostGIS 是否启用:

SELECT postgis_full_version();

创建模型和迁移

创建一个模型和迁移:

php artisan make:model Location -m

编辑迁移文件以添加 PostGIS 列:

use Illuminate\Database\Migrations\Migration;
use MStaack\LaravelPostgis\Schema\Blueprint;

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
        });
    }

    public function down() {
        Schema::dropIfExists('locations');
    }
}

运行迁移:

php artisan migrate

应用案例和最佳实践

应用案例

假设我们有一个应用程序需要存储和查询地理位置数据。我们可以使用 Laravel-PostGIS 来存储和查询这些数据。

存储地理位置数据
use App\Models\Location;
use MStaack\LaravelPostgis\Geometries\Point;

$location = new Location;
$location->name = 'Home';
$location->address = '123 Main St';
$location->location = new Point(49.87108851299, 8.6250264858452); // 纬度和经度
$location->save();
查询地理位置数据
use App\Models\Location;

$locations = Location::whereDistance('location', new Point(49.87108851299, 8.6250264858452), '<', 1000)->get();

最佳实践

  1. 使用 GeoJSON、WKB 和 WKT:利用 Laravel-PostGIS 提供的解析器和生成器来处理 GeoJSON、WKB 和 WKT 数据。
  2. 避免原始 SQL:使用 Laravel-PostGIS 提供的构建器函数来避免使用原始 SQL 访问 PostGIS 函数。
  3. 扩展 Schema 和 Query Builder:利用 Laravel-PostGIS 扩展的 Schema 和 Query Builder 来简化 PostGIS 函数的访问。

典型生态项目

Laravel-IDE-Helper

为了在 IDE 中获得更好的自动完成功能,建议使用 laravel-ide-helper

composer require --dev barryvdh/laravel-ide-helper

config/app.php 中注册服务提供者:

laravel-postgisPostgis extensions for laravel. Aims to make it easy to work with geometries from laravel models.项目地址:https://gitcode.com/gh_mirrors/la/laravel-postgis

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗恋蔷Samson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值