-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBBoxUtils.php
More file actions
80 lines (72 loc) · 2.05 KB
/
Copy pathBBoxUtils.php
File metadata and controls
80 lines (72 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Mindee\Geometry;
/**
* Utility class for BBox.
*/
abstract class BBoxUtils
{
/**
* Generates a BBox from a polygon. Returns null if no polygon is provided.
*
* @param Polygon $polygon Polygon to get the BBox of.
*/
public static function generateBBoxFromPolygon(Polygon $polygon): ?BBox
{
if (!$polygon->getCoordinates()) {
return null;
}
return new BBox(
$polygon->getMinX(),
$polygon->getMaxX(),
$polygon->getMinY(),
$polygon->getMaxY(),
);
}
/**
* Generates a BBox from an array of polygons. Returns null if no polygons are provided.
*
* @param array<Polygon|null> $polygons Series of polygons to get the BBox of.
*/
public static function generateBBoxFromPolygons(array $polygons): ?BBox
{
$bboxes = [];
foreach ($polygons as $polygon) {
if (null === $polygon || !$polygon->getCoordinates()) {
continue;
}
$bboxes[] = self::generateBBoxFromPolygon($polygon);
}
return self::mergeBBoxes($bboxes);
}
/**
* Merges an array of bboxes.
*
* @param array<BBox> $bboxes BBoxes to merge.
*/
public static function mergeBBoxes(array $bboxes): ?BBox
{
if (!$bboxes) {
return null;
}
$minX = null;
$maxX = null;
$minY = null;
$maxY = null;
foreach ($bboxes as $bbox) {
if (!$minX || $minX > $bbox->getMinX()) {
$minX = $bbox->getMinX();
}
if (!$minY || $minY > $bbox->getMinY()) {
$minY = $bbox->getMinY();
}
if (!$maxX || $maxX < $bbox->getMaxX()) {
$maxX = $bbox->getMaxX();
}
if (!$maxY || $maxY < $bbox->getMaxY()) {
$maxY = $bbox->getMaxY();
}
}
return new BBox((float) $minX, (float) $maxX, (float) $minY, (float) $maxY);
}
}