2017 © Pedro Peláez
 

library laravel-postgis

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

image

phaza/laravel-postgis

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  • Wednesday, July 4, 2018
  • by phaza
  • Repository
  • 17 Watchers
  • 132 Stars
  • 80,649 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 73 Forks
  • 16 Open issues
  • 40 Versions
  • 13 % Grown

The README.md

Laravel Wrapper for PostgreSQL's Geo-Extension Postgis

Build Status, (*1)

DEPRECATED

Consider using: https://github.com/clickbar/laravel-magellan, (*2)

Features

  • Work with geometry classes instead of arrays.
$model->myPoint = new Point(1,2);  //lat, long
  • Adds helpers in migrations.
$table->polygon('myColumn');

Warning

This Package has been moved to a new owner and aims for Laravel 6/7/8/9 and PHP 7 support only soon!, (*3)

Replace all your references to the new namespace:, (*4)

MStaack\LaravelPostgis

Thanks to : - https://github.com/njbarrett - https://github.com/phaza - https://github.com/mirzap, (*5)

Fluent in Laravel Packages and Postgres/Postgis? Consider contributing! We are looking for anyone that wants to help out!, (*6)

Installation

  • Use 3.* for Laravel 5
composer require "mstaack/laravel-postgis:3.*"
  • Use 5.* for Laravel 6/7/8/9
composer require mstaack/laravel-postgis

For laravel >=5.5 that's all. This package supports Laravel new Package Discovery., (*7)

If you are using Laravel < 5.5, you also need to add the DatabaseServiceProvider to your config/app.php file., (*8)

'MStaack\LaravelPostgis\DatabaseServiceProvider',

Usage

To start, ensure you have PostGIS enabled in your database - you can do this in a Laravel migration or manually via SQL., (*9)

Enable PostGIS via a Laravel migration

You need to publish the migration to easily enable PostGIS:, (*10)

php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="migrations"

And then you run the migrations:, (*11)

php artisan migrate

These methods are safe to use and will only enable / disable the PostGIS extension if relevant - they won't cause an error if PostGIS is / isn't already enabled., (*12)

If you prefer, you can use the enablePostgis() method which will throw an error if PostGIS is already enabled, and the disablePostgis() method twhich will throw an error if PostGIS isn't enabled., (*13)

Enable PostGIS manually

Use an SQL client to connect to your database and run the following command:, (*14)

CREATE EXTENSION postgis;

To verify that PostGIS is enabled you can run:, (*15)

SELECT postgis_full_version();

Migrations

Now create a model with a migration by running, (*16)

php artisan make:model Location

If you don't want a model and just a migration run, (*17)

php artisan make:migration create_locations_table

Open the created migrations with your editor., (*18)

use Illuminate\Database\Migrations\Migration;
use MStaack\LaravelPostgis\Schema\Blueprint;

class CreateLocationsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('locations', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('address')->unique();
            $table->point('location'); // GEOGRAPHY POINT column with SRID of 4326 (these are the default values).
            $table->point('location2', 'GEOGRAPHY', 4326); // GEOGRAPHY POINT column with SRID of 4326 with optional parameters.
            $table->point('location3', 'GEOMETRY', 27700); // GEOMETRY column with SRID of 27700.
            $table->polygon('polygon'); // GEOGRAPHY POLYGON column with SRID of 4326.
            $table->polygon('polygon2', 'GEOMETRY', 27700); // GEOMETRY POLYGON column with SRID of 27700.
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('locations');
    }

}

Available blueprint geometries:, (*19)

  • point
  • multipoint
  • linestring
  • multilinestring
  • polygon
  • multipolygon
  • geometrycollection

other methods:, (*20)

  • enablePostgis
  • disablePostgis

Models

All models which are to be PostGis enabled must use the PostgisTrait., (*21)

You must also define an array called $postgisFields which defines what attributes/columns on your model are to be considered geometry objects. By default, all attributes are of type geography. If you want to use geometry with a custom SRID, you have to define an array called $postgisTypes. The keys of this assoc array must match the entries in $postgisFields (all missing keys default to geography), the values are assoc arrays, too. They must have two keys: geomtype which is either geography or geometry and srid which is the desired SRID. Note: Custom SRID is only supported for geometry, not geography., (*22)

use Illuminate\Database\Eloquent\Model;
use MStaack\LaravelPostgis\Eloquent\PostgisTrait;
use MStaack\LaravelPostgis\Geometries\Point;

class Location extends Model
{
    use PostgisTrait;

    protected $fillable = [
        'name',
        'address'
    ];

    protected $postgisFields = [
        'location',
        'location2',
        'location3',
        'polygon',
        'polygon2'
    ];

    protected $postgisTypes = [
        'location' => [
            'geomtype' => 'geography',
            'srid' => 4326
        ],
        'location2' => [
            'geomtype' => 'geography',
            'srid' => 4326
        ],
        'location3' => [
            'geomtype' => 'geometry',
            'srid' => 27700
        ],
        'polygon' => [
            'geomtype' => 'geography',
            'srid' => 4326
        ],
        'polygon2' => [
            'geomtype' => 'geometry',
            'srid' => 27700
        ]
    ]
}

$linestring = new LineString(
    [
        new Point(0, 0),
        new Point(0, 1),
        new Point(1, 1),
        new Point(1, 0),
        new Point(0, 0)
    ]
);

$location1 = new Location();
$location1->name = 'Googleplex';
$location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
$location1->location = new Point(37.422009, -122.084047);
$location1->location2 = new Point(37.422009, -122.084047);
$location1->location3 = new Point(37.422009, -122.084047);
$location1->polygon = new Polygon([$linestring]);
$location1->polygon2 = new Polygon([$linestring]);
$location1->save();

$location2 = Location::first();
$location2->location instanceof Point // true

Available geometry classes:, (*23)

  • Point
  • MultiPoint
  • LineString
  • MultiLineString
  • Polygon
  • MultiPolygon
  • GeometryCollection

Publishing config

A configuration file exists for overriding default values. To add to your project, run:, (*24)

php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="postgis"

Coordinate precision

The precision of stored/displayed coordinated can be customised in the config., (*25)

# config/postgis.php
return [
    ...
    'precision' => 6,
];

See http://wiki.gis.com/wiki/index.php/Decimal_degrees#Accuracy for more information, (*26)

The Versions

04/07 2018

dev-master

9999999-dev

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

04/07 2018

3.4

3.4.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

08/06 2018

3.3.2

3.3.2.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

20/11 2017

3.3.1

3.3.1.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

17/11 2017

dev-revert-90-patch-1

dev-revert-90-patch-1

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

23/08 2017

3.3

3.3.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

15/07 2017

3.2

3.2.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

23/05 2017

3.1.3

3.1.3.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

21/05 2017

3.1.2

3.1.2.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

21/05 2017

5.1.x-dev

5.1.9999999.9999999-dev

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

21/05 2017

2.19

2.19.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

16/01 2017

dev-add-query-scopes

dev-add-query-scopes

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

16/01 2017

3.1.1

3.1.1.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

26/07 2016

2.18

2.18.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

26/07 2016

dev-fix-for-5.1-connection

dev-fix-for-5.1-connection

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

21/05 2016

3.1

3.1.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza
by Nicholas Barrett

26/01 2016

2.17

2.17.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

04/01 2016

2.16

2.16.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

11/11 2015

2.15

2.15.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

21/09 2015

2.14

2.14.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

10/06 2015

2.13

2.13.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

10/06 2015

2.12

2.12.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

20/05 2015

2.11

2.11.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

08/05 2015

2.10

2.10.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

06/05 2015

2.9

2.9.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

29/04 2015

2.8

2.8.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

17/04 2015

2.7

2.7.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

17/04 2015

2.6

2.6.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

17/04 2015

2.5

2.5.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

17/04 2015

2.4

2.4.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

15/04 2015

2.3

2.3.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

13/04 2015

2.2

2.2.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

13/04 2015

2.1

2.1.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

13/04 2015

2.0

2.0.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

27/03 2015

1.2

1.2.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

24/03 2015

1.1

1.1.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

24/03 2015

1.0

1.0.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

22/03 2015

0.2

0.2.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza

21/03 2015

0.1

0.1.0.0

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Haza