Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add PickPoint provider. #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2017
Merged
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
Add PickPoint provider.
Fixed style.

Apply review notes.
  • Loading branch information
cylon-v committed Jun 28, 2017
commit e0167b66a114ce3371beda49e35c3297c760f70c
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<server name="OPENCAGE_API_KEY" value="YOUR_GEOCODING_KEY" />
<server name="MAPZEN_API_KEY" value="YOUR_MAPZEN_API_KEY" />
<server name="IPINFODB_API_KEY" value="YOUR_API_KEY" />
<server name="PICKPOINT_API_KEY" value="YOUR_API_KEY" />
<!--<server name="MAXMIND_API_KEY" value="YOUR_API_KEY" />-->
</php>

Expand Down
4 changes: 4 additions & 0 deletions src/Provider/PickPoint/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
Tests/ export-ignore
3 changes: 3 additions & 0 deletions src/Provider/PickPoint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
16 changes: 16 additions & 0 deletions src/Provider/PickPoint/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: php
sudo: false

php: 7.0


install:
- composer update --prefer-stable --prefer-dist

script:
- composer test-ci

after_success:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml

7 changes: 7 additions & 0 deletions src/Provider/PickPoint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 4.0.0

First release of this library.
21 changes: 21 additions & 0 deletions src/Provider/PickPoint/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 — Vladimir Kalinkin <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
186 changes: 186 additions & 0 deletions src/Provider/PickPoint/PickPoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Provider\PickPoint;

use Geocoder\Collection;
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Location;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Provider\Provider;
use Http\Client\HttpClient;

/**
* @author Vladimir Kalinkin <[email protected]>
*/
final class PickPoint extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
const BASE_API_URL = 'https://api.pickpoint.io/v1';

/**
* @var string
*/
private $apiKey;

/**
* @param HttpClient $client an HTTP adapter
* @param string $apiKey an API key
*/
public function __construct(HttpClient $client, string $apiKey)
{
if (empty($apiKey)) {
throw new InvalidCredentials('No API key provided.');
}

$this->apiKey = $apiKey;
parent::__construct($client);
}

/**
* {@inheritdoc}
*/
public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();

$url = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $query->getLimit());
$content = $this->executeQuery($url, $query->getLocale());

$doc = new \DOMDocument();
if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) {
throw InvalidServerResponse::create($url);
}

$searchResult = $doc->getElementsByTagName('searchresults')->item(0);
$places = $searchResult->getElementsByTagName('place');

if (null === $places || 0 === $places->length) {
return new AddressCollection([]);
}

$results = [];
foreach ($places as $place) {
$results[] = $this->xmlResultToArray($place, $place);
}

return new AddressCollection($results);
}

/**
* {@inheritdoc}
*/
public function reverseQuery(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$longitude = $coordinates->getLongitude();
$latitude = $coordinates->getLatitude();
$url = sprintf($this->getReverseEndpointUrl(), $latitude, $longitude, $query->getData('zoom', 18));
$content = $this->executeQuery($url, $query->getLocale());

$doc = new \DOMDocument();
if (!@$doc->loadXML($content) || $doc->getElementsByTagName('error')->length > 0) {
return new AddressCollection([]);
}

$searchResult = $doc->getElementsByTagName('reversegeocode')->item(0);
$addressParts = $searchResult->getElementsByTagName('addressparts')->item(0);
$result = $searchResult->getElementsByTagName('result')->item(0);

return new AddressCollection([$this->xmlResultToArray($result, $addressParts)]);
}

/**
* @param \DOMElement $resultNode
* @param \DOMElement $addressNode
*
* @return Location
*/
private function xmlResultToArray(\DOMElement $resultNode, \DOMElement $addressNode): Location
{
$builder = new AddressBuilder($this->getName());

foreach (['state', 'county'] as $i => $tagName) {
if (null !== ($adminLevel = $this->getNodeValue($addressNode->getElementsByTagName($tagName)))) {
$builder->addAdminLevel($i + 1, $adminLevel, '');
}
}

// get the first postal-code when there are many
$postalCode = $this->getNodeValue($addressNode->getElementsByTagName('postcode'));
if (!empty($postalCode)) {
$postalCode = current(explode(';', $postalCode));
}
$builder->setPostalCode($postalCode);
$builder->setStreetName($this->getNodeValue($addressNode->getElementsByTagName('road')) ?: $this->getNodeValue($addressNode->getElementsByTagName('pedestrian')));
$builder->setStreetNumber($this->getNodeValue($addressNode->getElementsByTagName('house_number')));
$builder->setLocality($this->getNodeValue($addressNode->getElementsByTagName('city')));
$builder->setSubLocality($this->getNodeValue($addressNode->getElementsByTagName('suburb')));
$builder->setCountry($this->getNodeValue($addressNode->getElementsByTagName('country')));
$builder->setCountryCode(strtoupper($this->getNodeValue($addressNode->getElementsByTagName('country_code'))));
$builder->setCoordinates($resultNode->getAttribute('lat'), $resultNode->getAttribute('lon'));

$boundsAttr = $resultNode->getAttribute('boundingbox');
if ($boundsAttr) {
$bounds = [];
list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr);
$builder->setBounds($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']);
}

return $builder->build();
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'pickpoint';
}

/**
* @param string $url
* @param string|null $locale
*
* @return string
*/
private function executeQuery(string $url, string $locale = null): string
{
if (null !== $locale) {
$url = sprintf('%s&accept-language=%s', $url, $locale);
}

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

private function getGeocodeEndpointUrl(): string
{
return self::BASE_API_URL.'/forward?q=%s&format=xml&addressdetails=1&limit=%d&key='.$this->apiKey;
}

private function getReverseEndpointUrl(): string
{
return self::BASE_API_URL.'/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=%d&key='.$this->apiKey;
}

private function getNodeValue(\DOMNodeList $element)
{
return $element->length ? $element->item(0)->nodeValue : null;
}
}
22 changes: 22 additions & 0 deletions src/Provider/PickPoint/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# PickPoint Geocoder provider
[![Build Status](https://travis-ci.org/geocoder-php/pickpoint-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/pickpoint-provider)
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/pickpoint-provider/v/stable)](https://packagist.org/packages/geocoder-php/pickpoint-provider)
[![Total Downloads](https://poser.pugx.org/geocoder-php/pickpoint-provider/downloads)](https://packagist.org/packages/geocoder-php/pickpoint-provider)
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/pickpoint-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/pickpoint-provider)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/pickpoint-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/pickpoint-provider)
[![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/pickpoint-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/pickpoint-provider)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

This is the PickPoint provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.

### Install

```bash
composer require geocoder-php/pickpoint-provider
```

### Contribute

Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:1025:"<?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Tue, 27 Jun 17 20:19:20 +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&amp;key=pqxQyozFtbW9wx2xr93h'>
<result place_id="7270079" 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, 3e, Paris, Île-de-France, France métropolitaine, 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>3e</city_district><city>Paris</city><county>Paris</county><state>Île-de-France</state><country>France</country><postcode>75004</postcode><country_code>fr</country_code></addressparts></reversegeocode>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:414:"<?xml version="1.0" encoding="UTF-8" ?>
<searchresults timestamp='Tue, 27 Jun 17 20:19:19 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='2001:0db8:0000:0042:0000:8a2e:0370:7334' polygon='false' more_url='http://127.0.0.1/search.php?q=2001%3A0db8%3A0000%3A0042%3A0000%3A8a2e%3A0370%3A7334&amp;addressdetails=1&amp;format=xml'>
</searchresults>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:1054:"<?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Tue, 27 Jun 17 20:19:14 +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;key=pqxQyozFtbW9wx2xr93h&amp;accept-language=en'>
<result place_id="82486650" 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, Logan Circle, 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><suburb>Logan Circle</suburb><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 number Diff line number Diff line change
@@ -0,0 +1,4 @@
s:1275:"<?xml version="1.0" encoding="UTF-8" ?>
<searchresults timestamp='Tue, 27 Jun 17 20:19:12 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='10 Downing St, London, UK' polygon='false' exclude_place_ids='174040554' more_url='http://127.0.0.1/search.php?q=10+Downing+St%2C+London%2C+UK&amp;exclude_place_ids=174040554&amp;addressdetails=1&amp;format=xml'>
<place place_id='174040554' osm_type='relation' osm_id='1879842' place_rank='30' boundingbox="51.5032573,51.5036483,-0.1278356,-0.1273038" lat='51.50344025' lon='-0.127708209585621' 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.89147137691773' icon='http://127.0.0.1/images/mapicons/poi_point_of_interest.p.20.png'>
<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 number Diff line number Diff line change
@@ -0,0 +1,8 @@
s:3727:"<?xml version="1.0" encoding="UTF-8" ?>
<searchresults timestamp='Tue, 27 Jun 17 20:19:18 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='83.227.123.8' polygon='false' exclude_place_ids='172076749,164105368,109007929,164035111,163808290,109007808,86924766' more_url='http://nominatim-1/search.php?format=xml&amp;exclude_place_ids=172076749,164105368,109007929,164035111,163808290,109007808,86924766&amp;addressdetails=1&amp;q=83.227.123.8'>
<place place_id='172076749' 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.25' icon='http://nominatim-1/images/mapicons/poi_boundary_administrative.p.20.png'>
<suburb>8</suburb><city>Caloocan</city><county>Caloocan</county><region>Metro Manila</region><country>Philippines</country><country_code>ph</country_code></place><place place_id='164105368' osm_type='way' osm_id='442213181' place_rank='26' boundingbox="54.2570501,54.2572274,18.6509649,18.6512954" lat='54.2571653' lon='18.6511709' display_name='227, Osiedle Komarowo, Osiedle Lotników, Pruszcz Gdański, powiat gdański, Pomeranian Voivodeship, 83-000, Poland' class='highway' type='secondary' importance='0.1'>
<road>227</road><neighbourhood>Osiedle Komarowo</neighbourhood><suburb>Osiedle Lotników</suburb><town>Pruszcz Gdański</town><county>Pruszcz Gdański</county><state>Pomeranian Voivodeship</state><postcode>83-000</postcode><country>Poland</country><country_code>pl</country_code></place><place place_id='109007929' osm_type='way' osm_id='173224503' place_rank='26' boundingbox="54.2581747,54.258389,18.6497217,18.6498591" lat='54.2582805' lon='18.6497664' display_name='Powstańców Warszawy, Osiedle Komarowo, Osiedle Lotników, Pruszcz Gdański, powiat gdański, Pomeranian Voivodeship, 83-000, Poland' class='highway' type='secondary' importance='0.1'>
<road>Powstańców Warszawy</road><neighbourhood>Osiedle Komarowo</neighbourhood><suburb>Osiedle Lotników</suburb><town>Pruszcz Gdański</town><county>Pruszcz Gdański</county><state>Pomeranian Voivodeship</state><postcode>83-000</postcode><country>Poland</country><country_code>pl</country_code></place><place place_id='109007808' osm_type='way' osm_id='173224265' place_rank='26' boundingbox="54.258389,54.2599568,18.6497217,18.6505493" lat='54.2592181' lon='18.650227' display_name='Fryderyka Chopina, Osiedle Komarowo, Osiedle Lotników, Pruszcz Gdański, powiat gdański, Pomeranian Voivodeship, 83-000, Poland' class='highway' type='secondary' importance='0.1'>
<road>Fryderyka Chopina</road><neighbourhood>Osiedle Komarowo</neighbourhood><suburb>Osiedle Lotników</suburb><town>Pruszcz Gdański</town><county>Pruszcz Gdański</county><state>Pomeranian Voivodeship</state><postcode>83-000</postcode><country>Poland</country><country_code>pl</country_code></place><place place_id='86924766' osm_type='way' osm_id='89470423' place_rank='26' boundingbox="54.2599568,54.2604446,18.6505493,18.6506786" lat='54.2603127' lon='18.6506786' display_name='Fryderyka Chopina, Osiedle Mikołaja Kopernika, Osiedle Lotników, Pruszcz Gdański, powiat gdański, Pomeranian Voivodeship, 83-000, Poland' class='highway' type='secondary' importance='0.1'>
<road>Fryderyka Chopina</road><neighbourhood>Osiedle Mikołaja Kopernika</neighbourhood><suburb>Osiedle Lotników</suburb><town>Pruszcz Gdański</town><county>Pruszcz Gdański</county><state>Pomeranian Voivodeship</state><postcode>83-000</postcode><country>Poland</country><country_code>pl</country_code></place></searchresults>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:368:"<?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Tue, 27 Jun 17 20:19:14 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='format=xml&amp;lat=0.000000&amp;lon=0.000000&amp;addressdetails=1&amp;zoom=18&amp;key=pqxQyozFtbW9wx2xr93h'>
<error>Unable to geocode</error></reversegeocode>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:413:"<?xml version="1.0" encoding="UTF-8" ?>
<searchresults timestamp='Tue, 27 Jun 17 20:19:13 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='jsajhgsdkfjhsfkjhaldkadjaslgldasd' polygon='false' more_url='http://nominatim-1/search.php?format=xml&amp;exclude_place_ids=&amp;addressdetails=1&amp;q=jsajhgsdkfjhsfkjhaldkadjaslgldasd'>
</searchresults>";
36 changes: 36 additions & 0 deletions src/Provider/PickPoint/Tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Provider\PickPoint\Tests;

use Geocoder\IntegrationTest\ProviderIntegrationTest;
use Geocoder\Provider\PickPoint\PickPoint;
use Http\Client\HttpClient;

/**
* @author Vladimir Kalinkin <[email protected]>
*/
class IntegrationTest extends ProviderIntegrationTest
{
protected function createProvider(HttpClient $httpClient)
{
return new PickPoint($httpClient, $this->getApiKey());
}

protected function getCacheDir()
{
return __DIR__.'/.cached_responses';
}

protected function getApiKey()
{
return $_SERVER['PICKPOINT_API_KEY'];
}
}
Loading