-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic.php
More file actions
131 lines (101 loc) · 4.3 KB
/
dynamic.php
File metadata and controls
131 lines (101 loc) · 4.3 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
<?php
header("Content-Type: application/json");
include("phpfastcache/phpfastcache.php");
$cache = phpFastCache("files");
error_reporting (E_ALL);
$CACHE_KEY ="overpassResult";
$logMessage="";
if($_SERVER["HTTP_CACHE_CONTROL"]=="no-cache" || $_SERVER["HTTP_PRAGMA"]=="no-cache"){
// $cache->delete($CACHE_KEY);
// $cache->delete($CACHE_KEY.$queryParams['cacheKey']);
$cache->clean(); //clear complete cache, necessary if using argument-dependant cache key
$logMessage.='cache cleaned'.', ';
}
$queryParams=getQueryParams();
$logMessage.= stripLineBreaks(var_export($queryParams,true)).', ';
$cacheResult = $cache->get($CACHE_KEY.$queryParams['cacheKey']);
if($cacheResult == null) {
$logMessage.= 'not in cache'.', ';
//$overpassQuery='[out:json][bbox:51.403061,6.726379,51.489507,7.076569];(way[building=university]);(node(w)[entrance]);out;';
$overpassQuery='
[out:json];
(way(around:'.$queryParams['range'].','.$queryParams['lat'].','.$queryParams['lng'].')[building=university]);
(node(w)[entrance]);
out;
';
$overpassUrl='http://overpass-api.de/api/interpreter';
$querystr='?data='.urlencode($overpassQuery);
$logMessage.= stripLineBreaks($overpassQuery). ', ';
$GEOJSON_FILE_NAME=$overpassUrl.$querystr;
$logMessage.= $GEOJSON_FILE_NAME. ', ';
// echo '<a href="'.$GEOJSON_FILE_NAME.'">'.$GEOJSON_FILE_NAME.'</a><br>';
$geojsonContent= file_get_contents($GEOJSON_FILE_NAME);
//echo "<pre>".htmlspecialchars($geojsonContent)."</pre><br>";
$phpObj = json_decode ( $geojsonContent, false, 512 );
$jsonexp = json_encode ( $phpObj, 0 );
$allResults=$phpObj->elements;
$transferArray = Array ();
if (json_last_error ()) {
$logMessage.= 'json_last_error_msg='.json_last_error_msg ().',';
} else {
foreach ( $allResults as $curMarker ) {
if(!isset($curMarker->tags->name)){
$curMarker->tags->name=".";
}
$newElement = array (
'id' => $curMarker ->id.'',
'lat' => $curMarker->lat,
'lng' => $curMarker->lon,
'elevation' => 0,
'title' => $curMarker->tags->name,
'area' => 0,
'details' => '',
);
$transferArray [] = $newElement;
}
$output= array(
'status'=> 'OK',
'num_results'=> count($transferArray),
'results'=> $transferArray);
$output = json_encode ( $output, 0 );
$logMessage.= 'num_results='.count($transferArray).', ';
// Write to cache to save API calls next time
$cache->set($CACHE_KEY.$queryParams['cacheKey'], $output, 600);// set to cache for 600 seconds = 10 minutes and 0 = never expired
}
} else {
$logMessage.= 'was cached'.', ';
$output=$cacheResult;
}
writeLogMessage($logMessage);
echo $output;
function getQueryParams(){
$queryParams=array('lat'=>51.46184,'lng'=>7.01655,'range'=>5000.0); //set defaults (Campus Schützenbahn, 5km)
if(isset($_GET['latitude'])){
$queryParams['lat']=min(90.0,max(-90.0,floatval($_GET['latitude']))); // clean request latitude to be float between -90 and 90 (degrees)
}
if(isset($_GET['longitude'])){
$queryParams['lng']=min(180.0,max(-180.0,floatval($_GET['longitude']))); // clean request longitude to be float between -180 and 180 (degrees)
}
if(isset($_GET['radius'])){
$queryParams['range']=min(20.0,max(0.0,floatval($_GET['radius']))); // clean request range to be float between 0 and 20 (km)
$queryParams['range']=$queryParams['range']*1000; // HTTP URL query parameter (&radius=) given as km, but Overpass needs m
}
//round for better cacheablility, range to 100m and lat and lon to 3 decimal places (accuracy around 100m), see https://wiki.openstreetmap.org/wiki/DE:Genauigkeit_von_Koordinaten#Genauigkeit_der_Breite
$queryParams['range']=round($queryParams['range'], -2);
$queryParams['lat']=round($queryParams['lat'], 3);
$queryParams['lng']=round($queryParams['lng'], 3);
$queryParams['cacheKey']=$queryParams['lat'].','.$queryParams['lng'].','.$queryParams['range'];
return $queryParams;
}
function writeLogMessage($logMessage=""){
$LOGADATEI_NAME="log-dynamic.htm";
$LOG_PREFIX = strftime ("%A, %e. %B %Y %H:%M:%S (%Z)", time()).": ".$_SERVER['QUERY_STRING']. "(".$_SERVER['HTTP_USER_AGENT'].");";
$logdatei = fopen($LOGADATEI_NAME,"a+");
fputs($logdatei, $LOG_PREFIX);
fputs($logdatei, $logMessage);
fputs($logdatei, "<br /> \r\n");
}
function stripLineBreaks($inputStr){
$outputStr=str_replace(array("\r", "\n"), '', $inputStr);
return $outputStr;
}