Skip to content
Permalink
Browse files

feat: add geocode-earth provider (#962)

* feat: add geocode-earth provider

This adds a new provider for https://geocode.earth/

Resolves #808

* fix: update tests + add cached responses

* fix: update docs

* chore: add env to main test config
  • Loading branch information...
atymic authored and jbelien committed Jun 14, 2019
1 parent cd34e16 commit abbe45619ed9e22170ad9927fdef51e5d6029aa7
Showing with 717 additions and 0 deletions.
  1. +1 −0 phpunit.xml.dist
  2. +4 −0 src/Provider/GeocodeEarth/.gitattributes
  3. +3 −0 src/Provider/GeocodeEarth/.gitignore
  4. +16 −0 src/Provider/GeocodeEarth/.travis.yml
  5. +7 −0 src/Provider/GeocodeEarth/CHANGELOG.md
  6. +222 −0 src/Provider/GeocodeEarth/GeocodeEarth.php
  7. +21 −0 src/Provider/GeocodeEarth/LICENSE
  8. +27 −0 src/Provider/GeocodeEarth/README.md
  9. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_234c8399af607fe063e73b6cf481af365bb8ab59
  10. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_2b0341688ccd0380c46c97b2fdacb4d1361f965d
  11. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_323de0a6bf932727e5f6e0d21390a573a94b8d91
  12. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_67c28face3250fb85c57e575427d08672554faa8
  13. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_7520b32bf6580715c8f16cbb420945d9bc231e85
  14. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_77427123dd19ef22423f85d726bc945336984856
  15. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_bd7b556e223f9c6dda6171782466a69442c53862
  16. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_cf59f49faa404ec2e5739ce7a31d962b2d4913b5
  17. +1 −0 ...r/GeocodeEarth/Tests/.cached_responses/api.geocode.earth_ed417f8ba91150415a3942713573e7f3f2bb773e
  18. +287 −0 src/Provider/GeocodeEarth/Tests/GeocodeEarthTest.php
  19. +46 −0 src/Provider/GeocodeEarth/Tests/IntegrationTest.php
  20. +45 −0 src/Provider/GeocodeEarth/composer.json
  21. +29 −0 src/Provider/GeocodeEarth/phpunit.xml.dist
@@ -36,6 +36,7 @@
<server name="OPENCAGE_API_KEY" value="YOUR_GEOCODING_KEY" />
<server name="PICKPOINT_API_KEY" value="YOUR_API_KEY" />
<server name="TOMTOM_MAP_KEY" value="YOUR_MAP_KEY" />
<server name="GEOCODE_EARTH_API_KEY" value="YOUR_GEOCODE_EARTH_API_KEY" />
<!--<server name="MAXMIND_API_KEY" value="YOUR_API_KEY" />-->
</php>

@@ -0,0 +1,4 @@
.gitattributes export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
Tests/ export-ignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
@@ -0,0 +1,16 @@
language: php
sudo: false

php: 7.2


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

@@ -0,0 +1,7 @@
# Change Log

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

## 1.0.0

First release of this provider.
@@ -0,0 +1,222 @@
<?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\GeocodeEarth;
use Geocoder\Collection;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\Address;
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;
final class GeocodeEarth extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
const GEOCODE_ENDPOINT_URL = 'https://api.geocode.earth/v1/search?text=%s&api_key=%s&size=%d';
/**
* @var string
*/
const REVERSE_ENDPOINT_URL = 'https://api.geocode.earth/v1/reverse?point.lat=%f&point.lon=%f&api_key=%s&size=%d';
/**
* @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();
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The GeocodeEarth provider does not support IP addresses, only street addresses.');
}
$url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->apiKey, $query->getLimit());
return $this->executeQuery($url);
}
/**
* {@inheritdoc}
*/
public function reverseQuery(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$longitude = $coordinates->getLongitude();
$latitude = $coordinates->getLatitude();
$url = sprintf(self::REVERSE_ENDPOINT_URL, $latitude, $longitude, $this->apiKey, $query->getLimit());
return $this->executeQuery($url);
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'geocode_earth';
}
/**
* @param $url
*
* @return Collection
*/
private function executeQuery(string $url): AddressCollection
{
$content = $this->getUrlContents($url);
$json = json_decode($content, true);
if (isset($json['meta'])) {
switch ($json['meta']['status_code']) {
case 403:
throw new InvalidCredentials('Invalid or missing api key.');
case 429:
throw new QuotaExceeded('Valid request but quota exceeded.');
}
}
if (!isset($json['type']) || 'FeatureCollection' !== $json['type'] || !isset($json['features']) || 0 === count($json['features'])) {
return new AddressCollection([]);
}
$locations = $json['features'];
if (empty($locations)) {
return new AddressCollection([]);
}
$results = [];
foreach ($locations as $location) {
$bounds = [
'south' => null,
'west' => null,
'north' => null,
'east' => null,
];
if (isset($location['bbox'])) {
$bounds = [
'south' => $location['bbox'][3],
'west' => $location['bbox'][2],
'north' => $location['bbox'][1],
'east' => $location['bbox'][0],
];
}
$props = $location['properties'];
$adminLevels = [];
foreach (['region', 'county', 'locality', 'macroregion', 'country'] as $i => $component) {
if (isset($props[$component])) {
$adminLevels[] = ['name' => $props[$component], 'level' => $i + 1];
}
}
$results[] = Address::createFromArray([
'providedBy' => $this->getName(),
'latitude' => $location['geometry']['coordinates'][1],
'longitude' => $location['geometry']['coordinates'][0],
'bounds' => $bounds,
'streetNumber' => isset($props['housenumber']) ? $props['housenumber'] : null,
'streetName' => isset($props['street']) ? $props['street'] : null,
'subLocality' => isset($props['neighbourhood']) ? $props['neighbourhood'] : null,
'locality' => isset($props['locality']) ? $props['locality'] : null,
'postalCode' => isset($props['postalcode']) ? $props['postalcode'] : null,
'adminLevels' => $adminLevels,
'country' => isset($props['country']) ? $props['country'] : null,
'countryCode' => isset($props['country_a']) ? strtoupper($props['country_a']) : null,
]);
}
return new AddressCollection($results);
}
/**
* @param array $components
*
* @return null|string
*/
protected function guessLocality(array $components)
{
$localityKeys = ['city', 'town', 'village', 'hamlet'];
return $this->guessBestComponent($components, $localityKeys);
}
/**
* @param array $components
*
* @return null|string
*/
protected function guessStreetName(array $components)
{
$streetNameKeys = ['road', 'street', 'street_name', 'residential'];
return $this->guessBestComponent($components, $streetNameKeys);
}
/**
* @param array $components
*
* @return null|string
*/
protected function guessSubLocality(array $components)
{
$subLocalityKeys = ['neighbourhood', 'city_district'];
return $this->guessBestComponent($components, $subLocalityKeys);
}
/**
* @param array $components
* @param array $keys
*
* @return null|string
*/
protected function guessBestComponent(array $components, array $keys)
{
foreach ($keys as $key) {
if (isset($components[$key]) && !empty($components[$key])) {
return $components[$key];
}
}
return null;
}
}
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2011 — William Durand <william.durand1@gmail.com>

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.
@@ -0,0 +1,27 @@
# Geocode Earth Geocoder provider
[![Build Status](https://travis-ci.org/geocoder-php/geocode-earth-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/geocode-earth-provider)
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/geocode-earth-provider/v/stable)](https://packagist.org/packages/geocoder-php/geocode-earth-provider)
[![Total Downloads](https://poser.pugx.org/geocoder-php/geocode-earth-provider/downloads)](https://packagist.org/packages/geocoder-php/geocode-earth-provider)
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/geocode-earth-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/geocode-earth-provider)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/geocode-earth-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/geocode-earth-provider)
[![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/geocode-earth-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/geocode-earth-provider)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

This is the Geocode Earth 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/geocode-earth-provider
```

### API Documentation

Geocode Earth uses the Pelias Geocoder under the hood. You can view it's [documentation here](https://github.com/pelias/documentation).
The base API endpoint is https://api.geocode.earth.

### 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).
@@ -0,0 +1 @@
s:717:"{"geocoding":{"version":"0.2","attribution":"https://geocode.earth/guidelines","query":{"text":"jsajhgsdkfjhsfkjhaldkadjaslgldasd","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":"addressit","parsed_text":{}},"warnings":["performance optimization: excluding 'address' layer"],"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1559249221332},"type":"FeatureCollection","features":[]}";
@@ -0,0 +1 @@
s:4698:"{"geocoding":{"version":"0.2","attribution":"https://geocode.earth/guidelines","query":{"size":5,"private":false,"point.lat":38.900206,"point.lon":-77.036991,"boundary.circle.lat":38.900206,"boundary.circle.lon":-77.036991,"lang":{"name":"English","iso6391":"en","iso6393":"eng","defaulted":true},"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1559249222407},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-77.0372,38.90039]},"properties":{"id":"4140517","gid":"geonames:venue:4140517","layer":"venue","source":"geonames","source_id":"4140517","name":"Slidell House (historical)","confidence":0.8,"distance":0.027,"accuracy":"point","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"District of Columbia","region_gid":"whosonfirst:region:85688741","region_a":"DC","locality":"Washington","locality_gid":"whosonfirst:locality:85931779","neighbourhood":"Downtown","neighbourhood_gid":"whosonfirst:neighbourhood:85866033","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"Slidell House (historical), Washington, DC, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-77.0367,38.9003]},"properties":{"id":"6482379","gid":"geonames:venue:6482379","layer":"venue","source":"geonames","source_id":"6482379","name":"The Hay Adams across from the White House","confidence":0.8,"distance":0.027,"accuracy":"point","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"District of Columbia","region_gid":"whosonfirst:region:85688741","region_a":"DC","locality":"Washington","locality_gid":"whosonfirst:locality:85931779","neighbourhood":"Downtown","neighbourhood_gid":"whosonfirst:neighbourhood:85866033","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"The Hay Adams across from the White House, Washington, DC, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-77.036952,38.900522]},"properties":{"id":"way/55326891","gid":"openstreetmap:address:way/55326891","layer":"address","source":"openstreetmap","source_id":"way/55326891","name":"800 16th Street Northwest","housenumber":"800","street":"16th Street Northwest","postalcode":"20006","confidence":0.8,"distance":0.035,"accuracy":"point","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"District of Columbia","region_gid":"whosonfirst:region:85688741","region_a":"DC","locality":"Washington","locality_gid":"whosonfirst:locality:85931779","neighbourhood":"Downtown","neighbourhood_gid":"whosonfirst:neighbourhood:85866033","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"800 16th Street Northwest, Washington, DC, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-77.036952,38.900522]},"properties":{"id":"way/55326891","gid":"openstreetmap:venue:way/55326891","layer":"venue","source":"openstreetmap","source_id":"way/55326891","name":"Hay-Adams Hotel","housenumber":"800","street":"16th Street Northwest","postalcode":"20006","confidence":0.8,"distance":0.035,"accuracy":"point","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"District of Columbia","region_gid":"whosonfirst:region:85688741","region_a":"DC","locality":"Washington","locality_gid":"whosonfirst:locality:85931779","neighbourhood":"Downtown","neighbourhood_gid":"whosonfirst:neighbourhood:85866033","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"Hay-Adams Hotel, Washington, DC, USA"},"bbox":[-77.0371738,38.9003173,-77.0367231,38.9006934]},{"type":"Feature","geometry":{"type":"Point","coordinates":[-77.036959,38.900533]},"properties":{"id":"us/dc/statewide:2020e495cbdd377f","gid":"openaddresses:address:us/dc/statewide:2020e495cbdd377f","layer":"address","source":"openaddresses","source_id":"us/dc/statewide:2020e495cbdd377f","name":"800 16th Street NW","housenumber":"800","street":"16th Street NW","postalcode":"20006","confidence":0.8,"distance":0.036,"accuracy":"point","country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"District of Columbia","region_gid":"whosonfirst:region:85688741","region_a":"DC","locality":"Washington","locality_gid":"whosonfirst:locality:85931779","neighbourhood":"Downtown","neighbourhood_gid":"whosonfirst:neighbourhood:85866033","continent":"North America","continent_gid":"whosonfirst:continent:102191575","label":"800 16th Street NW, Washington, DC, USA"}}],"bbox":[-77.0372,38.9003,-77.0367,38.9006934]}";
@@ -0,0 +1 @@
s:2827:"{"geocoding":{"version":"0.2","attribution":"https://geocode.earth/guidelines","query":{"text":"Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany","size":5,"private":false,"lang":{"name":"English","iso6391":"en","iso6393":"eng","defaulted":true},"querySize":20,"parser":"libpostal","parsed_text":{"street":"kalbacher hauptstraße","number":"10","postalcode":"60437","city":"frankfurt","country":"germany"}},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1559249220260},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[8.636781,50.189017]},"properties":{"id":"de/he/city_of_frankfurtammain:af1f19357b787d33","gid":"openaddresses:address:de/he/city_of_frankfurtammain:af1f19357b787d33","layer":"address","source":"openaddresses","source_id":"de/he/city_of_frankfurtammain:af1f19357b787d33","name":"Kalbacher Hauptstraße 10a","housenumber":"10a","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":1,"match_type":"exact","accuracy":"point","country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","region_a":"HE","macrocounty":"Darmstadt Government Region","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt","county_gid":"whosonfirst:county:102063589","county_a":"FA","locality":"Frankfurt","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"Kalbacher Hauptstraße 10a, Frankfurt, Germany"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.636575,50.189044]},"properties":{"id":"de/he/city_of_frankfurtammain:f0bbbc440fb8b4b9","gid":"openaddresses:address:de/he/city_of_frankfurtammain:f0bbbc440fb8b4b9","layer":"address","source":"openaddresses","source_id":"de/he/city_of_frankfurtammain:f0bbbc440fb8b4b9","name":"Kalbacher Hauptstraße 10","housenumber":"10","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":1,"match_type":"exact","accuracy":"point","country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","region_a":"HE","macrocounty":"Darmstadt Government Region","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt","county_gid":"whosonfirst:county:102063589","county_a":"FA","locality":"Frankfurt","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","continent":"Europe","continent_gid":"whosonfirst:continent:102191581","label":"Kalbacher Hauptstraße 10, Frankfurt, Germany"}}],"bbox":[8.636575,50.189017,8.636781,50.189044]}";

0 comments on commit abbe456

Please sign in to comment.
You can’t perform that action at this time.