Skip to content
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

feat: add geocode-earth provider #962

Merged
merged 4 commits into from
Jun 14, 2019
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
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
4 changes: 4 additions & 0 deletions src/Provider/GeocodeEarth/.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/GeocodeEarth/.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/GeocodeEarth/.travis.yml
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions src/Provider/GeocodeEarth/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.

## 1.0.0

First release of this provider.
222 changes: 222 additions & 0 deletions src/Provider/GeocodeEarth/GeocodeEarth.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 21 additions & 0 deletions src/Provider/GeocodeEarth/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions src/Provider/GeocodeEarth/README.md
Original file line number Diff line number Diff line change
@@ -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).
Original file line number Diff line number Diff line change
@@ -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":[]}";
Original file line number Diff line number Diff line change
@@ -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]}";
Original file line number Diff line number Diff line change
@@ -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]}";
Loading