Skip to content

Commit 1e4d216

Browse files
committed
fix: bbox not working with MultiPolygon.
1 parent b27d649 commit 1e4d216

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/Packages/Bbox.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ public function __invoke(
5454
$minY = min($minY, $y);
5555
$maxX = max($maxX, $x);
5656
$maxY = max($maxY, $y);
57+
} elseif ($geometry instanceof MultiPolygon) {
58+
foreach ($geometry->getCoordinates() as $polygon) {
59+
foreach ($polygon as $ring) {
60+
foreach ($ring as $point) {
61+
$updateBBox(new Point($point));
62+
}
63+
}
64+
}
5765
} elseif ($geometry instanceof MultiPoint ||
5866
$geometry instanceof LineString ||
5967
$geometry instanceof MultiLineString ||
60-
$geometry instanceof Polygon ||
61-
$geometry instanceof MultiPolygon) {
68+
$geometry instanceof Polygon) {
6269
foreach ($geometry->getCoordinates() as $coord) {
6370
if (is_array($coord[0])) {
6471
foreach ($coord as $point) {

tests/BboxTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeoJson\Feature\Feature;
66
use GeoJson\GeoJson;
77
use GeoJson\Geometry\LineString;
8+
use GeoJson\Geometry\MultiPolygon;
89
use GeoJson\Geometry\Point;
910
use GeoJson\Geometry\Polygon;
1011
use PHPUnit\Framework\TestCase;
@@ -70,4 +71,35 @@ public function test_recompute_bbox(): void
7071
$bbox = Turf::bbox($feature, true);
7172
$this->assertEquals([10, 20, 10, 20], $bbox, 'Should recompute BBox when recompute is true');
7273
}
74+
75+
public function test_bbox_multipolygon(): void
76+
{
77+
$multiPolygon = new MultiPolygon([
78+
[
79+
[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]],
80+
],
81+
[
82+
[[2, 2], [4, 2], [4, 4], [2, 4], [2, 2]],
83+
],
84+
]);
85+
86+
$bbox = Turf::bbox($multiPolygon);
87+
$this->assertEquals([0, 0, 4, 4], $bbox, 'MultiPolygon BBox should encompass all polygons');
88+
}
89+
90+
public function test_bbox_multipolygon_with_holes(): void
91+
{
92+
$multiPolygon = new MultiPolygon([
93+
[
94+
[[0, 0], [3, 0], [3, 3], [0, 3], [0, 0]],
95+
[[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
96+
],
97+
[
98+
[[5, 5], [7, 5], [7, 7], [5, 7], [5, 5]],
99+
],
100+
]);
101+
102+
$bbox = Turf::bbox($multiPolygon);
103+
$this->assertEquals([0, 0, 7, 7], $bbox, 'MultiPolygon BBox with holes should encompass all rings');
104+
}
73105
}

0 commit comments

Comments
 (0)