/
V06.php
376 lines (354 loc) · 10.1 KB
/
V06.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* V6.php
* 08-Nov-2011
*
* PHP Version 5
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link V6.php
*/
/**
* Services_OpenStreetMap_API_V06
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link API_V06.php
*/
class Services_OpenStreetMap_API_V06
{
/**
* Elements supported by the API (v0.6).
* Used for validation purposes.
* @var array
* @internal
*/
protected $elements = array('changeset', 'node', 'relation', 'way');
protected $transport = null;
protected $config = null;
/**
* Counter for assigning IDs to [newly] created objects.
* @var int
* @internal
*/
protected $newId = -1;
/**
* Set Config object
*
* @param Services_OpenStreetMap_Config $config Config object.
*
* @return Services_OpenStreetMap_API_V06
*/
public function setConfig(Services_OpenStreetMap_Config $config)
{
$this->config = $config;
return $this;
}
/**
* Get current Config object
*
* @return Services_OpenStreetMap_Config
*/
public function getConfig()
{
return $this->config;
}
/**
* Set the Transport instance.
*
* @param Services_OpenStreetMap_Transport $transport Transport instance.
*
* @return Services_OpenStreetMap_Config
*/
public function setTransport($transport)
{
$this->transport = $transport;
return $this;
}
/**
* Retrieve the current Transport instance.
*
* @return Services_OpenStreetMap_Transport.
*/
public function getTransport()
{
return $this->transport;
}
/**
* Get details of specified relation, optionally specify which version of
* the relation to be retrieved.
*
* <pre>
* $r = $osm->getRelation(1234567);
* $r = $osm->getRelation(1234567, 2);
* </pre>
*
* @param mixed $relationID ID of relation
* @param mixed $version [optional] version of relation
*
* @return string
*/
public function getRelation($relationID, $version = null)
{
return $this->getTransport()->getObject('relation', $relationID, $version);
}
/**
* Return an array of specified relations
*
* <pre>
* $relations = $osm->getRelations($relationId, $relation2Id);
* </pre>
*
* @return array
*/
public function getRelations()
{
return $this->getTransport()->getObjects(
'relation',
Services_OpenStreetMap::getIDs(func_get_args())
);
}
/**
* Get details of specified changeset
*
* <code>
* $changeset = $osm->getChangeset(123456);
* </code>
*
* @param string $id numeric ID of changeset
* @param string $version optional
*
* @return string
*/
public function getChangeset($id, $version = null)
{
return $this->getTransport()->getObject('changeset', $id, $version);
}
/**
* Create a changeset, used to transmit changes (creation, updates, deletion)
* to the server. Username and password must be set.
*
* <code>
* $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva');
* $osm = new Services_OpenStreetMap($config);
* $changeset = $osm->createChangeset();
* </code>
*
* @param boolean $atomic atomic changeset?
*
* @return Services_OpenStreetMap_Changeset
* @see setConfig
*/
public function createChangeset($atomic = true)
{
$changeset = new Services_OpenStreetMap_Changeset($atomic);
$changeset->setTransport($this->getTransport());
$changeset->setConfig($this->getConfig());
return $changeset;
}
/**
* searchChangesets
*
* @param array $criteria Array of Services_OpenStreetMap_Criterion objects.
*
* @return Services_OpenStreetMap_Changesets
* @throws Services_OpenStreetMap_RuntimeException
*/
public function searchChangesets(array $criteria)
{
$types = array();
foreach ($criteria as $criterion) {
$types[] = $criterion->type();
}
if (array_search('user', $types) !== false
&& array_search('display_name', $types) !== false
) {
throw new Services_OpenStreetMap_RuntimeException(
'Can\'t supply both user and display_name criteria'
);
}
return $this->getTransport()->searchObjects('changeset', $criteria);
}
/**
* Create and return a Services_OpenStreetMap_Node
*
* <code>
* $node = $osm->createNode($lat, $lon, array('building' => 'yes'));
* </code>
*
* @param float $latitude Latitude of node
* @param float $longitude Longitude of node
* @param array $tags Array of key->value tag pairs.
*
* @return Services_OpenStreetMap_Node
*/
public function createNode($latitude, $longitude, array $tags = array())
{
$node = new Services_OpenStreetMap_Node();
$config = $this->getConfig();
$apiVersion = $config->getValue('api_version');
$userAgent = $config->getValue('User-Agent');
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<osm version='{$apiVersion}' generator='{$userAgent}'>
<node lat='{$latitude}' lon='{$longitude}' version='1'/></osm>";
$node->setLat($latitude);
$node->setLon($longitude);
$node->setXml(simplexml_load_string($xml));
$node->setId($this->newId--);
$node->setTag('created_by', $userAgent);
if (!empty($tags)) {
foreach ($tags as $key => $value) {
$node->setTag($key, $value);
}
}
return $node;
}
/**
* Get a Services_OpenStreetMap_User object for the [current] user.
*
* May return false if the user could not be found for any reason.
*
* @see setConfig
*
* @return Services_OpenStreetMap_User
* @throw Services_OpenStreetMap_Exception
*/
public function getUser()
{
$config = $this->getConfig()->asArray();
$url = $config['server']
. 'api/'
. $config['api_version']
. '/user/details';
$user = $config['user'];
$password = $config['password'];
try {
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_GET,
$user,
$password
);
} catch (Services_OpenStreetMap_Exception $ex) {
switch ($ex->getCode()) {
case Services_OpenStreetMap_Transport::NOT_FOUND:
case Services_OpenStreetMap_Transport::UNAUTHORISED:
case Services_OpenStreetMap_Transport::GONE:
return false;
default:
throw $ex;
}
}
$url = $config['server'] . 'api/'
. $config['api_version']
. '/user/preferences';
try {
$prefs = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_GET,
$user,
$password
);
} catch (Services_OpenStreetMap_Exception $ex) {
switch ($ex->getCode()) {
case Services_OpenStreetMap_Transport::NOT_FOUND:
case Services_OpenStreetMap_Transport::UNAUTHORISED:
case Services_OpenStreetMap_Transport::GONE:
return false;
default:
throw $ex;
}
}
$obj = new Services_OpenStreetMap_User();
$obj->setXml(simplexml_load_string($response->getBody()));
$obj->setPreferencesXml($prefs->getBody());
return $obj;
}
/**
* Get details of specified way
*
* @param mixed $wayID wayID
* @param mixed $version [optional] version of way
*
* @return string
*/
public function getWay($wayID, $version = null)
{
$way = $this->getTransport()->getObject('way', $wayID, $version);
if ($way !== false) {
$way->setTransport($this->getTransport());
$way->setConfig($this->getConfig());
}
return $way;
}
/**
* Return an array of specified ways.
*
* <pre>
* $ways = $osm->getWays($wayId, $way2Id);
* </pre>
*
* @return array
*/
public function getWays()
{
return $this->getTransport()->getObjects(
'way',
Services_OpenStreetMap::getIDs(func_get_args())
);
}
/**
* Get details of specified node
*
* <code>
* $osm = new Services_OpenStreetMap();
* var_dump($osm->getNode(52245107));
* </code>
*
* @param string $nodeID nodeID
* @param mixed $version [optional] version of node
*
* @return string
*/
public function getNode($nodeID, $version = null)
{
$node = $this->getTransport()->getObject('node', $nodeID, $version);
if ($node !== false) {
$node->setTransport($this->getTransport());
$node->setConfig($this->getConfig());
}
return $node;
}
/**
* Return an array of specified nodes.
*
* If none can be retrieved, for example if they all have been deleted,
* then the boolean false value is returned.
*
* <code>
* $osm = new Services_OpenStreetMap();
* var_dump($osm->getNodes(52245107, 52245108));
* </code>
* Or
* <code>
* $osm = new Services_OpenStreetMap();
* var_dump($osm->getNodes(array(52245107, 52245108)));
* </code>
*
* @return Services_OpenStreetMap_Nodes
*/
public function getNodes()
{
return $this->getTransport()->getObjects(
'node',
Services_OpenStreetMap::getIDs(func_get_args())
);
}
}
?>