Skip to content

Commit 5096914

Browse files
committed
fix minor bug for extending bounding boxes
Releases v0.3.1
1 parent b4dfd11 commit 5096914

6 files changed

Lines changed: 64 additions & 20 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "runalyze/static-maps",
33
"description": "Library to create static images from various map tile providers.",
4-
"version": "0.1.1",
4+
"version": "0.3.1",
55
"license": "MIT",
66
"require": {
77
"php": ">=7.0",

src/Feature/Route.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,13 @@ public function getBoundingBox(float $paddingInPercent = 0.0, float $paddingRigh
156156
}
157157
}
158158

159-
if (0.0 < max($paddingTop, $paddingRight, $paddingBottom, $paddingLeft)) {
160-
$deltaLatitude = $maxLatitude - $minLatitude;
161-
$deltaLongitude = $maxLongitude - $minLongitude;
159+
$boundingBox = new BoundingBox($minLatitude, $maxLatitude, $minLongitude, $maxLongitude);
162160

163-
$minLatitude -= $deltaLatitude * $paddingBottom / 100.0;
164-
$maxLatitude += $deltaLatitude * $paddingTop / 100.0;
165-
$minLongitude -= $deltaLongitude * $paddingLeft / 100.0;
166-
$maxLongitude += $deltaLongitude * $paddingRight / 100.0;
161+
if (0.0 < max($paddingTop, $paddingRight, $paddingBottom, $paddingLeft)) {
162+
return $boundingBox->extendBy($paddingTop, $paddingRight, $paddingBottom, $paddingLeft);
167163
}
168164

169-
return new BoundingBox($minLatitude, $maxLatitude, $minLongitude, $maxLongitude);
165+
return $boundingBox;
170166
}
171167

172168
public function isEmpty(): bool

src/Tests/Feature/RouteTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public function testBoundingBoxWithPadding()
4545
]);
4646

4747
$this->assertFalse($route->isEmpty());
48-
$this->assertThatBoundingBoxIsEqual(new BoundingBox(50.0, 51.0, 10.0, 10.1), $route->getBoundingBox());
49-
$this->assertThatBoundingBoxIsEqual(new BoundingBox(49.9, 51.1, 9.99, 10.11), $route->getBoundingBox(10.0));
50-
$this->assertThatBoundingBoxIsEqual(new BoundingBox(50.0, 51.1, 9.95, 10.1), $route->getBoundingBox(10.0, 0.0, 0.0, 50.0));
48+
$this->assertThatBoundingBoxIsEqual(new BoundingBox(50.000, 51.000, 10.000, 10.100), $route->getBoundingBox());
49+
$this->assertThatBoundingBoxIsEqual(new BoundingBox(49.888, 51.110, 9.989, 10.111), $route->getBoundingBox(10.0));
50+
$this->assertThatBoundingBoxIsEqual(new BoundingBox(50.000, 51.110, 9.900, 10.100), $route->getBoundingBox(10.0, 0.0, 0.0, 50.0));
5151
}
5252

5353
protected function assertThatBoundingBoxIsEqual(BoundingBoxInterface $expected, BoundingBoxInterface $actual)
5454
{
55-
$this->assertEquals($expected->getMinLatitude(), $actual->getMinLatitude());
56-
$this->assertEquals($expected->getMaxLatitude(), $actual->getMaxLatitude());
57-
$this->assertEquals($expected->getMinLongitude(), $actual->getMinLongitude());
58-
$this->assertEquals($expected->getMaxLongitude(), $actual->getMaxLongitude());
55+
$this->assertEquals($expected->getMinLatitude(), $actual->getMinLatitude(), '', 0.001);
56+
$this->assertEquals($expected->getMaxLatitude(), $actual->getMaxLatitude(), '', 0.001);
57+
$this->assertEquals($expected->getMinLongitude(), $actual->getMinLongitude(), '', 0.001);
58+
$this->assertEquals($expected->getMaxLongitude(), $actual->getMaxLongitude(), '', 0.001);
5959
}
6060
}

src/Tests/Viewport/BoundingBoxTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public function testCenterCoordinates(float $minLatitude, float $minLongitude, f
2323
{
2424
$boundingBox = new BoundingBox($minLatitude, $maxLatitude, $minLongitude, $maxLongitude);
2525

26-
$this->assertEquals($minLatitude, $boundingBox->getMinLatitude());
27-
$this->assertEquals($maxLatitude, $boundingBox->getMaxLatitude());
28-
$this->assertEquals($minLongitude, $boundingBox->getMinLongitude());
29-
$this->assertEquals($maxLongitude, $boundingBox->getMaxLongitude());
26+
$this->assertThatBoundingBoxEquals([$minLatitude, $maxLatitude, $minLongitude, $maxLongitude], $boundingBox);
3027
$this->assertEquals($expectedCenterLatitude, $boundingBox->getCenterLatitude());
3128
$this->assertEquals($expectedCenterLongitude, $boundingBox->getCenterLongitude());
3229
}
@@ -40,4 +37,21 @@ public function getProviderForCenterCoordinates()
4037
[15.0, -160.0, 19.0, -60.0, 17.0, -110.0]
4138
];
4239
}
40+
41+
public function testExtendByZero()
42+
{
43+
$boundingBox = new BoundingBox(49.6, 7.6, 49.7, 7.7);
44+
45+
$this->assertThatBoundingBoxEquals([49.6, 7.6, 49.7, 7.7], $boundingBox);
46+
$this->assertThatBoundingBoxEquals([49.6, 7.6, 49.7, 7.7], $boundingBox->extendBy(0.0));
47+
$this->assertThatBoundingBoxEquals([49.6, 7.6, 49.7, 7.7], $boundingBox->extendBy(0.0, 0.0, 0.0, 0.0));
48+
}
49+
50+
protected function assertThatBoundingBoxEquals(array $expectedBoundaries, BoundingBox $boundingBox)
51+
{
52+
$this->assertEquals($expectedBoundaries[0], $boundingBox->getMinLatitude());
53+
$this->assertEquals($expectedBoundaries[1], $boundingBox->getMaxLatitude());
54+
$this->assertEquals($expectedBoundaries[2], $boundingBox->getMinLongitude());
55+
$this->assertEquals($expectedBoundaries[3], $boundingBox->getMaxLongitude());
56+
}
4357
}

src/Viewport/BoundingBox.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,36 @@ public function getCenterLongitude(): float
7474

7575
return $center;
7676
}
77+
78+
public function extendBy(float $paddingInPercent = 0.0, float $paddingRight = null, float $paddingBottom = null, float $paddingLeft = null): BoundingBoxInterface
79+
{
80+
$paddingTop = $paddingInPercent / (100.0 - $paddingInPercent);
81+
$paddingRight = null === $paddingRight ? $paddingTop : $paddingRight / (100.0 - $paddingRight);
82+
$paddingBottom = null === $paddingBottom ? $paddingTop : $paddingBottom / (100.0 - $paddingBottom);
83+
$paddingLeft = null === $paddingLeft ? $paddingTop : $paddingLeft / (100.0 - $paddingLeft);
84+
85+
$deltaLatitudeY = ($this->projectLatitudeToY($this->MaxLatitude) - $this->projectLatitudeToY($this->MinLatitude));
86+
$deltaLongitude = $this->MaxLongitude - $this->MinLongitude;
87+
88+
$this->MinLatitude = $this->projectYToLatitude($this->projectLatitudeToY($this->MinLatitude) - $deltaLatitudeY * $paddingBottom);
89+
$this->MaxLatitude = $this->projectYToLatitude($this->projectLatitudeToY($this->MaxLatitude) + $deltaLatitudeY * $paddingTop);
90+
$this->MinLongitude -= $deltaLongitude * $paddingLeft;
91+
$this->MaxLongitude += $deltaLongitude * $paddingRight;
92+
93+
return $this;
94+
}
95+
96+
private function projectLatitudeToY(float $latitude): float
97+
{
98+
$zoom = 10;
99+
100+
return (1.0 - log(tan($latitude * pi() / 180.0) + 1.0 / cos($latitude * pi() / 180.0)) / pi()) / 2.0 * pow(2.0, $zoom);
101+
}
102+
103+
private function projectYToLatitude(float $y): float
104+
{
105+
$zoom = 10;
106+
107+
return atan(sinh(pi() * (1.0 - 2.0 * $y / pow(2.0, $zoom)))) * 180.0 / pi();
108+
}
77109
}

src/Viewport/BoundingBoxInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ public function getMaxLongitude(): float;
2828
public function getCenterLatitude(): float;
2929

3030
public function getCenterLongitude(): float;
31+
32+
public function extendBy(float $paddingInPercent = 0.0, float $paddingRight = null, float $paddingBottom = null, float $paddingLeft = null): self;
3133
}

0 commit comments

Comments
 (0)