Between points integrated and estimated times
[bus.git] / maxious-canberra-transit-feed / 05-addbetweenpointstotimetables.rb
blob:a/maxious-canberra-transit-feed/05-addbetweenpointstotimetables.rb -> blob:b/maxious-canberra-transit-feed/05-addbetweenpointstotimetables.rb
require 'rubygems' require 'rubygems'
require 'postgres' require 'postgres'
require 'pp' require 'pp'
require 'yaml' require 'yaml'
class String class String
def escape_single_quotes def escape_single_quotes
self.gsub(/'/, "''") self.gsub(/'/, "''")
end end
end end
class Array class Array
def to_yaml_style def to_yaml_style
:inline :inline
end end
end end
Dir.chdir("output") Dir.chdir("output")
   
connbus = PGconn.connect("localhost", 5432, '', '', "bus", "postgres", "snmc") connbus = PGconn.connect("localhost", 5432, '', '', "bus", "postgres", "snmc")
   
Dir.glob("*.yml") { |file| Dir.glob("*.yml") { |file|
timetable = YAML::load_file(file) timetable = YAML::load_file(file)
if timetable if timetable
route_name = timetable["short_name"] route_name = timetable["short_name"]
timetable["between_stops"] = {} timetable["between_stops"] = {}
for i in 0..timetable["time_points"].length-2 for i in 0..timetable["time_points"].length-2
begin begin
searchFrom = timetable["time_points"][i].escape_single_quotes.split("-")[0].strip searchFrom = timetable["time_points"][i].escape_single_quotes.split("(")[0].strip
searchTo = timetable["time_points"][i+1].escape_single_quotes.split("-")[0].strip searchTo = timetable["time_points"][i+1].escape_single_quotes.split("(")[0].strip
  # print "SELECT * from between_stops
  # WHERE fromlocation = '#{searchFrom}'
  # AND tolocation = '#{searchTo}' AND routes LIKE '%#{route_name};%'"
between_points = connbus.exec("SELECT * from between_stops between_points = connbus.exec("SELECT * from between_stops
WHERE fromlocation = '#{searchFrom}' WHERE fromlocation = '#{searchFrom}'
AND tolocation = '#{searchTo}' AND routes LIKE '%#{route_name};%'") AND tolocation = '#{searchTo}' AND routes LIKE '%#{route_name};%'")
rescue PGError => e rescue PGError => e
puts "Error selecting matching between points from DB #{e}" puts "Error selecting matching between points from DB #{e}"
#conn.close() if conn #conn.close() if conn
end end
between_points.each do |between_point_row| between_points.each do |between_point_row|
timetable["between_stops"][timetable["time_points"][i] + '-' +timetable["time_points"][i+1]] \ points = between_point_row['points'].split(";")
= between_point_row['points'].split(";"); points.delete("")
  timetable["between_stops"][timetable["time_points"][i] + '-' +timetable["time_points"][i+1]] = points;
end end
end end
#pp timetable["between_stops"] #pp timetable["between_stops"]
File.open(file, "w") do |f| File.open(file, "w") do |f|
f.write timetable.to_yaml f.write timetable.to_yaml
end end
else else
print "error, #{file} empty\n" print "error, #{file} empty\n"
end end
} }