-
-
Notifications
You must be signed in to change notification settings - Fork 367
[Map] Add Clustering Algorithms #2554
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
Open
smnandre
wants to merge
3
commits into
symfony:2.x
Choose a base branch
from
smnandre:map/feat-clustering
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+631
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||||||||
<?php | ||||||||||||
|
||||||||||||
/* | ||||||||||||
* This file is part of the Symfony package. | ||||||||||||
* | ||||||||||||
* (c) Fabien Potencier <[email protected]> | ||||||||||||
* | ||||||||||||
* For the full copyright and license information, please view the LICENSE | ||||||||||||
* file that was distributed with this source code. | ||||||||||||
*/ | ||||||||||||
|
||||||||||||
namespace Symfony\UX\Map\Cluster; | ||||||||||||
|
||||||||||||
use Symfony\UX\Map\Point; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Cluster representation. | ||||||||||||
* | ||||||||||||
* @implements \IteratorAggregate<int, Point> | ||||||||||||
* | ||||||||||||
* @author Simon André <[email protected]> | ||||||||||||
*/ | ||||||||||||
final class Cluster implements \Countable, \IteratorAggregate | ||||||||||||
{ | ||||||||||||
/** | ||||||||||||
* @var Point[] | ||||||||||||
*/ | ||||||||||||
private array $points = []; | ||||||||||||
|
||||||||||||
private float $sumLat; | ||||||||||||
private float $sumLng; | ||||||||||||
private int $count; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Initializes the cluster with an initial point. | ||||||||||||
*/ | ||||||||||||
public function __construct(Point $initialPoint) | ||||||||||||
{ | ||||||||||||
$this->points[] = $initialPoint; | ||||||||||||
$this->sumLat = $initialPoint->getLatitude(); | ||||||||||||
$this->sumLng = $initialPoint->getLongitude(); | ||||||||||||
$this->count = 1; | ||||||||||||
Comment on lines
+39
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe..?
Suggested change
|
||||||||||||
} | ||||||||||||
|
||||||||||||
public function addPoint(Point $point): void | ||||||||||||
{ | ||||||||||||
$this->points[] = $point; | ||||||||||||
$this->sumLat += $point->getLatitude(); | ||||||||||||
$this->sumLng += $point->getLongitude(); | ||||||||||||
++$this->count; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns the center of the cluster as a Point. | ||||||||||||
*/ | ||||||||||||
public function getCenter(): Point | ||||||||||||
{ | ||||||||||||
return new Point($this->sumLat / $this->count, $this->sumLng / $this->count); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @return Point[] | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can be more precise here:
Suggested change
|
||||||||||||
*/ | ||||||||||||
public function getPoints(): array | ||||||||||||
{ | ||||||||||||
return $this->points; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns the number of points in the cluster. | ||||||||||||
*/ | ||||||||||||
public function count(): int | ||||||||||||
{ | ||||||||||||
return $this->count; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @return \Traversable<int, Point> | ||||||||||||
*/ | ||||||||||||
public function getIterator(): \Traversable | ||||||||||||
{ | ||||||||||||
return new \ArrayIterator($this->points); | ||||||||||||
} | ||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Cluster; | ||
|
||
use Symfony\UX\Map\Point; | ||
|
||
/** | ||
* Interface for various Clustering implementations. | ||
*/ | ||
interface ClusteringAlgorithmInterface | ||
{ | ||
/** | ||
* Clusters a set of points. | ||
* | ||
* @param Point[] $points List of points to be clustered | ||
* @param float $zoom The zoom level, determining grid resolution | ||
* | ||
* @return Cluster[] An array of clusters, each containing grouped points | ||
*/ | ||
public function cluster(array $points, float $zoom): array; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Cluster; | ||
|
||
use Symfony\UX\Map\Point; | ||
|
||
/** | ||
* Grid-based clustering algorithm for spatial data. | ||
* | ||
* This algorithm groups points into fixed-size grid cells based on the given zoom level. | ||
* | ||
* Best for: | ||
* - Fast, scalable clustering on large geographical datasets | ||
* - Real-time clustering where performance is critical | ||
* - Use cases where a simple, predictable grid structure is sufficient | ||
* | ||
* Slower for: | ||
* - Highly dynamic data that requires adaptive cluster sizes | ||
* - Scenarios where varying density should influence cluster sizes (e.g., DBSCAN-like approaches) | ||
* - Irregularly shaped clusters that do not fit a strict grid pattern | ||
* | ||
* @author Simon André <[email protected]> | ||
*/ | ||
final class GridClusteringAlgorithm implements ClusteringAlgorithmInterface | ||
{ | ||
/** | ||
* Clusters a set of points using a fixed grid resolution based on the zoom level. | ||
* | ||
* @param Point[] $points List of points to be clustered | ||
* @param float $zoom The zoom level, determining grid resolution | ||
* | ||
* @return Cluster[] An array of clusters, each containing grouped points | ||
*/ | ||
public function cluster(iterable $points, float $zoom): array | ||
{ | ||
$gridResolution = 1 << (int) $zoom; | ||
$gridSize = 360 / $gridResolution; | ||
$invGridSize = 1 / $gridSize; | ||
|
||
$cells = []; | ||
|
||
foreach ($points as $point) { | ||
$lng = $point->getLongitude(); | ||
$lat = $point->getLatitude(); | ||
$gridX = (int) (($lng + 180) * $invGridSize); | ||
$gridY = (int) (($lat + 90) * $invGridSize); | ||
$key = ($gridX << 16) | $gridY; | ||
|
||
if (!isset($cells[$key])) { | ||
$cells[$key] = new Cluster($point); | ||
} else { | ||
$cells[$key]->addPoint($point); | ||
} | ||
} | ||
|
||
return array_values($cells); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Cluster; | ||
|
||
use Symfony\UX\Map\Point; | ||
|
||
/** | ||
* Clustering algorithm based on Morton codes (Z-order curves). | ||
* | ||
* This approach is optimized for spatial data and preserves locality efficiently. | ||
* | ||
* Best for: | ||
* - Large-scale spatial clustering | ||
* - Hierarchical clustering with fast locality-based grouping | ||
* - Datasets where preserving spatial proximity is crucial | ||
* | ||
* Slower for: | ||
* - High-dimensional data (beyond 2D/3D) due to Morton code limitations | ||
* - Non-spatial or categorical data | ||
* - Scenarios requiring dynamic cluster adjustments (e.g., streaming data) | ||
* | ||
* @author Simon André <[email protected]> | ||
*/ | ||
final class MortonClusteringAlgorithm implements ClusteringAlgorithmInterface | ||
{ | ||
/** | ||
* @param Point[] $points | ||
* | ||
* @return Cluster[] | ||
*/ | ||
public function cluster(iterable $points, float $zoom): array | ||
{ | ||
$resolution = 1 << (int) $zoom; | ||
$clustersMap = []; | ||
|
||
foreach ($points as $point) { | ||
$xNorm = ($point->getLatitude() + 180) / 360; | ||
$yNorm = ($point->getLongitude() + 90) / 180; | ||
|
||
$x = (int) floor($xNorm * $resolution); | ||
$y = (int) floor($yNorm * $resolution); | ||
|
||
$x &= 0xFFFF; | ||
$y &= 0xFFFF; | ||
|
||
$x = ($x | ($x << 8)) & 0x00FF00FF; | ||
$x = ($x | ($x << 4)) & 0x0F0F0F0F; | ||
$x = ($x | ($x << 2)) & 0x33333333; | ||
$x = ($x | ($x << 1)) & 0x55555555; | ||
|
||
$y = ($y | ($y << 8)) & 0x00FF00FF; | ||
$y = ($y | ($y << 4)) & 0x0F0F0F0F; | ||
$y = ($y | ($y << 2)) & 0x33333333; | ||
$y = ($y | ($y << 1)) & 0x55555555; | ||
|
||
$code = ($y << 1) | $x; | ||
|
||
if (!isset($clustersMap[$code])) { | ||
$clustersMap[$code] = new Cluster($point); | ||
} else { | ||
$clustersMap[$code]->addPoint($point); | ||
} | ||
} | ||
|
||
return array_values($clustersMap); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Map\Tests\Cluster; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\UX\Map\Cluster\Cluster; | ||
use Symfony\UX\Map\Point; | ||
|
||
class ClusterTest extends TestCase | ||
{ | ||
public function testAddPointAndGetCenter(): void | ||
{ | ||
$point1 = new Point(10.0, 20.0); | ||
$cluster = new Cluster($point1); | ||
|
||
$this->assertEquals(10.0, $cluster->getCenter()->getLatitude()); | ||
$this->assertEquals(20.0, $cluster->getCenter()->getLongitude()); | ||
|
||
$point2 = new Point(12.0, 22.0); | ||
$cluster->addPoint($point2); | ||
|
||
$this->assertEquals(11.0, $cluster->getCenter()->getLatitude()); | ||
$this->assertEquals(21.0, $cluster->getCenter()->getLongitude()); | ||
} | ||
|
||
public function testGetPoints(): void | ||
{ | ||
$point1 = new Point(10.0, 20.0); | ||
$point2 = new Point(12.0, 22.0); | ||
$cluster = new Cluster($point1); | ||
$cluster->addPoint($point2); | ||
|
||
$points = $cluster->getPoints(); | ||
$this->assertCount(2, $points); | ||
$this->assertSame($point1, $points[0]); | ||
$this->assertSame($point2, $points[1]); | ||
} | ||
|
||
public function testCount(): void | ||
{ | ||
$cluster = new Cluster(new Point(10.0, 20.0)); | ||
$this->assertCount(1, $cluster); | ||
|
||
$cluster->addPoint(new Point(10.0, 20.0)); | ||
$this->assertCount(2, $cluster); | ||
} | ||
|
||
public function testIterator(): void | ||
{ | ||
$point1 = new Point(10.0, 20.0); | ||
$point2 = new Point(12.0, 22.0); | ||
$cluster = new Cluster($point1); | ||
$cluster->addPoint($point2); | ||
|
||
$points = iterator_to_array($cluster); | ||
$this->assertCount(2, $points); | ||
$this->assertSame($point1, $points[0]); | ||
$this->assertSame($point2, $points[1]); | ||
} | ||
|
||
public function testCreateCluster(): void | ||
{ | ||
$point1 = new Point(10.0, 20.0); | ||
$cluster = new Cluster($point1); | ||
|
||
$this->assertCount(1, $cluster->getPoints()); | ||
$this->assertEquals(10.0, $cluster->getCenter()->getLatitude()); | ||
$this->assertEquals(20.0, $cluster->getCenter()->getLongitude()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about:
?