1 |
|
<?php |
2 |
|
/** |
3 |
|
* V6.php |
4 |
|
* 08-Nov-2011 |
5 |
|
* |
6 |
|
* PHP Version 5 |
7 |
|
* |
8 |
|
* @category Services |
9 |
|
* @package Services_OpenStreetMap |
10 |
|
* @author Ken Guest <kguest@php.net> |
11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
12 |
|
* @version Release: @package_version@ |
13 |
|
* @link V6.php |
14 |
|
*/ |
15 |
|
|
16 |
|
/** |
17 |
|
* Services_OpenStreetMap_API_V06 |
18 |
|
* |
19 |
|
* @category Services |
20 |
|
* @package Services_OpenStreetMap |
21 |
|
* @author Ken Guest <kguest@php.net> |
22 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
23 |
|
* @link API_V06.php |
24 |
|
*/ |
25 |
|
class Services_OpenStreetMap_API_V06 |
26 |
|
{ |
27 |
|
/** |
28 |
|
* Elements supported by the API (v0.6). |
29 |
|
* Used for validation purposes. |
30 |
|
* @var array |
31 |
|
* @internal |
32 |
|
*/ |
33 |
|
protected $elements = array('changeset', 'node', 'relation', 'way'); |
34 |
|
|
35 |
|
protected $transport = null; |
36 |
|
|
37 |
|
protected $config = null; |
38 |
|
|
39 |
|
/** |
40 |
|
* Counter for assigning IDs to [newly] created objects. |
41 |
|
* @var int |
42 |
|
* @internal |
43 |
|
*/ |
44 |
|
protected $newId = -1; |
45 |
|
|
46 |
|
/** |
47 |
|
* Set Config object |
48 |
|
* |
49 |
|
* @param Services_OpenStreetMap_Config $config Config object. |
50 |
|
* |
51 |
|
* @return Services_OpenStreetMap_API_V06 |
52 |
|
*/ |
53 |
|
public function setConfig(Services_OpenStreetMap_Config $config) |
54 |
|
{ |
55 |
98 |
$this->config = $config; |
56 |
98 |
return $this; |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
|
* Get current Config object |
61 |
|
* |
62 |
|
* @return Services_OpenStreetMap_Config |
63 |
|
*/ |
64 |
|
public function getConfig() |
65 |
|
{ |
66 |
36 |
return $this->config; |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
|
* Set the Transport instance. |
71 |
|
* |
72 |
|
* @param Services_OpenStreetMap_Transport $transport Transport instance. |
73 |
|
* |
74 |
|
* @return Services_OpenStreetMap_Config |
75 |
|
*/ |
76 |
|
public function setTransport(Services_OpenStreetMap_Transport $transport) |
77 |
|
{ |
78 |
98 |
$this->transport = $transport; |
79 |
98 |
return $this; |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
|
* Retrieve the current Transport instance. |
84 |
|
* |
85 |
|
* @return Services_OpenStreetMap_Transport. |
86 |
|
*/ |
87 |
|
public function getTransport() |
88 |
|
{ |
89 |
48 |
return $this->transport; |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
|
* Get details of specified relation, optionally specify which version of |
94 |
|
* the relation to be retrieved. |
95 |
|
* |
96 |
|
* <pre> |
97 |
|
* $r = $osm->getRelation(1234567); |
98 |
|
* $r = $osm->getRelation(1234567, 2); |
99 |
|
* </pre> |
100 |
|
* |
101 |
|
* @param mixed $relationID ID of relation |
102 |
|
* @param mixed $version [optional] version of relation |
103 |
|
* |
104 |
|
* @return string |
105 |
|
*/ |
106 |
|
public function getRelation($relationID, $version = null) |
107 |
|
{ |
108 |
1 |
return $this->getTransport()->getObject('relation', $relationID, $version); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* Return an array of specified relations |
113 |
|
* |
114 |
|
* <pre> |
115 |
|
* $relations = $osm->getRelations($relationId, $relation2Id); |
116 |
|
* </pre> |
117 |
|
* |
118 |
|
* @return array |
119 |
|
*/ |
120 |
|
public function getRelations() |
121 |
|
{ |
122 |
2 |
return $this->getTransport()->getObjects( |
123 |
2 |
'relation', |
124 |
2 |
Services_OpenStreetMap::getIDs(func_get_args()) |
125 |
2 |
); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
|
* Get details of specified changeset |
130 |
|
* |
131 |
|
* <code> |
132 |
|
* $changeset = $osm->getChangeset(123456); |
133 |
|
* </code> |
134 |
|
* |
135 |
|
* @param string $id numeric ID of changeset |
136 |
|
* @param string $version optional |
137 |
|
* |
138 |
|
* @return string |
139 |
|
*/ |
140 |
|
public function getChangeset($id, $version = null) |
141 |
|
{ |
142 |
2 |
return $this->getTransport()->getObject('changeset', $id, $version); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* Create a changeset, used to transmit changes (creation, updates, deletion) |
147 |
|
* to the server. Username and password must be set. |
148 |
|
* |
149 |
|
* <code> |
150 |
|
* $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva'); |
151 |
|
* $osm = new Services_OpenStreetMap($config); |
152 |
|
* $changeset = $osm->createChangeset(); |
153 |
|
* </code> |
154 |
|
* |
155 |
|
* @param boolean $atomic atomic changeset? |
156 |
|
* |
157 |
|
* @return Services_OpenStreetMap_Changeset |
158 |
|
* @see setConfig |
159 |
|
*/ |
160 |
|
public function createChangeset($atomic = true) |
161 |
|
{ |
162 |
10 |
$changeset = new Services_OpenStreetMap_Changeset($atomic); |
163 |
10 |
$changeset->setTransport($this->getTransport()); |
164 |
10 |
$changeset->setConfig($this->getConfig()); |
165 |
10 |
return $changeset; |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
|
* searchChangesets |
170 |
|
* |
171 |
|
* @param array $criteria Array of Services_OpenStreetMap_Criterion objects. |
172 |
|
* |
173 |
|
* @return Services_OpenStreetMap_Changesets |
174 |
|
* @throws Services_OpenStreetMap_RuntimeException |
175 |
|
*/ |
176 |
|
public function searchChangesets(array $criteria) |
177 |
|
{ |
178 |
6 |
$types = array(); |
179 |
6 |
foreach ($criteria as $criterion) { |
180 |
6 |
$types[] = $criterion->type(); |
181 |
6 |
} |
182 |
|
|
183 |
6 |
if (array_search('user', $types) !== false |
184 |
6 |
&& array_search('display_name', $types) !== false |
185 |
6 |
) { |
186 |
1 |
throw new Services_OpenStreetMap_RuntimeException( |
187 |
|
'Can\'t supply both user and display_name criteria' |
188 |
1 |
); |
189 |
1 |
} |
190 |
|
|
191 |
5 |
return $this->getTransport()->searchObjects('changeset', $criteria); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
|
* Create and return a Services_OpenStreetMap_Node |
196 |
|
* |
197 |
|
* <code> |
198 |
|
* $node = $osm->createNode($lat, $lon, array('building' => 'yes')); |
199 |
|
* </code> |
200 |
|
* |
201 |
|
* @param float $latitude Latitude of node |
202 |
|
* @param float $longitude Longitude of node |
203 |
|
* @param array $tags Array of key->value tag pairs. |
204 |
|
* |
205 |
|
* @return Services_OpenStreetMap_Node |
206 |
|
*/ |
207 |
|
public function createNode($latitude, $longitude, array $tags = array()) |
208 |
|
{ |
209 |
10 |
$node = new Services_OpenStreetMap_Node(); |
210 |
10 |
$config = $this->getConfig(); |
211 |
10 |
$apiVersion = $config->getValue('api_version'); |
212 |
10 |
$userAgent = $config->getValue('User-Agent'); |
213 |
|
$xml = "<?xml version='1.0' encoding='UTF-8'?> |
214 |
10 |
<osm version='{$apiVersion}' generator='{$userAgent}'> |
215 |
10 |
<node lat='{$latitude}' lon='{$longitude}' version='1'/></osm>"; |
216 |
10 |
$node->setLat($latitude); |
217 |
7 |
$node->setLon($longitude); |
218 |
4 |
$node->setXml(simplexml_load_string($xml)); |
219 |
4 |
$node->setId($this->newId--); |
220 |
4 |
$node->setTag('created_by', $userAgent); |
221 |
4 |
if (!empty($tags)) { |
222 |
3 |
foreach ($tags as $key => $value) { |
223 |
3 |
$node->setTag($key, $value); |
224 |
3 |
} |
225 |
3 |
} |
226 |
4 |
return $node; |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
|
* Get a Services_OpenStreetMap_User object for the [current] user. |
231 |
|
* |
232 |
|
* May return false if the user could not be found for any reason. |
233 |
|
* |
234 |
|
* @see setConfig |
235 |
|
* |
236 |
|
* @return Services_OpenStreetMap_User |
237 |
|
* @throws Services_OpenStreetMap_Exception |
238 |
|
*/ |
239 |
|
public function getUser() |
240 |
|
{ |
241 |
4 |
$config = $this->getConfig()->asArray(); |
242 |
4 |
$url = $config['server'] |
243 |
|
. 'api/' |
244 |
4 |
. $config['api_version'] |
245 |
4 |
. '/user/details'; |
246 |
4 |
$user = $config['user']; |
247 |
4 |
$password = $config['password']; |
248 |
|
try { |
249 |
4 |
$response = $this->getTransport()->getResponse( |
250 |
4 |
$url, |
251 |
4 |
HTTP_Request2::METHOD_GET, |
252 |
4 |
$user, |
253 |
|
$password |
254 |
4 |
); |
255 |
4 |
} catch (Services_OpenStreetMap_Exception $ex) { |
256 |
|
switch ($ex->getCode()) { |
257 |
|
case Services_OpenStreetMap_Transport::NOT_FOUND: |
258 |
|
case Services_OpenStreetMap_Transport::UNAUTHORISED: |
259 |
|
case Services_OpenStreetMap_Transport::GONE: |
260 |
|
return false; |
261 |
|
default: |
262 |
|
throw $ex; |
263 |
|
} |
264 |
|
} |
265 |
4 |
$url = $config['server'] . 'api/' |
266 |
4 |
. $config['api_version'] |
267 |
4 |
. '/user/preferences'; |
268 |
|
try { |
269 |
4 |
$prefs = $this->getTransport()->getResponse( |
270 |
4 |
$url, |
271 |
4 |
HTTP_Request2::METHOD_GET, |
272 |
4 |
$user, |
273 |
|
$password |
274 |
4 |
); |
275 |
4 |
} catch (Services_OpenStreetMap_Exception $ex) { |
276 |
1 |
switch ($ex->getCode()) { |
277 |
1 |
case Services_OpenStreetMap_Transport::NOT_FOUND: |
278 |
1 |
case Services_OpenStreetMap_Transport::UNAUTHORISED: |
279 |
1 |
case Services_OpenStreetMap_Transport::GONE: |
280 |
|
return false; |
281 |
1 |
default: |
282 |
1 |
throw $ex; |
283 |
1 |
} |
284 |
|
} |
285 |
3 |
$obj = new Services_OpenStreetMap_User(); |
286 |
3 |
$obj->setXml(simplexml_load_string($response->getBody())); |
287 |
3 |
$obj->setPreferencesXml($prefs->getBody()); |
288 |
3 |
return $obj; |
289 |
|
} |
290 |
|
|
291 |
|
public function getUserById($id) |
292 |
|
{ |
293 |
|
$config = $this->getConfig()->asArray(); |
294 |
|
$url = $config['server'] |
295 |
|
. 'api/' |
296 |
|
. $config['api_version'] |
297 |
|
. '/user/' . $id; |
298 |
|
try { |
299 |
|
$response = $this->getTransport()->getResponse( |
300 |
|
$url, |
301 |
|
HTTP_Request2::METHOD_GET |
302 |
|
); |
303 |
|
} catch (Services_OpenStreetMap_Exception $ex) { |
304 |
|
switch ($ex->getCode()) { |
305 |
|
case Services_OpenStreetMap_Transport::NOT_FOUND: |
306 |
|
case Services_OpenStreetMap_Transport::UNAUTHORISED: |
307 |
|
case Services_OpenStreetMap_Transport::GONE: |
308 |
|
return false; |
309 |
|
default: |
310 |
|
throw $ex; |
311 |
|
} |
312 |
|
} |
313 |
|
$obj = new Services_OpenStreetMap_User(); |
314 |
|
$obj->setXml(simplexml_load_string($response->getBody())); |
315 |
|
return $obj; |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
|
* Get details of specified way |
320 |
|
* |
321 |
|
* @param mixed $wayID wayID |
322 |
|
* @param mixed $version [optional] version of way |
323 |
|
* |
324 |
|
* @return string |
325 |
|
*/ |
326 |
|
public function getWay($wayID, $version = null) |
327 |
|
{ |
328 |
11 |
$way = $this->getTransport()->getObject('way', $wayID, $version); |
329 |
11 |
if ($way !== false) { |
330 |
11 |
$way->setTransport($this->getTransport()); |
331 |
11 |
$way->setConfig($this->getConfig()); |
332 |
11 |
} |
333 |
11 |
return $way; |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Return an array of specified ways. |
338 |
|
* |
339 |
|
* <pre> |
340 |
|
* $ways = $osm->getWays($wayId, $way2Id); |
341 |
|
* </pre> |
342 |
|
* |
343 |
|
* @return array |
344 |
|
*/ |
345 |
|
public function getWays() |
346 |
|
{ |
347 |
3 |
return $this->getTransport()->getObjects( |
348 |
3 |
'way', |
349 |
3 |
Services_OpenStreetMap::getIDs(func_get_args()) |
350 |
3 |
); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
|
* Get details of specified node |
355 |
|
* |
356 |
|
* <code> |
357 |
|
* $osm = new Services_OpenStreetMap(); |
358 |
|
* var_dump($osm->getNode(52245107)); |
359 |
|
* </code> |
360 |
|
* |
361 |
|
* @param string $nodeID nodeID |
362 |
|
* @param mixed $version [optional] version of node |
363 |
|
* |
364 |
|
* @return string |
365 |
|
*/ |
366 |
|
public function getNode($nodeID, $version = null) |
367 |
|
{ |
368 |
12 |
$node = $this->getTransport()->getObject('node', $nodeID, $version); |
369 |
11 |
if ($node !== false) { |
370 |
9 |
$node->setTransport($this->getTransport()); |
371 |
9 |
$node->setConfig($this->getConfig()); |
372 |
9 |
} |
373 |
11 |
return $node; |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* Return an array of specified nodes. |
378 |
|
* |
379 |
|
* If none can be retrieved, for example if they all have been deleted, |
380 |
|
* then the boolean false value is returned. |
381 |
|
* |
382 |
|
* <code> |
383 |
|
* $osm = new Services_OpenStreetMap(); |
384 |
|
* var_dump($osm->getNodes(52245107, 52245108)); |
385 |
|
* </code> |
386 |
|
* Or |
387 |
|
* <code> |
388 |
|
* $osm = new Services_OpenStreetMap(); |
389 |
|
* var_dump($osm->getNodes(array(52245107, 52245108))); |
390 |
|
* </code> |
391 |
|
* |
392 |
|
* @return Services_OpenStreetMap_Nodes |
393 |
|
*/ |
394 |
|
public function getNodes() |
395 |
|
{ |
396 |
6 |
return $this->getTransport()->getObjects( |
397 |
6 |
'node', |
398 |
6 |
Services_OpenStreetMap::getIDs(func_get_args()) |
399 |
6 |
); |
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
|
?> |