mstaack/laravel-postgis

为 Laravel 提供的 PostGIS 扩展。旨在简化 Laravel 模型中几何形状的操作

6.1 2023-03-28 10:20 UTC

README

Build Status

已弃用

建议使用: https://github.com/clickbar/laravel-magellan

特性

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

警告

此包已转移到新所有者,并旨在很快支持 Laravel 6/7/8/9 和 PHP 7!

替换所有对新命名空间的所有引用

MStaack\LaravelPostgis

感谢

精通 Laravel 包和 Postgres/Postgis?请考虑贡献!我们正在寻找愿意帮助的人!

安装

  • 使用 3.* 为 Laravel 5
composer require "mstaack/laravel-postgis:3.*"
  • 使用 5.* 为 Laravel 6/7/8/9
composer require mstaack/laravel-postgis

对于 laravel >=5.5,这就可以了。此包支持 Laravel 的新 包发现

如果您使用的是 Laravel < 5.5,您还需要将 DatabaseServiceProvider 添加到您的 config/app.php 文件中。

'MStaack\LaravelPostgis\DatabaseServiceProvider',

用法

首先,确保您的数据库已启用 PostGIS - 您可以在 Laravel 迁移中或手动通过 SQL 完成。

通过 Laravel 迁移启用 PostGIS

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

php artisan vendor:publish --provider="MStaack\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 MStaack\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),值也是关联数组。它们必须有两个键:geomtype,它是 geographygeometry,和 srid,它是所需的 SRID。 注意:自定义 SRID 仅支持 geometry,不支持 geography

use Illuminate\Database\Eloquent\Model;
use MStaack\LaravelPostgis\Eloquent\PostgisTrait;
use MStaack\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

可用的几何类

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

发布配置

存在一个配置文件,可以覆盖默认值。要将它添加到您的项目中,请运行:

php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="postgis"

坐标精度

存储/显示的坐标精度可以在配置中进行自定义。

# config/postgis.php
return [
    ...
    'precision' => 6,
];

有关更多信息,请参阅http://wiki.gis.com/wiki/index.php/Decimal_degrees#Accuracy