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 | |
| 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
| -rw-r--r-- | lib/stop_areas_to_way_costs_converter.rb | 21 | ||||
| -rw-r--r-- | lib/way_cost.rb | 7 | ||||
| -rw-r--r-- | spec/lib/stop_areas_to_way_costs_converter_spec.rb | 70 |
3 files changed, 95 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 diff --git a/spec/lib/stop_areas_to_way_costs_converter_spec.rb b/spec/lib/stop_areas_to_way_costs_converter_spec.rb new file mode 100644 index 000000000..7e09ede2e --- /dev/null +++ b/spec/lib/stop_areas_to_way_costs_converter_spec.rb @@ -0,0 +1,70 @@ +RSpec.describe StopAreasToWayCostsConverter do + describe "#convert" do + it "converts a StopArea collection to WayCosts" do + route = create(:route_common) + coordinates = [ + [1.5, 1.8], + [2.2, 2.1], + [3.0, 3.6], + [4.9, 4.3] + ] + + stop_areas = coordinates.map do |latlng| + stop_area = create( + :stop_area, + area_type: 'zdep', + latitude: latlng[0], + longitude: latlng[1] + ) + + create( + :stop_point, + route: route, + stop_area: stop_area + ) + + stop_area + end + + way_costs = [ + WayCost.new( + departure: Geokit::LatLng.new( + stop_areas[0].latitude, + stop_areas[0].longitude + ), + arrival: Geokit::LatLng.new( + stop_areas[1].latitude, + stop_areas[1].longitude + ), + id: "#{stop_areas[0].id}-#{stop_areas[1].id}" + ), + WayCost.new( + departure: Geokit::LatLng.new( + stop_areas[1].latitude, + stop_areas[1].longitude + ), + arrival: Geokit::LatLng.new( + stop_areas[2].latitude, + stop_areas[2].longitude + ), + id: "#{stop_areas[1].id}-#{stop_areas[2].id}" + ), + WayCost.new( + departure: Geokit::LatLng.new( + stop_areas[2].latitude, + stop_areas[2].longitude + ), + arrival: Geokit::LatLng.new( + stop_areas[3].latitude, + stop_areas[3].longitude + ), + id: "#{stop_areas[2].id}-#{stop_areas[3].id}" + ) + ] + + expect( + StopAreasToWayCostsConverter.new(stop_areas).convert + ).to eq(way_costs) + end + end +end |
