Point(float $latitude, float $longitude, int $srid = 0)
- MySQL PointMultiPoint(Point[] | Collection<Point> $geometries, int $srid = 0)
- MySQL MultiPointLineString(Point[] | Collection<Point> $geometries, int $srid = 0)
- MySQL LineStringMultiLineString(LineString[] | Collection<LineString> $geometries, int $srid = 0)
- MySQL MultiLineStringPolygon(LineString[] | Collection<LineString> $geometries, int $srid = 0)
- MySQL PolygonMultiPolygon(Polygon[] | Collection<Polygon> $geometries, int $srid = 0)
- MySQL MultiPolygonGeometryCollection(Geometry[] | Collection<Geometry> $geometries, int $srid = 0)
- MySQL GeometryCollection
Geometry classes can be also created by these static methods:
fromArray(array $geometry)
- Creates a geometry object from a GeoJSON array.fromJson(string $geoJson, int $srid = 0)
- Creates a geometry object from a GeoJSON string.fromWkt(string $wkt, int $srid = 0)
- Creates a geometry object from a WKT.fromWkb(string $wkb, int $srid = 0)
- Creates a geometry object from a WKB.
-
toArray()
- Serializes the geometry object into a GeoJSON associative array. -
toJson()
- Serializes the geometry object into an GeoJSON string. -
toFeatureCollectionJson()
- Serializes the geometry object into an GeoJSON's FeatureCollection string. -
toWkt()
- Serializes the geometry object into a WKT. -
toWkb()
- Serializes the geometry object into a WKB. -
getCoordinates()
- Returns the coordinates of the geometry object. -
toSqlExpression(ConnectionInterface $connection)
- Serializes the geometry object into an SQL query. In addition,GeometryCollection
also has these functions: -
getGeometries()
- Returns a geometry array. Can be used withArrayAccess
as well.
$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
- withDistance
- whereDistance
- orderByDistance
- withDistanceSphere
- whereDistanceSphere
- orderByDistanceSphere
- whereWithin
- whereNotWithin
- whereContains
- whereNotContains
- whereTouches
- whereIntersects
- whereCrosses
- whereDisjoint
- whereEquals
- whereSrid
Retrieves the distance between 2 geometry objects. Uses ST_Distance.
parameter name | type | default |
---|---|---|
$column |
string |
|
$geometryOrColumn |
Geometry \ string |
|
$alias |
string |
'distance' |
Example
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
Filters records by distance. Uses ST_Distance.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
$operator |
string |
$value |
int \ float |
Example
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
Orders records by distance. Uses ST_Distance.
parameter name | type | default |
---|---|---|
$column |
string |
|
$geometryOrColumn |
Geometry \ string |
|
$direction |
string |
'asc' |
Example
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
Retrieves the spherical distance between 2 geometry objects. Uses ST_Distance_Sphere.
parameter name | type | default |
---|---|---|
$column |
string |
|
$geometryOrColumn |
Geometry \ string |
|
$alias |
string |
'distance' |
Example
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
Filters records by spherical distance. Uses ST_Distance_Sphere.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
$operator |
string |
$value |
int \ float |
Example
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
Orders records by spherical distance. Uses ST_Distance_Sphere.
parameter name | type | default |
---|---|---|
$column |
string |
|
$geometryOrColumn |
Geometry \ string |
|
$direction |
string |
'asc' |
Example
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
Filters records by the ST_Within function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Within function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Contains function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Contains function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Touches function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Intersects function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Crosses function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Disjoint function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
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
Filters records by the ST_Equal function.
parameter name | type |
---|---|
$column |
string |
$geometryOrColumn |
Geometry \ string |
Example
Place::create(['location' => new Point(0, 0, 4326)]);
Place::query()
->whereEquals('location', new Point(0, 0, 4326))
->exists(); // true
Filters records by the ST_Srid function.
parameter name | type |
---|---|
$column |
string |
$operator |
string |
$value |
int |
Example
Place::create(['location' => new Point(0, 0, 4326)]);
Place::query()
->whereSrid('location', '=', 4326)
->exists(); // true