Skip to content

Commit 8317461

Browse files
fix spatial types (#49634)
1 parent c9cc5aa commit 8317461

10 files changed

+112
-572
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,100 +1348,26 @@ public function macAddress($column = 'mac_address')
13481348
* Create a new geometry column on the table.
13491349
*
13501350
* @param string $column
1351+
* @param string|null $subtype
1352+
* @param int $srid
13511353
* @return \Illuminate\Database\Schema\ColumnDefinition
13521354
*/
1353-
public function geometry($column)
1355+
public function geometry($column, $subtype = null, $srid = 0)
13541356
{
1355-
return $this->addColumn('geometry', $column);
1357+
return $this->addColumn('geometry', $column, compact('subtype', 'srid'));
13561358
}
13571359

13581360
/**
1359-
* Create a new point column on the table.
1361+
* Create a new geography column on the table.
13601362
*
13611363
* @param string $column
1362-
* @param int|null $srid
1364+
* @param string|null $subtype
1365+
* @param int $srid
13631366
* @return \Illuminate\Database\Schema\ColumnDefinition
13641367
*/
1365-
public function point($column, $srid = null)
1368+
public function geography($column, $subtype = null, $srid = 4326)
13661369
{
1367-
return $this->addColumn('point', $column, compact('srid'));
1368-
}
1369-
1370-
/**
1371-
* Create a new linestring column on the table.
1372-
*
1373-
* @param string $column
1374-
* @return \Illuminate\Database\Schema\ColumnDefinition
1375-
*/
1376-
public function lineString($column)
1377-
{
1378-
return $this->addColumn('linestring', $column);
1379-
}
1380-
1381-
/**
1382-
* Create a new polygon column on the table.
1383-
*
1384-
* @param string $column
1385-
* @return \Illuminate\Database\Schema\ColumnDefinition
1386-
*/
1387-
public function polygon($column)
1388-
{
1389-
return $this->addColumn('polygon', $column);
1390-
}
1391-
1392-
/**
1393-
* Create a new geometrycollection column on the table.
1394-
*
1395-
* @param string $column
1396-
* @return \Illuminate\Database\Schema\ColumnDefinition
1397-
*/
1398-
public function geometryCollection($column)
1399-
{
1400-
return $this->addColumn('geometrycollection', $column);
1401-
}
1402-
1403-
/**
1404-
* Create a new multipoint column on the table.
1405-
*
1406-
* @param string $column
1407-
* @return \Illuminate\Database\Schema\ColumnDefinition
1408-
*/
1409-
public function multiPoint($column)
1410-
{
1411-
return $this->addColumn('multipoint', $column);
1412-
}
1413-
1414-
/**
1415-
* Create a new multilinestring column on the table.
1416-
*
1417-
* @param string $column
1418-
* @return \Illuminate\Database\Schema\ColumnDefinition
1419-
*/
1420-
public function multiLineString($column)
1421-
{
1422-
return $this->addColumn('multilinestring', $column);
1423-
}
1424-
1425-
/**
1426-
* Create a new multipolygon column on the table.
1427-
*
1428-
* @param string $column
1429-
* @return \Illuminate\Database\Schema\ColumnDefinition
1430-
*/
1431-
public function multiPolygon($column)
1432-
{
1433-
return $this->addColumn('multipolygon', $column);
1434-
}
1435-
1436-
/**
1437-
* Create a new multipolygon column on the table.
1438-
*
1439-
* @param string $column
1440-
* @return \Illuminate\Database\Schema\ColumnDefinition
1441-
*/
1442-
public function multiPolygonZ($column)
1443-
{
1444-
return $this->addColumn('multipolygonz', $column);
1370+
return $this->addColumn('geography', $column, compact('subtype', 'srid'));
14451371
}
14461372

14471373
/**

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MySqlGrammar extends Grammar
1818
*/
1919
protected $modifiers = [
2020
'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable',
21-
'Srid', 'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First',
21+
'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First',
2222
];
2323

2424
/**
@@ -1077,86 +1077,29 @@ protected function typeMacAddress(Fluent $column)
10771077
* @param \Illuminate\Support\Fluent $column
10781078
* @return string
10791079
*/
1080-
public function typeGeometry(Fluent $column)
1080+
protected function typeGeometry(Fluent $column)
10811081
{
1082-
return 'geometry';
1083-
}
1084-
1085-
/**
1086-
* Create the column definition for a spatial Point type.
1087-
*
1088-
* @param \Illuminate\Support\Fluent $column
1089-
* @return string
1090-
*/
1091-
public function typePoint(Fluent $column)
1092-
{
1093-
return 'point';
1094-
}
1095-
1096-
/**
1097-
* Create the column definition for a spatial LineString type.
1098-
*
1099-
* @param \Illuminate\Support\Fluent $column
1100-
* @return string
1101-
*/
1102-
public function typeLineString(Fluent $column)
1103-
{
1104-
return 'linestring';
1105-
}
1106-
1107-
/**
1108-
* Create the column definition for a spatial Polygon type.
1109-
*
1110-
* @param \Illuminate\Support\Fluent $column
1111-
* @return string
1112-
*/
1113-
public function typePolygon(Fluent $column)
1114-
{
1115-
return 'polygon';
1116-
}
1117-
1118-
/**
1119-
* Create the column definition for a spatial GeometryCollection type.
1120-
*
1121-
* @param \Illuminate\Support\Fluent $column
1122-
* @return string
1123-
*/
1124-
public function typeGeometryCollection(Fluent $column)
1125-
{
1126-
return 'geometrycollection';
1127-
}
1082+
$subtype = $column->subtype ? strtolower($column->subtype) : null;
11281083

1129-
/**
1130-
* Create the column definition for a spatial MultiPoint type.
1131-
*
1132-
* @param \Illuminate\Support\Fluent $column
1133-
* @return string
1134-
*/
1135-
public function typeMultiPoint(Fluent $column)
1136-
{
1137-
return 'multipoint';
1138-
}
1084+
if (! in_array($subtype, ['point', 'linestring', 'polygon', 'geometrycollection', 'multipoint', 'multilinestring', 'multipolygon'])) {
1085+
$subtype = null;
1086+
}
11391087

1140-
/**
1141-
* Create the column definition for a spatial MultiLineString type.
1142-
*
1143-
* @param \Illuminate\Support\Fluent $column
1144-
* @return string
1145-
*/
1146-
public function typeMultiLineString(Fluent $column)
1147-
{
1148-
return 'multilinestring';
1088+
return sprintf('%s%s',
1089+
$subtype ?? 'geometry',
1090+
$column->srid ? ' srid '.$column->srid : ''
1091+
);
11491092
}
11501093

11511094
/**
1152-
* Create the column definition for a spatial MultiPolygon type.
1095+
* Create the column definition for a spatial Geography type.
11531096
*
11541097
* @param \Illuminate\Support\Fluent $column
11551098
* @return string
11561099
*/
1157-
public function typeMultiPolygon(Fluent $column)
1100+
protected function typeGeography(Fluent $column)
11581101
{
1159-
return 'multipolygon';
1102+
return $this->typeGeometry($column);
11601103
}
11611104

11621105
/**
@@ -1377,20 +1320,6 @@ protected function modifyComment(Blueprint $blueprint, Fluent $column)
13771320
}
13781321
}
13791322

1380-
/**
1381-
* Get the SQL for a SRID column modifier.
1382-
*
1383-
* @param \Illuminate\Database\Schema\Blueprint $blueprint
1384-
* @param \Illuminate\Support\Fluent $column
1385-
* @return string|null
1386-
*/
1387-
protected function modifySrid(Blueprint $blueprint, Fluent $column)
1388-
{
1389-
if (is_int($column->srid) && $column->srid > 0) {
1390-
return ' srid '.$column->srid;
1391-
}
1392-
}
1393-
13941323
/**
13951324
* Wrap a single string in keyword identifiers.
13961325
*

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,115 +1084,32 @@ protected function typeMacAddress(Fluent $column)
10841084
*/
10851085
protected function typeGeometry(Fluent $column)
10861086
{
1087-
return $this->formatPostGisType('geometry', $column);
1088-
}
1089-
1090-
/**
1091-
* Create the column definition for a spatial Point type.
1092-
*
1093-
* @param \Illuminate\Support\Fluent $column
1094-
* @return string
1095-
*/
1096-
protected function typePoint(Fluent $column)
1097-
{
1098-
return $this->formatPostGisType('point', $column);
1099-
}
1100-
1101-
/**
1102-
* Create the column definition for a spatial LineString type.
1103-
*
1104-
* @param \Illuminate\Support\Fluent $column
1105-
* @return string
1106-
*/
1107-
protected function typeLineString(Fluent $column)
1108-
{
1109-
return $this->formatPostGisType('linestring', $column);
1110-
}
1111-
1112-
/**
1113-
* Create the column definition for a spatial Polygon type.
1114-
*
1115-
* @param \Illuminate\Support\Fluent $column
1116-
* @return string
1117-
*/
1118-
protected function typePolygon(Fluent $column)
1119-
{
1120-
return $this->formatPostGisType('polygon', $column);
1121-
}
1122-
1123-
/**
1124-
* Create the column definition for a spatial GeometryCollection type.
1125-
*
1126-
* @param \Illuminate\Support\Fluent $column
1127-
* @return string
1128-
*/
1129-
protected function typeGeometryCollection(Fluent $column)
1130-
{
1131-
return $this->formatPostGisType('geometrycollection', $column);
1132-
}
1133-
1134-
/**
1135-
* Create the column definition for a spatial MultiPoint type.
1136-
*
1137-
* @param \Illuminate\Support\Fluent $column
1138-
* @return string
1139-
*/
1140-
protected function typeMultiPoint(Fluent $column)
1141-
{
1142-
return $this->formatPostGisType('multipoint', $column);
1143-
}
1144-
1145-
/**
1146-
* Create the column definition for a spatial MultiLineString type.
1147-
*
1148-
* @param \Illuminate\Support\Fluent $column
1149-
* @return string
1150-
*/
1151-
public function typeMultiLineString(Fluent $column)
1152-
{
1153-
return $this->formatPostGisType('multilinestring', $column);
1154-
}
1155-
1156-
/**
1157-
* Create the column definition for a spatial MultiPolygon type.
1158-
*
1159-
* @param \Illuminate\Support\Fluent $column
1160-
* @return string
1161-
*/
1162-
protected function typeMultiPolygon(Fluent $column)
1163-
{
1164-
return $this->formatPostGisType('multipolygon', $column);
1165-
}
1087+
if ($column->subtype) {
1088+
return sprintf('geometry(%s%s)',
1089+
strtolower($column->subtype),
1090+
$column->srid ? ','.$column->srid : ''
1091+
);
1092+
}
11661093

1167-
/**
1168-
* Create the column definition for a spatial MultiPolygonZ type.
1169-
*
1170-
* @param \Illuminate\Support\Fluent $column
1171-
* @return string
1172-
*/
1173-
protected function typeMultiPolygonZ(Fluent $column)
1174-
{
1175-
return $this->formatPostGisType('multipolygonz', $column);
1094+
return 'geometry';
11761095
}
11771096

11781097
/**
1179-
* Format the column definition for a PostGIS spatial type.
1098+
* Create the column definition for a spatial Geography type.
11801099
*
1181-
* @param string $type
11821100
* @param \Illuminate\Support\Fluent $column
11831101
* @return string
11841102
*/
1185-
private function formatPostGisType($type, Fluent $column)
1103+
protected function typeGeography(Fluent $column)
11861104
{
1187-
if ($column->isGeometry === null) {
1188-
return sprintf('geography(%s, %s)', $type, $column->projection ?? '4326');
1189-
}
1190-
1191-
if ($column->projection !== null) {
1192-
return sprintf('geometry(%s, %s)', $type, $column->projection);
1105+
if ($column->subtype) {
1106+
return sprintf('geography(%s%s)',
1107+
strtolower($column->subtype),
1108+
$column->srid ? ','.$column->srid : ''
1109+
);
11931110
}
11941111

1195-
return "geometry({$type})";
1112+
return 'geography';
11961113
}
11971114

11981115
/**

0 commit comments

Comments
 (0)