mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
Add geocoding api
This commit is contained in:
parent
88f2b7a585
commit
dfb57a37df
74
Api/Geocoding/Nominatim.php
Normal file
74
Api/Geocoding/Nominatim.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
/**
|
||||
* Karaka
|
||||
*
|
||||
* PHP Version 8.1
|
||||
*
|
||||
* @package phpOMS\Api\Geocoding
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 2.0
|
||||
* @version 1.0.0
|
||||
* @link https://jingga.app
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Api\Geocoding;
|
||||
|
||||
use phpOMS\Message\Http\HttpRequest;
|
||||
use phpOMS\Message\Http\RequestMethod;
|
||||
use phpOMS\Message\Http\Rest;
|
||||
use phpOMS\Uri\HttpUri;
|
||||
|
||||
/**
|
||||
* Check EU VAT.
|
||||
*
|
||||
* @package phpOMS\Api\Geocoding
|
||||
* @license OMS License 2.0
|
||||
* @link https://jingga.app
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class Nominatim
|
||||
{
|
||||
private static float $lastRun = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function geocoding(string $country, string $city, string $address = '') : array
|
||||
{
|
||||
$URL = 'https://nominatim.openstreetmap.org/search.php?format=jsonv2';
|
||||
|
||||
$request = new HttpRequest(
|
||||
new HttpUri(
|
||||
$URL . '&country=' . \urlencode($country) . '&city=' . \urlencode($city) . ($address = '' ? '' : '&street=' . \urlencode($address))
|
||||
)
|
||||
);
|
||||
$request->setMethod(RequestMethod::GET);
|
||||
|
||||
// Required according to the api documentation
|
||||
$request->header->set('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1');
|
||||
|
||||
// Handling rate limit of the Api
|
||||
$time = \microtime(true);
|
||||
if ($time - self::$lastRun < 1000000) {
|
||||
\usleep((int) (1000000 - ($time - self::$lastRun) + 100));
|
||||
}
|
||||
|
||||
$body = Rest::request($request)->getBody();
|
||||
$result['body'] = $body;
|
||||
|
||||
/** @var array $json */
|
||||
$json = \json_decode($body, true);
|
||||
if ($json === false) {
|
||||
return [
|
||||
'lat' => 0.0,
|
||||
'lon' => 0.0,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'lat' => (float) ($json[0]['lat'] ?? 0.0),
|
||||
'lon' => (float) ($json[0]['lon'] ?? 0.0),
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user