Skip to content

Commit 148efd8

Browse files
Added bounds/bbox to Graphhopper Query (#1148)
* Added bounds/bbox to Graphhopper Query * Added bounds/bbox to Graphhopper Query * very small StyleCi change * stricter check on Bounds Co-authored-by: scfJens <[email protected]>
1 parent 6c529e8 commit 148efd8

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/Provider/GraphHopper/GraphHopper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Geocoder\Exception\UnsupportedOperation;
1818
use Geocoder\Model\Address;
1919
use Geocoder\Model\AddressCollection;
20+
use Geocoder\Model\Bounds;
2021
use Geocoder\Query\GeocodeQuery;
2122
use Geocoder\Query\ReverseQuery;
2223
use Geocoder\Http\Provider\AbstractHttpProvider;
@@ -71,6 +72,11 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7172

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

75+
$bounds = $query->getBounds();
76+
if ($bounds instanceof Bounds) {
77+
$url .= sprintf('&bbox=%f,%f,%f,%f', $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
78+
}
79+
7480
return $this->executeQuery($url);
7581
}
7682

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:25:"{"hits":[],"locale":"fr"}";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}";

src/Provider/GraphHopper/Tests/GraphHopperTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Geocoder\Provider\GraphHopper\Tests;
1414

1515
use Geocoder\IntegrationTest\BaseTestCase;
16+
use Geocoder\Model\Bounds;
1617
use Geocoder\Query\GeocodeQuery;
1718
use Geocoder\Query\ReverseQuery;
1819
use Geocoder\Provider\GraphHopper\GraphHopper;
@@ -77,6 +78,47 @@ public function testGeocodeWithRealAddressAndLocale()
7778
$this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
7879
}
7980

81+
public function testGeocodeInsideBounds()
82+
{
83+
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
84+
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
85+
}
86+
87+
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
88+
$results = $provider->geocodeQuery(
89+
GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
90+
->withLocale('fr')
91+
->withBounds(new Bounds(50, -10, 55, 10))
92+
);
93+
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
94+
$this->assertCount(1, $results);
95+
96+
/** @var \Geocoder\Model\Address $result */
97+
$result = $results->first();
98+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
99+
$this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
100+
$this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
101+
$this->assertEquals('Acklam Road', $result->getStreetName());
102+
$this->assertEquals('Londres', $result->getLocality());
103+
$this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
104+
}
105+
106+
public function testGeocodeOutsideBounds()
107+
{
108+
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
109+
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
110+
}
111+
112+
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
113+
$results = $provider->geocodeQuery(
114+
GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
115+
->withLocale('fr')
116+
->withBounds(new Bounds(20, 10, 30, 20))
117+
);
118+
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
119+
$this->assertCount(0, $results);
120+
}
121+
80122
public function testReverseWithRealCoordinates()
81123
{
82124
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {

0 commit comments

Comments
 (0)