Skip to content

Commit c81fed0

Browse files
authoredJun 15, 2017
Prepare Nominatim provider for a separate package (#672)
* Prepare Nominatim provider for a separate package * cs * Use stable version of integration tests
1 parent a099dad commit c81fed0

14 files changed

+161
-401
lines changed
 

‎.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
sudo: false
3+
4+
php: 7.0
5+
6+
7+
install:
8+
- composer update --prefer-stable --prefer-dist
9+
10+
script:
11+
- ./vendor/bin/phpunit
12+

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2011 — William Durand <william.durand1@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎Nominatim.php

+23-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Geocoder\Collection;
1616
use Geocoder\Exception\InvalidServerResponse;
1717
use Geocoder\Exception\UnsupportedOperation;
18+
use Geocoder\Location;
19+
use Geocoder\Model\AddressBuilder;
1820
use Geocoder\Model\AddressCollection;
1921
use Geocoder\Query\GeocodeQuery;
2022
use Geocoder\Query\ReverseQuery;
@@ -87,10 +89,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection
8789

8890
$results = [];
8991
foreach ($places as $place) {
90-
$results[] = array_merge($this->getDefaults(), $this->xmlResultToArray($place, $place));
92+
$results[] = $this->xmlResultToArray($place, $place);
9193
}
9294

93-
return $this->returnResults($results);
95+
return new AddressCollection($results);
9496
}
9597

9698
/**
@@ -113,18 +115,23 @@ public function reverseQuery(ReverseQuery $query): Collection
113115
$addressParts = $searchResult->getElementsByTagName('addressparts')->item(0);
114116
$result = $searchResult->getElementsByTagName('result')->item(0);
115117

116-
return $this->returnResults([
117-
array_merge($this->getDefaults(), $this->xmlResultToArray($result, $addressParts)),
118-
]);
118+
return new AddressCollection([$this->xmlResultToArray($result, $addressParts)]);
119119
}
120120

121+
/**
122+
* @param \DOMElement $resultNode
123+
* @param \DOMElement $addressNode
124+
*
125+
* @return Location
126+
*/
121127
private function xmlResultToArray(\DOMElement $resultNode, \DOMElement $addressNode)
122128
{
129+
$builder = new AddressBuilder($this->getName());
123130
$adminLevels = [];
124131

125132
foreach (['state', 'county'] as $i => $tagName) {
126133
if (null !== ($adminLevel = $this->getNodeValue($addressNode->getElementsByTagName($tagName)))) {
127-
$adminLevels[] = ['name' => $adminLevel, 'level' => $i + 1];
134+
$builder->addAdminLevel($i + 1, $adminLevel, '');
128135
}
129136
}
130137

@@ -133,30 +140,23 @@ private function xmlResultToArray(\DOMElement $resultNode, \DOMElement $addressN
133140
if (!empty($postalCode)) {
134141
$postalCode = current(explode(';', $postalCode));
135142
}
136-
137-
$result = [
138-
'latitude' => $resultNode->getAttribute('lat'),
139-
'longitude' => $resultNode->getAttribute('lon'),
140-
'postalCode' => $postalCode,
141-
'adminLevels' => $adminLevels,
142-
'streetNumber' => $this->getNodeValue($addressNode->getElementsByTagName('house_number')),
143-
'streetName' => $this->getNodeValue($addressNode->getElementsByTagName('road')) ?: $this->getNodeValue($addressNode->getElementsByTagName('pedestrian')),
144-
'locality' => $this->getNodeValue($addressNode->getElementsByTagName('city')),
145-
'subLocality' => $this->getNodeValue($addressNode->getElementsByTagName('suburb')),
146-
'country' => $this->getNodeValue($addressNode->getElementsByTagName('country')),
147-
'countryCode' => strtoupper($this->getNodeValue($addressNode->getElementsByTagName('country_code'))),
148-
];
143+
$builder->setPostalCode($postalCode);
144+
$builder->setStreetName($this->getNodeValue($addressNode->getElementsByTagName('road')) ?: $this->getNodeValue($addressNode->getElementsByTagName('pedestrian')));
145+
$builder->setStreetNumber($this->getNodeValue($addressNode->getElementsByTagName('house_number')));
146+
$builder->setLocality($this->getNodeValue($addressNode->getElementsByTagName('city')));
147+
$builder->setSubLocality($this->getNodeValue($addressNode->getElementsByTagName('suburb')));
148+
$builder->setCountry($this->getNodeValue($addressNode->getElementsByTagName('country')));
149+
$builder->setCountryCode(strtoupper($this->getNodeValue($addressNode->getElementsByTagName('country_code'))));
150+
$builder->setCoordinates($resultNode->getAttribute('lat'), $resultNode->getAttribute('lon'));
149151

150152
$boundsAttr = $resultNode->getAttribute('boundingbox');
151153
if ($boundsAttr) {
152154
$bounds = [];
153-
154155
list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr);
155-
156-
$result['bounds'] = $bounds;
156+
$builder->setBounds($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']);
157157
}
158158

159-
return $result;
159+
return $builder->build();
160160
}
161161

162162
/**

‎Readme.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Bing Maps Geocoder provider
2+
[![Build Status](https://travis-ci.org/geocoder-php/nominatim-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/nominatim-provider)
3+
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/nominatim-provider/v/stable)](https://packagist.org/packages/geocoder-php/nominatim-provider)
4+
[![Total Downloads](https://poser.pugx.org/geocoder-php/nominatim-provider/downloads)](https://packagist.org/packages/geocoder-php/nominatim-provider)
5+
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/nominatim-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/nominatim-provider)
6+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
7+
8+
This is the Nominatim provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
9+
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
10+
11+
### Install
12+
13+
```bash
14+
composer require geocoder-php/nominatim-provider
15+
```
16+
17+
### Contribute
18+
19+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
20+
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
s:1313:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<searchresults timestamp='Sun, 25 Dec 16 01:12:48 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='10 Downing St, London, UK' polygon='false' exclude_place_ids='159235015' more_url='https://nominatim.openstreetmap.org/search.php?format=xml&amp;exclude_place_ids=159235015&amp;addressdetails=1&amp;q=10+Downing+St%2C+London%2C+UK'>
3+
<place place_id='159235015' osm_type='relation' osm_id='1879842' place_rank='30' boundingbox="51.5032573,51.5036483,-0.1278355,-0.1273037" lat='51.50344025' lon='-0.127708109585621' display_name='10 Downing Street, 10, Downing Street, St. James&#039;s, Covent Garden, Westminster, London, Greater London, England, SW1A 2AA, United Kingdom' class='tourism' type='attraction' importance='0.78147137691774' icon='https://nominatim.openstreetmap.org/images/mapicons/poi_point_of_interest.p.20.png'>
4+
<attraction>10 Downing Street</attraction><house_number>10</house_number><road>Downing Street</road><neighbourhood>St. James's</neighbourhood><suburb>Covent Garden</suburb><city>London</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AA</postcode><country>United Kingdom</country><country_code>gb</country_code></place></searchresults>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s:982:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<reversegeocode timestamp='Sun, 25 Dec 16 01:12:52 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='format=xml&amp;lat=38.900206&amp;lon=-77.036991&amp;addressdetails=1&amp;zoom=18&amp;accept-language=en'>
3+
<result place_id="79241348" osm_type="way" osm_id="55326891" ref="Hay-Adams Hotel" lat="38.90050395" lon="-77.0368998247139" boundingbox="38.9003173,38.9006934,-77.0371737,-77.036723">Hay-Adams Hotel, 800, 16th Street Northwest, Golden Triangle, Washington, District of Columbia, 20006, United States of America</result><addressparts><hotel>Hay-Adams Hotel</hotel><house_number>800</house_number><road>16th Street Northwest</road><neighbourhood>Golden Triangle</neighbourhood><city>Washington</city><state>District of Columbia</state><postcode>20006</postcode><country>United States of America</country><country_code>us</country_code></addressparts></reversegeocode>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
s:978:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<searchresults timestamp='Thu, 15 Jun 17 20:58:30 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='83.227.123.8' polygon='false' exclude_place_ids='173665796' more_url='https://nominatim.openstreetmap.org/search.php?q=83.227.123.8&amp;exclude_place_ids=173665796&amp;addressdetails=1&amp;format=xml'>
3+
<place place_id='173665796' osm_type='relation' osm_id='4443470' place_rank='20' boundingbox="14.6547531,14.6595016,120.9616812,120.9696958" lat='14.6570929' lon='120.965690321509' display_name='8, Caloocan, Metro Manila, Philippines' class='boundary' type='administrative' importance='0.36' icon='https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png'>
4+
<suburb>8</suburb><city>Caloocan</city><county>Caloocan</county><region>Metro Manila</region><country>Philippines</country><country_code>ph</country_code></place></searchresults>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
s:639:"<html>
2+
<head>
3+
<title>Bandwidth limit exceeded</title>
4+
</head>
5+
<body>
6+
<h1>Bandwidth limit exceeded</h1>
7+
8+
<p>You have been temporarily blocked because you have been overusing OSM's geocoding service or because you have not provided sufficient identification of your application. This block will be automatically lifted after a while. Please take the time and adapt your scripts to reduce the number of requests and make sure that you send a valid UserAgent or Referer.</p>
9+
10+
<p>For more information, consult the <a href="https://operations.osmfoundation.org/policies/nominatim/">usage policy</a> for the OSM Nominatim server.
11+
</body>
12+
</head>
13+
";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s:407:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<searchresults timestamp='Sun, 04 Jun 17 08:55:06 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='jsajhgsdkfjhsfkjhaldkadjaslgldasd' polygon='false' more_url='https://nominatim.openstreetmap.org/search.php?q=jsajhgsdkfjhsfkjhaldkadjaslgldasd&amp;addressdetails=1&amp;format=xml'>
3+
</searchresults>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s:972:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<reversegeocode timestamp='Sun, 09 Apr 17 12:49:17 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='format=xml&amp;lat=48.860000&amp;lon=2.350000&amp;addressdetails=1&amp;zoom=18'>
3+
<result place_id="6704819" osm_type="node" osm_id="700309516" ref="Bistrot Beaubourg" lat="48.8600408" lon="2.3499857" boundingbox="48.8599408,48.8601408,2.3498857,2.3500857">Bistrot Beaubourg, 25, Rue Quincampoix, Beaubourg, St-Merri, 4e, Paris, Île-de-France, 75004, France</result><addressparts><cafe>Bistrot Beaubourg</cafe><house_number>25</house_number><pedestrian>Rue Quincampoix</pedestrian><neighbourhood>Beaubourg</neighbourhood><suburb>St-Merri</suburb><city_district>4e</city_district><city>Paris</city><county>Paris</county><state>Île-de-France</state><postcode>75004</postcode><country>France</country><country_code>fr</country_code></addressparts></reversegeocode>";

‎Tests/IntegrationTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Provider\Nominatim\Tests;
12+
13+
use Geocoder\IntegrationTest\ProviderIntegrationTest;
14+
use Geocoder\Provider\Nominatim\Nominatim;
15+
use Http\Client\HttpClient;
16+
17+
/**
18+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
19+
*/
20+
class IntegrationTest extends ProviderIntegrationTest
21+
{
22+
protected $testAddress = true;
23+
protected $testReverse = true;
24+
protected $testIpv4 = true;
25+
protected $testIpv6 = false;
26+
27+
protected function createProvider(HttpClient $httpClient)
28+
{
29+
return Nominatim::withOpenStreetMapServer($httpClient);
30+
}
31+
32+
protected function getCacheDir()
33+
{
34+
return __DIR__.'/.cached_responses';
35+
}
36+
37+
protected function getApiKey()
38+
{
39+
return null;
40+
}
41+
}

‎Tests/NominatimTest.php

+11-377
Large diffs are not rendered by default.

‎composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^6.1",
20+
"geocoder-php/provider-integration-tests": "^1.0",
2021
"php-http/message": "^1.0",
2122
"php-http/curl-client": "^1.7",
2223
"nyholm/psr7": "^0.2.2"

‎phpunit.xml.dist

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
bootstrap="vendor/autoload.php"
88
>
99
<php>
10-
<ini name="error_reporting" value="-1" />
10+
<server name="BINGMAPS_API_KEY" value="YOUR_API_KEY" />
11+
<server name="USE_CACHED_RESPONSES" value="true" />
1112
</php>
1213

1314
<testsuites>

0 commit comments

Comments
 (0)
Please sign in to comment.