Skip to content

Commit ed0a91d

Browse files
committed
Improve Cluster interface with getCenter(), count and iteratore
1 parent f903389 commit ed0a91d

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

src/Map/src/Cluster/Cluster.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,30 @@
1616
/**
1717
* Cluster representation.
1818
*
19+
* @implements \IteratorAggregate<int, Point>
20+
*
1921
* @author Simon André <[email protected]>
2022
*/
21-
final class Cluster
23+
final class Cluster implements \Countable, \IteratorAggregate
2224
{
2325
/**
2426
* @var Point[]
2527
*/
2628
private array $points = [];
2729

28-
private float $sumLat = 0.0;
29-
private float $sumLng = 0.0;
30-
private int $count = 0;
30+
private float $sumLat;
31+
private float $sumLng;
32+
private int $count;
3133

34+
/**
35+
* Initializes the cluster with an initial point.
36+
*/
3237
public function __construct(Point $initialPoint)
3338
{
34-
$this->addPoint($initialPoint);
39+
$this->points[] = $initialPoint;
40+
$this->sumLat = $initialPoint->getLatitude();
41+
$this->sumLng = $initialPoint->getLongitude();
42+
$this->count = 1;
3543
}
3644

3745
public function addPoint(Point $point): void
@@ -42,14 +50,12 @@ public function addPoint(Point $point): void
4250
++$this->count;
4351
}
4452

45-
public function getCenterLat(): float
46-
{
47-
return $this->count > 0 ? $this->sumLat / $this->count : 0.0;
48-
}
49-
50-
public function getCenterLng(): float
53+
/**
54+
* Returns the center of the cluster as a Point.
55+
*/
56+
public function getCenter(): Point
5157
{
52-
return $this->count > 0 ? $this->sumLng / $this->count : 0.0;
58+
return new Point($this->sumLat / $this->count, $this->sumLng / $this->count);
5359
}
5460

5561
/**
@@ -59,4 +65,20 @@ public function getPoints(): array
5965
{
6066
return $this->points;
6167
}
68+
69+
/**
70+
* Returns the number of points in the cluster.
71+
*/
72+
public function count(): int
73+
{
74+
return $this->count;
75+
}
76+
77+
/**
78+
* @return \Traversable<int, Point>
79+
*/
80+
public function getIterator(): \Traversable
81+
{
82+
return new \ArrayIterator($this->points);
83+
}
6284
}

src/Map/tests/Cluster/ClusterTest.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public function testAddPointAndGetCenter(): void
2222
$point1 = new Point(10.0, 20.0);
2323
$cluster = new Cluster($point1);
2424

25-
$this->assertEquals(10.0, $cluster->getCenterLat());
26-
$this->assertEquals(20.0, $cluster->getCenterLng());
25+
$this->assertEquals(10.0, $cluster->getCenter()->getLatitude());
26+
$this->assertEquals(20.0, $cluster->getCenter()->getLongitude());
2727

2828
$point2 = new Point(12.0, 22.0);
2929
$cluster->addPoint($point2);
3030

31-
$this->assertEquals(11.0, $cluster->getCenterLat());
32-
$this->assertEquals(21.0, $cluster->getCenterLng());
31+
$this->assertEquals(11.0, $cluster->getCenter()->getLatitude());
32+
$this->assertEquals(21.0, $cluster->getCenter()->getLongitude());
3333
}
3434

3535
public function testGetPoints(): void
@@ -45,13 +45,35 @@ public function testGetPoints(): void
4545
$this->assertSame($point2, $points[1]);
4646
}
4747

48-
public function testEmptyCluster(): void
48+
public function testCount(): void
49+
{
50+
$cluster = new Cluster(new Point(10.0, 20.0));
51+
$this->assertCount(1, $cluster);
52+
53+
$cluster->addPoint(new Point(10.0, 20.0));
54+
$this->assertCount(2, $cluster);
55+
}
56+
57+
public function testIterator(): void
4958
{
5059
$point1 = new Point(10.0, 20.0);
51-
$cluster = new Cluster($point1); // Start an empty cluster
52-
$points = $cluster->getPoints();
53-
$this->assertCount(1, $points);
54-
$this->assertEquals(10.0, $cluster->getCenterLat());
55-
$this->assertEquals(20.0, $cluster->getCenterLng());
60+
$point2 = new Point(12.0, 22.0);
61+
$cluster = new Cluster($point1);
62+
$cluster->addPoint($point2);
63+
64+
$points = iterator_to_array($cluster);
65+
$this->assertCount(2, $points);
66+
$this->assertSame($point1, $points[0]);
67+
$this->assertSame($point2, $points[1]);
68+
}
69+
70+
public function testCreateCluster(): void
71+
{
72+
$point1 = new Point(10.0, 20.0);
73+
$cluster = new Cluster($point1);
74+
75+
$this->assertCount(1, $cluster->getPoints());
76+
$this->assertEquals(10.0, $cluster->getCenter()->getLatitude());
77+
$this->assertEquals(20.0, $cluster->getCenter()->getLongitude());
5678
}
5779
}

src/Map/tests/Cluster/GridClusteringAlgorithmTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function testSinglePointCreatesSingleCluster(): void
2929
/** @var Cluster $cluster */
3030
$cluster = $clusters[0];
3131

32-
$this->assertEquals(10.0, $cluster->getCenterLat());
33-
$this->assertEquals(20.0, $cluster->getCenterLng());
32+
$this->assertEquals(10.0, $cluster->getCenter()->getLatitude());
33+
$this->assertEquals(20.0, $cluster->getCenter()->getLongitude());
3434
$this->assertCount(1, $cluster->getPoints());
3535
}
3636

@@ -48,8 +48,8 @@ public function testPointsInSameGridAreClusteredTogether(): void
4848
$cluster = $clusters[0];
4949

5050
$this->assertCount(2, $cluster->getPoints());
51-
$this->assertEqualsWithDelta(10.05, $cluster->getCenterLat(), 0.0001);
52-
$this->assertEqualsWithDelta(20.05, $cluster->getCenterLng(), 0.0001);
51+
$this->assertEqualsWithDelta(10.05, $cluster->getCenter()->getLatitude(), 0.0001);
52+
$this->assertEqualsWithDelta(20.05, $cluster->getCenter()->getLongitude(), 0.0001);
5353
}
5454

5555
public function testPointsInDifferentGridsAreNotClustered(): void

src/Map/tests/Cluster/MortonClusteringAlgorithmTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function testSinglePointCreatesSingleCluster(): void
2929
/** @var Cluster $cluster */
3030
$cluster = $clusters[0];
3131

32-
$this->assertEquals(10.0, $cluster->getCenterLat());
33-
$this->assertEquals(20.0, $cluster->getCenterLng());
32+
$this->assertEquals(10.0, $cluster->getCenter()->getLatitude());
33+
$this->assertEquals(20.0, $cluster->getCenter()->getLongitude());
3434
$this->assertCount(1, $cluster->getPoints());
3535
}
3636

0 commit comments

Comments
 (0)