-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm.php
236 lines (201 loc) · 4.53 KB
/
osm.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
<?php
abstract class Element
{
private $id, $version, $changeset, $timestamp, $uid, $user, $tags;
function __construct($xml)
{
$this->id = (int)($xml->getAttribute('id'));
$this->version = (int)($xml->getAttribute('version'));
$this->changeset = (int)($xml->getAttribute('changeset'));
$this->timestamp = strtotime($xml->getAttribute('timestamp'));
$this->uid = (int)($xml->getAttribute('uid'));
$this->user = $xml->getAttribute('user');
$this->tags = array();
foreach($xml->childNodes as $child)
{
if($child->localName == 'tag')
$this->tags[$child->getAttribute('k')] = $child->getAttribute('v');
}
}
public function getId()
{
return $this->id;
}
public function getVersion()
{
return $this->version;
}
public function getChangeset()
{
return $this->changeset;
}
public function getTimestamp()
{
return $this->timestamp;
}
public function getUid()
{
return $this->uid;
}
public function getUser()
{
return $this->user;
}
public function getMeta()
{
return array('id' => $this->id, 'version' => $this->version, 'changeset' => $this->changeset, 'timestamp' => $this->timestamp, 'uid' => $this->uid, 'user' => $this->user);
}
public function getTags()
{
return $this->tags;
}
public function getTag($key)
{
return (array_key_exists($key, $this->tags) ? $this->tags[$key] : "");
}
}
class Node extends Element
{
private $lat, $lon;
function __construct($xml)
{
parent::__construct($xml);
$this->lat = (float)($xml->getAttribute('lat'));
$this->lon = (float)($xml->getAttribute('lon'));
}
public function getLat()
{
return $this->lat;
}
public function getLon()
{
return $this->lon;
}
public function getCoords()
{
return array('lat' => $this->lat, 'lon' => $this->lon);
}
}
class Way extends Element
{
private $nodes;
function __construct($xml)
{
parent::__construct($xml);
$this->nodes = array();
foreach($xml->childNodes as $child)
{
if($child->localName == 'nd')
$this->nodes[] = (int)($child->getAttribute('ref'));
}
}
public function getNodes()
{
return $this->nodes;
}
public function isClosed()
{
return($this->nodes[0] == $this->nodes[count($this->nodes) - 1]);
}
}
class Relation extends Element
{
private $members;
function __construct($xml)
{
parent::__construct($xml);
$this->members = array();
foreach($xml->childNodes as $child)
{
if($child->localName == 'member')
$this->members[] = array('role' => $child->getAttribute('role'), 'type' => $child->getAttribute('type'), 'ref' => (int)($child->getAttribute('ref')));
}
}
public function getMembers()
{
return $this->members;
}
public function getMembersByRole($role)
{
return array_filter($this->members, function ($x) use($role) { return preg_match($role, $x['role']); });
}
public function getMembersByType($type)
{
return array_filter($this->members, function ($x) use($type) { return($x['type'] == $type); });
}
public function getMembersByRoleAndType($role, $type)
{
return array_filter($this->members, function ($x) use($role, $type) { return(preg_match($role, $x['role']) && ($x['type'] == $type)); });
}
}
class OSM
{
private $nodes, $ways, $relations;
function __construct($data)
{
$this->nodes = array();
$this->ways = array();
$this->relations = array();
$xml = new DOMDocument;
$xml->formatOutput = false;
$xml->loadXML($data);
$xmlnodes = $xml->getElementsByTagName("node");
foreach($xmlnodes as $xmlnode)
{
$node = new Node($xmlnode);
$this->nodes[$node->getId()] = $node;
}
$xmlways = $xml->getElementsByTagName("way");
foreach($xmlways as $xmlway)
{
$way = new Way($xmlway);
$this->ways[$way->getId()] = $way;
}
$xmlrelations = $xml->getElementsByTagName("relation");
foreach($xmlrelations as $xmlrelation)
{
$relation = new Relation($xmlrelation);
$this->relations[$relation->getId()] = $relation;
}
}
public function getNode($id)
{
if(array_key_exists($id, $this->nodes))
return $this->nodes[$id];
else
return false;
}
public function getWay($id)
{
if(array_key_exists($id, $this->ways))
return $this->ways[$id];
else
return false;
}
public function getRelation($id)
{
if(array_key_exists($id, $this->relations))
return $this->relations[$id];
else
return false;
}
public function getElement($type, $id)
{
switch($type)
{
case 'node':
return $this->getNode($id);
break;
case 'way':
return $this->getWay($id);
break;
case 'relation':
return $this->getRelation($id);
break;
default:
return false;
break;
}
}
}
?>