shaozeming/lumen-postgis

为laravel+lumen提供Postgis扩展。旨在使laravel模型中的几何处理更加容易。

1.0 2017-04-23 06:06 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:45:52 UTC


README

Build Status Code Climate Coverage Status

庄严声明

修改位置

  1. 修改了readme.md中关于lumen5.4安装的注册服务,需要注册两个服务(见下文)。
  2. 添加了几何数据迁移命令$table->geometry('geom');
  3. 添加 $location1->location = new GeomPoint(37.422009, -122.084047); 实现GIS几何数据直接利用模型插入

特性

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

未来计划

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

版本

  • 适用于Laravel 5.3.*;5.4.*
  • 适用于Lumen 5.3.*;5.4.*

安装

composer require shaozeming/lumen-postgis 'dev-master'

laravel Next 将DatabaseServiceProvider添加到您的config/app.php文件中。

    'Shaozeming\LumenPostgis\DatabaseServiceProvider',

这就完了。

Lumen Next 将DatabaseServiceProvider添加到您的bootstrap/app.php文件中。

     $app->register(Bosnadev\Database\DatabaseServiceProvider::class);   //多添加这个后,就可以解决问题。
     $app->register(Shaozeming\LumenPostgis\DatabaseServiceProvider::class);

这就完了。

使用方法

首先,请确保已启用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 Shaozeming\LumenPostgis\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->geometry('geom');
            $table->polygon('polygon');
            $table->timestamps();
        });
    }

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

}

可用的蓝图几何类型

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

其他方法

  • enablePostgis
  • disablePostgis

模型

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

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

use Illuminate\Database\Eloquent\Model;
use Shaozeming\LumenPostgis\Eloquent\PostgisTrait;
use Shaozeming\LumenPostgis\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->geom = new GeomPoint(37.422009, -122.084047);  //这个可以执行成功实现ORM操作gis几何数据。
$location1->save();

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

可用的几何类

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