More buffer flush while printing unsorted lists
[busui.git] / labs / tripPlannerTester.kml.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
<?php
// http://www.herethere.net/~samson/php/color_gradient/color_gradient_generator.php.txt
// return the interpolated value between pBegin and pEnd
function interpolate($pBegin, $pEnd, $pStep, $pMax)
{
        if ($pBegin < $pEnd) {
                return (($pEnd - $pBegin) * ($pStep / $pMax)) + $pBegin;
        }
        else {
                return (($pBegin - $pEnd) * (1 - ($pStep / $pMax))) + $pEnd;
        }
}
function Gradient($HexFrom, $HexTo, $ColorSteps)
{
        $theColorBegin = hexdec($HexFrom);
        $theColorEnd = hexdec($HexTo);
        $theNumSteps = intval($ColorSteps);
        $theR0 = ($theColorBegin & 0xff0000) >> 16;
        $theG0 = ($theColorBegin & 0x00ff00) >> 8;
        $theB0 = ($theColorBegin & 0x0000ff) >> 0;
        $theR1 = ($theColorEnd & 0xff0000) >> 16;
        $theG1 = ($theColorEnd & 0x00ff00) >> 8;
        $theB1 = ($theColorEnd & 0x0000ff) >> 0;
        $GradientColors = array();
        // generate gradient swathe now
        for ($i = 0; $i <= $theNumSteps; $i++) {
                $theR = interpolate($theR0, $theR1, $i, $theNumSteps);
                $theG = interpolate($theG0, $theG1, $i, $theNumSteps);
                $theB = interpolate($theB0, $theB1, $i, $theNumSteps);
                $theVal = ((($theR << 8) | $theG) << 8) | $theB;
                $GradientColors[] = sprintf("%06X", $theVal);
        }
        return $GradientColors;
}
function processLeg($legNumber, $leg)
{
        $legArray = object2array($leg);
        if ($legArray["@mode"] === "BUS") {
                return "bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']);
        }
        else {
                return "walk";
                //$walkingstep = "walk ";
                //if (strpos($step->streetName, "from") !== false && strpos($step->streetName, "way") !== false) {
                //      $walkingstep.= "footpath";
                //}
                //else {
                //      $walkingstep.= $step->streetName;
                //}
                //$walkingstep.= floor($step->distance) . "m";
                //return $walkingstep;
        }
}
$csv = false;
$kml = true;
if ($kml) {
        //header('Content-Type: application/vnd.google-earth.kml+xml');
        echo '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"><Document>';
}
include ('../include/common.inc.php');
//Test code to grab transit times
// make sure to sleep(10);
$boundingBoxes = Array(
        "belconnen" => Array(
                "startlat" => - 35.1928,
                "startlon" => 149.006,
                "finishlat" => - 35.2630,
                "finishlon" => 149.1045,
        ) ,
        "north gungahlin civic" => Array(
                "startlat" => - 35.1828,
                "startlon" => 149.1045,
                "finishlat" => - 35.2955,
                "finishlon" => 149.1559,
        ) ,
        "west duffy" => Array(
                "startlat" => - 35.3252,
                "startlon" => 149.0240,
                "finishlat" => - 35.3997,
                "finishlon" => 149.0676,
        ) ,
        "central south" => Array(
                "startlat" => - 35.3042,
                "startlon" => 149.0762,
                "finishlat" => - 35.3370,
                "finishlon" => 149.1806,
        ) ,
        "south" => Array(
                "startlat" => - 35.3403,
                "startlon" => 149.0714,
                "finishlat" => - 35.4607,
                "finishlon" => 149.1243,
        )
);
$latdeltasize = 0.01;
$londeltasize = 0.01;
$from = "Wattle Street";
$fromPlace = (startsWith($from, "-") ? $from : geocode($from, false));
$startTime = "9:00 am";
$startDate = "03/21/2011"; // american dates, OTP does not validate!
$counter = 0;
$regionTimes = Array();
$testRegions = Array();
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
if ($csv) echo "<pre>";
if ($csv) echo "lat,lon,time,latdeltasize, londeltasize, region key name\n";
foreach ($boundingBoxes as $key => $boundingBox) {
        for ($i = $boundingBox['startlat']; $i >= $boundingBox['finishlat']; $i-= $latdeltasize) {
                for ($j = $boundingBox['startlon']; $j <= $boundingBox['finishlon']; $j+= $londeltasize) {
                        $url = $otpAPIurl . "ws/plan?date=" . urlencode($startDate) . "&time=" . urlencode($startTime) . "&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=440&wheelchair=false&toPlace=" . $i . "," . $j . "&fromPlace=$fromPlace";
                        $ch = curl_init($url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                "Accept: application/json"
                        ));
                        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                        $page = curl_exec($ch);
                        if (curl_errno($ch)) {
                                if ($csv) echo "Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch);
                        }
                        else {
                                $tripplan = json_decode($page); 
                                $plans = Array();
                                if (is_array($tripplan->plan->itineraries->itinerary)) {
                                        foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) {
                                                $plans[floor($itinerary->duration / 60000) ] = $itinerary;
                                        }
                                }
                                else {
                                        $plans[floor($tripplan->plan->itineraries->itinerary->duration / 60000) ] = $tripplan->plan->itineraries->itinerary;
                                }
                                if ($csv) echo "$i,$j," . min(array_keys($plans)) . ",$latdeltasize, $londeltasize,$key\n";
                                if ($kml) {
                                        $time = min(array_keys($plans));
                                        $plan = "";
                                        if (is_array($plans[min(array_keys($plans)) ]->legs->leg)) {
                                                foreach ($plans[min(array_keys($plans)) ]->legs->leg as $legNumber => $leg) {
                                                        $plan .= processLeg($legNumber, $leg).",";
                                                }
                                        }
                                        else {
                                                $plan .= processLeg(0, $plans[min(array_keys($plans)) ]->legs->leg);
                                        }
                                                if (isset($tripplan->error) && $tripplan->error->id == 404) {
                                                        $time = 999;
                                                        $plan = "Trip not possible without excessive walking from nearest bus stop";
                                                }
                                        $testRegions[] = Array(
                                                "lat" => $i,
                                                "lon" => $j,
                                                "time" => $time,
                                                "latdeltasize" => $latdeltasize,
                                                "londeltasize" => $londeltasize,
                                                "regionname" => $key,
                                                "plan" => $plan . "<br/><a href='". htmlspecialchars($url)."'>original plan</a>"
                                        );
                                        $regionTimes[] = $time;
                                }
                        }
                        flush(); @ob_flush();
                        curl_close($ch);
                }
        }
}
if ($kml) {
        $colorSteps = 9;
        //$minTime = min($regionTimes);
        //$maxTime = max($regionTimes);
        //$rangeTime = $maxTime - $minTime;
        //$deltaTime = $rangeTime / $colorSteps;
        $Gradients = Gradient(strrev("66FF00"), strrev("FF0000"), $colorSteps); // KML is BGR not RGB so strrev
        foreach ($testRegions as $testRegion) {
                //$band = (floor(($testRegion[time] - $minTime) / $deltaTime));
                $band = (floor($testRegion[time] / 10));
                if ($band > $colorSteps) $band = $colorSteps;
                echo "<Placemark>
  <name>" . $testRegion['regionname'] . " time {$testRegion['time']} band $band</name>
  <description> {$testRegion['plan']} </description>
    <Style>
        <PolyStyle>
            <color>c7" . $Gradients[$band] . "</color>" . // 7f = 50% alpha, c7=78%
                "</PolyStyle>
        <LineStyle>
            <color>c7" . $Gradients[$band] . "</color>" . "</LineStyle>
    </Style>
   <Polygon>
<altitudeMode>relativeToGround</altitudeMode>
    <outerBoundaryIs>
      <LinearRing>
        <coordinates>
          " . ($testRegion['lon'] - ($testRegion['londeltasize'] / 2)) . "," . ($testRegion['lat'] - ($testRegion['latdeltasize'] / 2)) . ",500\n" . ($testRegion['lon'] - ($testRegion['londeltasize'] / 2)) . "," . ($testRegion['lat'] + ($testRegion['latdeltasize'] / 2)) . ",500\n" . ($testRegion['lon'] + ($testRegion['londeltasize'] / 2)) . "," . ($testRegion['lat'] + ($testRegion['latdeltasize'] / 2)) . ",500\n" . ($testRegion['lon'] + ($testRegion['londeltasize'] / 2)) . "," . ($testRegion['lat'] - ($testRegion['latdeltasize'] / 2)) . ",500\n" . ($testRegion['lon'] - ($testRegion['londeltasize'] / 2)) . "," . ($testRegion['lat'] - ($testRegion['latdeltasize'] / 2)) . ",500\n" . "
          
        </coordinates>
      </LinearRing>
    </outerBoundaryIs>
  </Polygon>
</Placemark>";
        }
        echo "\n</Document></kml>\n";
}
if ($csv) echo "</pre>";
?>