-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPhoton.php
174 lines (139 loc) · 4.96 KB
/
Photon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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\Photon;
use Geocoder\Collection;
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Location;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Photon\Model\PhotonAddress;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Psr\Http\Client\ClientInterface;
/**
* @author Niklas Närhinen <niklas@narhinen.net>
* @author Jonathan Beliën <jbe@geo6.be>
*/
final class Photon extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
private $rootUrl;
/**
* @param ClientInterface $client an HTTP client
*/
public static function withKomootServer(ClientInterface $client): self
{
return new self($client, 'https://photon.komoot.io');
}
/**
* @param ClientInterface $client an HTTP client
* @param string $rootUrl Root URL of the photon server
*/
public function __construct(ClientInterface $client, $rootUrl)
{
parent::__construct($client);
$this->rootUrl = rtrim($rootUrl, '/');
}
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 Photon provider does not support IP addresses.');
}
$url = $this->rootUrl
.'/api?'
.http_build_query([
'q' => $address,
'limit' => $query->getLimit(),
'lang' => $query->getLocale(),
]);
$json = $this->executeQuery($url);
if (!isset($json->features) || empty($json->features)) {
return new AddressCollection([]);
}
$results = [];
foreach ($json->features as $feature) {
$results[] = $this->featureToAddress($feature);
}
return new AddressCollection($results);
}
public function reverseQuery(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$longitude = $coordinates->getLongitude();
$latitude = $coordinates->getLatitude();
$url = $this->rootUrl
.'/reverse?'
.http_build_query([
'lat' => $latitude,
'lon' => $longitude,
'lang' => $query->getLocale(),
]);
$json = $this->executeQuery($url);
if (!isset($json->features) || empty($json->features)) {
return new AddressCollection([]);
}
$results = [];
foreach ($json->features as $feature) {
$results[] = $this->featureToAddress($feature);
}
return new AddressCollection($results);
}
private function featureToAddress(\stdClass $feature): Location
{
$builder = new AddressBuilder($this->getName());
$coordinates = $feature->geometry->coordinates;
$properties = $feature->properties;
$builder->setCoordinates(floatval($coordinates[1]), floatval($coordinates[0]));
$builder->setStreetName($properties->street ?? null);
$builder->setStreetNumber($properties->housenumber ?? null);
$builder->setPostalCode($properties->postcode ?? null);
$builder->setLocality($properties->city ?? null);
$builder->setCountry($properties->country ?? null);
$builder->setCountryCode($properties->countrycode ?? null);
if (isset($properties->extent)) {
$builder->setBounds($properties->extent[0], $properties->extent[2], $properties->extent[1], $properties->extent[3]);
}
/** @var PhotonAddress $address */
$address = $builder->build(PhotonAddress::class);
$address = $address
->withOSMId($properties->osm_id ?? null)
->withOSMType($properties->osm_type ?? null)
->withOSMTag(
$properties->osm_key ?? null,
$properties->osm_value ?? null
)
->withName($properties->name ?? null)
->withState($properties->state ?? null)
->withCounty($properties->county ?? null)
->withDistrict($properties->district ?? null);
return $address;
}
public function getName(): string
{
return 'photon';
}
private function executeQuery(string $url): \stdClass
{
$content = $this->getUrlContents($url);
$json = json_decode($content);
// API error
if (is_null($json)) {
throw InvalidServerResponse::create($url);
}
return $json;
}
}