1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die();
11
12 13 14 15 16 17
18 class JOpenstreetmapElements extends JOpenstreetmapObject
19 {
20 21 22 23 24 25 26 27 28 29 30 31
32 public function createNode($changeset, $latitude, $longitude, $tags)
33 {
34 $token = $this->oauth->getToken();
35
36
37 $parameters = array(
38 'oauth_token' => $token['key'],
39 );
40
41
42 $base = 'node/create';
43
44
45 $path = $this->getOption('api.url') . $base;
46
47 $tag_list = '';
48
49
50 if (!empty($tags))
51 {
52 foreach ($tags as $key => $value)
53 {
54 $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
55 }
56 }
57
58 $xml = '<?xml version="1.0" encoding="UTF-8"?>
59 <osm version="0.6" generator="JOpenstreetmap">
60 <node changeset="' . $changeset . '" lat="' . $latitude . '" lon="' . $longitude . '">'
61 . $tag_list .
62 '</node>
63 </osm>';
64
65 $header['Content-Type'] = 'text/xml';
66
67
68 $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
69
70 return $response->body;
71 }
72
73 74 75 76 77 78 79 80 81 82 83
84 public function createWay($changeset, $tags, $nds)
85 {
86 $token = $this->oauth->getToken();
87
88
89 $parameters = array(
90 'oauth_token' => $token['key'],
91 );
92
93
94 $base = 'way/create';
95
96
97 $path = $this->getOption('api.url') . $base;
98
99 $tag_list = '';
100
101
102 if (!empty($tags))
103 {
104 foreach ($tags as $key => $value)
105 {
106 $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
107 }
108 }
109
110 $nd_list = '';
111
112 if (!empty($nds))
113 {
114 foreach ($nds as $value)
115 {
116 $nd_list .= '<nd ref="' . $value . '"/>';
117 }
118 }
119
120 $xml = '<?xml version="1.0" encoding="UTF-8"?>
121 <osm version="0.6" generator="JOpenstreetmap">
122 <way changeset="' . $changeset . '">'
123 . $tag_list
124 . $nd_list .
125 '</way>
126 </osm>';
127
128 $header['Content-Type'] = 'text/xml';
129
130
131 $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
132
133 return $response->body;
134 }
135
136 137 138 139 140 141 142 143 144 145 146 147
148 public function createRelation($changeset, $tags, $members)
149 {
150 $token = $this->oauth->getToken();
151
152
153 $parameters = array(
154 'oauth_token' => $token['key'],
155 );
156
157
158 $base = 'relation/create';
159
160
161 $path = $this->getOption('api.url') . $base;
162
163 $tag_list = '';
164
165
166 if (!empty($tags))
167 {
168 foreach ($tags as $key => $value)
169 {
170 $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
171 }
172 }
173
174
175 $member_list = '';
176
177 if (!empty($members))
178 {
179 foreach ($members as $member)
180 {
181 if ($member['type'] == 'node')
182 {
183 $member_list .= '<member type="' . $member['type'] . '" role="' . $member['role'] . '" ref="' . $member['ref'] . '"/>';
184 }
185 elseif ($member['type'] == 'way')
186 {
187 $member_list .= '<member type="' . $member['type'] . '" ref="' . $member['ref'] . '"/>';
188 }
189 }
190 }
191
192 $xml = '<?xml version="1.0" encoding="UTF-8"?>
193 <osm version="0.6" generator="JOpenstreetmap">
194 <relation relation="' . $changeset . '" >'
195 . $tag_list
196 . $member_list .
197 '</relation>
198 </osm>';
199
200 $header['Content-Type'] = 'text/xml';
201
202
203 $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
204
205 return $response->body;
206 }
207
208 209 210 211 212 213 214 215 216 217 218
219 public function readElement($element, $id)
220 {
221 if ($element != 'node' && $element != 'way' && $element != 'relation')
222 {
223 throw new DomainException('Element should be a node, a way or a relation');
224 }
225
226
227 $base = $element . '/' . $id;
228
229
230 $path = $this->getOption('api.url') . $base;
231
232
233 $xml_string = $this->sendRequest($path);
234
235 return $xml_string->$element;
236 }
237
238 239 240 241 242 243 244 245 246 247 248 249
250 public function updateElement($element, $xml, $id)
251 {
252 if ($element != 'node' && $element != 'way' && $element != 'relation')
253 {
254 throw new DomainException('Element should be a node, a way or a relation');
255 }
256
257 $token = $this->oauth->getToken();
258
259
260 $parameters = array(
261 'oauth_token' => $token['key'],
262 );
263
264
265 $base = $element . '/' . $id;
266
267
268 $path = $this->getOption('api.url') . $base;
269
270 $header['Content-Type'] = 'text/xml';
271
272
273 $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
274
275 return $response->body;
276 }
277
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
293 public function deleteElement($element, $id, $version, $changeset, $latitude = null, $longitude = null)
294 {
295 if ($element != 'node' && $element != 'way' && $element != 'relation')
296 {
297 throw new DomainException('Element should be a node, a way or a relation');
298 }
299
300 $token = $this->oauth->getToken();
301
302
303 $parameters = array(
304 'oauth_token' => $token['key'],
305 );
306
307
308 $base = $element . '/' . $id;
309
310
311 $path = $this->getOption('api.url') . $base;
312
313
314 $xml = '<?xml version="1.0" encoding="UTF-8"?>
315 <osm version="0.6" generator="JOpenstreetmap">
316 <' . $element . ' id="' . $id . '" version="' . $version . '" changeset="' . $changeset . '"';
317
318 if (!empty($latitude) && !empty($longitude))
319 {
320 $xml .= ' lat="' . $latitude . '" lon="' . $longitude . '"';
321 }
322
323 $xml .= '/></osm>';
324
325 $header['Content-Type'] = 'text/xml';
326
327
328 $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters, $xml, $header);
329
330 return $response->body;
331 }
332
333 334 335 336 337 338 339 340 341 342 343
344 public function historyOfElement($element, $id)
345 {
346 if ($element != 'node' && $element != 'way' && $element != 'relation')
347 {
348 throw new DomainException('Element should be a node, a way or a relation');
349 }
350
351
352 $base = $element . '/' . $id . '/history';
353
354
355 $path = $this->getOption('api.url') . $base;
356
357
358 $xml_string = $this->sendRequest($path);
359
360 return $xml_string->$element;
361 }
362
363 364 365 366 367 368 369 370 371 372 373 374
375 public function versionOfElement($element, $id, $version)
376 {
377 if ($element != 'node' && $element != 'way' && $element != 'relation')
378 {
379 throw new DomainException('Element should be a node, a way or a relation');
380 }
381
382
383 $base = $element . '/' . $id . '/' . $version;
384
385
386 $path = $this->getOption('api.url') . $base;
387
388
389 $xml_string = $this->sendRequest($path);
390
391 return $xml_string->$element;
392 }
393
394 395 396 397 398 399 400 401 402 403 404
405 public function multiFetchElements($element, $params)
406 {
407 if ($element != 'nodes' && $element != 'ways' && $element != 'relations')
408 {
409 throw new DomainException('Element should be nodes, ways or relations');
410 }
411
412
413 $single_element = substr($element, 0, strlen($element) - 1);
414
415
416 $base = $element . '?' . $element . '=' . $params;
417
418
419 $path = $this->getOption('api.url') . $base;
420
421
422 $xml_string = $this->sendRequest($path);
423
424 return $xml_string->$single_element;
425 }
426
427 428 429 430 431 432 433 434 435 436 437
438 public function relationsForElement($element, $id)
439 {
440 if ($element != 'node' && $element != 'way' && $element != 'relation')
441 {
442 throw new DomainException('Element should be a node, a way or a relation');
443 }
444
445
446 $base = $element . '/' . $id . '/relations';
447
448
449 $path = $this->getOption('api.url') . $base;
450
451
452 $xml_string = $this->sendRequest($path);
453
454 return $xml_string->$element;
455 }
456
457 458 459 460 461 462 463 464 465
466 public function waysForNode($id)
467 {
468
469 $base = 'node/' . $id . '/ways';
470
471
472 $path = $this->getOption('api.url') . $base;
473
474
475 $xml_string = $this->sendRequest($path);
476
477 return $xml_string->way;
478 }
479
480 481 482 483 484 485 486 487 488 489 490
491 public function fullElement($element, $id)
492 {
493 if ($element != 'way' && $element != 'relation')
494 {
495 throw new DomainException('Element should be a way or a relation');
496 }
497
498
499 $base = $element . '/' . $id . '/full';
500
501
502 $path = $this->getOption('api.url') . $base;
503
504
505 $xml_string = $this->sendRequest($path);
506
507 return $xml_string->node;
508 }
509
510 511 512 513 514 515 516 517 518 519 520 521 522
523 public function redaction($element, $id, $version, $redaction_id)
524 {
525 if ($element != 'node' && $element != 'way' && $element != 'relation')
526 {
527 throw new DomainException('Element should be a node, a way or a relation');
528 }
529
530 $token = $this->oauth->getToken();
531
532
533 $parameters = array(
534 'oauth_token' => $token['key'],
535 );
536
537
538 $base = $element . '/' . $id . '/' . $version . '/redact?redaction=' . $redaction_id;
539
540
541 $path = $this->getOption('api.url') . $base;
542
543
544 $response = $this->oauth->oauthRequest($path, 'PUT', $parameters);
545
546 $xml_string = simplexml_load_string($response->body);
547
548 return $xml_string;
549 }
550 }
551