Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/GeoJson/BoundingBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace App\GeoJson;

use App\GeoJson\Geometry\Geometry;
use App\GeoJson\Geometry\Point;
use App\GeoJson\Geometry\Polygon;

class BoundingBox
{
public float $minX;
Expand All @@ -19,4 +23,23 @@ public function __construct(float $minX, float $minY, float $maxX, float $maxY)
$this->maxX = $maxX;
$this->maxY = $maxY;
}

public function contains(Geometry $geometry)
{

if ($geometry instanceof Point) {
return ($geometry->x >= $this->minX) && ($geometry->x <= $this->maxX) && ($geometry->y >= $this->minY) && ($geometry->y <= $this->maxY);
}

if ($geometry instanceof Polygon) {
foreach ($geometry->points as $point) {
$contains = $this->contains($point);
if (! $contains) {
return false;
}
}

return true;
}
}
}
39 changes: 39 additions & 0 deletions tests/Unit/GeoJson/BoundingBoxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Unit\GeoJson;

use App\GeoJson\BoundingBox;
use App\GeoJson\Geometry\Point;
use App\GeoJson\Geometry\Polygon;
use Tests\TestCase;

class BoundingBoxTest extends TestCase
{
public function test_contains_point(): void
{
$bbox = new BoundingBox(minX: 1, maxX: 4, minY: 1, maxY: 4);
$pointContained = new Point(x: 2, y: 3);
$this->assertTrue($bbox->contains($pointContained));
}

public function test_does_not_contain_point(): void
{
$bbox = new BoundingBox(minX: 1, maxX: 4, minY: 1, maxY: 4);
$pointNotContained = new Point(x: 2, y: 5);
$this->assertFalse($bbox->contains($pointNotContained));
}

public function test_contains_polygon(): void
{
$bbox = new BoundingBox(minX: 1, maxX: 4, minY: 1, maxY: 4);
$pointContained = new Polygon(points: []);
$this->assertTrue($bbox->contains($pointContained));
}

public function test_does_not_contain_polygon(): void
{
$bbox = new BoundingBox(minX: 1, maxX: 4, minY: 1, maxY: 4);
$pointNotContained = new Polygon(points: []);
$this->assertFalse($bbox->contains($pointNotContained));
}
}