-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxapi.php
More file actions
executable file
·187 lines (157 loc) · 4.31 KB
/
Copy pathxapi.php
File metadata and controls
executable file
·187 lines (157 loc) · 4.31 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
<?php
require_once('system.php');
require_once('fileutils.php');
if(!ENABLE_XAPI)
{
header ('HTTP/1.1 503 Service Unavailable');
echo "XAPI is disabled in config.php.";
return;
}
CallFuncByMessage(Message::SCRIPT_START,Null);
$lock=GetReadDatabaseLock();
//$fi = fopen("log.txt","at");
//fwrite($fi,$_SERVER['REQUEST_URI']."\n");
$pathInfo = GetRequestPath();
$urlExp = explode("/",$pathInfo);
$xapiArg = $urlExp[2];
$xapiArgs = explode("[",$xapiArg);
//Split predicates
$xapiType = $xapiArgs[0];
$predicates = array();
for($i=1;$i<count($xapiArgs);$i++)
{
$temp = explode("]",$xapiArgs[$i]);
array_push($predicates, $temp[0]);
}
if($xapiType != "*" and $xapiType != "node" and $xapiType != "way" and $xapiType != "relation")
{
header("Content-Type:text/plain");
echo "Type ".$xapiType." is not supported.\n";
}
//TODO Handle non-predicate bbox
//Process predicates into individual variables
$bbox = null;
$key = null;
$value = null;
foreach ($predicates as $pred)
{
$temp = explode("=",$pred);
if(count($temp)!=2)
{
//continue;
header("Content-Type:text/plain");
echo "Predicate not supported in this implementation.\n";
exit(0);
}
$k = $temp[0];
$v = $temp[1];
if($k == "bbox") $bbox = $v;
else {$key = $k; $value = $v;}
}
if($value == "*") $value = null;
if($key == "*")
{
header("Content-Type:text/plain");
echo "Key wildcard is not supported.\n";
exit(0);
}
if(is_null($key))
{
header("Content-Type:text/plain");
echo "Key and value predicate must be specified in this XAPI implentation.\n";
exit(0);
}
//Convert bbox into float array
$bboxAr= null;
if(!is_null($bbox))
{
$bboxAr = explode(",",$bbox);
$bboxAr = array_map('floatval', $bboxAr);
}
//TODO validate bbox
//Query database
try
{
$refs = CallFuncByMessage(Message::XAPI_QUERY, array($xapiType,$bboxAr,$key,$value));
if($refs===null)
throw new Exception("Null response from XAPI module (if it exists)");
else
$osmxml = XapiQueryToXml($refs, $bbox);
header("Content-Type:text/xml");
echo $osmxml;
}
catch (Exception $e)
{
header('HTTP/1.1 500 Internal Server Error');
header("Content-Type:text/plain");
echo "Internal server error: ".$e->getMessage()."\n";
dprint("getTrace",$e->getTrace());
}
function XapiQueryToXml($refs,$bbox)
{
//Extract needed elements from database
$els = array();
foreach($refs as $elidstr)
{
$elIdStrExp = explode("-",$elidstr);
$type = $elIdStrExp[0];
$id = (int)$elIdStrExp[1];
$obj = CallFuncByMessage(Message::GET_OBJECT_BY_ID,array($type,$id,Null));
if(is_null($obj)) throw new Exception("Could not get element needed to fulfil XAPI query");
array_push($els, $obj);
}
//Return result
$out = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$out .= '<osm version="0.6" generator="'.SERVER_NAME.'">'."\n";
//Specify bounds
if(!is_null($bbox))
{
$out=$out.'<bounds minlat="'.$bbox[1].'" minlon="'.$bbox[0];
$out=$out.'" maxlat="'.$bbox[3].'" maxlon="'.$bbox[2].'"/>'."\n";
}
$ignoreMissing = 1;
foreach($els as $el)
{
$problemFound = 0;
//Also get the elements associated nodes and ways
foreach($el->members as $mem)
{
if($mem[0] == "node")
{
//For each referenced nodes,
$id = $mem[1];
$n = CallFuncByMessage(Message::GET_OBJECT_BY_ID,array("node",(int)$id, Null));
if(!is_object($n) and !$ignoreMissing)
throw new Exception("node needed in XAPI way not found, node ".$id);
if(is_object($n)) $out = $out.$n->ToXmlString()."\n";
else $problemFound = 1;
}
if($mem[0] == "way")
{
//For each referenced way,
$id = $mem[1];
$w = CallFuncByMessage(Message::GET_OBJECT_BY_ID,array("way",(int)$id, Null));
if(!is_object($w)) throw new Exception("way needed in XAPI way not found, way ".$id);
//Get the child nodes for this way also
foreach($w->nodes as $nd)
{
$id = $nd[0];
$n = CallFuncByMessage(Message::GET_OBJECT_BY_ID,array("node",(int)$id, Null));
if(!is_object($n) and !$ignoreMissing)
throw new Exception("node of way needed in XAPI way not found ".$id);
if(is_object($n)) $out = $out.$n->ToXmlString()."\n";
else $problemFound = 1;
}
if(is_object($w) and !$problemFound)
$out = $out.$w->ToXmlString()."\n";
}
}
//Output XAPI matched element
if(is_object($el) and !$problemFound)
$out .= $el->ToXmlString();
}
$out .= "</osm>\n";
return $out;
}
CallFuncByMessage(Message::SCRIPT_END,Null);
?>