Skip to content

Commit

Permalink
fix minor bug for extending bounding boxes
Browse files Browse the repository at this point in the history
Releases v0.3.1
  • Loading branch information
laufhannes committed Oct 12, 2018
1 parent b4dfd11 commit 5096914
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "runalyze/static-maps",
"description": "Library to create static images from various map tile providers.",
"version": "0.1.1",
"version": "0.3.1",
"license": "MIT",
"require": {
"php": ">=7.0",
Expand Down
12 changes: 4 additions & 8 deletions src/Feature/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,13 @@ public function getBoundingBox(float $paddingInPercent = 0.0, float $paddingRigh
}
}

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

$minLatitude -= $deltaLatitude * $paddingBottom / 100.0;
$maxLatitude += $deltaLatitude * $paddingTop / 100.0;
$minLongitude -= $deltaLongitude * $paddingLeft / 100.0;
$maxLongitude += $deltaLongitude * $paddingRight / 100.0;
if (0.0 < max($paddingTop, $paddingRight, $paddingBottom, $paddingLeft)) {
return $boundingBox->extendBy($paddingTop, $paddingRight, $paddingBottom, $paddingLeft);
}

return new BoundingBox($minLatitude, $maxLatitude, $minLongitude, $maxLongitude);
return $boundingBox;
}

public function isEmpty(): bool
Expand Down
14 changes: 7 additions & 7 deletions src/Tests/Feature/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public function testBoundingBoxWithPadding()
]);

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

protected function assertThatBoundingBoxIsEqual(BoundingBoxInterface $expected, BoundingBoxInterface $actual)
{
$this->assertEquals($expected->getMinLatitude(), $actual->getMinLatitude());
$this->assertEquals($expected->getMaxLatitude(), $actual->getMaxLatitude());
$this->assertEquals($expected->getMinLongitude(), $actual->getMinLongitude());
$this->assertEquals($expected->getMaxLongitude(), $actual->getMaxLongitude());
$this->assertEquals($expected->getMinLatitude(), $actual->getMinLatitude(), '', 0.001);
$this->assertEquals($expected->getMaxLatitude(), $actual->getMaxLatitude(), '', 0.001);
$this->assertEquals($expected->getMinLongitude(), $actual->getMinLongitude(), '', 0.001);
$this->assertEquals($expected->getMaxLongitude(), $actual->getMaxLongitude(), '', 0.001);
}
}
22 changes: 18 additions & 4 deletions src/Tests/Viewport/BoundingBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public function testCenterCoordinates(float $minLatitude, float $minLongitude, f
{
$boundingBox = new BoundingBox($minLatitude, $maxLatitude, $minLongitude, $maxLongitude);

$this->assertEquals($minLatitude, $boundingBox->getMinLatitude());
$this->assertEquals($maxLatitude, $boundingBox->getMaxLatitude());
$this->assertEquals($minLongitude, $boundingBox->getMinLongitude());
$this->assertEquals($maxLongitude, $boundingBox->getMaxLongitude());
$this->assertThatBoundingBoxEquals([$minLatitude, $maxLatitude, $minLongitude, $maxLongitude], $boundingBox);
$this->assertEquals($expectedCenterLatitude, $boundingBox->getCenterLatitude());
$this->assertEquals($expectedCenterLongitude, $boundingBox->getCenterLongitude());
}
Expand All @@ -40,4 +37,21 @@ public function getProviderForCenterCoordinates()
[15.0, -160.0, 19.0, -60.0, 17.0, -110.0]
];
}

public function testExtendByZero()
{
$boundingBox = new BoundingBox(49.6, 7.6, 49.7, 7.7);

$this->assertThatBoundingBoxEquals([49.6, 7.6, 49.7, 7.7], $boundingBox);
$this->assertThatBoundingBoxEquals([49.6, 7.6, 49.7, 7.7], $boundingBox->extendBy(0.0));
$this->assertThatBoundingBoxEquals([49.6, 7.6, 49.7, 7.7], $boundingBox->extendBy(0.0, 0.0, 0.0, 0.0));
}

protected function assertThatBoundingBoxEquals(array $expectedBoundaries, BoundingBox $boundingBox)
{
$this->assertEquals($expectedBoundaries[0], $boundingBox->getMinLatitude());
$this->assertEquals($expectedBoundaries[1], $boundingBox->getMaxLatitude());
$this->assertEquals($expectedBoundaries[2], $boundingBox->getMinLongitude());
$this->assertEquals($expectedBoundaries[3], $boundingBox->getMaxLongitude());
}
}
32 changes: 32 additions & 0 deletions src/Viewport/BoundingBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,36 @@ public function getCenterLongitude(): float

return $center;
}

public function extendBy(float $paddingInPercent = 0.0, float $paddingRight = null, float $paddingBottom = null, float $paddingLeft = null): BoundingBoxInterface
{
$paddingTop = $paddingInPercent / (100.0 - $paddingInPercent);
$paddingRight = null === $paddingRight ? $paddingTop : $paddingRight / (100.0 - $paddingRight);
$paddingBottom = null === $paddingBottom ? $paddingTop : $paddingBottom / (100.0 - $paddingBottom);
$paddingLeft = null === $paddingLeft ? $paddingTop : $paddingLeft / (100.0 - $paddingLeft);

$deltaLatitudeY = ($this->projectLatitudeToY($this->MaxLatitude) - $this->projectLatitudeToY($this->MinLatitude));
$deltaLongitude = $this->MaxLongitude - $this->MinLongitude;

$this->MinLatitude = $this->projectYToLatitude($this->projectLatitudeToY($this->MinLatitude) - $deltaLatitudeY * $paddingBottom);
$this->MaxLatitude = $this->projectYToLatitude($this->projectLatitudeToY($this->MaxLatitude) + $deltaLatitudeY * $paddingTop);
$this->MinLongitude -= $deltaLongitude * $paddingLeft;
$this->MaxLongitude += $deltaLongitude * $paddingRight;

return $this;
}

private function projectLatitudeToY(float $latitude): float
{
$zoom = 10;

return (1.0 - log(tan($latitude * pi() / 180.0) + 1.0 / cos($latitude * pi() / 180.0)) / pi()) / 2.0 * pow(2.0, $zoom);
}

private function projectYToLatitude(float $y): float
{
$zoom = 10;

return atan(sinh(pi() * (1.0 - 2.0 * $y / pow(2.0, $zoom)))) * 180.0 / pi();
}
}
2 changes: 2 additions & 0 deletions src/Viewport/BoundingBoxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public function getMaxLongitude(): float;
public function getCenterLatitude(): float;

public function getCenterLongitude(): float;

public function extendBy(float $paddingInPercent = 0.0, float $paddingRight = null, float $paddingBottom = null, float $paddingLeft = null): self;
}

0 comments on commit 5096914

Please sign in to comment.