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 10 : $this->config = $config;
56 10 : return $this;
57 : }
58 :
59 : /**
60 : * Get current Config object
61 : *
62 : * @return Services_OpenStreetMap_Config
63 : */
64 : public function getConfig()
65 : {
66 9 : 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($transport)
77 : {
78 10 : $this->transport = $transport;
79 10 : return $this;
80 : }
81 :
82 : /**
83 : * Retrieve the current Transport instance.
84 : *
85 : * @return Services_OpenStreetMap_Transport.
86 : */
87 : public function getTransport()
88 : {
89 10 : 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 0 : 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 0 : return $this->getTransport()->getObjects(
123 0 : 'relation',
124 0 : Services_OpenStreetMap::getIDs(func_get_args())
125 0 : );
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 0 : 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 0 : $changeset = new Services_OpenStreetMap_Changeset($atomic);
163 0 : $changeset->setTransport($this->getTransport());
164 0 : $changeset->setConfig($this->getConfig());
165 0 : 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 0 : $types = array();
179 0 : foreach ($criteria as $criterion) {
180 0 : $types[] = $criterion->type();
181 0 : }
182 :
183 0 : if (array_search('user', $types) !== false
184 0 : && array_search('display_name', $types) !== false
185 0 : ) {
186 0 : throw new Services_OpenStreetMap_RuntimeException(
187 : 'Can\'t supply both user and display_name criteria'
188 0 : );
189 : }
190 :
191 0 : 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 1 : $node = new Services_OpenStreetMap_Node();
210 1 : $config = $this->getConfig();
211 1 : $apiVersion = $config->getValue('api_version');
212 1 : $userAgent = $config->getValue('User-Agent');
213 : $xml = "<?xml version='1.0' encoding='UTF-8'?>
214 1 : <osm version='{$apiVersion}' generator='{$userAgent}'>
215 1 : <node lat='{$latitude}' lon='{$longitude}' version='1'/></osm>";
216 1 : $node->setLat($latitude);
217 1 : $node->setLon($longitude);
218 1 : $node->setXml(simplexml_load_string($xml));
219 1 : $node->setId($this->newId--);
220 1 : $node->setTag('created_by', $userAgent);
221 1 : if (!empty($tags)) {
222 0 : foreach ($tags as $key => $value) {
223 0 : $node->setTag($key, $value);
224 0 : }
225 0 : }
226 1 : 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 : * @throw Services_OpenStreetMap_Exception
238 : */
239 : public function getUser()
240 : {
241 0 : $config = $this->getConfig()->asArray();
242 0 : $url = $config['server']
243 : . 'api/'
244 0 : . $config['api_version']
245 0 : . '/user/details';
246 0 : $user = $config['user'];
247 0 : $password = $config['password'];
248 : try {
249 0 : $response = $this->getTransport()->getResponse(
250 0 : $url,
251 0 : HTTP_Request2::METHOD_GET,
252 0 : $user,
253 : $password
254 0 : );
255 0 : } catch (Services_OpenStreetMap_Exception $ex) {
256 0 : switch ($ex->getCode()) {
257 0 : case Services_OpenStreetMap_Transport::NOT_FOUND:
258 0 : case Services_OpenStreetMap_Transport::UNAUTHORISED:
259 0 : case Services_OpenStreetMap_Transport::GONE:
260 0 : return false;
261 0 : default:
262 0 : throw $ex;
263 0 : }
264 : }
265 0 : $url = $config['server'] . 'api/'
266 0 : . $config['api_version']
267 0 : . '/user/preferences';
268 : try {
269 0 : $prefs = $this->getTransport()->getResponse(
270 0 : $url,
271 0 : HTTP_Request2::METHOD_GET,
272 0 : $user,
273 : $password
274 0 : );
275 0 : } catch (Services_OpenStreetMap_Exception $ex) {
276 0 : switch ($ex->getCode()) {
277 0 : case Services_OpenStreetMap_Transport::NOT_FOUND:
278 0 : case Services_OpenStreetMap_Transport::UNAUTHORISED:
279 0 : case Services_OpenStreetMap_Transport::GONE:
280 0 : return false;
281 0 : default:
282 0 : throw $ex;
283 0 : }
284 : }
285 0 : $obj = new Services_OpenStreetMap_User();
286 0 : $obj->setXml(simplexml_load_string($response->getBody()));
287 0 : $obj->setPreferencesXml($prefs->getBody());
288 0 : return $obj;
289 : }
290 :
291 : /**
292 : * Get details of specified way
293 : *
294 : * @param mixed $wayID wayID
295 : * @param mixed $version [optional] version of way
296 : *
297 : * @return string
298 : */
299 : public function getWay($wayID, $version = null)
300 : {
301 9 : $way = $this->getTransport()->getObject('way', $wayID, $version);
302 9 : if ($way !== false) {
303 9 : $way->setTransport($this->getTransport());
304 9 : $way->setConfig($this->getConfig());
305 9 : }
306 9 : return $way;
307 : }
308 :
309 : /**
310 : * Return an array of specified ways.
311 : *
312 : * <pre>
313 : * $ways = $osm->getWays($wayId, $way2Id);
314 : * </pre>
315 : *
316 : * @return array
317 : */
318 : public function getWays()
319 : {
320 1 : return $this->getTransport()->getObjects(
321 1 : 'way',
322 1 : Services_OpenStreetMap::getIDs(func_get_args())
323 1 : );
324 : }
325 :
326 : /**
327 : * Get details of specified node
328 : *
329 : * <code>
330 : * $osm = new Services_OpenStreetMap();
331 : * var_dump($osm->getNode(52245107));
332 : * </code>
333 : *
334 : * @param string $nodeID nodeID
335 : * @param mixed $version [optional] version of node
336 : *
337 : * @return string
338 : */
339 : public function getNode($nodeID, $version = null)
340 : {
341 0 : $node = $this->getTransport()->getObject('node', $nodeID, $version);
342 0 : if ($node !== false) {
343 0 : $node->setTransport($this->getTransport());
344 0 : $node->setConfig($this->getConfig());
345 0 : }
346 0 : return $node;
347 : }
348 :
349 : /**
350 : * Return an array of specified nodes.
351 : *
352 : * If none can be retrieved, for example if they all have been deleted,
353 : * then the boolean false value is returned.
354 : *
355 : * <code>
356 : * $osm = new Services_OpenStreetMap();
357 : * var_dump($osm->getNodes(52245107, 52245108));
358 : * </code>
359 : * Or
360 : * <code>
361 : * $osm = new Services_OpenStreetMap();
362 : * var_dump($osm->getNodes(array(52245107, 52245108)));
363 : * </code>
364 : *
365 : * @return Services_OpenStreetMap_Nodes
366 : */
367 : public function getNodes()
368 : {
369 0 : return $this->getTransport()->getObjects(
370 0 : 'node',
371 0 : Services_OpenStreetMap::getIDs(func_get_args())
372 0 : );
373 : }
374 : }
375 :
376 : ?>
|