diff options
| author | Zog | 2018-04-30 11:02:24 +0200 | 
|---|---|---|
| committer | Zog | 2018-04-30 11:02:24 +0200 | 
| commit | 6779a070b8d5d6d738539f84e4405a86337f7dfb (patch) | |
| tree | 55b826ecb627723b69086997b5191a23409eb1ab | |
| parent | 147bb21a2ca1ea9cd1f52f062c5eb3d1901da8cb (diff) | |
| download | chouette-core-6779a070b8d5d6d738539f84e4405a86337f7dfb.tar.bz2 | |
Fix VehicleJourney#fill_passing_times!
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 52 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 6 | 
2 files changed, 37 insertions, 21 deletions
| diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 8b5354863..818287b04 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -355,32 +355,48 @@ module Chouette        end      end -    def fill_passing_time_at_borders -      encountered_borders = [] +    def fill_passing_times! +      encountered_empty_vjas = []        previous_stop = nil        vehicle_journey_at_stops.each do |vjas|          sp = vjas.stop_point -        if sp.stop_area.area_type == "border" -          encountered_borders << vjas +        if vjas.arrival_time.nil? && vjas.departure_time.nil? +          encountered_empty_vjas << vjas          else -          if encountered_borders.any? -            before_cost = journey_pattern.costs_between previous_stop.stop_point, encountered_borders.first.stop_point -            after_cost = journey_pattern.costs_between encountered_borders.last.stop_point, sp -            if before_cost && before_cost[:distance] && after_cost && after_cost[:distance] -              before_distance = before_cost[:distance].to_f -              after_distance = after_cost[:distance].to_f +          if encountered_empty_vjas.any? +            raise "Cannot extrapolate passing times without an initial time" if previous_stop.nil? +            distance_between_known = 0 +            distance_from_last_known = 0 +            cost = journey_pattern.costs_between previous_stop.stop_point, encountered_empty_vjas.first.stop_point +            raise "MISSING cost between #{previous_stop.stop_point.stop_area.registration_number} AND #{encountered_empty_vjas.first.stop_point.stop_area.registration_number}" unless cost.present? +            distance_between_known += cost[:distance].to_f +            cost = journey_pattern.costs_between encountered_empty_vjas.last.stop_point, sp +            raise "MISSING cost between #{encountered_empty_vjas.last.stop_point.stop_area.registration_number} AND #{sp.stop_area.registration_number}" unless cost.present? +            distance_between_known += cost[:distance].to_f +            distance_between_known += encountered_empty_vjas.each_slice(2).inject(0) do |sum, slice| +              cost = journey_pattern.costs_between slice.first.stop_point, slice.last.stop_point +              raise "MISSING cost between #{slice.first.stop_point.stop_area.registration_number} AND #{slice.last.stop_point.stop_area.registration_number}" unless cost.present? +              sum + cost[:distance].to_f +            end + +            previous = previous_stop +            encountered_empty_vjas.each do |empty_vjas| +              cost = journey_pattern.costs_between previous.stop_point, empty_vjas.stop_point +              raise "MISSING cost between #{previous.stop_point.stop_area.registration_number} AND #{empty_vjas.stop_point.stop_area.registration_number}" unless cost.present? +              distance_from_last_known += cost[:distance] +                arrival_time = vjas.arrival_time + (vjas.arrival_day_offset - previous_stop.departure_day_offset)*24.hours -              time = previous_stop.departure_time + before_distance / (before_distance+after_distance) * (arrival_time - previous_stop.departure_time) +              time = previous_stop.departure_time + distance_from_last_known.to_f / distance_between_known.to_f * (arrival_time - previous_stop.departure_time)                day_offset = time.day - 1                time -= day_offset*24.hours -              encountered_borders.each do |b| -                b.update_attribute :arrival_time, time -                b.update_attribute :arrival_day_offset, previous_stop.arrival_day_offset + day_offset -                b.update_attribute :departure_time, time -                b.update_attribute :departure_day_offset, previous_stop.departure_day_offset + day_offset -              end + +              empty_vjas.update_attribute :arrival_time, time +              empty_vjas.update_attribute :arrival_day_offset, previous_stop.arrival_day_offset + day_offset +              empty_vjas.update_attribute :departure_time, time +              empty_vjas.update_attribute :departure_day_offset, previous_stop.departure_day_offset + day_offset +              previous = empty_vjas              end -            encountered_borders = [] +            encountered_empty_vjas = []            end            previous_stop = vjas          end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 01e350455..0fc75bec9 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -923,7 +923,7 @@ describe Chouette::VehicleJourney, :type => :model do      "2000-01-01 #{new_time.hour}:#{new_time.min}:#{new_time.sec} UTC".to_time    end -  describe "#fill_passing_time_at_borders" do +  describe "#fill_passing_times!" do      before do        start = create :stop_area        border = create :stop_area, kind: :non_commercial, area_type: :border @@ -962,7 +962,7 @@ describe Chouette::VehicleJourney, :type => :model do      end      it "should compute passing time" do -      @journey.reload.fill_passing_time_at_borders +      @journey.reload.fill_passing_times!        expect(@target.reload.arrival_time.to_i).to eq offset_passing_time(@start.reload.departure_time, 1.0/3 * (@middle.reload.arrival_time - @start.departure_time)).to_i        expect(@target_2.reload.arrival_time).to eq @target.arrival_time        expect(@target.departure_time).to eq @target.arrival_time @@ -979,7 +979,7 @@ describe Chouette::VehicleJourney, :type => :model do        end        it "should compute passing time" do -        @journey.reload.fill_passing_time_at_borders +        @journey.reload.fill_passing_times!          expect(@target.reload.arrival_time.to_i).to eq offset_passing_time(@start.reload.departure_time, 1.0/3 * (@middle.reload.arrival_time - @start.departure_time)).to_i          expect(@target_2.reload.arrival_time).to eq @target.arrival_time          expect(@target.departure_time).to eq @target.arrival_time | 
