aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/osrm_route_section_processor.rb36
-rw-r--r--lib/tasks/route_sections.rake17
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/osrm_route_section_processor.rb b/lib/osrm_route_section_processor.rb
new file mode 100644
index 000000000..102c53966
--- /dev/null
+++ b/lib/osrm_route_section_processor.rb
@@ -0,0 +1,36 @@
+require 'open-uri'
+
+class OsrmRouteSectionProcessor
+
+ def call(route_section)
+ 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 router.project-osrm.org for RouteSection StopArea:#{route_section.departure.id} -> StopArea:#{route_section.arrival.id}"
+
+ response = open "http://router.project-osrm.org/viaroute?#{points_string}instructions=false"
+ 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 "router.project-osrm.org 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
new file mode 100644
index 000000000..d48ddbba4
--- /dev/null
+++ b/lib/tasks/route_sections.rake
@@ -0,0 +1,17 @@
+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