Skip to content
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

Add support for custom properties in toFeatureCollectionJson method #116

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Geometry classes can be also created by these static methods:

* `toArray()` - Serializes the geometry object into a GeoJSON associative array.
* `toJson()` - Serializes the geometry object into an GeoJSON string.
* `toFeatureCollectionJson()` - Serializes the geometry object into an GeoJSON's FeatureCollection string.
* `toFeatureCollectionJson(array $properties = [])` - Serializes the geometry object into an GeoJSON's FeatureCollection string, with optional `properties` to be added to each feature.
* `toWkt()` - Serializes the geometry object into a WKT.
* `toWkb()` - Serializes the geometry object into a WKB.
* `getCoordinates()` - Returns the coordinates of the geometry object.
Expand Down
10 changes: 7 additions & 3 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ public function toArray(): array
}

/**
* @param array<array<string, mixed>> $properties
*
* @return string
*
* @throws JsonException
*/
public function toFeatureCollectionJson(): string
public function toFeatureCollectionJson(array $properties = []): string
{
if (static::class === GeometryCollection::class) {
/** @var GeometryCollection $this */
Expand All @@ -164,10 +168,10 @@ public function toFeatureCollectionJson(): string
$geometries = collect([$this]);
}

$features = $geometries->map(static function (self $geometry): array {
$features = $geometries->map(static function (self $geometry, $index) use ($properties) {
return [
'type' => 'Feature',
'properties' => [],
'properties' => $properties[$index] ?? [],
'geometry' => $geometry->toArray(),
];
});
Expand Down
8 changes: 6 additions & 2 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@
]),
new Point(0, 180),
]);
$properties = [
['name' => 'Polygon property', 'description' => 'Polygon description'],
['name' => 'Point property', 'description' => 'Point description'],
];

$featureCollectionJson = $geometryCollection->toFeatureCollectionJson();
$featureCollectionJson = $geometryCollection->toFeatureCollectionJson($properties);

$expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}';
$expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"Polygon property","description":"Polygon description"},"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":{"name":"Point property","description":"Point description"},"geometry":{"type":"Point","coordinates":[180,0]}}]}';
expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson);
});

Expand Down
Loading