nanoson / yii2-postgis

用于处理Postgis数据的Yii2扩展

安装数: 40,116

依赖关系: 0

建议者: 0

安全: 0

星标: 18

关注者: 4

分支: 7

开放问题: 1

类型:yii2-extension

1.0.1 2017-02-15 17:09 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:37:30 UTC


README

用于与Postgis一起工作的扩展。使用Geo Json作为中间格式。

安装

安装此扩展的首选方法是使用Composer。

{
  "require": {
    "nanson/yii2-postgis": "*"
  }
}

GeometryBehavior

将坐标数组转换为SQL表达式以在插入/更新前保存到Postgis二进制格式,并在查找后从Postgis二进制格式转换为数组。

<?php

use yii\db\ActiveRecord;
use nanson\postgis\behaviors\GeometryBehavior;

class MyModel extends ActiveRecord
{

    // ...
    
    public function behaviors()
    {
        return [
            [
                'class' => GeometryBehavior::className(),
                'type' => GeometryBehavior::GEOMETRY_POINT,
                'attribute' => 'point',
                // explicitly set custom db connection if you do not want to use
                // static::getDb() or Yii::$app->getDb() connections
                'db' => 'db_custom'
            ],
            [
                'class' => GeometryBehavior::className(),
                'type' => GeometryBehavior::GEOMETRY_LINESTRING,
                'attribute' => 'line',
                // skip attribute if it was not selected as Geo Json (by PostgisQueryTrait), because it requires a separate query.
                'skipAfterFindPostgis' => true,
            ],
        ];
    }

    // ...

}

// ...

$model = new MyModel;

$model->point = [39.234, 54.456];
$model->line = [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]];

$model->save();

?>

StBufferBehavior

基于几何形状和半径在插入/更新前生成SQL表达式

<?php

use yii\db\ActiveRecord;
use nanson\postgis\behaviors\GeometryBehavior;
use nanson\postgis\behaviors\StBufferBehavior;

class MyModel extends ActiveRecord
{

    // ...
    
    public function behaviors()
    {
        return [
            [
                'class' => GeometryBehavior::className(),
                'attribute' => 'point',
                'type' => GeometryBehavior::GEOMETRY_POINT,
            ],
            [
                'class' => StBufferBehavior::className(),
                'attribute' => 'buffer',
                'attributeGeometry' => 'point',
                'attributeRadius' => 'radius',
            ],
        ];
    }

    // ...

}

// ...

$model = new MyModel;

$model->point = [39.234, 54.456];
$model->radius = 5;

// It will be save St_Buffer for `$model->point` with `$model->radius` in `$model->buffer`
$model->save();

?>

PostgisQueryTrait

扩展ActiveQuery以用于处理Postgis数据。

<?php

class MyQuery extends \yii\db\ActiveQuery
{
    use \nanson\postgis\db\PostgisQueryTrait;
    
    // ...
}

// ...

class MyModel extends \yii\db\ActiveRecord
{
    public static function find(){
        return \Yii::createObject([
            'class' => MyQuery::className(),
        ], [get_called_class()]);
    }
}
?>

GeoJsonHelper

用于处理Geo Json的辅助工具