RailGem 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/RailCalc.rb +87 -0
  2. metadata +61 -0
data/lib/RailCalc.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'ierail'
2
+ require 'json'
3
+ module RailCalc
4
+
5
+ def self.setupLoc(lat1, long1)
6
+ @lat = lat1
7
+ @long = long1
8
+ # set min as an instance variable using @
9
+ @min = 9999999999
10
+ # create an instance variable to hold the station name
11
+ @station = nil
12
+ self.getStation
13
+ end
14
+
15
+ def self.getStation
16
+ ir = IERail.new
17
+ trainStations = ir.stations
18
+ hash = {}
19
+ for code in trainStations do
20
+ hash[code.description]=code.location
21
+ end
22
+ #puts hash
23
+ #stations = {
24
+ #'Pearse' => [53.343160, -6.247490],
25
+ #'Tara Street' => [53.347070, -6.253980],
26
+ #'Dublin Connolly' => [53.351361, -6.250067],
27
+ #'Clontarf Road' => [53.362897, -6227767],
28
+ #'Killester' => [53.372762, -6.205333],
29
+ #'Harmonstown' => [53.378554, -6.192266],
30
+ #'Raheny' => [53.381485, -6177020]
31
+ #}
32
+
33
+ hash.each do |station,coords|
34
+ # puts station
35
+ # distance is = to hte result of the haversine with the following coords taken in
36
+ distance = haversine(@lat,@long,coords[1].to_f,coords[0].to_f)
37
+ # if distance is less than the set @min then replace @min with the new distance
38
+ if distance < @min
39
+ @min = distance
40
+ @station = station#, coords[1],coords[0]
41
+ @coords = coords[1]
42
+ @coords2= coords[0]
43
+ end
44
+ #puts "#{@lat} #{@long} #{coords[1]} #{coords[0]}"
45
+ end
46
+ #############################
47
+ times = ir.station_times(@station.to_s,30)
48
+ timesHash = Hash.new
49
+ #test = ir.stations
50
+
51
+ for code in times do
52
+ #timesHash[code.traincode]=code.origin,code.destination, code.train_type ,code.duein ,code.last_location
53
+ timesHash={:traincode => code.traincode, :traintype => code.train_type, :duein => code.duein, :lastlocation => code.last_location, :direction => code.direction}
54
+ #, :origin => code.origin, :destination => code.destination_time
55
+
56
+ end
57
+ @times = timesHash
58
+ puts "load #{@times.to_json}"
59
+ #puts "load #{times}"
60
+ #puts "load #{timesHash}"
61
+ #############################
62
+ return @station, @coords , @coords2
63
+ #return @times
64
+ end
65
+
66
+ def self.haversine(lat1, long1, lat2, long2)
67
+ dtor = Math::PI/180
68
+ r = 6378.14*1000
69
+ #puts "The values are #{lat1}, #{long1}, #{lat2}, #{long2}"
70
+ rlat1 = lat1*dtor
71
+ rlong1 = long1*dtor
72
+ rlat2 = lat2*dtor
73
+ rlong2 = long2*dtor
74
+
75
+ dlon = rlong1 - rlong2
76
+ dlat = rlat1 - rlat2
77
+
78
+ a = power(Math::sin(dlat/2), 2) + Math::cos(rlat1)*Math::cos(rlat2)*power(Math::sin(dlon/2), 2)
79
+ c = 2*Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
80
+ d = r*c
81
+ d
82
+ end
83
+
84
+ def self.power(num, pow)
85
+ num ** pow
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RailGem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ddwyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ierail
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.4
30
+ description: gem that calculates the nearest trainstation to the inputed lat and long
31
+ email: dwyerdan3@yahoo.ie
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/RailCalc.rb
37
+ homepage: http://rubygems.org/gems/Railgem
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.25
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: gem that calculates the nearest trainstation to the inputed lat and long
61
+ test_files: []