ajthinking/laravel-postgis

laravel 的 PostGIS 扩展。旨在使从 laravel 模型操作几何形状变得简单。

v6.0.6 2022-08-25 08:19 UTC

This package is auto-updated.

Last update: 2024-08-25 13:24:15 UTC


README

Build Status

仅支持 Laravel 9+ 和 PHP 8+

特性

  • 使用几何类而不是数组。
$model->myPoint = new Point(1,2);  //lat, long
  • 在迁移中添加辅助函数。
$table->polygon('myColumn');

安装

composer require mstaack/laravel-postgis

使用方法

首先,确保您已将 PostGIS 启用数据库中 - 您可以在 Laravel 迁移中执行此操作,或手动通过 SQL 执行。

通过 Laravel 迁移启用 PostGIS

您需要发布迁移以轻松启用 PostGIS

php artisan vendor:publish --provider="Ajthinking\LaravelPostgis\DatabaseServiceProvider" --tag="migrations"

然后您运行迁移

php artisan migrate

这些方法可以安全使用,并且只会启用/禁用相关的 PostGIS 扩展 - 如果 PostGIS 已经启用或未启用,它们不会引发错误。

如果您愿意,可以使用 enablePostgis() 方法,如果 PostGIS 已经启用,它将引发错误,以及 disablePostgis() 方法,如果 PostGIS 未启用,它将引发错误。

手动启用 PostGIS

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

CREATE EXTENSION postgis;

要验证 PostGIS 是否已启用,您可以运行

SELECT postgis_full_version();

迁移

现在通过运行创建一个具有迁移的模型

php artisan make:model Location

如果您不需要模型,只想运行迁移,请运行

php artisan make:migration create_locations_table

使用您的编辑器打开创建的迁移。

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

class CreateLocationsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('locations', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('address')->unique();
            $table->point('location'); // GEOGRAPHY POINT column with SRID of 4326 (these are the default values).
            $table->point('location2', 'GEOGRAPHY', 4326); // GEOGRAPHY POINT column with SRID of 4326 with optional parameters.
            $table->point('location3', 'GEOMETRY', 27700); // GEOMETRY column with SRID of 27700.
            $table->polygon('polygon'); // GEOGRAPHY POLYGON column with SRID of 4326.
            $table->polygon('polygon2', 'GEOMETRY', 27700); // GEOMETRY POLYGON column with SRID of 27700.
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('locations');
    }

}

可用的蓝图几何形状

  • 多点
  • 线串
  • 多线串
  • 多边形
  • 多多边形
  • 几何集合

其他方法

  • enablePostgis
  • disablePostgis

模型

所有要启用 PostGis 的模型 必须 使用 PostgisTrait

您还必须定义一个名为 $postgisFields 的数组,该数组定义了模型上哪些属性/列被视为几何对象。默认情况下,所有属性都是 geography 类型。如果您想使用具有自定义 SRID 的 geometry,您必须定义一个名为 $postgisTypes 的数组。此关联数组的键必须与 $postgisFields 中的条目匹配(所有缺失的键默认为 geography),值也是关联数组。它们必须有两个键:geomtypegeographygeometry,以及 srid 是所需的 SRID。注意:自定义 SRID 仅支持 geometry,不支持 geography

use Illuminate\Database\Eloquent\Model;
use Ajthinking\LaravelPostgis\Eloquent\PostgisTrait;
use Ajthinking\LaravelPostgis\Geometries\Point;

class Location extends Model
{
    use PostgisTrait;

    protected $fillable = [
        'name',
        'address'
    ];

    protected $postgisFields = [
        'location',
        'location2',
        'location3',
        'polygon',
        'polygon2'
    ];

    protected $postgisTypes = [
        'location' => [
            'geomtype' => 'geography',
            'srid' => 4326
        ],
        'location2' => [
            'geomtype' => 'geography',
            'srid' => 4326
        ],
        'location3' => [
            'geomtype' => 'geometry',
            'srid' => 27700
        ],
        'polygon' => [
            'geomtype' => 'geography',
            'srid' => 4326
        ],
        'polygon2' => [
            'geomtype' => 'geometry',
            'srid' => 27700
        ]
    ]
}

$linestring = new LineString(
    [
        new Point(0, 0),
        new Point(0, 1),
        new Point(1, 1),
        new Point(1, 0),
        new Point(0, 0)
    ]
);

$location1 = new Location();
$location1->name = 'Googleplex';
$location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
$location1->location = new Point(37.422009, -122.084047);
$location1->location2 = new Point(37.422009, -122.084047);
$location1->location3 = new Point(37.422009, -122.084047);
$location1->polygon = new Polygon([$linestring]);
$location1->polygon2 = new Polygon([$linestring]);
$location1->save();

$location2 = Location::first();
$location2->location instanceof Point // true

可用的几何类

  • 多点
  • 线串
  • 多线串
  • 多边形
  • 多多边形
  • 几何集合

致谢

这是来自伟大人士工作的 分支。感谢