Yii2-extension to work with postgis data

1.0.0 2024-10-10 12:37 UTC

This package is auto-updated.

Last update: 2024-10-11 17:15:34 UTC


README

Extension for working with Postgis. As intermediate format used Geo Json.

Installing

The preferred way to install this extension is through Composer.

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

GeometryBehavior

Converts coordinates array to SQL expression for saving in postgis binary format before insert/update and from postgis binary to array after find.

<?php

use yii\db\ActiveRecord;
use Yii2\Extension\Postgis\Behaviors\GeometryBehavior;

class MyModel extends ActiveRecord
{

    // ...
    
    public function behaviors()
    {
        return [
            [
                'class' => GeometryBehavior::class,
                '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::class,
                '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

Generate SQL expression before insert/update based on geometry and radius

<?php

use yii\db\ActiveRecord;
use Yii2\Extension\Postgis\Behaviors\GeometryBehavior;
use Yii2\Extension\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

Extends ActiveQuery for working with Postgis data.

<?php

class MyQuery extends \yii\db\ActiveQuery
{
    use \Yii2\Extension\Postgis\Db\PostgisQueryTrait;
    
    // ...
}

// ...

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

GeoJsonHelper

Helper for working with Geo Json

CREDITS

This extension was forked and lives here to keep going great work in keeping with Yii2 community open source spirit. Find Original code at https://github.com/nanson/yii2-postgis