Skip to content

[11.x] Make spatial types consistent #49634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 9 additions & 83 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1348,100 +1348,26 @@ public function macAddress($column = 'mac_address')
* Create a new geometry column on the table.
*
* @param string $column
* @param string|null $subtype
* @param int $srid
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function geometry($column)
public function geometry($column, $subtype = null, $srid = 0)
{
return $this->addColumn('geometry', $column);
return $this->addColumn('geometry', $column, compact('subtype', 'srid'));
}

/**
* Create a new point column on the table.
* Create a new geography column on the table.
*
* @param string $column
* @param int|null $srid
* @param string|null $subtype
* @param int $srid
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function point($column, $srid = null)
public function geography($column, $subtype = null, $srid = 4326)
{
return $this->addColumn('point', $column, compact('srid'));
}

/**
* Create a new linestring column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function lineString($column)
{
return $this->addColumn('linestring', $column);
}

/**
* Create a new polygon column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function polygon($column)
{
return $this->addColumn('polygon', $column);
}

/**
* Create a new geometrycollection column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function geometryCollection($column)
{
return $this->addColumn('geometrycollection', $column);
}

/**
* Create a new multipoint column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function multiPoint($column)
{
return $this->addColumn('multipoint', $column);
}

/**
* Create a new multilinestring column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function multiLineString($column)
{
return $this->addColumn('multilinestring', $column);
}

/**
* Create a new multipolygon column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function multiPolygon($column)
{
return $this->addColumn('multipolygon', $column);
}

/**
* Create a new multipolygon column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function multiPolygonZ($column)
{
return $this->addColumn('multipolygonz', $column);
return $this->addColumn('geography', $column, compact('subtype', 'srid'));
}

/**
Expand Down
97 changes: 13 additions & 84 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MySqlGrammar extends Grammar
*/
protected $modifiers = [
'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable',
'Srid', 'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First',
'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First',
];

/**
Expand Down Expand Up @@ -1077,86 +1077,29 @@ protected function typeMacAddress(Fluent $column)
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeGeometry(Fluent $column)
protected function typeGeometry(Fluent $column)
{
return 'geometry';
}

/**
* Create the column definition for a spatial Point type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typePoint(Fluent $column)
{
return 'point';
}

/**
* Create the column definition for a spatial LineString type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeLineString(Fluent $column)
{
return 'linestring';
}

/**
* Create the column definition for a spatial Polygon type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typePolygon(Fluent $column)
{
return 'polygon';
}

/**
* Create the column definition for a spatial GeometryCollection type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeGeometryCollection(Fluent $column)
{
return 'geometrycollection';
}
$subtype = $column->subtype ? strtolower($column->subtype) : null;

/**
* Create the column definition for a spatial MultiPoint type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeMultiPoint(Fluent $column)
{
return 'multipoint';
}
if (! in_array($subtype, ['point', 'linestring', 'polygon', 'geometrycollection', 'multipoint', 'multilinestring', 'multipolygon'])) {
$subtype = null;
}

/**
* Create the column definition for a spatial MultiLineString type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeMultiLineString(Fluent $column)
{
return 'multilinestring';
return sprintf('%s%s',
$subtype ?? 'geometry',
$column->srid ? ' srid '.$column->srid : ''
);
}

/**
* Create the column definition for a spatial MultiPolygon type.
* Create the column definition for a spatial Geography type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeMultiPolygon(Fluent $column)
protected function typeGeography(Fluent $column)
{
return 'multipolygon';
return $this->typeGeometry($column);
}

/**
Expand Down Expand Up @@ -1377,20 +1320,6 @@ protected function modifyComment(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a SRID column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifySrid(Blueprint $blueprint, Fluent $column)
{
if (is_int($column->srid) && $column->srid > 0) {
return ' srid '.$column->srid;
}
}

/**
* Wrap a single string in keyword identifiers.
*
Expand Down
113 changes: 15 additions & 98 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,115 +1084,32 @@ protected function typeMacAddress(Fluent $column)
*/
protected function typeGeometry(Fluent $column)
{
return $this->formatPostGisType('geometry', $column);
}

/**
* Create the column definition for a spatial Point type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePoint(Fluent $column)
{
return $this->formatPostGisType('point', $column);
}

/**
* Create the column definition for a spatial LineString type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeLineString(Fluent $column)
{
return $this->formatPostGisType('linestring', $column);
}

/**
* Create the column definition for a spatial Polygon type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePolygon(Fluent $column)
{
return $this->formatPostGisType('polygon', $column);
}

/**
* Create the column definition for a spatial GeometryCollection type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeGeometryCollection(Fluent $column)
{
return $this->formatPostGisType('geometrycollection', $column);
}

/**
* Create the column definition for a spatial MultiPoint type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMultiPoint(Fluent $column)
{
return $this->formatPostGisType('multipoint', $column);
}

/**
* Create the column definition for a spatial MultiLineString type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
public function typeMultiLineString(Fluent $column)
{
return $this->formatPostGisType('multilinestring', $column);
}

/**
* Create the column definition for a spatial MultiPolygon type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMultiPolygon(Fluent $column)
{
return $this->formatPostGisType('multipolygon', $column);
}
if ($column->subtype) {
return sprintf('geometry(%s%s)',
strtolower($column->subtype),
$column->srid ? ','.$column->srid : ''
);
}

/**
* Create the column definition for a spatial MultiPolygonZ type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMultiPolygonZ(Fluent $column)
{
return $this->formatPostGisType('multipolygonz', $column);
return 'geometry';
}

/**
* Format the column definition for a PostGIS spatial type.
* Create the column definition for a spatial Geography type.
*
* @param string $type
* @param \Illuminate\Support\Fluent $column
* @return string
*/
private function formatPostGisType($type, Fluent $column)
protected function typeGeography(Fluent $column)
{
if ($column->isGeometry === null) {
return sprintf('geography(%s, %s)', $type, $column->projection ?? '4326');
}

if ($column->projection !== null) {
return sprintf('geometry(%s, %s)', $type, $column->projection);
if ($column->subtype) {
return sprintf('geography(%s%s)',
strtolower($column->subtype),
$column->srid ? ','.$column->srid : ''
);
}

return "geometry({$type})";
return 'geography';
}

/**
Expand Down
Loading