aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-11-09 11:25:42 +0100
committerXinhui2017-11-09 11:25:42 +0100
commit7d3a9ca840b479e0394005ba72311fedbd4160ff (patch)
treee0c606a5771165577f827c7ab25fe37a80855510
parent1d35d77ff3801c6e1ac56d10f1898ad6527a1048 (diff)
downloadchouette-core-7d3a9ca840b479e0394005ba72311fedbd4160ff.tar.bz2
Remove model route_sections
-rw-r--r--app/models/chouette/journey_pattern.rb1
-rw-r--r--app/models/chouette/journey_pattern_section.rb20
-rw-r--r--app/models/chouette/route_section.rb81
-rw-r--r--app/models/referential.rb4
-rw-r--r--app/models/route_section_search.rb33
-rw-r--r--app/models/route_sections_selector.rb101
-rw-r--r--lib/osrm_route_section_processor.rb42
-rw-r--r--lib/tasks/route_sections.rake17
-rw-r--r--spec/factories/chouette_route_sections.rb6
-rw-r--r--spec/models/chouette/route_section_spec.rb93
-rw-r--r--spec/models/route_sections_selector_spec.rb43
11 files changed, 0 insertions, 441 deletions
diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb
index fa2a9c8bb..3c61f448b 100644
--- a/app/models/chouette/journey_pattern.rb
+++ b/app/models/chouette/journey_pattern.rb
@@ -10,7 +10,6 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord
has_and_belongs_to_many :stop_points, -> { order("stop_points.position") }, :before_add => :vjas_add, :before_remove => :vjas_remove, :after_add => :shortcuts_update_for_add, :after_remove => :shortcuts_update_for_remove
has_many :stop_areas, through: :stop_points
has_many :journey_pattern_sections
- has_many :route_sections, through: :journey_pattern_sections, dependent: :destroy
validates_presence_of :route
validates_presence_of :name
diff --git a/app/models/chouette/journey_pattern_section.rb b/app/models/chouette/journey_pattern_section.rb
deleted file mode 100644
index 3ccba8ec0..000000000
--- a/app/models/chouette/journey_pattern_section.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-class Chouette::JourneyPatternSection < Chouette::ActiveRecord
- belongs_to :journey_pattern
- belongs_to :route_section
-
- validates :journey_pattern_id, presence: true
- validates :route_section_id, presence: true
- validates :rank, presence: true, numericality: :only_integer
- validates :journey_pattern_id, uniqueness: { scope: [:route_section_id, :rank] }
-
- default_scope { order(:rank) }
-
- def self.update_by_journey_pattern_rank(journey_pattern_id, route_section_id, rank)
- jps = self.find_or_initialize_by(journey_pattern_id: journey_pattern_id, rank: rank)
- if route_section_id.present?
- jps.update_attributes(route_section_id: route_section_id)
- else
- jps.destroy
- end
- end
-end
diff --git a/app/models/chouette/route_section.rb b/app/models/chouette/route_section.rb
deleted file mode 100644
index 3ce4232be..000000000
--- a/app/models/chouette/route_section.rb
+++ /dev/null
@@ -1,81 +0,0 @@
-class Chouette::RouteSection < Chouette::TridentActiveRecord
- belongs_to :departure, class_name: 'Chouette::StopArea'
- belongs_to :arrival, class_name: 'Chouette::StopArea'
- has_many :journey_pattern_sections
- has_many :journey_patterns, through: :journey_pattern_sections, dependent: :destroy
-
- validates :departure, :arrival, :processed_geometry, presence: true
-
- scope :by_endpoint_name, ->(endpoint, name) do
- joins("INNER JOIN stop_areas #{endpoint} ON #{endpoint}.id = route_sections.#{endpoint}_id").where(["#{endpoint}.name ilike ?", "%#{name}%"])
- end
- scope :by_line_id, ->(line_id) do
- joins(:journey_pattern_sections, :journey_patterns).joins('INNER JOIN routes ON journey_patterns.route_id = routes.id').where("routes.line_id = #{line_id}")
- end
-
- def stop_areas
- [departure, arrival].compact
- end
-
- def default_geometry
- points = stop_areas.collect(&:geometry).compact
- GeoRuby::SimpleFeatures::LineString.from_points(points) if points.many?
- end
-
- def name
- stop_areas.map do |stop_area|
- stop_area.try(:name) or '?'
- end.join(' - ') + " (#{geometry_description})"
- end
-
- def via_count
- input_geometry ? [ input_geometry.points.count - 2, 0 ].max : 0
- end
-
- def geometry_description
- if input_geometry || processed_geometry
- [ "#{distance.to_i}m" ].tap do |parts|
- parts << "#{via_count} #{'via'.pluralize(via_count)}" if via_count > 0
- end.join(' - ')
- else
- "-"
- end
- end
-
- DEFAULT_PROCESSOR = Proc.new { |section| section.input_geometry || section.default_geometry.try(:to_rgeo) }
-
- @@processor = DEFAULT_PROCESSOR
- cattr_accessor :processor
-
- def instance_processor
- no_processing || processor.nil? ? DEFAULT_PROCESSOR : processor
- end
-
- def process_geometry
- if input_geometry_changed? || processed_geometry.nil?
- self.processed_geometry = instance_processor.call(self)
- self.distance = processed_geometry.to_georuby.to_wgs84.spherical_distance if processed_geometry
- end
-
- true
- end
- before_validation :process_geometry
-
- def editable_geometry=(geometry)
- self.input_geometry = geometry
- end
-
- def editable_geometry
- input_geometry.try(:to_georuby) or default_geometry
- end
-
- def editable_geometry_before_type_cast
- editable_geometry.to_ewkt
- end
-
- def geometry(mode = nil)
- mode ||= :processed
- mode == :editable ? editable_geometry : processed_geometry.to_georuby
- end
-
-end
diff --git a/app/models/referential.rb b/app/models/referential.rb
index ed13cd077..fad91fa7f 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -123,10 +123,6 @@ class Referential < ActiveRecord::Base
Chouette::VehicleJourneyFrequency.all
end
- def route_sections
- Chouette::RouteSection.all
- end
-
def routing_constraint_zones
Chouette::RoutingConstraintZone.all
end
diff --git a/app/models/route_section_search.rb b/app/models/route_section_search.rb
deleted file mode 100644
index bf53cdf05..000000000
--- a/app/models/route_section_search.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-class RouteSectionSearch
- include ActiveModel::Conversion
- extend ActiveModel::Naming
-
- attr_accessor :departure_name, :arrival_name, :line_id
- attr_accessor :scope
-
- def scope
- scope ||= Chouette::RouteSection
- end
-
- def initialize(attributes = {})
- attributes.each { |k,v| send "#{k}=", v } if attributes
- end
-
- def collection
- collection = scope
-
- [:departure, :arrival].each do |endpoint|
- endpoint_name = send "#{endpoint}_name"
- collection = collection.by_endpoint_name(endpoint, endpoint_name) if endpoint_name.present?
- end
-
- collection = collection.by_line_id(line_id) if line_id.present?
-
- collection
- end
-
- def persisted?
- false
- end
-
-end
diff --git a/app/models/route_sections_selector.rb b/app/models/route_sections_selector.rb
deleted file mode 100644
index 0db09ed25..000000000
--- a/app/models/route_sections_selector.rb
+++ /dev/null
@@ -1,101 +0,0 @@
-class RouteSectionsSelector
- extend ActiveModel::Naming
- include ActiveModel::Conversion
-
- include ActiveModel::Validations
-
- attr_reader :itinerary
-
- def initialize(journey_pattern, attributes = {})
- @itinerary = journey_pattern
-
- self.attributes = attributes
- end
-
- def attributes=(attributes)
- attributes.each { |k,v| send "#{k}=", v }
- end
-
- def update_attributes(attributes)
- self.attributes = attributes
- save
- end
-
- delegate :stop_points, to: :itinerary
-
- def sections
- @sections ||= create_sections
- end
-
- def create_sections
- [].tap do |sections|
- stop_points.each_cons(2).each_with_index do |(departure, arrival), index|
- journey_pattern_section = Chouette::JourneyPatternSection.find_by(journey_pattern: @itinerary, rank: index)
-
- if journey_pattern_section
- route_section = journey_pattern_section ? journey_pattern_section.route_section : nil
- else
- route_section = Chouette::RouteSection.find_by(departure: departure.stop_area, arrival: arrival.stop_area)
- end
-
- sections << Section.new(departure.stop_area, arrival.stop_area, index, route_section)
- end
- end
- end
-
- def sections_attributes=(attributes)
- # Process the attributes hash
- attributes.each do |index, section_attributes|
- sections[index.to_i].attributes = section_attributes
- end
- end
-
- def save
- sections.each do |s|
- Chouette::JourneyPatternSection.update_by_journey_pattern_rank(itinerary.id, s.route_section_id, s.rank)
- end
- end
-
- class Section
- extend ActiveModel::Translation
-
- attr_accessor :departure, :arrival, :rank, :route_section_id
-
- def initialize(departure, arrival, rank, route_section = nil)
- @departure, @arrival, @rank = departure, arrival, rank
-
- self.route_section = route_section
- end
-
- def route_section=(route_section)
- @route_section = route_section
- @route_section_id = route_section.respond_to?(:id) ? route_section.id : nil
- end
-
- def route_section
- @route_section ||= candidates.find_by id: route_section_id
- end
-
- def persisted?
- false
- end
-
- def candidates
- @candidates ||= Chouette::RouteSection.where(departure: departure, arrival: arrival)
- end
-
- def create_candidate
- Chouette::RouteSection.create(departure: departure, arrival: arrival)
- end
-
- def attributes=(attributes)
- attributes.each { |k,v| send "#{k}=", v }
- end
-
- def valid?
- route_section.present?
- end
-
- end
-
-end
diff --git a/lib/osrm_route_section_processor.rb b/lib/osrm_route_section_processor.rb
deleted file mode 100644
index e9f92def0..000000000
--- a/lib/osrm_route_section_processor.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require 'open-uri'
-
-class OsrmRouteSectionProcessor
-
- def call(route_section)
- osrm_endpoint = Rails.application.secrets.osrm_endpoint
-
- points_string = (route_section.input_geometry || route_section.default_geometry).points.map do |point|
- "loc=#{point.y.to_f},#{point.x.to_f}"
- end.join
-
- Rails.logger.info "Invoke #{osrm_endpoint} for RouteSection StopArea:#{route_section.departure.id} -> StopArea:#{route_section.arrival.id}"
-
- response = open "#{osrm_endpoint}/viaroute?#{points_string}instructions=false"
- return nil unless response
-
- geometry = JSON.parse(response.read.to_s)['route_geometry']
- if geometry
- decoded_geometry = Polylines::Decoder.decode_polyline(geometry, 1e6).map do |point|
- GeoRuby::SimpleFeatures::Point.from_x_y(point[1], point[0], 4326)
- end
-
- GeoRuby::SimpleFeatures::LineString.from_points(decoded_geometry).try(:to_rgeo) if decoded_geometry.many?
- end
- rescue OpenURI::HTTPError => e
- Rails.logger.error "#{osrm_endpoint} failed: #{e}"
- nil
- rescue IOError => e
- Rails.logger.error "#{osrm_endpoint} failed: #{e}"
- nil
- end
-
- def self.create_all
- Chouette::JourneyPattern.find_each do |journey_pattern|
- selector = RouteSectionsSelector.new(journey_pattern)
- selector.sections.each do |section|
- section.create_candidate unless section.candidates.present?
- end
- end
- end
-
-end
diff --git a/lib/tasks/route_sections.rake b/lib/tasks/route_sections.rake
deleted file mode 100644
index d48ddbba4..000000000
--- a/lib/tasks/route_sections.rake
+++ /dev/null
@@ -1,17 +0,0 @@
-namespace :route_sections do
-
- def find_referential(id_or_slug)
- if id_or_slug.to_s =~ /\A\d+\Z/
- Referential.find id_or_slug.to_i
- else
- Referential.find_by slug: id_or_slug
- end
- end
-
- desc "Generate all RouteSections for a given Referential"
- task :create_all, [:referential] => [:environment] do |t, args|
- find_referential(args[:referential]).switch
- OsrmRouteSectionProcessor.create_all
- end
-
-end
diff --git a/spec/factories/chouette_route_sections.rb b/spec/factories/chouette_route_sections.rb
deleted file mode 100644
index d5e2cf69d..000000000
--- a/spec/factories/chouette_route_sections.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-FactoryGirl.define do
- factory :route_section, :class => Chouette::RouteSection do
- association :departure, :factory => :stop_area
- association :arrival, :factory => :stop_area
- end
-end
diff --git a/spec/models/chouette/route_section_spec.rb b/spec/models/chouette/route_section_spec.rb
deleted file mode 100644
index f064d38ea..000000000
--- a/spec/models/chouette/route_section_spec.rb
+++ /dev/null
@@ -1,93 +0,0 @@
-# require 'spec_helper'
-#
-# RSpec.describe Chouette::RouteSection, :type => :model do
-#
-# subject { create :route_section }
-#
-# it { should validate_presence_of(:departure) }
-# it { should validate_presence_of(:arrival) }
-#
-# describe "#default_geometry" do
-#
-# it "should return nil when departure isn't defined" do
-# subject.departure = nil
-# expect(subject.default_geometry).to be_nil
-# end
-#
-# it "should return nil when arrival isn't defined" do
-# subject.arrival = nil
-# expect(subject.default_geometry).to be_nil
-# end
-#
-# it "should return nil when departure has no geometry" do
-# subject.departure.stub :geometry
-# expect(subject.default_geometry).to be_nil
-# end
-#
-# it "should return nil when arrival has no geometry" do
-# subject.arrival.stub :geometry
-# expect(subject.default_geometry).to be_nil
-# end
-#
-# it "should use departure geometry as first point" do
-# expect(subject.default_geometry.first).to eq(subject.departure.geometry)
-# end
-#
-# end
-#
-# describe "#process_geometry" do
-#
-# let(:sample_geometry) { line_string("0 0,1 1").to_rgeo }
-#
-# context "without processor" do
-#
-# it "should use the input geometry" do
-# subject.input_geometry = sample_geometry
-# subject.process_geometry
-# expect(subject.processed_geometry).to eq(subject.input_geometry)
-# end
-#
-# it "should use the default geometry when no input is defined" do
-# subject.input_geometry = nil
-# subject.process_geometry
-# expect(subject.processed_geometry).to eq(subject.default_geometry.to_rgeo)
-# end
-#
-# end
-#
-# # context "with a processor" do
-# #
-# # it "should use the processor result" do
-# # subject.processor = Proc.new { |s| sample_geometry }
-# # subject.process_geometry
-# # subject.processor = nil
-# # expect(subject.processed_geometry).to eq(sample_geometry)
-# # end
-# # end
-# end
-#
-# describe "#distance" do
-#
-# context "with simple line" do
-# let(:sample_geometry) { line_string("2.329534 48.842397,2.325725 48.855839").to_rgeo }
-# it "should return the right distance" do
-# subject.input_geometry = sample_geometry
-# subject.process_geometry
-# expect(subject.distance).to eq(sample_geometry.to_georuby.spherical_distance)
-# end
-# end
-#
-# context "with complex line" do
-# let(:sample_geometry) { line_string("2.329561 48.842397, 2.329351 48.843119, 2.329152 48.843801, 2.3289820000000003 48.844426,2.3287960000000005 48.845059,2.3286540000000007 48.845575,2.3283130000000005 48.846748,2.3281220000000005 48.847404999999995,2.3279330000000003 48.848088,2.3278860000000003 48.848245999999996,2.3273240000000004 48.850142999999996,2.3273030000000006 48.850218999999996,2.3271630000000005 48.850745999999994,2.3270140000000006 48.85130999999999,2.3269350000000006 48.85142799999999,2.3268640000000005 48.85153599999999,2.3268290000000005 48.85161099999999,2.3267490000000004 48.85180999999999,2.3267700000000002 48.852053999999995,2.326759 48.852216999999996,2.326687 48.852427999999996,2.3266620000000002 48.852512,2.3264280000000004 48.853286,2.3264050000000003 48.853362,2.3263710000000004 48.853483,2.326125 48.854343,2.3259980000000002 48.854727,2.325737 48.855833999999994").to_rgeo }
-# it "should return the right distance" do
-# subject.input_geometry = sample_geometry
-# subject.process_geometry
-# expect(subject.distance).to eq(sample_geometry.to_georuby.spherical_distance)
-# end
-# end
-#
-# end
-#
-#
-#
-# end
diff --git a/spec/models/route_sections_selector_spec.rb b/spec/models/route_sections_selector_spec.rb
deleted file mode 100644
index 19863c315..000000000
--- a/spec/models/route_sections_selector_spec.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-# require 'spec_helper'
-#
-# describe RouteSectionsSelector, :type => :model do
-#
-# let(:stop_points) { create_list :stop_point, 5 }
-# let(:itinerary) { double stop_points: stop_points, route_sections: [] }
-#
-# subject { RouteSectionsSelector.new itinerary }
-#
-# describe "#sections" do
-#
-# it "should create a Section between each StopPoint" do
-# expect(subject.sections.size).to eq(stop_points.size - 1)
-# end
-#
-# end
-#
-# end
-#
-# describe RouteSectionsSelector::Section, :type => :model do
-#
-# let(:departure) { create :stop_point }
-# let(:arrival) { create :stop_point }
-#
-# subject { RouteSectionsSelector::Section.new departure, arrival }
-#
-# let(:route_sections) do
-# create_list :route_section, 5,
-# departure: departure.stop_area,
-# arrival: arrival.stop_area
-# end
-#
-# describe "#candidates" do
-# it "should return an empty array when no RouteSection exists" do
-# expect(subject.candidates).to be_empty
-# end
-#
-# it "should return the RouteSections with the same departure/arrival StopAreas" do
-# expect(subject.candidates).to match_array(route_sections)
-# end
-# end
-#
-# end