Skip to content

Commit 879dcb3

Browse files
committed
fix feature collection schema
1 parent 030e561 commit 879dcb3

File tree

8 files changed

+24
-10
lines changed

8 files changed

+24
-10
lines changed

src/Objects/Geometry.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public function toArray(): array
7878
];
7979
}
8080

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

src/Objects/GeometryCollection.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ public function getGeometries(): array
8080

8181
public function toFeatureCollectionJson(): string
8282
{
83-
$features = static::class === self::class
84-
? $this->toArray()['geometries']
85-
: [$this->toArray()];
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+
});
8691

8792
return json_encode([
8893
'type' => 'FeatureCollection',

tests/Objects/GeometryCollectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function it_stores_geometry_collection_from_feature_collection_geo_json()
102102
{
103103
/** @var TestPlace $testPlace */
104104
$testPlace = TestPlace::factory()->create([
105-
'geometry_collection' => GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]},{"type":"Point","coordinates":[0,0]}]}'),
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]}}]}'),
106106
])->fresh();
107107

108108
$this->assertTrue($testPlace->geometry_collection instanceof GeometryCollection);
@@ -172,7 +172,7 @@ public function it_generates_geometry_collection_feature_collection_json(): void
172172
]);
173173

174174
$this->assertEquals(
175-
'{"type":"FeatureCollection","features":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]},{"type":"Point","coordinates":[0,0]}]}',
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]}}]}',
176176
$geometryCollection->toFeatureCollectionJson()
177177
);
178178
}

tests/Objects/LineStringTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ public function it_generates_line_string_feature_collection_json(): void
7474
new Point(1, 1),
7575
]);
7676

77-
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"LineString","coordinates":[[0,0],[1,1]]}]}', $lineString->toFeatureCollectionJson());
77+
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"LineString","coordinates":[[0,0],[1,1]]}}]}', $lineString->toFeatureCollectionJson());
7878
}
7979
}

tests/Objects/MultiLineStringTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ public function it_generates_multi_line_string_feature_collection_json(): void
8383
]),
8484
]);
8585

86-
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"MultiLineString","coordinates":[[[0,0],[1,1]]]}]}', $multiLineString->toFeatureCollectionJson());
86+
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiLineString","coordinates":[[[0,0],[1,1]]]}}]}', $multiLineString->toFeatureCollectionJson());
8787
}
8888
}

tests/Objects/MultiPointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ public function it_generates_multi_point_feature_collection_json(): void
6767
new Point(0, 0),
6868
]);
6969

70-
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"MultiPoint","coordinates":[[0,0]]}]}', $multiPoint->toFeatureCollectionJson());
70+
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiPoint","coordinates":[[0,0]]}}]}', $multiPoint->toFeatureCollectionJson());
7171
}
7272
}

tests/Objects/MultiPolygonTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ public function it_generates_multi_polygon_feature_collection_json(): void
113113
]),
114114
]);
115115

116-
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"MultiPolygon","coordinates":[[[[0,0],[1,1],[2,2],[3,3],[0,0]]]]}]}', $multiPolygon->toFeatureCollectionJson());
116+
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiPolygon","coordinates":[[[[0,0],[1,1],[2,2],[3,3],[0,0]]]]}}]}', $multiPolygon->toFeatureCollectionJson());
117117
}
118118
}

tests/Objects/PolygonTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,6 @@ public function it_generates_polygon_feature_collection_json(): void
104104
]),
105105
]);
106106

107-
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]}]}', $polygon->toFeatureCollectionJson());
107+
$this->assertEquals('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]}}]}', $polygon->toFeatureCollectionJson());
108108
}
109109
}

0 commit comments

Comments
 (0)