laravel-spatial

API

Available geometry classes

Geometry classes can be also created by these static methods:

Available geometry class methods

$geometryCollection = new GeometryCollection([
        new Polygon([
            new LineString([
                new Point(0, 180),
                new Point(1, 179),
                new Point(2, 178),
                new Point(3, 177),
                new Point(0, 180),
            ]),
        ]),
        new Point(0, 180),
    ]),
]);

echo $geometryCollection->getGeometries()[1]->latitude; // 0
// or access as an array:
echo $geometryCollection[1]->latitude; // 0

Available Enums

Spatial reference identifiers (SRID) identify the type of coordinate system to use.

An enum is provided with the following values:

Identifier Value Description
Srid::WGS84 4326 Geographic coordinate system
Srid::WEB_MERCATOR 3857 Mercator coordinate system

Available spatial scopes

withDistance

Retrieves the distance between 2 geometry objects. Uses ST_Distance.

parameter name type default
$column Geometry \ string  
$geometryOrColumn Geometry \ string  
$alias string 'distance'
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); $placeWithDistance = Place::query() ->withDistance('location', new Point(1, 1, 4326)) ->first(); echo $placeWithDistance->distance; // 156897.79947260793 // when using alias: $placeWithDistance = Place::query() ->withDistance('location', new Point(1, 1, 4326), 'distance_in_meters') ->first(); echo $placeWithDistance->distance_in_meters; // 156897.79947260793 ```

whereDistance

Filters records by distance. Uses ST_Distance.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
$operator string
$value int \ float
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::create(['location' => new Point(50, 50, 4326)]); $placesCountWithinDistance = Place::query() ->whereDistance('location', new Point(1, 1, 4326), '<', 160000) ->count(); echo $placesCountWithinDistance; // 1 ```

orderByDistance

Orders records by distance. Uses ST_Distance.

parameter name type default
$column Geometry \ string  
$geometryOrColumn Geometry \ string  
$direction string 'asc'
Example ```php Place::create([ 'name' => 'first', 'location' => new Point(0, 0, 4326), ]); Place::create([ 'name' => 'second', 'location' => new Point(50, 50, 4326), ]); $places = Place::query() ->orderByDistance('location', new Point(1, 1, 4326), 'desc') ->get(); echo $places[0]->name; // second echo $places[1]->name; // first ```

withDistanceSphere

Retrieves the spherical distance between 2 geometry objects. Uses ST_Distance_Sphere.

parameter name type default
$column Geometry \ string  
$geometryOrColumn Geometry \ string  
$alias string 'distance'
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); $placeWithDistance = Place::query() ->withDistanceSphere('location', new Point(1, 1, 4326)) ->first(); echo $placeWithDistance->distance; // 157249.59776850493 // when using alias: $placeWithDistance = Place::query() ->withDistanceSphere('location', new Point(1, 1, 4326), 'distance_in_meters') ->first(); echo $placeWithDistance->distance_in_meters; // 157249.59776850493 ```

whereDistanceSphere

Filters records by spherical distance. Uses ST_Distance_Sphere.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
$operator string
$value int \ float
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::create(['location' => new Point(50, 50, 4326)]); $placesCountWithinDistance = Place::query() ->whereDistanceSphere('location', new Point(1, 1, 4326), '<', 160000) ->count(); echo $placesCountWithinDistance; // 1 ```

orderByDistanceSphere

Orders records by spherical distance. Uses ST_Distance_Sphere.

parameter name type default
$column Geometry \ string  
$geometryOrColumn Geometry \ string  
$direction string 'asc'
Example ```php Place::create([ 'name' => 'first', 'location' => new Point(0, 0, 4326), ]); Place::create([ 'name' => 'second', 'location' => new Point(100, 100, 4326), ]); $places = Place::query() ->orderByDistanceSphere('location', new Point(1, 1, 4326), 'desc') ->get(); echo $places[0]->name; // second echo $places[1]->name; // first ```

whereWithin

Filters records by the ST_Within function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereWithin('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}')) ->exists(); // true ```

whereNotWithin

Filters records by the ST_Within function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereNotWithin('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}')) ->exists(); // false ```

whereContains

Filters records by the ST_Contains function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['area' => Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'),]); Place::query() ->whereContains('area', new Point(0, 0, 4326)) ->exists(); // true ```

whereNotContains

Filters records by the ST_Contains function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['area' => Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'),]); Place::query() ->whereNotContains('area', new Point(0, 0, 4326)) ->exists(); // false ```

whereTouches

Filters records by the ST_Touches function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereTouches('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[0,-1],[0,0],[-1,0],[-1,-1]]]}')) ->exists(); // true ```

whereIntersects

Filters records by the ST_Intersects function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereIntersects('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}')) ->exists(); // true ```

whereCrosses

Filters records by the ST_Crosses function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['line_string' => LineString::fromJson('{"type":"LineString","coordinates":[[0,0],[2,0]]}')]); Place::query() ->whereCrosses('line_string', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}')) ->exists(); // true ```

whereDisjoint

Filters records by the ST_Disjoint function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereDisjoint('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[-0.5,-1],[-0.5,-0.5],[-1,-0.5],[-1,-1]]]}')) ->exists(); // true ```

whereEquals

Filters records by the ST_Equal function.

parameter name type
$column Geometry \ string
$geometryOrColumn Geometry \ string
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereEquals('location', new Point(0, 0, 4326)) ->exists(); // true ```

whereSrid

Filters records by the ST_Srid function.

parameter name type
$column Geometry \ string
$operator string
$value int
Example ```php Place::create(['location' => new Point(0, 0, 4326)]); Place::query() ->whereSrid('location', '=', 4326) ->exists(); // true ```