Skip to content

Commit 904a0a8

Browse files
authored
Cast geometry from array (#71)
* create geometry from array (serialized geometry) * fromArray method, tests * documentation, move tests, typo fix, etc
1 parent 37eb8a1 commit 904a0a8

File tree

5 files changed

+90
-31
lines changed

5 files changed

+90
-31
lines changed

API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Geometry classes can be also created by these static methods:
1414

1515
* `fromJson(string $geoJson, int $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
16+
* `fromArray(array $geometry)` - Creates a geometry object from a array. Reverse to `->toArray()` method.
1617
* `fromWkt(string $wkt, int $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
1718
* `fromWkb(string $wkb, int $srid = 0)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary).
1819

src/GeometryCast.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function set($model, string $key, $value, array $attributes): Expression|
6262
return null;
6363
}
6464

65+
if (is_array($value)) {
66+
$value = Geometry::fromArray($value);
67+
}
68+
6569
if (! ($value instanceof $this->className)) {
6670
$geometryType = is_object($value) ? $value::class : gettype($value);
6771
throw new InvalidArgumentException(

src/Objects/Geometry.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,35 @@ public static function fromWkt(string $wkt, int $srid = 0): static
106106
return $geometry;
107107
}
108108

109-
/**
110-
* @param string $geoJson
111-
* @param int $srid
112-
* @return static
113-
*
114-
* @throws InvalidArgumentException
115-
*/
116-
public static function fromJson(string $geoJson, int $srid = 0): static
117-
{
118-
$geometry = Factory::parse($geoJson);
119-
$geometry->srid = $srid;
120-
121-
if (! ($geometry instanceof static)) {
122-
throw new InvalidArgumentException(
123-
sprintf('Expected %s, %s given.', static::class, $geometry::class)
124-
);
109+
/**
110+
* @param string $geoJson
111+
* @param int $srid
112+
* @return static
113+
*
114+
* @throws InvalidArgumentException
115+
*/
116+
public static function fromJson(string $geoJson, int $srid = 0): static
117+
{
118+
$geometry = Factory::parse($geoJson);
119+
$geometry->srid = $srid;
120+
121+
if (! ($geometry instanceof static)) {
122+
throw new InvalidArgumentException(
123+
sprintf('Expected %s, %s given.', static::class, $geometry::class)
124+
);
125+
}
126+
127+
return $geometry;
125128
}
126129

127-
return $geometry;
128-
}
130+
/**
131+
* @param array<mixed> $geometry
132+
* @return static
133+
*/
134+
public static function fromArray(array $geometry): static
135+
{
136+
return static::fromJson(json_encode($geometry));
137+
}
129138

130139
/**
131140
* @return array<mixed>

tests/Objects/GeometryCollectionTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@
5252
});
5353

5454
it('creates geometry collection from JSON', function (): void {
55-
$geometryCollection = new GeometryCollection([
56-
new Polygon([
57-
new LineString([
58-
new Point(0, 180),
59-
new Point(1, 179),
60-
new Point(2, 178),
61-
new Point(3, 177),
62-
new Point(0, 180),
55+
$geometryCollection = new GeometryCollection([
56+
new Polygon([
57+
new LineString([
58+
new Point(0, 180),
59+
new Point(1, 179),
60+
new Point(2, 178),
61+
new Point(3, 177),
62+
new Point(0, 180),
63+
]),
6364
]),
64-
]),
65-
new Point(0, 180),
66-
]);
65+
new Point(0, 180),
66+
]);
6767

68-
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');
68+
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');
6969

70-
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
70+
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
7171
});
7272

7373
it('creates geometry collection with SRID from JSON', function (): void {

tests/Objects/GeometryTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
use Illuminate\Database\QueryException;
44
use Illuminate\Support\Facades\DB;
55
use MatanYadaev\EloquentSpatial\AxisOrder;
6+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
7+
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
68
use MatanYadaev\EloquentSpatial\Objects\LineString;
79
use MatanYadaev\EloquentSpatial\Objects\Point;
10+
use MatanYadaev\EloquentSpatial\Objects\Polygon;
811
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
912

1013
it('throws exception when generating geometry from other geometry WKB', function (): void {
@@ -78,3 +81,45 @@
7881
LineString::fromJson($pointJson);
7982
})->toThrow(InvalidArgumentException::class);
8083
});
84+
85+
it('creates geometry collection from array (serialized model)', function (): void {
86+
$geometryCollection = new GeometryCollection([
87+
new Polygon([
88+
new LineString([
89+
new Point(0, 180),
90+
new Point(1, 179),
91+
new Point(2, 178),
92+
new Point(3, 177),
93+
new Point(0, 180),
94+
]),
95+
]),
96+
new Point(0, 180),
97+
]);
98+
$geometryCollectionArray = $geometryCollection->toArray();
99+
100+
$geometryCollectionFromArray = GeometryCollection::fromArray($geometryCollectionArray);
101+
102+
expect($geometryCollectionFromArray)->toEqual($geometryCollection);
103+
});
104+
105+
it('throws exception when generating geometry from wrong array', function (): void {
106+
expect(function (): void {
107+
$geometryCollectionArray = [
108+
'type' => 'Point2',
109+
'coordinates' => [0, 180],
110+
];
111+
112+
Geometry::fromArray($geometryCollectionArray);
113+
})->toThrow(InvalidArgumentException::class);
114+
});
115+
116+
it('throws exception when generating geometry from other geometry array', function (): void {
117+
expect(function (): void {
118+
$geometryCollectionArray = [
119+
'type' => 'Point',
120+
'coordinates' => [0, 180],
121+
];
122+
123+
LineString::fromArray($geometryCollectionArray);
124+
})->toThrow(InvalidArgumentException::class);
125+
});

0 commit comments

Comments
 (0)