Skip to content

Commit

Permalink
Added bounds/bbox to Graphhopper Query (#1148)
Browse files Browse the repository at this point in the history
* Added bounds/bbox to Graphhopper Query

* Added bounds/bbox to Graphhopper Query

* very small StyleCi change

* stricter check on Bounds

Co-authored-by: scfJens <jens.jakobsen@supplychainfactory.com>
  • Loading branch information
jjjakobsen and jensjakobsenscf committed Feb 11, 2022
1 parent 18c37c4 commit fbefabb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions GraphHopper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Model\Bounds;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Geocoder\Http\Provider\AbstractHttpProvider;
Expand Down Expand Up @@ -71,6 +72,11 @@ public function geocodeQuery(GeocodeQuery $query): Collection

$url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->apiKey, $query->getLocale(), $query->getLimit());

$bounds = $query->getBounds();
if ($bounds instanceof Bounds) {
$url .= sprintf('&bbox=%f,%f,%f,%f', $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
}

return $this->executeQuery($url);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:25:"{"hits":[],"locale":"fr"}";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:409:"{"hits":[{"point":{"lat":51.52133105,"lng":-0.2027840665787136},"extent":[-0.2032737,51.5210557,-0.2019824,51.5215607],"name":"Westbourne Studios","country":"Royaume-Uni","countrycode":"GB","city":"Londres","state":"Angleterre","street":"Acklam Road","postcode":"W10 5JJ","osm_id":526221246,"osm_type":"W","housenumber":"242","osm_key":"building","osm_value":"commercial","house_number":"242"}],"locale":"fr"}";
42 changes: 42 additions & 0 deletions Tests/GraphHopperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Geocoder\Provider\GraphHopper\Tests;

use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Model\Bounds;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Geocoder\Provider\GraphHopper\GraphHopper;
Expand Down Expand Up @@ -77,6 +78,47 @@ public function testGeocodeWithRealAddressAndLocale()
$this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
}

public function testGeocodeInsideBounds()
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}

$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->geocodeQuery(
GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
->withLocale('fr')
->withBounds(new Bounds(50, -10, 55, 10))
);
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(1, $results);

/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Acklam Road', $result->getStreetName());
$this->assertEquals('Londres', $result->getLocality());
$this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
}

public function testGeocodeOutsideBounds()
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}

$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->geocodeQuery(
GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
->withLocale('fr')
->withBounds(new Bounds(20, 10, 30, 20))
);
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(0, $results);
}

public function testReverseWithRealCoordinates()
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
Expand Down

0 comments on commit fbefabb

Please sign in to comment.