-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwwimp.php
154 lines (131 loc) · 5.82 KB
/
nwwimp.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
<?php
include './Tonic.php';
use Tonic\Tonic as Tonic;
/**
* The API Key for the Wiener Linien Routing API
* @var string
*/
$wlSender = '';
/**
* URL for the Wiener Linien Routing API
* @var string
*/
$routingRawUrl = 'http://www.wienerlinien.at/ogd_routing/XML_TRIP_REQUEST2?locationServerActive=1&type_origin=coord&name_origin=%f:%f:WGS84&type_destination=coord&name_destination=%f:%f:WGS84&sender='.$wlSender;
/**
* Reverse geocoding url by the City Of Vienna.
* With placeholder for coordinates.
* Not used in this version of the program.
* @var string
*/
$reverseGeocodingRawUrl = 'http://data.wien.gv.at/daten/OGDAddressService.svc/ReverseGeocode?location=%f,%f&crs=EPSG:4326&type=A3:8012';
/**
* Url for the christmas market data from the City Of Vienna.
* @var string
*/
$punschUrl = 'http://data.wien.gv.at/daten/geo?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:ADVENTMARKTOGD&srsName=EPSG:4326&outputFormat=json';
define('latitude', (float) $_GET['latitude']);
define('longitude', (float) $_GET['longitude']);
/**
* Function for various curl requests made throughout the program.
* @param string $url The url to be called.
* @return string Returns the content of the response.
*/
function request($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/**
* Checks if the coordinates are in a close range of each your gps location.
* GPS location is given as GET Parameter by a JS-File (index.html) and
* defined as constant some lines above.
* @param array $coordinates The coordinates in an array.
* @return boolean True if coordinates are close
*/
function isClose($coordinates){
$minLat = latitude - 0.01;
$maxLat = latitude + 0.01;
$minLong = longitude - 0.01;
$maxLong = longitude + 0.01;
if ($coordinates[1] > $minLat && $coordinates[1] < $maxLat && $coordinates[0] > $minLong && $coordinates[0] < $maxLong){
return true;
}
return false;
}
/**
* Returns a minified version of the object that contains data from the
* christmas market.
* @param object $punsch The SimpleXMLObject containing data
* @return array Contains the minified version
*/
function minifyPunsch($punsch){
$minifyPunsch = ["properties" => (array) $punsch->properties,
"coordinates" => (array) $punsch->geometry->coordinates];
return $minifyPunsch;
}
/**
* Wiener Linien Routing API return departure time somewhat unconvenient.
* 08:09 is 8:9 - This function corrects that.
* @param int $int number witout leading zero
* @return int number with leading zero
*/
function correctTime($int){
if ($int < 10){
return '0'.$int;
}
return $int;
}
/**
* Minifes the route XML to an Array
* @param object $route SimpleXMLObject containing all the information.
* @return array Contains only important information
*/
function minifyRoute($route){
foreach ($route->itdTripRequest->itdItinerary->itdRouteList->itdRoute[0]->itdPartialRouteList[0] as $partialRoute){
$walkway = 2;
if ($partialRoute->itdMeansOfTransport['productName'] == "Fussweg"){
$walkway = 1;
$cssSelector = "transportWalk";
} else if($partialRoute->itdMeansOfTransport['productName'] == "U-Bahn"){
$cssSelector = "transport".$partialRoute->itdMeansOfTransport['shortname'];
} else if ($partialRoute->itdMeansOfTransport['productName'] == "Straßenbahn") {
$cssSelector = "transportTram";
} else {
$cssSelector = "transport".$partialRoute->itdMeansOfTransport['productName'];
}
$punschRoute[] = ['origin' =>
["name" => str_replace("Wien, ", '', str_replace("Wien ", '', (string) $partialRoute->itdPoint[0]['name'])),
"time" => (string) correctTime($partialRoute->itdPoint[0]->itdDateTime->itdTime['hour']).':'.correctTime($partialRoute->itdPoint[0]->itdDateTime->itdTime['minute'])],
'destination' =>
["name" => str_replace("Wien, ", '', str_replace("Wien ", '', (string) $partialRoute->itdPoint[1]['name'])),
"time" => (string) correctTime($partialRoute->itdPoint[1]->itdDateTime->itdTime['hour']).':'.correctTime($partialRoute->itdPoint[1]->itdDateTime->itdTime['minute'])],
'meansOfTransport' =>
['type' => (string) $partialRoute->itdMeansOfTransport['productName'],
'linenumber' => (string) $partialRoute->itdMeansOfTransport['shortname'],
'direction' => str_replace("Wien, ", '', str_replace("Wien ", '',(string) $partialRoute->itdMeansOfTransport['destination']))
],
'isWalkWay' => $walkway,
'cssSelector' => $cssSelector];
}
return $punschRoute;
}
$allPunsch = json_decode(request($punschUrl));
foreach ($allPunsch->features as $punsch){
if (isClose($punsch->geometry->coordinates)){
$closePunsch[]['punsch'] = minifyPunsch($punsch);
}
}
for ($i=0; $i < count($closePunsch); $i++) {
$routingModifiedUrl = sprintf($routingRawUrl, longitude, latitude,$closePunsch[$i]['punsch']['coordinates'][0],$closePunsch[$i]['punsch']['coordinates'][1]);
$routingResponse = request($routingModifiedUrl);
$routingXML = simplexml_load_string($routingResponse);
$closePunsch[$i]['route'] = minifyRoute($routingXML);
}
$tpl = new Tonic("display.html");
$tpl->location = ['latitude' => latitude, 'longitude' => longitude];
$tpl->closePunsch = $closePunsch;
$tpl->fussweg = "Fussweg";
echo $tpl->render();