Skip to content

Commit a7a7b8a

Browse files
committed
Mutlipolygon implements JsonSerializable interface
1 parent ba9ccc1 commit a7a7b8a

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/Geometries/MultiPolygon.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,19 @@ protected static function assembleParts(array $parts)
9999

100100
return $polygons;
101101
}
102+
103+
/**
104+
* Convert to GeoJson MultiPolygon that is jsonable to GeoJSON
105+
*
106+
* @return \GeoJson\Geometry\MultiPolygon
107+
*/
108+
public function jsonSerialize()
109+
{
110+
$polygons = [];
111+
foreach ($this->polygons as $polygon) {
112+
$polygons[] = $polygon->jsonSerialize();
113+
}
114+
115+
return new \GeoJson\Geometry\MultiPolygon($polygons);
116+
}
102117
}

tests/Geometries/MultiPolygonTest.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77

88
class MultiPolygonTest extends BaseTestCase
99
{
10-
public function testFromWKT()
11-
{
12-
$polygon = MultiPolygon::fromWKT(
13-
'MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'
14-
);
15-
$this->assertInstanceOf(MultiPolygon::class, $polygon);
16-
17-
$this->assertEquals(2, $polygon->count());
18-
}
10+
/**
11+
* @var MultiPolygon
12+
*/
13+
private $multiPolygon;
1914

20-
public function testToWKT()
15+
protected function setUp()
2116
{
2217
$collection1 = new LineString(
2318
[
@@ -54,11 +49,34 @@ public function testToWKT()
5449

5550
$polygon2 = new Polygon([$collection3]);
5651

57-
$multipolygon = new MultiPolygon([$polygon1, $polygon2]);
52+
$this->multiPolygon = new MultiPolygon([$polygon1, $polygon2]);
53+
}
54+
55+
public function testFromWKT()
56+
{
57+
$polygon = MultiPolygon::fromWKT(
58+
'MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'
59+
);
60+
$this->assertInstanceOf(MultiPolygon::class, $polygon);
61+
62+
$this->assertEquals(2, $polygon->count());
63+
}
64+
5865

66+
public function testToWKT()
67+
{
5968
$this->assertEquals(
6069
'MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0),(10 10,20 10,20 20,10 20,10 10)),((100 100,200 100,200 200,100 200,100 100)))',
61-
$multipolygon->toWKT()
70+
$this->multiPolygon->toWKT()
71+
);
72+
}
73+
74+
public function testJsonSerialize()
75+
{
76+
$this->assertInstanceOf(\GeoJson\Geometry\MultiPolygon::class, $this->multiPolygon->jsonSerialize());
77+
$this->assertSame(
78+
'{"type":"MultiPolygon","coordinates":[[[[0,0],[0,1],[1,1],[1,0],[0,0]],[[10,10],[10,20],[20,20],[20,10],[10,10]]],[[[100,100],[100,200],[200,200],[200,100],[100,100]]]]}',
79+
json_encode($this->multiPolygon)
6280
);
6381
}
6482
}

0 commit comments

Comments
 (0)