PHP code example of clickbar / laravel-magellan
1. Go to this page and download the library: Download clickbar/laravel-magellan 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/ */
clickbar / laravel-magellan example snippets
$table->magellanPoint('location', 4326);
protected $casts = [
/** ... */
'location' => Point::class,
'bounds' => Box2D::class,
];
$point = Point::make(51.087, 8.76);
function make(float $x, float $y, ?float $z = null, ?float $m = null, ?int $srid = null): self
function makeGeodetic(float $latitude, float $longitude, ?float $altitude = null, ?float $m = null): self
$point = Point::makeGeodetic(51.087, 8.76);
json_encode($point); // returns GeoJson
// "{"type":"Point","coordinates":[8.76,51.087]}"
$parser = app(WKTParser::class);
$point = $parser->parse('SRID=4326;POINT (2, 2)');
$generator = new WKBGenerator();
$generator->generate($point);
// "0101000020E610000000000000000000400000000000000040"
class StorePortRequest extends FormRequest
{
use TransformsGeojsonGeometry;
public function rules(): array
{
return [
'name' => ['ction geometries(): array
{
return ['location'];
}
}
Schema::create('ports', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('country');
$table->magellanPoint('location');
$table->timestamps();
});
class Port extends Model
{
use HasFactory;
protected $guarded = [];
protected $casts = [
'location' => Point::class,
];
}
Port::create([
'name' => 'Magellan Home Port',
'country' => 'Germany',
'location' => Point::makeGeodetic(49.87108851299202, 8.625026485851762),
]);
$port->location = Point::makeGeodetic(55, 11);
$port->save();
// -- or --
Port::where('name', 'Magellan Home Port')
->update(['location' => Point::makeGeodetic(55, 11)]);
$point = Point::make(473054.9891044726, 5524365.310057224, srid: 25832);
Port::create([
'name' => 'Magellan Home Port',
'country' => 'Germany',
'location' => ST::transform($point, 4326),
]);
// -- or --
$port = Port::find(1);
$port->query()->update([
'location' => ST::transform(Point::make(473054.9891044726, 5524365.310057224, srid: 25832), 4326),
]);
$port = Port::first();
dd($port->location);
->where(ST::contains('location', 'polygon'), true)
use Clickbar\Magellan\Data\Geometries\Point;
use Clickbar\Magellan\Database\PostgisFunctions\ST;
$currentShipPosition = Point::makeGeodetic(50.107471773560114, 8.679861151457937);
$portsWithDistance = Port::select() // use select() because we want SELECT *, distance and not only the distance
->addSelect(ST::distanceSphere($currentShipPosition, 'location')->as('distance_to_ship'))
->get();
$currentShipPosition = Point::makeGeodetic(50.107471773560114, 8.679861151457937);
$portsWithDistance = Port::select()
->addSelect(ST::distanceSphere($currentShipPosition, 'location')->as('distance_to_ship'))
->where(ST::distanceSphere($currentShipPosition, 'location'), '<=', 50000)
->get();
$currentShipPosition = Point::makeGeodetic(50.107471773560114, 8.679861151457937);
$portsWithDistance = Port::select()
->addSelect(ST::distanceSphere($currentShipPosition, 'location')->as('distance_to_ship'))
->where(ST::distanceSphere($currentShipPosition, 'location'), '<=', 50000)
->orderBy(ST::distanceSphere($currentShipPosition, 'location'))
->get();
$hullsWithArea = Port::query()
->select([
'country',
ST::convexHull(ST::collect('location'))->as('hull'),
ST::area(ST::convexHull(ST::collect('location')))->as('area')
])
->groupBy('country')
->get();
->select(ST::distanceSphere($currentShipPosition, 'location')->as('distance_to_ship'))
//--> leads to SELECT ST_DistanceSphere(<<currentShipPosition, 'location') AS distance_to_ship
$bufferedPorts = Port::query()
->select(ST::buffer(new AsGeography('location'), 50)->as('buffered_location'))
->withCasts(['buffered_location' => Polygon::class])
->get();
$hullWithArea = Port::query()
->select([
'country',
ST::convexHull(ST::collect('location'))->as('hull'),
ST::area(ST::convexHull(ST::collect('location')))->as('area')
])
->groupBy('country')
->withMagellanCasts() /* <======= */
->first();
// ⬆️ instead of ⬇️
$hullWithArea = Port::query()
->select([
'country',
ST::convexHull(ST::collect('location'))->as('hull'),
ST::area(ST::convexHull(ST::collect('location')))->as('area')
])
->groupBy('country')
->withCasts(['hull' => Polygon::class]) /* <======= */
->first();
bash
php artisan vendor:publish --tag="magellan-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="magellan-config"