Skip to content

Commit 07d9a88

Browse files
authored
Merge pull request #15 from MatanYadaev/to-feature-collection-json
Add `GeometryCollection@toFeatureCollectionJson`
2 parents 051c008 + 99267a4 commit 07d9a88

14 files changed

+314
-150
lines changed

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ parameters:
55
- src
66
- tests
77
level: max
8+
ignoreErrors:
9+
- '#Method MatanYadaev\\EloquentSpatial\\Objects\\.+::getGeometries\(\) should return array<.+> but returns array<MatanYadaev\\EloquentSpatial\\Objects\\Geometry>\.#'
10+
- '#Method MatanYadaev\\EloquentSpatial\\Objects\\(Geometry|GeometryCollection)::(toJson|toFeatureCollectionJson)\(\) should return string but returns string\|false\.#'
811
excludePaths:
912
- ./src/Factory.php
1013
checkMissingIterableValueType: true

src/Objects/Geometry.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public static function fromWkt(string $wkt): static
3838

3939
public function toJson($options = 0): string
4040
{
41-
/* @phpstan-ignore-next-line */
4241
return json_encode($this, $options);
4342
}
4443

@@ -79,6 +78,15 @@ public function toArray(): array
7978
];
8079
}
8180

81+
public function toFeature(): array
82+
{
83+
return [
84+
'type' => 'Feature',
85+
'properties' => [],
86+
'geometry' => $this->toArray(),
87+
];
88+
}
89+
8290
/**
8391
* @return array<mixed>
8492
*/

src/Objects/GeometryCollection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ public function getGeometries(): array
7878
return $this->geometries->all();
7979
}
8080

81+
public function toFeatureCollectionJson(): string
82+
{
83+
/** @var Collection<Geometry> $geometries */
84+
$geometries = static::class === self::class
85+
? $this->geometries
86+
: collect([$this]);
87+
88+
$features = $geometries->map(static function (Geometry $geometry): array {
89+
return $geometry->toFeature();
90+
});
91+
92+
return json_encode([
93+
'type' => 'FeatureCollection',
94+
'features' => $features,
95+
]);
96+
}
97+
8198
/**
8299
* @throws InvalidArgumentException
83100
*/

src/Objects/LineString.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public function toWkt(): Expression
1515
{
1616
return DB::raw("LINESTRING({$this->toCollectionWkt()})");
1717
}
18+
19+
/**
20+
* @return array<Point>
21+
*/
22+
public function getGeometries(): array
23+
{
24+
return parent::getGeometries();
25+
}
1826
}

src/Objects/MultiLineString.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Facades\DB;
1010

11-
/**
12-
* @method array<LineString> getGeometries()
13-
*/
1411
class MultiLineString extends GeometryCollection
1512
{
1613
/** @var Collection<LineString> */
@@ -32,4 +29,12 @@ public function toWkt(): Expression
3229
{
3330
return DB::raw("MULTILINESTRING({$this->toCollectionWkt()})");
3431
}
32+
33+
/**
34+
* @return array<LineString>
35+
*/
36+
public function getGeometries(): array
37+
{
38+
return parent::getGeometries();
39+
}
3540
}

src/Objects/MultiPolygon.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
use Illuminate\Support\Facades\DB;
1010
use InvalidArgumentException;
1111

12-
/**
13-
* @method array<Polygon> getGeometries()
14-
*/
1512
class MultiPolygon extends GeometryCollection
1613
{
1714
/** @var Collection<Polygon> */
@@ -35,4 +32,12 @@ public function toWkt(): Expression
3532
{
3633
return DB::raw("MULTIPOLYGON({$this->toCollectionWkt()})");
3734
}
35+
36+
/**
37+
* @return array<Polygon>
38+
*/
39+
public function getGeometries(): array
40+
{
41+
return parent::getGeometries();
42+
}
3843
}

src/Objects/PointCollection.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
use Illuminate\Support\Collection;
88
use InvalidArgumentException;
99

10-
/**
11-
* @method array<Point> getGeometries()
12-
*/
1310
abstract class PointCollection extends GeometryCollection
1411
{
1512
/** @var Collection<Point> */
@@ -26,4 +23,12 @@ public function __construct(Collection | array $geometries)
2623
{
2724
parent::__construct($geometries);
2825
}
26+
27+
/**
28+
* @return array<Point>
29+
*/
30+
public function getGeometries(): array
31+
{
32+
return parent::getGeometries();
33+
}
2934
}

tests/Objects/GeometryCollectionTest.php

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ public function it_stores_geometry_collection(): void
2222
'geometry_collection' => new GeometryCollection([
2323
new Polygon([
2424
new LineString([
25-
new Point(23.1, 55.5),
26-
new Point(23.2, 55.6),
27-
new Point(23.3, 55.7),
28-
new Point(23.1, 55.5),
25+
new Point(0, 0),
26+
new Point(1, 1),
27+
new Point(2, 2),
28+
new Point(3, 3),
29+
new Point(0, 0),
2930
]),
3031
]),
31-
new Point(23.1, 55.5),
32+
new Point(0, 0),
3233
]),
3334
])->fresh();
3435

@@ -40,20 +41,22 @@ public function it_stores_geometry_collection(): void
4041
$lineStrings = $polygon->getGeometries();
4142
$points = $lineStrings[0]->getGeometries();
4243

43-
$this->assertEquals(23.1, $points[0]->latitude);
44-
$this->assertEquals(55.5, $points[0]->longitude);
45-
$this->assertEquals(23.2, $points[1]->latitude);
46-
$this->assertEquals(55.6, $points[1]->longitude);
47-
$this->assertEquals(23.3, $points[2]->latitude);
48-
$this->assertEquals(55.7, $points[2]->longitude);
49-
$this->assertEquals(23.1, $points[3]->latitude);
50-
$this->assertEquals(55.5, $points[3]->longitude);
44+
$this->assertEquals(0, $points[0]->latitude);
45+
$this->assertEquals(0, $points[0]->longitude);
46+
$this->assertEquals(1, $points[1]->latitude);
47+
$this->assertEquals(1, $points[1]->longitude);
48+
$this->assertEquals(2, $points[2]->latitude);
49+
$this->assertEquals(2, $points[2]->longitude);
50+
$this->assertEquals(3, $points[3]->latitude);
51+
$this->assertEquals(3, $points[3]->longitude);
52+
$this->assertEquals(0, $points[4]->latitude);
53+
$this->assertEquals(0, $points[4]->longitude);
5154

5255
/** @var Point $point */
5356
$point = $geometries[1];
5457

55-
$this->assertEquals(23.1, $point->latitude);
56-
$this->assertEquals(55.5, $point->longitude);
58+
$this->assertEquals(0, $point->latitude);
59+
$this->assertEquals(0, $point->longitude);
5760

5861
$this->assertDatabaseCount($testPlace->getTable(), 1);
5962
}
@@ -63,7 +66,7 @@ public function it_stores_geometry_collection_from_geo_json(): void
6366
{
6467
/** @var TestPlace $testPlace */
6568
$testPlace = TestPlace::factory()->create([
66-
'geometry_collection' => GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]},{"type":"Point","coordinates":[55.5,23.1]}]}'),
69+
'geometry_collection' => GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]},{"type":"Point","coordinates":[0,0]}]}'),
6770
])->fresh();
6871

6972
$this->assertTrue($testPlace->geometry_collection instanceof GeometryCollection);
@@ -74,20 +77,22 @@ public function it_stores_geometry_collection_from_geo_json(): void
7477
$lineStrings = $polygon->getGeometries();
7578
$points = $lineStrings[0]->getGeometries();
7679

77-
$this->assertEquals(23.1, $points[0]->latitude);
78-
$this->assertEquals(55.5, $points[0]->longitude);
79-
$this->assertEquals(23.2, $points[1]->latitude);
80-
$this->assertEquals(55.6, $points[1]->longitude);
81-
$this->assertEquals(23.3, $points[2]->latitude);
82-
$this->assertEquals(55.7, $points[2]->longitude);
83-
$this->assertEquals(23.1, $points[3]->latitude);
84-
$this->assertEquals(55.5, $points[3]->longitude);
80+
$this->assertEquals(0, $points[0]->latitude);
81+
$this->assertEquals(0, $points[0]->longitude);
82+
$this->assertEquals(1, $points[1]->latitude);
83+
$this->assertEquals(1, $points[1]->longitude);
84+
$this->assertEquals(2, $points[2]->latitude);
85+
$this->assertEquals(2, $points[2]->longitude);
86+
$this->assertEquals(3, $points[3]->latitude);
87+
$this->assertEquals(3, $points[3]->longitude);
88+
$this->assertEquals(0, $points[4]->latitude);
89+
$this->assertEquals(0, $points[4]->longitude);
8590

8691
/** @var Point $point */
8792
$point = $geometries[1];
8893

89-
$this->assertEquals(23.1, $point->latitude);
90-
$this->assertEquals(55.5, $point->longitude);
94+
$this->assertEquals(0, $point->latitude);
95+
$this->assertEquals(0, $point->longitude);
9196

9297
$this->assertDatabaseCount($testPlace->getTable(), 1);
9398
}
@@ -97,7 +102,7 @@ public function it_stores_geometry_collection_from_feature_collection_geo_json()
97102
{
98103
/** @var TestPlace $testPlace */
99104
$testPlace = TestPlace::factory()->create([
100-
'geometry_collection' => GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]}},{"type":"Feature","geometry":{"type":"Point","coordinates":[55.5,23.1]}}]}'),
105+
'geometry_collection' => GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[0,0]}}]}'),
101106
])->fresh();
102107

103108
$this->assertTrue($testPlace->geometry_collection instanceof GeometryCollection);
@@ -108,39 +113,67 @@ public function it_stores_geometry_collection_from_feature_collection_geo_json()
108113
$lineStrings = $polygon->getGeometries();
109114
$points = $lineStrings[0]->getGeometries();
110115

111-
$this->assertEquals(23.1, $points[0]->latitude);
112-
$this->assertEquals(55.5, $points[0]->longitude);
113-
$this->assertEquals(23.2, $points[1]->latitude);
114-
$this->assertEquals(55.6, $points[1]->longitude);
115-
$this->assertEquals(23.3, $points[2]->latitude);
116-
$this->assertEquals(55.7, $points[2]->longitude);
117-
$this->assertEquals(23.1, $points[3]->latitude);
118-
$this->assertEquals(55.5, $points[3]->longitude);
116+
$this->assertEquals(0, $points[0]->latitude);
117+
$this->assertEquals(0, $points[0]->longitude);
118+
$this->assertEquals(1, $points[1]->latitude);
119+
$this->assertEquals(1, $points[1]->longitude);
120+
$this->assertEquals(2, $points[2]->latitude);
121+
$this->assertEquals(2, $points[2]->longitude);
122+
$this->assertEquals(3, $points[3]->latitude);
123+
$this->assertEquals(3, $points[3]->longitude);
124+
$this->assertEquals(0, $points[4]->latitude);
125+
$this->assertEquals(0, $points[4]->longitude);
119126

120127
/** @var Point $point */
121128
$point = $geometries[1];
122129

123-
$this->assertEquals(23.1, $point->latitude);
124-
$this->assertEquals(55.5, $point->longitude);
130+
$this->assertEquals(0, $point->latitude);
131+
$this->assertEquals(0, $point->longitude);
125132

126133
$this->assertDatabaseCount($testPlace->getTable(), 1);
127134
}
128135

129136
/** @test */
130-
public function it_generates_multi_polygon_geo_json(): void
137+
public function it_generates_geometry_collection_geo_json(): void
131138
{
132139
$geometryCollection = new GeometryCollection([
133140
new Polygon([
134141
new LineString([
135-
new Point(23.1, 55.5),
136-
new Point(23.2, 55.6),
137-
new Point(23.3, 55.7),
138-
new Point(23.1, 55.5),
142+
new Point(0, 0),
143+
new Point(1, 1),
144+
new Point(2, 2),
145+
new Point(3, 3),
146+
new Point(0, 0),
139147
]),
140148
]),
141-
new Point(23.1, 55.5),
149+
new Point(0, 0),
142150
]);
143151

144-
$this->assertEquals('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]},{"type":"Point","coordinates":[55.5,23.1]}]}', $geometryCollection->toJson());
152+
$this->assertEquals(
153+
'{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]},{"type":"Point","coordinates":[0,0]}]}',
154+
$geometryCollection->toJson()
155+
);
156+
}
157+
158+
/** @test */
159+
public function it_generates_geometry_collection_feature_collection_json(): void
160+
{
161+
$geometryCollection = new GeometryCollection([
162+
new Polygon([
163+
new LineString([
164+
new Point(0, 0),
165+
new Point(1, 1),
166+
new Point(2, 2),
167+
new Point(3, 3),
168+
new Point(0, 0),
169+
]),
170+
]),
171+
new Point(0, 0),
172+
]);
173+
174+
$this->assertEquals(
175+
'{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[0,0]}}]}',
176+
$geometryCollection->toFeatureCollectionJson()
177+
);
145178
}
146179
}

0 commit comments

Comments
 (0)