Skip to content

Commit ef27332

Browse files
gaelgjbelien
authored andcommitted
OpenRouteService provider (#1026)
* OpenRouteService provider * Better handling of the version to remove from Openrouteservice URLs * Prepare for v2 API * Code style fix * Strict comparison * Machine name change: openrouteservice with no dashes nor underscores
0 parents  commit ef27332

19 files changed

+584
-0
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gitattributes export-ignore
2+
.travis.yml export-ignore
3+
phpunit.xml.dist export-ignore
4+
Tests/ export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
sudo: false
3+
4+
php: 7.2
5+
6+
7+
install:
8+
- composer update --prefer-stable --prefer-dist
9+
10+
script:
11+
- composer test-ci
12+
13+
after_success:
14+
- wget https://scrutinizer-ci.com/ocular.phar
15+
- php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
16+

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## 1.0.0
6+
7+
First release of this provider.

LICENSE

Lines changed: 21 additions & 0 deletions
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.

OpenRouteService.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Provider\OpenRouteService;
14+
15+
use Geocoder\Collection;
16+
use Geocoder\Exception\InvalidCredentials;
17+
use Geocoder\Query\GeocodeQuery;
18+
use Geocoder\Query\ReverseQuery;
19+
use Geocoder\Provider\Pelias\Pelias;
20+
use Geocoder\Provider\Provider;
21+
use Http\Client\HttpClient;
22+
23+
final class OpenRouteService extends Pelias implements Provider
24+
{
25+
const API_URL = 'https://api.openrouteservice.org/geocode';
26+
27+
const API_VERSION = 1;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $apiKey;
33+
34+
/**
35+
* @param HttpClient $client an HTTP adapter
36+
* @param string $apiKey an API key
37+
*/
38+
public function __construct(HttpClient $client, string $apiKey)
39+
{
40+
if (empty($apiKey)) {
41+
throw new InvalidCredentials('No API key provided.');
42+
}
43+
44+
$this->apiKey = $apiKey;
45+
parent::__construct($client, self::API_URL, self::API_VERSION);
46+
47+
/*
48+
* Openrouteservice does not use /v1 in first version, but plan to add
49+
* /v2 in next version.
50+
*
51+
* @see https://ask.openrouteservice.org/t/pelias-version-in-api-url/1021
52+
*/
53+
if (self::API_VERSION === 1) {
54+
$this->root = self::API_URL;
55+
}
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function geocodeQuery(GeocodeQuery $query): Collection
62+
{
63+
$url = $this->getGeocodeQueryUrl($query, [
64+
'api_key' => $this->apiKey,
65+
]);
66+
67+
return $this->executeQuery($url);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function reverseQuery(ReverseQuery $query): Collection
74+
{
75+
$url = $this->getReverseQueryUrl($query, [
76+
'api_key' => $this->apiKey,
77+
]);
78+
79+
return $this->executeQuery($url);
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
public function getName(): string
86+
{
87+
return 'openrouteservice';
88+
}
89+
}

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# OpenRouteService Geocoder provider
2+
[![Build Status](https://travis-ci.org/geocoder-php/openrouteservice-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/openrouteservice-provider)
3+
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/openrouteservice-provider/v/stable)](https://packagist.org/packages/geocoder-php/openrouteservice-provider)
4+
[![Total Downloads](https://poser.pugx.org/geocoder-php/openrouteservice-provider/downloads)](https://packagist.org/packages/geocoder-php/openrouteservice-provider)
5+
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/openrouteservice-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/openrouteservice-provider)
6+
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/openrouteservice-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/openrouteservice-provider)
7+
[![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/openrouteservice-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/openrouteservice-provider)
8+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
9+
10+
This is the OpenRouteService provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
11+
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
12+
13+
### Install
14+
15+
```bash
16+
composer require geocoder-php/openrouteservice-provider
17+
```
18+
19+
### API Documentation
20+
21+
OpenRouteService uses the Pelias Geocoder under the hood. You can view it's [documentation here](https://github.com/pelias/documentation).
22+
The base API endpoint is https://api.openrouteservice.org/geocode.
23+
24+
### Contribute
25+
26+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
27+
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:6375:"{"geocoding":{"version":"0.2","attribution":"http://192.168.2.20:3100/attribution","query":{"size":5,"private":false,"point.lat":49.1390924,"point.lon":1.6572462,"boundary.circle.lat":49.1390924,"boundary.circle.lon":1.6572462,"lang":{"name":"English","iso6391":"en","iso6393":"eng","defaulted":true},"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1571320064483},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657246,49.139092]},"properties":{"id":"node/1564292039","gid":"openstreetmap:venue:node/1564292039","layer":"venue","source":"openstreetmap","source_id":"node/1564292039","name":"Les Jardins d'Épicure","confidence":1,"distance":0,"accuracy":"point","country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Ile-of-France","macroregion_gid":"whosonfirst:macroregion:404227465","macroregion_a":"IF","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","region_a":"VO","macrocounty":"arrondissement of Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-en-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bray-et-Lû","locality_gid":"whosonfirst:locality:1125876391","neighbourhood":"Bus-Saint-Rémy","neighbourhood_gid":"whosonfirst:neighbourhood:1360698119","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"Les Jardins d'Épicure, Bray-et-Lû, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657114,49.13946]},"properties":{"id":"fr/val_doise:1084c45fa72d54fa","gid":"openaddresses:address:fr/val_doise:1084c45fa72d54fa","layer":"address","source":"openaddresses","source_id":"fr/val_doise:1084c45fa72d54fa","name":"16 Grande Rue","housenumber":"16","street":"Grande Rue","confidence":0.8,"distance":0.042,"accuracy":"point","country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Ile-of-France","macroregion_gid":"whosonfirst:macroregion:404227465","macroregion_a":"IF","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","region_a":"VO","macrocounty":"arrondissement of Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-en-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bray-et-Lû","locality_gid":"whosonfirst:locality:1125876391","neighbourhood":"Bus-Saint-Rémy","neighbourhood_gid":"whosonfirst:neighbourhood:1360698119","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"16 Grande Rue, Bray-et-Lû, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657682,49.139611]},"properties":{"id":"polyline:22476882","gid":"openstreetmap:street:polyline:22476882","layer":"street","source":"openstreetmap","source_id":"polyline:22476882","name":"Grande Rue","street":"Grande Rue","confidence":0.8,"distance":0.066,"accuracy":"centroid","country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Ile-of-France","macroregion_gid":"whosonfirst:macroregion:404227465","macroregion_a":"IF","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","region_a":"VO","macrocounty":"arrondissement of Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-en-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bray-et-Lû","locality_gid":"whosonfirst:locality:1125876391","neighbourhood":"Bus-Saint-Rémy","neighbourhood_gid":"whosonfirst:neighbourhood:1360698119","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"Grande Rue, Bray-et-Lû, France"},"bbox":[1.656169,49.139324,1.65918,49.13993]},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.658166,49.139008]},"properties":{"id":"fr/val_doise:9a64597420016413","gid":"openaddresses:address:fr/val_doise:9a64597420016413","layer":"address","source":"openaddresses","source_id":"fr/val_doise:9a64597420016413","name":"2 Rue Des Pres","housenumber":"2","street":"Rue Des Pres","confidence":0.8,"distance":0.068,"accuracy":"point","country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Ile-of-France","macroregion_gid":"whosonfirst:macroregion:404227465","macroregion_a":"IF","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","region_a":"VO","macrocounty":"arrondissement of Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-en-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bray-et-Lû","locality_gid":"whosonfirst:locality:1125876391","neighbourhood":"Bus-Saint-Rémy","neighbourhood_gid":"whosonfirst:neighbourhood:1360698119","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"2 Rue Des Pres, Bray-et-Lû, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.658165,49.139241]},"properties":{"id":"fr/val_doise:af72ea1457abb8e6","gid":"openaddresses:address:fr/val_doise:af72ea1457abb8e6","layer":"address","source":"openaddresses","source_id":"fr/val_doise:af72ea1457abb8e6","name":"3 Rue Des Pres","housenumber":"3","street":"Rue Des Pres","confidence":0.8,"distance":0.069,"accuracy":"point","country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Ile-of-France","macroregion_gid":"whosonfirst:macroregion:404227465","macroregion_a":"IF","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","region_a":"VO","macrocounty":"arrondissement of Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-en-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bray-et-Lû","locality_gid":"whosonfirst:locality:1125876391","neighbourhood":"Bus-Saint-Rémy","neighbourhood_gid":"whosonfirst:neighbourhood:1360698119","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"3 Rue Des Pres, Bray-et-Lû, France"}}],"bbox":[1.656169,49.139008,1.65918,49.13993]}";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:4534:"{"geocoding":{"version":"0.2","attribution":"http://192.168.2.20:3100/attribution","query":{"text":"Hanover","size":5,"layers":["venue","street","country","macroregion","region","county","localadmin","locality","borough","neighbourhood","continent","empire","dependency","macrocounty","macrohood","microhood","disputed","postalcode","ocean","marinearea"],"private":false,"lang":{"name":"English","iso6391":"en","iso6393":"eng","defaulted":true},"querySize":20,"parser":"libpostal","parsed_text":{"city":"hanover"}},"warnings":["performance optimization: excluding 'address' layer"],"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1571320064675},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[9.787455,52.379952]},"properties":{"id":"101748819","gid":"whosonfirst:locality:101748819","layer":"locality","source":"whosonfirst","source_id":"101748819","name":"Hanover","confidence":1,"match_type":"exact","accuracy":"centroid","country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Niedersachsen","region_gid":"whosonfirst:region:85682555","region_a":"NI","county":"Hannover ","county_gid":"whosonfirst:county:102063941","county_a":"HA","locality":"Hanover","locality_gid":"whosonfirst:locality:101748819","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"Hanover, Germany"},"bbox":[9.6351335,52.3170855,9.8810641501,52.4482873275]},{"type":"Feature","geometry":{"type":"Point","coordinates":[9.73711,52.37362]},"properties":{"id":"1125331247","gid":"whosonfirst:localadmin:1125331247","layer":"localadmin","source":"whosonfirst","source_id":"1125331247","name":"Hanover","confidence":1,"match_type":"exact","accuracy":"centroid","country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Niedersachsen","region_gid":"whosonfirst:region:85682555","region_a":"NI","county":"Hannover ","county_gid":"whosonfirst:county:102063941","county_a":"HA","localadmin":"Hanover","localadmin_gid":"whosonfirst:localadmin:1125331247","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"Hanover, Germany"},"bbox":[9.73711,52.37362,9.73711,52.37362]},{"type":"Feature","geometry":{"type":"Point","coordinates":[-78.107687,18.401137]},"properties":{"id":"85672563","gid":"whosonfirst:region:85672563","layer":"region","source":"whosonfirst","source_id":"85672563","name":"Hanover","confidence":0.3,"match_type":"fallback","accuracy":"centroid","country":"Jamaica","country_gid":"whosonfirst:country:85632215","country_a":"JAM","region":"Hanover","region_gid":"whosonfirst:region:85672563","region_a":"HA","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"Hanover, Jamaica"},"bbox":[-78.344668,18.307772,-77.909728,18.462863]},{"type":"Feature","geometry":{"type":"Point","coordinates":[-76.72414,39.19289]},"properties":{"id":"1125874787","gid":"whosonfirst:locality:1125874787","layer":"locality","source":"whosonfirst","source_id":"1125874787","name":"Hanover","confidence":1,"match_type":"exact","accuracy":"centroid","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Maryland","region_gid":"whosonfirst:region:85688501","region_a":"MD","county":"Howard County","county_gid":"whosonfirst:county:102084263","county_a":"HO","locality":"Hanover","locality_gid":"whosonfirst:locality:1125874787","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"Hanover, MD, USA"},"bbox":[-76.72414,39.19289,-76.72414,39.19289]},{"type":"Feature","geometry":{"type":"Point","coordinates":[-70.81199,42.11316]},"properties":{"id":"1125765125","gid":"whosonfirst:locality:1125765125","layer":"locality","source":"whosonfirst","source_id":"1125765125","name":"Hanover","confidence":1,"match_type":"exact","accuracy":"centroid","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Massachusetts","region_gid":"whosonfirst:region:85688645","region_a":"MA","county":"Plymouth County","county_gid":"whosonfirst:county:102084367","county_a":"PL","localadmin":"Hanover","localadmin_gid":"whosonfirst:localadmin:404476511","locality":"Hanover","locality_gid":"whosonfirst:locality:1125765125","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"Hanover, MA, USA"},"bbox":[-70.81199,42.11316,-70.81199,42.11316]}],"bbox":[-78.344668,18.307772,9.8810641501,52.4482873275]}";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:1586:"{"geocoding":{"version":"0.2","attribution":"http://192.168.2.20:3100/attribution","query":{"text":"10 Downing St, London, UK","size":5,"private":false,"lang":{"name":"English","iso6391":"en","iso6393":"eng","defaulted":true},"querySize":20,"parser":"libpostal","parsed_text":{"number":"10","street":"downing st","city":"london","country":"uk"}},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1571320063378},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-0.127636,51.50328]},"properties":{"id":"relation/1879842","gid":"openstreetmap:address:relation/1879842","layer":"address","source":"openstreetmap","source_id":"relation/1879842","name":"10 Downing Street","housenumber":"10","street":"Downing Street","postalcode":"SW1A 2AA","confidence":1,"match_type":"exact","accuracy":"point","country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Greater London","region_gid":"whosonfirst:region:1360698645","locality":"London","locality_gid":"whosonfirst:locality:101750367","borough":"Westminster","borough_gid":"whosonfirst:borough:1158857245","neighbourhood":"Whitehall","neighbourhood_gid":"whosonfirst:neighbourhood:85793317","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"10 Downing Street, London, England, United Kingdom","addendum":{"osm":{"wikidata":"Q169101","wikipedia":"en:10 Downing Street"}}}}],"bbox":[-0.127636,51.50328,-0.127636,51.50328]}";

0 commit comments

Comments
 (0)