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\UrbIS; |
14
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
16
|
|
|
use Geocoder\Exception\InvalidArgument; |
17
|
|
|
use Geocoder\Exception\InvalidServerResponse; |
18
|
|
|
use Geocoder\Exception\UnsupportedOperation; |
19
|
|
|
use Geocoder\Http\Provider\AbstractHttpProvider; |
20
|
|
|
use Geocoder\Model\Address; |
21
|
|
|
use Geocoder\Model\AddressBuilder; |
22
|
|
|
use Geocoder\Model\AddressCollection; |
23
|
|
|
use Geocoder\Provider\Provider; |
24
|
|
|
use Geocoder\Query\GeocodeQuery; |
25
|
|
|
use Geocoder\Query\ReverseQuery; |
26
|
|
|
use Http\Client\HttpClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Jonathan Beliën <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class UrbIS extends AbstractHttpProvider implements Provider |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
const GEOCODE_ENDPOINT_URL = 'https://geoservices.irisnet.be/localization/Rest/Localize/getaddresses?spatialReference=4326&language=%s&address=%s'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const REVERSE_ENDPOINT_URL = 'https://geoservices.irisnet.be/localization/Rest/Localize/getaddressfromxy?json=%s'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param HttpClient $client an HTTP adapter |
45
|
|
|
*/ |
46
|
15 |
|
public function __construct(HttpClient $client) |
47
|
|
|
{ |
48
|
15 |
|
parent::__construct($client); |
49
|
15 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
9 |
|
public function geocodeQuery(GeocodeQuery $query): Collection |
55
|
|
|
{ |
56
|
9 |
|
$address = $query->getText(); |
57
|
|
|
// This API does not support IP |
58
|
9 |
|
if (filter_var($address, FILTER_VALIDATE_IP)) { |
59
|
3 |
|
throw new UnsupportedOperation('The UrbIS provider does not support IP addresses, only street addresses.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Save a request if no valid address entered |
63
|
6 |
|
if (empty($address)) { |
64
|
|
|
throw new InvalidArgument('Address cannot be empty.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$language = ''; |
68
|
6 |
|
if (!is_null($query->getLocale()) && preg_match('/^(fr|nl).*$/', $query->getLocale(), $matches) === 1) { |
69
|
1 |
|
$language = $matches[1]; |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
$url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($language), urlencode($address)); |
73
|
6 |
|
$json = $this->executeQuery($url); |
74
|
|
|
|
75
|
|
|
// no result |
76
|
1 |
|
if (empty($json->result)) { |
77
|
|
|
return new AddressCollection([]); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$results = []; |
81
|
1 |
|
foreach ($json->result as $location) { |
82
|
1 |
|
$streetName = !empty($location->address->street->name) ? $location->address->street->name : null; |
83
|
1 |
|
$number = !empty($location->address->number) ? $location->address->number : null; |
84
|
1 |
|
$municipality = !empty($location->address->street->municipality) ? $location->address->street->municipality : null; |
85
|
1 |
|
$postCode = !empty($location->address->street->postCode) ? $location->address->street->postCode : null; |
86
|
|
|
|
87
|
1 |
|
$builder = new AddressBuilder($this->getName()); |
88
|
1 |
|
$builder->setCoordinates($location->point->y, $location->point->x) |
89
|
1 |
|
->setStreetNumber($number) |
90
|
1 |
|
->setStreetName($streetName) |
91
|
1 |
|
->setLocality($municipality) |
92
|
1 |
|
->setPostalCode($postCode) |
93
|
1 |
|
->setBounds($location->extent->ymin, $location->extent->xmin, $location->extent->ymax, $location->extent->xmax); |
94
|
|
|
|
95
|
1 |
|
$results[] = $builder->build(); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return new AddressCollection($results); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
6 |
|
public function reverseQuery(ReverseQuery $query): Collection |
105
|
|
|
{ |
106
|
6 |
|
$coordinates = $query->getCoordinates(); |
107
|
6 |
|
$language = $query->getLocale() ?? ''; |
108
|
|
|
|
109
|
|
|
$jsonQuery = [ |
110
|
6 |
|
'language' => $language, |
111
|
|
|
'point' => [ |
112
|
|
|
// x, y are switched in the API |
113
|
6 |
|
'y' => $coordinates->getLongitude(), |
114
|
6 |
|
'x' => $coordinates->getLatitude(), |
115
|
|
|
], |
116
|
6 |
|
'SRS_In' => 4326, |
117
|
|
|
]; |
118
|
|
|
|
119
|
6 |
|
$url = sprintf(self::REVERSE_ENDPOINT_URL, urlencode(json_encode($jsonQuery))); |
120
|
6 |
|
$json = $this->executeQuery($url); |
121
|
|
|
|
122
|
|
|
// no result |
123
|
1 |
|
if (empty($json->result)) { |
124
|
|
|
return new AddressCollection([]); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$results = []; |
128
|
1 |
|
$location = $json->result; |
129
|
|
|
|
130
|
1 |
|
$streetName = !empty($location->address->street->name) ? $location->address->street->name : null; |
131
|
1 |
|
$number = !empty($location->address->number) ? $location->address->number : null; |
132
|
1 |
|
$municipality = !empty($location->address->street->municipality) ? $location->address->street->municipality : null; |
133
|
1 |
|
$postCode = !empty($location->address->street->postCode) ? $location->address->street->postCode : null; |
134
|
|
|
|
135
|
1 |
|
$builder = new AddressBuilder($this->getName()); |
136
|
1 |
|
$builder->setCoordinates($location->point->y, $location->point->x) |
137
|
1 |
|
->setStreetNumber($number) |
138
|
1 |
|
->setStreetName($streetName) |
139
|
1 |
|
->setLocality($municipality) |
140
|
1 |
|
->setPostalCode($postCode); |
141
|
|
|
|
142
|
1 |
|
$results[] = $builder->build(); |
143
|
|
|
|
144
|
1 |
|
return new AddressCollection($results); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
2 |
|
public function getName(): string |
151
|
|
|
{ |
152
|
2 |
|
return 'urbis'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $url |
157
|
|
|
* |
158
|
|
|
* @return \stdClass |
159
|
|
|
*/ |
160
|
12 |
|
private function executeQuery(string $url): \stdClass |
161
|
|
|
{ |
162
|
12 |
|
$content = $this->getUrlContents($url); |
163
|
2 |
|
$json = json_decode($content); |
164
|
|
|
// API error |
165
|
2 |
|
if (!isset($json)) { |
166
|
|
|
throw InvalidServerResponse::create($url); |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
return $json; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|