PHP code example of shabushabu / laravel-postgis

1. Go to this page and download the library: Download shabushabu/laravel-postgis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shabushabu / laravel-postgis example snippets


use ShabuShabu\PostGIS\Expressions\As;
use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\PostGIS\Expressions\Enums\Option;

Track::query()
    ->select(new Alias(new As\GeoJSON('geom', 6, Option::bbox), 'json'))
    ->where('trail_id', 27)
    ->value('json');

use ShabuShabu\PostGIS\Expressions\As;
use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\PostGIS\Expressions\Casts\AsJson;
use ShabuShabu\PostGIS\Expressions\Helpers\JsonAgg;
use ShabuShabu\PostGIS\Expressions\Helpers\JsonBuildObject;

DB::query()
    ->select([
        new Alias(new JsonBuildObject([
            'type'     => 'FeatureCollection',
            'features' => new JsonAgg(new AsJson(new As\GeoJson('t.*', null, null)))
        ]), 'features')
    ])
    ->from(
        Track::query()->select(['geom', 'id', 'title']), 't'
    );

use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\PostGIS\Expressions\Collect;
use ShabuShabu\PostGIS\Expressions\Simplify;
use ShabuShabu\PostGIS\Expressions\DumpPoints;
use ShabuShabu\PostGIS\Expressions\Position\Elevation;
use ShabuShabu\PostGIS\Expressions\Position\Timestamp;

DB::query()->select([
    new Alias(new Elevation('geom'), 'x'),
    new Alias(new Timestamp('geom'), 'y'),
])->from(
    Track::query()->select(
        new Alias(new DumpPoints(new Simplify(new Collect('geom'), 0.15)), 'geom')
    )->where('trail_id', 27), 't'
);

use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\PostGIS\Expressions\Collect;
use ShabuShabu\PostGIS\Expressions\Envelope;
use ShabuShabu\PostGIS\Expressions\Box\TwoD;
use ShabuShabu\PostGIS\Expressions\Math\Round;
use ShabuShabu\PostGIS\Expressions\Casts\AsJson;
use ShabuShabu\PostGIS\Expressions\Casts\AsNumeric;
use ShabuShabu\PostGIS\Expressions\Helpers\MakeArray;
use ShabuShabu\PostGIS\Expressions\Helpers\ArrayToJson;
use ShabuShabu\PostGIS\Expressions\Position\MinLatitude;
use ShabuShabu\PostGIS\Expressions\Position\MaxLatitude;
use ShabuShabu\PostGIS\Expressions\Position\MinLongitude;
use ShabuShabu\PostGIS\Expressions\Position\MaxLongitude;

DB::query()
    ->select([
        new Alias(new AsJson(new ArrayToJson(new MakeArray([
            new Round(new AsNumeric(new MinLongitude('bbox')), 9),
            new Round(new AsNumeric(new MinLatitude('bbox')), 9),
            new Round(new AsNumeric(new MaxLongitude('bbox')), 9),
            new Round(new AsNumeric(new MaxLatitude('bbox')), 9),
        ]))), 'bbox')
    ])
    ->from(
        Track::query()->select([
            new Alias(new TwoD(new Envelope(new Collect('geom'))), 'bbox')
        ]), 't'
    );

use ShabuShabu\PostGIS\Expressions\SetSRID;
use ShabuShabu\PostGIS\Expressions\Position\MakePoint;

Schema::create('locations', static function (Blueprint $table) {
    // all the other table columns...
    
    $table->decimal('lat', 10, 6)->nullable();
    $table->decimal('lng', 10, 6)->nullable();
    $table
        ->geometry('geom', 'point', 4326)
        ->storedAs(new SetSRID(new MakePoint('lng', 'lat'), 4326));
});

use ShabuShabu\PostGIS\Expressions\Centroid;

Schema::create('countries', static function (Blueprint $table) {
    // all the other table columns...
   
    $table->geometry('geom', 'multipolygon', 4326);
    $table
        ->geometry('center', 'point', 4326)
        ->storedAs(new Centroid('geom'));
});

use ShabuShabu\PostGIS\Expressions\Area;
use Tpetry\QueryExpressions\Value\Value;
use ShabuShabu\PostGIS\Expressions\Math\Round;
use ShabuShabu\PostGIS\Expressions\Casts\AsNumeric;
use ShabuShabu\PostGIS\Expressions\Casts\AsGeography;
use Tpetry\QueryExpressions\Operator\Arithmetic\Divide;

Schema::create('provinces', static function (Blueprint $table) {
    // all the other table columns...
   
    $table->geometry('geom', 'multipolygon', 4326);
    $table
        ->integer('area_km2')
        ->storedAs(new Round(
            new AsNumeric(
                new Divide(
                    new Area(new AsGeography('geom')),
                    new Value(1e+6)
                )
            )
        ));
});

use Brick\Geo\Point;
use ShabuShabu\PostGIS\Casts\Geometry;
use Illuminate\Database\Eloquent\Model;

class Location extends Model
{
    // ...
    
    protected function casts(): array
    {
        return [
            'point' => Geometry::using(Point::class),
        ];
    }
}

use Brick\Geo\Polygon;
use ShabuShabu\PostGIS\Geometry;

echo app(Geometry::class)->area(
    Polygon::fromText('POLYGON ((0 0, 0 3, 3 3, 0 0))')
);

Gate::define(
    'access-tile-server',
    static fn (User $user) => $user->is_admin,
);



declare(strict_types=1);

namespace App\Services\MVT;

use BackedEnum;
use App\Models\Country;
use ShabuShabu\PostGIS\Servers\Tiles\Source;
use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\PostGIS\Expressions\Simplify;
use Illuminate\Contracts\Database\Query\Builder;

class Countries extends Source
{
    public function name(): string | BackedEnum
    {
        return 'countries';
    }

    public function query(): Builder
    {
        return Country::query()->select([
            'id',
            new Alias(new Simplify('geom', 0.1), 'geom'),
            new Alias('name', 'title'),
            'code',
        ]);
    }

    public function columns(): array
    {
        return ['title', 'code'];
    }
}

return [
    'tiles' => [
        // other values...
        
        'sources' => [
            \App\Services\MVT\Countries::class,
        ],
        
        // other values...
    ],   
];

Gate::define(
    'access-feature-server',
    static fn (User $user) => $user->is_admin,
);

return [
    new Alias('continent_id', 'continent'),
    'name',
    'description',
    'slug',
];

Gate::define(
    'access-collection-server',
    static fn (User $user) => $user->is_admin,
);



declare(strict_types=1);

namespace App\Services\Features;

use BackedEnum;
use App\Models\Country;
use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\PostGIS\Expressions\Simplify;
use Illuminate\Contracts\Database\Query\Builder;
use ShabuShabu\PostGIS\Servers\Features\Collection;

class Countries extends Collection
{
    public function name(): string | BackedEnum
    {
        return 'countries';
    }

    public function query(): Builder
    {
        return Country::query()->select([
            new Alias(new Simplify('geom', 0.1), 'geom'),
            'name',
            'code',
        ]);
    }
}

return [
    'features' => [
        // other values...
        
        'sources' => [
            \App\Services\Features\Countries::class,
        ],
        
        // other values...
    ],   
];