diff options
| author | Teddy Wing | 2018-03-13 11:43:14 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2018-03-14 16:38:40 +0100 | 
| commit | 87947a7b6da52305029954341c6dc40b8cde6f86 (patch) | |
| tree | 006391e653e92cf82b93cff37d9136e984f7fc2a /lib | |
| parent | 33c2dad6ba2483a4c71d7248979bf9d82a65f234 (diff) | |
| download | chouette-core-87947a7b6da52305029954341c6dc40b8cde6f86.tar.bz2 | |
Add `StopAreasToWayCostsConverter`
A new class that converts a list of `StopArea`s to `WayCost`s. This will
be called from `Chouette::Route` to get `WayCost`s from its stops, which
will then be JSON serialised and stored in `Route#costs`.
Update `WayCost`:
* Remove comment about calculating the ID automatically. It actually
  needs to be the same as the `JourneyPattern#cost` ID (`key`), which is
  a string with the IDs of the departure and arrival stops.
* Make `#==` check that `id`s are the same, which necessitates making
  `id` a reader.
Refs #6095
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/stop_areas_to_way_costs_converter.rb | 21 | ||||
| -rw-r--r-- | lib/way_cost.rb | 7 | 
2 files changed, 25 insertions, 3 deletions
| diff --git a/lib/stop_areas_to_way_costs_converter.rb b/lib/stop_areas_to_way_costs_converter.rb new file mode 100644 index 000000000..c6a00d31b --- /dev/null +++ b/lib/stop_areas_to_way_costs_converter.rb @@ -0,0 +1,21 @@ +class StopAreasToWayCostsConverter +  def initialize(stop_areas) +    @stop_areas = stop_areas +  end + +  def convert +    @stop_areas.each_cons(2).map do |stop_area_pair| +      WayCost.new( +        departure: Geokit::LatLng.new( +          stop_area_pair[0].latitude, +          stop_area_pair[0].longitude +        ), +        arrival: Geokit::LatLng.new( +          stop_area_pair[1].latitude, +          stop_area_pair[1].longitude +        ), +        id: "#{stop_area_pair[0].id}-#{stop_area_pair[1].id}" +      ) +    end +  end +end diff --git a/lib/way_cost.rb b/lib/way_cost.rb index 5de64177f..9f860308c 100644 --- a/lib/way_cost.rb +++ b/lib/way_cost.rb @@ -1,5 +1,5 @@  class WayCost -  attr_reader :departure, :arrival +  attr_reader :departure, :arrival, :id    attr_accessor :distance, :time    def initialize( @@ -7,7 +7,7 @@ class WayCost      arrival:,      distance: nil,      time: nil, -    id: nil  # TODO: calculate ID automatically +    id: nil    )      @departure = departure      @arrival = arrival @@ -21,6 +21,7 @@ class WayCost        @departure == other.departure &&        @arrival == other.arrival &&        @distance == other.distance && -      @time == other.time +      @time == other.time && +      @id == other.id    end  end | 
