Suburb geocoder for stop locations
[bus.git] / betweenpoint.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
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <script src="openlayers/OpenLayers.js"></script>
 <SCRIPT TYPE="text/javascript" SRC="OpenStreetMap.js"></SCRIPT> 
<script type="text/javascript" src="jquery.1.3.2.min.js"></script>
    <script type="text/javascript">
 
function init()
{
    // create the ol map object
    var map = new OpenLayers.Map('map');
    
  var osmtiles = new OpenLayers.Layer.OSM("local", "http://127.0.0.1/tiles/${z}/${x}/${y}.png")
// use http://open.atlas.free.fr/GMapsTransparenciesImgOver.php and http://code.google.com/p/googletilecutter/ to make tiles
    markers = new OpenLayers.Layer.Markers("Between Stop Markers");
 
 
<?php
  $conn = pg_connect("dbname=bus user=postgres password=snmc");
  if (!$conn) {
      echo "An error occured.\n";
      exit;
  }
  $result_stops = pg_query($conn, "Select * FROM stops");
  
  while ($stop = pg_fetch_assoc($result_stops)) {
      echo 'marker = new OpenLayers.Marker(new OpenLayers.LonLat(' . ($stop['lng'] / 10000000) . "," . ($stop['lat'] / 10000000) . ')
            .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
            ));';
      
      echo '
            marker.id="' . $stop['geohash'] . '";
            markers.addMarker(marker);
marker.events.register("mousedown", marker, function() {
document.getElementById("between_points").innerHTML += this.id+";";
});
';
  }
?>
var timeicon = new OpenLayers.Icon("http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png",new OpenLayers.Size(32,32));
var timepoints = new OpenLayers.Layer.GeoRSS("Timing Points", "displaytimepoints.georss.php", { icon: timeicon });
 
            map.addLayers([osmtiles, markers,timepoints]);
            map.addControl(new OpenLayers.Control.LayerSwitcher());
      map.zoomToExtent(markers.getDataExtent());
 }
    </script>
        <script type="text/javascript">
function submitBetween () {
        $.post("betweenpoint.submit.php", $("#inputform").serialize(), function(html){
        $("#response").html(html);
        //clearForms();
        return false;
      });
};
 
function OnChange(dropdown)
{
    var myindex  = dropdown.selectedIndex
    var selValue = dropdown.options[myindex].value;
  $("#routes").val(selValue.split(":",2)[0]);
  fromto = selValue.split(":",2)[1];
  $("#from").val(fromto.split("->",2)[0]);
  $("#to").val(fromto.split("->",2)[1]);
 document.getElementById("between_points").innerHTML = "";
    return true;
}
 
// function will clear input elements on each form
function clearForms(){
document.getElementById("between_points").innerHTML = "";
  // declare element type
  var type = null;
  // loop through forms on HTML page
  for (var x=0; x<document.forms.length; x++){
    // loop through each element on form
    for (var y=0; y<document.forms[x].elements.length; y++){
      // define element type
      type = document.forms[x].elements[y].type
      // alert before erasing form element
      //alert('form='+x+' element='+y+' type='+type);
      // switch on element type
      switch(type){
        case "text":
        case "textarea":
        case "password":
        //case "hidden":
          document.forms[x].elements[y].value = "";
          break;
        case "radio":
        case "checkbox":
          document.forms[x].elements[y].checked = true;
          break;
        case "select-one":
          document.forms[x].elements[y].options[0].selected = true;
          break;
        case "select-multiple":
          for (z=0; z<document.forms[x].elements[y].options.length; z++){
            document.forms[x].elements[y].options[z].selected = false;
          }
        break;
      }
    }
  }
}
    </script>
 
  </head>
  <body onload="init()">
   <div id="inputpane"><form id="inputform">
<select name=selectPair onchange='OnChange(this.form.selectPair);'>
<option>Select a from/to pair...</option>
<?php
  include('spyc/spyc.php');
  //$timetable = Spyc::YAMLLoad('../spyc.yaml');
  $path = "maxious-canberra-transit-feed/output/";
  $dhandle = opendir("maxious-canberra-transit-feed/output/");
  // define an array to hold the files
  $files = array();
  $paths = array();
  
  if ($dhandle) {
      // loop through all of the files
      while (false !== ($fname = readdir($dhandle))) {
          if (($fname != '.') && ($fname != '..')) {
              $timetable = Spyc::YAMLLoad("maxious-canberra-transit-feed/output/" . $fname);
                // Strip off individual platforms because it usually doesn't matter for routes
                $timetable["time_points"] = preg_replace("/\(Platform.*/","",$timetable["time_points"]);
              for ($i = 0; $i < sizeof($timetable["time_points"]) - 1; $i++) {
                $key = trim($timetable["time_points"][$i]) . "->" . trim($timetable["time_points"][$i + 1]);
                if (strstr($paths[$key],";" . $timetable["short_name"] . ";") === false)
                  @$paths[$key] .= $timetable["short_name"] . ";";
              }
          }
      }
  }
  ksort($paths);
  
  $completedPaths = array();
  $result_betweenstops = pg_query($conn, "Select * FROM between_stops");
  while ($path = pg_fetch_assoc($result_betweenstops)) {
    $key = trim($path['fromlocation']) . "->" . trim($path['tolocation']);
    $completedPaths[$key].= trim($path['routes']);
    
  }
 
 
  foreach ($paths as $path => $routes) {
      if (!in_array($path, array_keys($completedPaths))) {
          echo "<option value=\"$routes:$path\">" . sizeof(explode(";", $routes)) . " $path</option>\n";
      } else {
        $completedRoutes = explode(";", $completedPaths[$path]);
         $incompleteRoutes = "";
        foreach (explode(";", $routes) as $route) {
 
          if (!in_array($route,$completedRoutes) && strstr($incompleteRoutes,';'.$route.';') === false) {
          $incompleteRoutes .= $route.';';
          }
          
        }
        if ($incompleteRoutes != "") {
          echo "<option value=\"$incompleteRoutes:$path\">" . sizeof(explode(";", $incompleteRoutes)) . " $path</option>\n";
        }
      }
      
  }
?>
</select>
 from <input type="text" name="from" id="from"/>
 to <input type="text" name="to" id="to"/>
<br>
 on routes <input type="text" name="routes" id="routes"/>
Reverse? <input type="checkbox" name="reverse" id="reverse" checked="true"/> 
<input type="button" onclick="javascript:submitBetween()" value="Submit!">
<input type="button" value="Clear" onclick="javascript:clearForms()" title="Start clearForms() JavaScript function">
<br>
<textarea name="between_points" id="between_points" rows="1" cols="120"></textarea>
</form>
<div id="response">
    <!-- Our message will be echoed out here -->
  </div>
</div>
   <div id="map" width="100%" height="100%"></div>
  </body>
</html>