mammutgroup / laravel-spatial

Laravel的MySQL几何扩展。旨在使从Laravel模型中处理几何体变得容易。

3.1.3 2017-01-08 04:36 UTC

README

Build Status Code Climate Coverage Status

特性

  • 使用几何类而不是数组。($myModel->myPoint = new Point(1,2)
  • 在迁移中添加助手。($table->polygon('myColumn')

未来计划

  • 几何类上的几何函数(contains(), equals(), distance()等……(帮助!))

版本

使用2.*适用于Laravel 5.1.*
使用3.*适用于Laravel 5.2.*

安装

composer require phaza/laravel-postgis 

接下来,将DatabaseServiceProvider添加到您的config/app.php文件中。

'Mammutgroup\LaravelPostgis\DatabaseServiceProvider',

这就完成了。

用法

首先,确保启用postgis。

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 Mammutgroup\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');
            $table->polygon('polygon');
            $table->timestamps();
        });
    }

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

}

可用的蓝图几何形状

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

其他方法

  • 启用Postgis
  • 禁用Postgis

模型

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

您还必须定义一个名为$postgisFields的数组,该数组定义了您的模型中哪些属性/列应被视为几何对象。

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

class Location extends Model
{
    use PostgisTrait;

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

    protected $postgisFields = [
        'location',
        'polygon',
    ];

}

$location1 = new Location();
$location1->name = 'Googleplex';
$location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
$location1->location = new Point(37.422009, -122.084047);
$location1->save();

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

可用的几何类

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