Module: RailCalc

Defined in:
lib/RailCalc.rb

Class Method Summary collapse

Class Method Details

.getStationObject



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
# File 'lib/RailCalc.rb', line 15

def self.getStation
  ir = IERail.new
    trainStations = ir.stations
  hash = {}
for code in trainStations do 
  hash[code.description]=code.location
end 
#puts hash
  #stations = { 
 #'Pearse' => [53.343160, -6.247490],
 #'Tara Street' => [53.347070, -6.253980],
 #'Dublin Connolly' => [53.351361, -6.250067],
 #'Clontarf Road' => [53.362897, -6227767],
 #'Killester' => [53.372762, -6.205333],
 #'Harmonstown' => [53.378554, -6.192266],
 #'Raheny' => [53.381485, -6177020]
#}

 hash.each do |station,coords|
# puts station
  # distance is = to hte result of the haversine with the following coords taken in
   distance = haversine(@lat,@long,coords[1].to_f,coords[0].to_f)
  # if distance is less than the set @min then replace @min with the new distance
   if distance < @min
     @min = distance
     @station = station#, coords[1],coords[0]
@coords = coords[1]
@coords2= coords[0]
   end
#puts "#{@lat} #{@long} #{coords[1]} #{coords[0]}" 
 end
 #############################
times = ir.station_times(@station.to_s,30)
 timesHash = Hash.new
#test = ir.stations

for code in times do 
#timesHash[code.traincode]=code.origin,code.destination, code.train_type ,code.duein ,code.last_location
  timesHash={:traincode => code.traincode, :traintype => code.train_type, :duein => code.duein, :lastlocation => code.last_location, :direction => code.direction}
  #, :origin => code.origin, :destination => code.destination_time

end
@times = timesHash
puts "load #{@times.to_json}"
#puts "load #{times}"
#puts "load #{timesHash}"
 #############################
return @station, @coords , @coords2
#return @times
end

.haversine(lat1, long1, lat2, long2) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/RailCalc.rb', line 66

def self.haversine(lat1, long1, lat2, long2)
  dtor = Math::PI/180
  r = 6378.14*1000
  #puts  "The values are #{lat1}, #{long1}, #{lat2}, #{long2}"
  rlat1 = lat1*dtor
rlong1 = long1*dtor
  rlat2 = lat2*dtor
  rlong2 = long2*dtor

  dlon = rlong1 - rlong2
  dlat = rlat1 - rlat2

  a = power(Math::sin(dlat/2), 2) + Math::cos(rlat1)*Math::cos(rlat2)*power(Math::sin(dlon/2), 2)
  c = 2*Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
  d = r*c
  d
end

.power(num, pow) ⇒ Object



84
85
86
# File 'lib/RailCalc.rb', line 84

def self.power(num, pow)
  num ** pow
end

.setupLoc(lat1, long1) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/RailCalc.rb', line 5

def self.setupLoc(lat1, long1)
  @lat = lat1
  @long = long1
    # set min as an instance variable using @
    @min = 9999999999
    # create an instance variable to hold the station name
    @station = nil 
  self.getStation
end