-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBBoxUtils.php
More file actions
85 lines (81 loc) · 2.22 KB
/
Copy pathBBoxUtils.php
File metadata and controls
85 lines (81 loc) · 2.22 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
81
82
83
84
85
<?php
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.
* @return BBox|null
*/
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 $polygons Series of polygons to get the BBox of.
* @return BBox|null
*/
public static function generateBBoxFromPolygons(array $polygons): ?BBox
{
if (!$polygons) {
return null;
}
$merged = $polygons[0];
foreach ($polygons as $polygon) {
if ($polygon && $merged !== $polygon) {
$merged = PolygonUtils::merge($merged, $polygon);
}
}
return new BBox(
$merged->getMinX(),
$merged->getMaxX(),
$merged->getMinY(),
$merged->getMaxY(),
);
}
/**
* Merges an array of bboxes.
*
* @param array $bboxes BBoxes to merge.
* @return BBox|null
*/
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);
}
}