Add suburb geocode to timingpoints tables too
Add suburb geocode to timingpoints tables too

#!/usr/bin/ruby #!/usr/bin/ruby
   
require 'highline.rb' require 'highline.rb'
include HighLine include HighLine
   
require 'rubygems' require 'rubygems'
require 'postgres' require 'postgres'
require 'json' require 'json'
require 'yaml' require 'yaml'
require 'pp' require 'pp'
# make - { name: Civic Interchange Platform 1,stop_code: civic_platform_1, lat: -35.2794347, lng: 149.130588} # make - { name: Civic Interchange Platform 1,stop_code: civic_platform_1, lat: -35.2794347, lng: 149.130588}
connbus = PGconn.connect("localhost", 5432, '', '', "bus", "postgres", "snmc") connbus = PGconn.connect("localhost", 5432, '', '', "bus", "postgres", "snmc")
   
f = File.open('cbrtable.yml.in.in') f = File.open('cbrtable.yml.in.in')
header = f.readlines header = f.readlines
f.close f.close
   
File.open('cbrtable.yml.in', 'w') do |f2| File.open('cbrtable.yml.in', 'w') do |f2|
f2.puts header f2.puts header
f2.puts "stops:\n"; f2.puts "stops:\n";
begin begin
time_points = connbus.exec("SELECT * from timing_point ORDER BY name") time_points = connbus.exec("SELECT * from timing_point ORDER BY name")
rescue PGError => e rescue PGError => e
puts "Error reading from DB #{e}" puts "Error reading from DB #{e}"
#conn.close() if conn #conn.close() if conn
end end
time_points.each do |time_point| time_points.each do |time_point|
#pp time_point #pp time_point
# 0 = name # 0 = name
# 1 = lat*100000 # 1 = lat*100000
# 2 = lng*100000 # 2 = lng*100000
  # 7 = suburb(s)
#pp time_point[0] #pp time_point[0]
f2.puts " - { name: #{time_point[0]},stop_code: #{time_point[0]}, lat: #{Float(time_point[1])/10000000}, lng: #{Float(time_point[2])/10000000}}" f2.puts " - { name: #{time_point[0]},stop_code: #{time_point[0]}, lat: #{Float(time_point[1])/10000000}, lng: #{Float(time_point[2])/10000000}, zone_id: #{time_point[7]} }"
end end
begin begin
stops = connbus.exec("SELECT * from stops") stops = connbus.exec("SELECT * from stops")
rescue PGError => e rescue PGError => e
puts "Error reading from DB #{e}" puts "Error reading from DB #{e}"
#conn.close() if conn #conn.close() if conn
end end
stops.each do |stop| stops.each do |stop|
#pp stop #pp stop
# 0 = geoPo # 0 = geoPo
# 1 = lat*100000 # 1 = lat*100000
# 2 = lng*100000 # 2 = lng*100000
# 3 = name # 3 = name
# 4 = suburb(s) # 4 = suburb(s)
#pp time_point[0] #pp time_point[0]
f2.puts " - { name: #{stop[3]},stop_code: #{stop[0]}, lat: #{Float(stop[1])/10000000}, lng: #{Float(stop[2])/10000000}, zone_id: #{stop[4]} }" f2.puts " - { name: #{stop[3]},stop_code: #{stop[0]}, lat: #{Float(stop[1])/10000000}, lng: #{Float(stop[2])/10000000}, zone_id: #{stop[4]} }"
end end
f2.puts "routes:\n"; f2.puts "routes:\n";
end end
   
   
<?php <?php
function getPage($url) function getPage($url)
{ {
$ch = curl_init($url); $ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_setopt( $ch, CURLOPT_HEADER, 0 );
$page = curl_exec($ch); $page = curl_exec($ch);
curl_close($ch); curl_close($ch);
return $page; return $page;
} }
// //
// http://developers.cloudmade.com/wiki/geocoding-http-api/Documentation // http://developers.cloudmade.com/wiki/geocoding-http-api/Documentation
$conn = pg_connect("dbname=bus user=postgres password=snmc"); $conn = pg_connect("dbname=bus user=postgres password=snmc");
if (!$conn) { if (!$conn) {
echo "An error occured.\n"; echo "An error occured.\n";
exit; exit;
} }
  echo "reverse geocode stops<br>";
$sql = "Select * from stops where name is null or suburb is null"; $sql = "Select * from stops where name is null or suburb is null";
$result_stops = pg_query($conn, $sql); $result_stops = pg_query($conn, $sql);
if (!$result_stops) { if (!$result_stops) {
echo("Error in SQL query: " . pg_last_error() ."<br>\n"); echo("Error in SQL query: " . pg_last_error() ."<br>\n");
} }
while ($stop = pg_fetch_assoc($result_stops)) { while ($stop = pg_fetch_assoc($result_stops)) {
if ($stop['name'] == "") { if ($stop['name'] == "") {
echo "Processing ".$stop['geohash'] . " streetname ... "; echo "Processing ".$stop['geohash'] . " streetname ... ";
$url = "http://geocoding.cloudmade.com/daa03470bb8740298d4b10e3f03d63e6/geocoding/v2/find.js?around=".($stop['lat']/10000000).",".($stop['lng']/10000000)."&distance=closest&object_type=road"; $url = "http://geocoding.cloudmade.com/daa03470bb8740298d4b10e3f03d63e6/geocoding/v2/find.js?around=".($stop['lat']/10000000).",".($stop['lng']/10000000)."&distance=closest&object_type=road";
$contents = json_decode(getPage($url)); $contents = json_decode(getPage($url));
//print_r($contents); //print_r($contents);
$name = $contents->features[0]->properties->name; $name = $contents->features[0]->properties->name;
echo "Saving $name ! <br>" ; echo "Saving $name ! <br>" ;
$result_save = pg_query($conn, "UPDATE stops set name = '".pg_escape_string($name)."' where geohash = '{$stop['geohash']}' "); $result_save = pg_query($conn, "UPDATE stops set name = '".pg_escape_string($name)."' where geohash = '{$stop['geohash']}' ");
if (!$result_save) { if (!$result_save) {
echo("Error in SQL query: " . pg_last_error() ."<br>\n"); echo("Error in SQL query: " . pg_last_error() ."<br>\n");
} }
} }
if ($stop['suburb'] == "") { if ($stop['suburb'] == "") {
echo "Processing ".$stop['geohash'] . " suburb ... "; echo "Processing ".$stop['geohash'] . " suburb ... ";
$sql = "select * from suburbs where the_geom @> 'POINT(".($stop['lng']/10000000)." ".($stop['lat']/10000000).")'::geometry"; $sql = "select * from suburbs where the_geom @> 'POINT(".($stop['lng']/10000000)." ".($stop['lat']/10000000).")'::geometry";
$result_suburbs = pg_query($conn, $sql); $result_suburbs = pg_query($conn, $sql);
if (!$result_suburbs) { if (!$result_suburbs) {
echo("Error in SQL query: " . pg_last_error() ."<br>\n"); echo("Error in SQL query: " . pg_last_error() ."<br>\n");
} }
$suburbs = ""; $suburbs = "";
while ($suburb = pg_fetch_assoc($result_suburbs)) { while ($suburb = pg_fetch_assoc($result_suburbs)) {
$suburbs .= $suburb['name_2006'].";"; $suburbs .= $suburb['name_2006'].";";
} }
echo "Saving $suburbs ! <br>" ; echo "Saving $suburbs ! <br>" ;
$result_save = pg_query($conn, "UPDATE stops set suburb = '".pg_escape_string($suburbs)."' where geohash = '{$stop['geohash']}' "); $result_save = pg_query($conn, "UPDATE stops set suburb = '".pg_escape_string($suburbs)."' where geohash = '{$stop['geohash']}' ");
if (!$result_save) { if (!$result_save) {
echo("Error in SQL query: " . pg_last_error() ."<br>\n"); echo("Error in SQL query: " . pg_last_error() ."<br>\n");
} }
} }
flush(); flush();
} }
  echo "reverse geocode timing points<br>";
  $sql = "Select * from timing_point where suburb is null";
  $result_timingpoints = pg_query($conn, $sql);
  if (!$result_timingpoints) {
  echo("Error in SQL query: " . pg_last_error() ."<br>\n");
  }
  while ($timingpoint = pg_fetch_assoc($result_timingpoints)) {
  if ($timingpoint['suburb'] == "") {
  echo "Processing ".$timingpoint['name'] . " suburb ... ";
  $sql = "select * from suburbs where the_geom @> 'POINT(".($timingpoint['lng']/10000000)." ".($timingpoint['lat']/10000000).")'::geometry";
  $result_suburbs = pg_query($conn, $sql);
  if (!$result_suburbs) {
  echo("Error in SQL query: " . pg_last_error() ."<br>\n");
  }
  $suburbs = "";
  while ($suburb = pg_fetch_assoc($result_suburbs)) {
  $suburbs .= $suburb['name_2006'].";";
  }
  echo "Saving $suburbs ! <br>" ;
  $result_save = pg_query($conn, "UPDATE timing_point set suburb = '".pg_escape_string($suburbs)."' where name = '".pg_escape_string($timingpoint['name'])."'");
  if (!$result_save) {
  echo("Error in SQL query: " . pg_last_error() ."<br>\n");
  }
  }
  flush();
  }
   
?> ?>
   
   
options: options:
start_date: 20101115 start_date: 20101115
end_date: 20111231 end_date: 20111231
remove_date: 20111231 remove_date: 20111231
agency_name: ACT Internal Omnibus Network (ACTION) agency_name: ACT Internal Omnibus Network (ACTION)
agency_url: http://www.action.act.gov.au/ agency_url: http://www.action.act.gov.au/
agency_timezone: Australia/Sydney agency_timezone: Australia/Sydney
  agency_phone: 131710
  agency_lang: en
   
stops: stops:
- { name: ACTEW AGL House,stop_code: ACTEW AGL House, lat: -35.282374, lng: 149.132047} - { name: ACTEW AGL House,stop_code: ACTEW AGL House, lat: -35.282374, lng: 149.132047, zone_id: Unclassified ACT;City; }
- { name: ADFA,stop_code: ADFA, lat: -35.2937972, lng: 149.1643403} - { name: ADFA,stop_code: ADFA, lat: -35.2937972, lng: 149.1643403, zone_id: Unclassified ACT;Campbell; }
- { name: Ainslie,stop_code: Ainslie, lat: -35.2620105, lng: 149.1443302} - { name: Ainslie,stop_code: Ainslie, lat: -35.2620105, lng: 149.1443302, zone_id: Unclassified ACT;Ainslie; }
- { name: Alexander Maconochie Centre,stop_code: Alexander Maconochie Centre, lat: -35.3720651, lng: 149.1696618} - { name: Alexander Maconochie Centre,stop_code: Alexander Maconochie Centre, lat: -35.3720651, lng: 149.1696618, zone_id: Unclassified ACT;Hume; }
- { name: Anthony Rolfe Av / Moonlight Av,stop_code: Anthony Rolfe Av / Moonlight Av, lat: -35.1856021, lng: 149.1543639} - { name: Anthony Rolfe Av / Moonlight Av,stop_code: Anthony Rolfe Av / Moonlight Av, lat: -35.1856021, lng: 149.1543639, zone_id: Unclassified ACT;Bonner;Gungahlin; }
- { name: Aranda,stop_code: Aranda, lat: -35.257534, lng: 149.0762963} - { name: Aranda,stop_code: Aranda, lat: -35.257534, lng: 149.0762963, zone_id: Unclassified ACT;Aranda;Bruce;Acton; }
- { name: Athllon / Sulwood Kambah,stop_code: Athllon / Sulwood Kambah, lat: -35.38442, lng: 149.09328} - { name: Athllon / Sulwood Kambah,stop_code: Athllon / Sulwood Kambah, lat: -35.38442, lng: 149.09328, zone_id: Wanniassa;Unclassified ACT; }
- { name: Australian Institute of Sport,stop_code: Australian Institute of Sport, lat: -35.246351, lng: 149.101478} - { name: Australian Institute of Sport,stop_code: Australian Institute of Sport, lat: -35.246351, lng: 149.101478, zone_id: Unclassified ACT;O'Connor;Bruce; }
- { name: Belconnen Community Bus Station,stop_code: Belconnen Community Bus Station, lat: -35.2398858, lng: 149.0690795} - { name: Belconnen Community Bus Station,stop_code: Belconnen Community Bus Station, lat: -35.2398858, lng: 149.0690795, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Community Bus Station (Platform 1),stop_code: Belconnen Community Bus Station (Platform 1), lat: -35.23982, lng: 149.06978} - { name: Belconnen Community Bus Station (Platform 1),stop_code: Belconnen Community Bus Station (Platform 1), lat: -35.23982, lng: 149.06978, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Community Bus Station (Platform 2),stop_code: Belconnen Community Bus Station (Platform 2), lat: -35.23982, lng: 149.06926} - { name: Belconnen Community Bus Station (Platform 2),stop_code: Belconnen Community Bus Station (Platform 2), lat: -35.23982, lng: 149.06926, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Community Bus Station (Platform 3),stop_code: Belconnen Community Bus Station (Platform 3), lat: -35.23986, lng: 149.06847} - { name: Belconnen Community Bus Station (Platform 3),stop_code: Belconnen Community Bus Station (Platform 3), lat: -35.23986, lng: 149.06847, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Community Bus Station (Platform 4),stop_code: Belconnen Community Bus Station (Platform 4), lat: -35.23994, lng: 149.06887} - { name: Belconnen Community Bus Station (Platform 4),stop_code: Belconnen Community Bus Station (Platform 4), lat: -35.23994, lng: 149.06887, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Community Bus Station (Platform 5),stop_code: Belconnen Community Bus Station (Platform 5), lat: -35.23994, lng: 149.06928} - { name: Belconnen Community Bus Station (Platform 5),stop_code: Belconnen Community Bus Station (Platform 5), lat: -35.23994, lng: 149.06928, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Community Bus Station (Platform 6),stop_code: Belconnen Community Bus Station (Platform 6), lat: -35.23994, lng: 149.0698} - { name: Belconnen Community Bus Station (Platform 6),stop_code: Belconnen Community Bus Station (Platform 6), lat: -35.23994, lng: 149.0698, zone_id: Unclassified ACT;Belconnen; }
- { name: Belconnen Way,stop_code: Belconnen Way, lat: -35.2410162, lng: 149.0409512} - { name: Belconnen Way,stop_code: Belconnen Way, lat: -35.2410162, lng: 149.0409512, zone_id: Scullin;Unclassified ACT;Hawker; }
- { name: Bimberi Centre,stop_code: Bimberi Centre, lat: -35.2219941, lng: 149.1546928} - { name: Bimberi Centre,stop_code: Bimberi Centre, lat: -35.2219941, lng: 149.1546928, zone_id: Unclassified ACT;Bonner; }
- { name: Black Mountain Telst