PHP code example of karomap / laravel-geo

1. Go to this page and download the library: Download karomap/laravel-geo 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/ */

    

karomap / laravel-geo example snippets


Illuminate\Database\DatabaseServiceProvider::class,

Karomap\GeoLaravel\DatabaseServiceProvider::class

'GeoModel'      => Karomap\GeoLaravel\Eloquent\Model::class,



use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use Karomap\GeoLaravel\Database\Schema\Blueprint; // Replace "Illuminate\Database\Schema\Blueprint"

class CreateNationsTable extends Migration {
    public function up() {
        Schema::create('nations', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->polygon('national_bounds');
            $table->point('capital');
            $table->multipolygon('regions_bounds');
            $table->multipoint('regions_capitals');
            $table->linestring('highway');
        });
    }
}

 namespace App;

use Karomap\GeoLaravel\Eloquent\Model as GeoModel;

class Country extends GeoModel
{
    protected $table = "countries";

    protected $geometries = [
        "polygons" =>   ['national_bounds'],
        "points" => ['capital'],
        "multipolygons" => ['regions_bounds'],
        "multipoints" => ['regions_capitals'],
        "linestrings" => ['highway']
    ];
}


use Karomap\PHPOGC\DataTypes\Point as Point;
use Karomap\PHPOGC\DataTypes\Linestring as Linestring;
use Karomap\PHPOGC\DataTypes\Polygon as Polygon;

$rome = new Point(41.9102415,12.3959149);
$milan = new Point(45.4628328,9.1076927);
$naples = new Point(40.8540943,14.1765626);
$regions_capital = new MultiPoint([$rome, $milan, $naples, ....]);
$italy_bounds = new Polygon([new LineString(getPointArrayOfItalianBounds())]);
$lazio = new LineString(getPointArrayOfLazioBounds());
$campania = new LineString(getPointArrayOfCampaniaBounds());
$lombardia = new LineString(getPointArrayOfLombardiaBounds());
$molise = new LineString(getPointArrayOfMoliseBounds()); # raise MoliseNotFoundException
$regions_bounds = new MultiPolygon([$lazio, $campania, $lombardia, ....]);
$a1 = new LineString(getPointArrayOfA1());

$italy = Country::create([
    'name' => 'Italy',
    'capital' => $rome,
    'national_bounds' => $italy_bounds,
    'regions_bounds' => $regions_bounds,
    'regions_capitals' => $regions_capital,
    'highway' => $a1
]);

$italy = Country::whereName('Italy')->first();
echo get_class($italy->capital); // Karomap\PHPOGC\DataTypes\Point
echo get_class($italy->national_bounds); // Karomap\PHPOGC\DataTypes\Polygon
echo get_class($italy->regions_bounds); // Karomap\PHPOGC\DataTypes\Polygon
echo get_class($italy->regions_capitals); // Karomap\PHPOGC\DataTypes\MultiPoint
echo get_class($italy->highway); // Karomap\PHPOGC\DataTypes\LineString