-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel-common.php
More file actions
executable file
·277 lines (223 loc) · 6.92 KB
/
Copy pathmodel-common.php
File metadata and controls
executable file
·277 lines (223 loc) · 6.92 KB
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
<?php
include_once('osmtypes.php');
abstract class OsmDatabaseCommon
{
//**********************
//General main query
//**********************
public abstract function GetNodesInBbox($bbox);
//{
//Return node objects in array
// return array();
//}
public abstract function GetParentWaysOfNodes(&$nodes);
//{
//Return way objects in array
// return array();
//}
public function GetNodesToCompleteWays(&$nodes, &$ways)
{
//List node Ids
$nids = array();
foreach($nodes as $node)
{
array_push($nids,$node->attr['id']);
}
//print_r(count($nids));
$additionalNodes = array();
foreach($ways as $way)
{
//print_r($way);
foreach($way->members as $data)
{
$id = $data[1];
if(in_array($id,$nids)) continue;
array_push($additionalNodes, $id);
}
}
//print_r($additionalNodes);
foreach($additionalNodes as $id)
{
$obj = $this->GetElementById("node",$id);
if(!is_object($obj)) throw new Exception("Could not complete way, node ".$id." not found.");
array_push($nodes,$obj);
}
return 1;
}
public abstract function GetParentRelations(&$els);
//{
// //Return way objects in array
// return array();
//}
public function MapQuery($bbox)
{
//Get nodes
$out = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$out = '<osm version="0.6" generator="'.SERVER_NAME.'">'."\n";
//Specify bounds
$bbox = ValidateBbox($bbox);
$out=$out.'<bounds minlat="'.$bbox[1].'" minlon="'.$bbox[0];
$out=$out.'" maxlat="'.$bbox[3].'" maxlon="'.$bbox[2].'"/>'."\n";
$timers = array();
$startTimer = microtime(1);
$nodes = $this->GetNodesInBbox($bbox);
$timers['nodes']=(microtime(1) - $startTimer);
foreach($nodes as $n) if(!is_object($n))
throw new Exception("Retrieved database object type ".gettype($n)." incorrect");
$startTimer = microtime(1);
$ways = $this->GetParentWaysOfNodes($nodes);
$timers['ways']=(microtime(1) - $startTimer);
$startTimer = microtime(1);
$this->GetNodesToCompleteWays($nodes, $ways);
$timers['ways2']=(microtime(1) - $startTimer);
foreach($nodes as $n) if(!is_object($n))
throw new Exception("Retrieved database object type ".gettype($n)." incorrect");
$elsQuery = array_merge($nodes, $ways);
$startTimer = microtime(1);
$relations = $this->GetParentRelations($elsQuery);
$timers['relations']=(microtime(1) - $startTimer);
//print_r($timers); die();
foreach($nodes as $obj)
{
//print_r($obj); echo "\n";
if(!is_object($obj))
throw new Exception("Retrieved database object type ".gettype($obj)." incorrect");
$out=$out.$obj->ToXmlString();
}
foreach($ways as $obj)
{
if(!is_object($obj))
throw new Exception("Retrieved database object type ".gettype($obj)." incorrect");
$out=$out.$obj->ToXmlString();
}
foreach($relations as $obj)
{
if(!is_object($obj))
throw new Exception("Retrieved database object type ".gettype($obj)." incorrect");
$out=$out.$obj->ToXmlString();
}
$out = $out."</osm>";
//return array();
return $out;
}
//**********************************
//Get specific info from database
//**********************************
public function GetCurentVerOfElement($type,$id)
{
$obj = $this->GetElementById($type,$id);
if(!is_object($obj)) return $obj; //Not found or gone
if(!isset($obj->attr['version']))
throw new Exception("Internal database has missing version attribute.");
return $obj->attr['version'];
}
public abstract function GetElementById($type,$id,$version=null);
public function GetElementAsXmlString($type,$id)
{
$obj = $this->GetElementById($type,$id);
if(!is_object($obj)) return $obj;
return $obj->ToXmlString();
}
public function CheckElementExists($type,$id)
{
$obj = $this->GetElementById($type,$id);
return is_object($obj);
}
public function GetCitingWaysOfNode($id)
{
$obj = new OsmNode();
$obj->attr['id'] = $id;
$objarr = array($obj);
$ret = $this->GetParentWaysOfNodes($objarr);
$out = array();
foreach($ret as $obj) array_push($out,$obj->attr['id']);
return $out;
}
public function GetCitingRelations($type,$id)
{
$obj = OsmElementFactory($type);
$obj->attr['id'] = $id;
$objarr = array($obj);
$ret = $this->GetParentRelations($objarr);
$out = array();
foreach($ret as $obj) array_push($out,$obj->attr['id']);
return $out;
}
public function GetBboxOfElement($type,$id,$depth = 0)
{
//Get the bounding box of an element
//Return format: min_lon,min_lat,max_lon,max_lat
$bbox= null;
//Prevent infinite recursion
$maxDepth = 10;
if($depth>$maxDepth) return null;
//Get parent element
$el = $this->GetElementById($type,$id);
if(!is_object($el)) return null;
//Use own position attribute
if(isset($el->attr['lon']) and isset($el->attr['lat']))
UpdateBbox($bbox,
array($el->attr['lon'],$el->attr['lat'],
$el->attr['lon'],$el->attr['lat']));
//Recursively get member elements
foreach($el->members as $member)
UpdateBbox($bbox,$this->GetBboxOfElement($member[0],$member[1],$depth+1));
return $bbox;
}
function GetFullDetailsOfElement($firstObj,$depth = 0,$maxDepth=5)
{
if(!is_object($firstObj)) throw new Exception("Parent object must be defined");
//echo $type.$id."\n";
//if($depth > $maxDepth) return Null;
//$firstObj = $this->GetElementById($type,(int)$id, Null);
//print_r($firstObj);
if($firstObj===null or $firstObj===0) return "not-found";
if($firstObj===-2) return "gone";
//Get members recursively
$out = array($firstObj);
foreach($firstObj->members as $data)
{
$memtype = $data[0];
$memid = $data[1];
$memObj = $this->GetElementById($memtype,(int)$memid, Null);
if(!is_object($memObj)) throw new Exception("Return type is not object, as expected");
$obj = $this->GetFullDetailsOfElement($memObj,$depth,$maxDepth);
if(!is_array($obj)) throw new Exception("Return type is not array, as expected");
$out = array_merge($out,$obj);
if(count($out) > 10000) throw new Exception("Buffer too large. Halting to protect data.");
}
return $out;
}
function GetFullParentsOfElement($firstObj,$maxHeight=5)
{
if(!is_object($firstObj)) throw new Exception("Initial object must be defined");
$id = $firstObj->attr['id'];
$type = $firstObj->GetType();
$queryObjs = array($firstObj);
#If this is a node, get parent ways
if($type=="node")
{
$queryObjs = array_merge($queryObjs, $this->GetParentWaysOfNodes($queryObjs));
}
$out = $queryObjs;
#Recursively get relations
$height = 0;
while(count($queryObjs)>0 and $height < $maxHeight)
{
$rels = $this->GetParentRelations($queryObjs);
$queryObjs = $rels;
$out = array_merge($out, $rels);
$height++;
}
return $out;
}
public abstract function CheckPermissions();
//***********************
//Modification functions
//***********************
public abstract function CreateElement($type,$id,$el);
public abstract function ModifyElement($type,$id,$el);
public abstract function DeleteElement($type,$id,$el);
public abstract function Purge();
}
?>