From ad35f9dfd0b178bf88330433f037eeda56df145f Mon Sep 17 00:00:00 2001 From: halilsen Date: Tue, 12 Oct 2021 15:12:33 +0200 Subject: [PATCH 1/4] Fix encoded_polylines typo --- optimizer_wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimizer_wrapper.rb b/optimizer_wrapper.rb index f5a39d5c1..e24bae02f 100644 --- a/optimizer_wrapper.rb +++ b/optimizer_wrapper.rb @@ -699,7 +699,7 @@ def self.route_details(vrp, route, vehicle) unless segments.empty? details = vrp.router.compute_batch(OptimizerWrapper.config[:router][:url], vehicle.router_mode.to_sym, vehicle.router_dimension, - segments, vrp.restitution_geometry.include?(:encoded_polyline), + segments, vrp.restitution_geometry.include?(:encoded_polylines), vehicle.router_options) raise RouterError.new('Route details cannot be received') unless details end From b03acba72a64fe03a4cc0f1f7333e1dc72d202ea Mon Sep 17 00:00:00 2001 From: halilsen Date: Tue, 12 Oct 2021 15:18:51 +0200 Subject: [PATCH 2/4] Include all polyline parts in geojson --- lib/output_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/output_helper.rb b/lib/output_helper.rb index 47e611cbf..c3acf4d61 100644 --- a/lib/output_helper.rb +++ b/lib/output_helper.rb @@ -473,7 +473,7 @@ def self.generate_polylines_geometry(result) }, geometry: { type: 'LineString', - coordinates: route[:geometry][0] + coordinates: route[:geometry].flatten(1) } } } From b7fbc60179d9c40d444f84bd78331a78ff1b4d14 Mon Sep 17 00:00:00 2001 From: halilsen Date: Tue, 12 Oct 2021 15:20:11 +0200 Subject: [PATCH 3/4] Use different colors for vehicles even if no :day --- lib/output_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/output_helper.rb b/lib/output_helper.rb index c3acf4d61..76a5c3cfc 100644 --- a/lib/output_helper.rb +++ b/lib/output_helper.rb @@ -459,15 +459,15 @@ def self.generate_points_geometry(result) def self.generate_polylines_geometry(result) polylines = [] - result[:routes].each{ |route| + result[:routes].each_with_index{ |route, route_index| next unless route[:geometry] - color = compute_color([], nil, route[:day]) + color = route[:day] ? compute_color([], nil, route[:day]) : compute_color([], :vehicle, route_index) polylines << { type: 'Feature', properties: { color: color, - name: "#{route[:original_vehicle_id]}, day #{route[:day]} route", + name: "#{route[:original_vehicle_id]}#{route[:day] ? '' : ", day #{route[:day]} route"}", vehicle: route[:original_vehicle_id], day: route[:day] }, From a95d4f3312044266f63da683e4f8ef2eb3603d8f Mon Sep 17 00:00:00 2001 From: halilsen Date: Tue, 12 Oct 2021 15:20:50 +0200 Subject: [PATCH 4/4] Reactivate poyline generation for geojsons --- api/v01/entities/vrp_input.rb | 6 +++--- lib/output_helper.rb | 3 +-- models/types/geometry_type.rb | 6 ++---- test/api/v01/output_test.rb | 6 ------ test/api/v01/vrp_test.rb | 10 ++++------ 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/api/v01/entities/vrp_input.rb b/api/v01/entities/vrp_input.rb index 8d7430d77..fb6609666 100644 --- a/api/v01/entities/vrp_input.rb +++ b/api/v01/entities/vrp_input.rb @@ -192,11 +192,11 @@ module VrpConfiguration end params :vrp_request_restitution do - # TODO : when we find a good way to return polylines in geojson result, add authorized values here - # (we do not want users to send :polylines, :encoded_polylines for now) optional(:geometry, type: Array[Symbol], default: [], coerce_with: ->(value) { GeometryType.type_cast(value) }, - desc: 'Specifies the geometry structures to be returned. Can be a subset of `[partitions]` or a boolean value to output all or no geometry. Polylines and encoded_polylines are not compatible.') + desc: ENV['OPTIM_GENERATE_GEOJSON_POLYLINES'] ? + 'Specifies the geometry structures to be returned. Can be a subset of `[polylines encoded_polylines partitions]` or a boolean value to output all or no geometry. Polylines and encoded_polylines are not compatible together.' : + 'Specifies the geometry structures to be returned. Can be `partitions` to generate geometry structure for each partition or `true` for generating the geometry structure under each route.') optional(:geometry_polyline, type: Boolean, documentation: { hidden: true }, desc: '[DEPRECATED] Use geometry instead, with :polylines or :encoded_polylines') optional(:intermediate_solutions, type: Boolean, desc: 'Return intermediate solutions if available') optional(:csv, type: Boolean, desc: 'The output is a CSV file if you do not specify api format') diff --git a/lib/output_helper.rb b/lib/output_helper.rb index 76a5c3cfc..fe49c8488 100644 --- a/lib/output_helper.rb +++ b/lib/output_helper.rb @@ -318,8 +318,7 @@ def self.generate_geometry(solution) geojson[:partitions] = generate_partitions_geometry(result) if expected_geometry.include?(:partitions) geojson[:points] = generate_points_geometry(result) - # TODO : re-activate this function call when we find a good way to return polylines in result - # geojson[:polylines] = generate_polylines_geometry(result) if expected_geometry.include?(:polylines) + geojson[:polylines] = generate_polylines_geometry(result) if expected_geometry.include?(:polylines) && ENV['OPTIM_GENERATE_GEOJSON_POLYLINES'] geojson } end diff --git a/models/types/geometry_type.rb b/models/types/geometry_type.rb index baaf52a5a..c9380ba04 100644 --- a/models/types/geometry_type.rb +++ b/models/types/geometry_type.rb @@ -25,8 +25,7 @@ def self.type_cast(value) if value.is_a?(FalseClass) [] elsif value.is_a?(TrueClass) - # ALL_TYPES - %i[polylines encoded_polylines] # ensures old behaviour is respected when geometry is true + ALL_TYPES elsif value.is_a?(Array) to_return = [] value.each{ |geometry_type| @@ -34,8 +33,7 @@ def self.type_cast(value) raise ArgumentError.new("Invalid geometry value: #{geometry_type}") end - # to_return << geometry_type.to_sym - to_return << geometry_type.to_sym unless %i[polylines encoded_polylines].include?(geometry_type.to_sym) + to_return << geometry_type.to_sym } if (to_return & [:polylines, :encoded_polylines]).size == 2 diff --git a/test/api/v01/output_test.rb b/test/api/v01/output_test.rb index 4a9c9d41a..a8613eb04 100644 --- a/test/api/v01/output_test.rb +++ b/test/api/v01/output_test.rb @@ -438,12 +438,6 @@ def test_geojsons_returned refute(result['geojsons'].first['partitions'].key?('work_day')) # points should always be returned refute_empty(result['geojsons'].first['points']) - - # TODO : remove when returning polylines is more performant and we allow this field in geometry : - vrp[:configuration][:restitution] = { geometry: [:partitions, :polylines] } - @job_id = submit_vrp api_key: 'ortools', vrp: vrp - result = wait_status @job_id, 'completed', api_key: 'ortools' - refute(result['geojsons'].first.key?('polylines')) end skip 'Remaining part of this test is skipped because at the moment POST does not return the same result as GET ' \ diff --git a/test/api/v01/vrp_test.rb b/test/api/v01/vrp_test.rb index 75f38a1d4..d4a105bb4 100644 --- a/test/api/v01/vrp_test.rb +++ b/test/api/v01/vrp_test.rb @@ -355,14 +355,12 @@ def test_ask_for_geometry case case_index when 0 assert_empty services_vrps.first[:vrp].restitution_geometry - when 1 - assert_equal %i[polylines], services_vrps.first[:vrp].restitution_geometry + when 1 || 5 || 8 + assert_equal %i[polylines partitions], services_vrps.first[:vrp].restitution_geometry when 2 - assert_equal %i[encoded_polylines], services_vrps.first[:vrp].restitution_geometry - when 5 || 8 - assert_equal %i[partitions], services_vrps.first[:vrp].restitution_geometry + assert_equal %i[encoded_polylines partitions], services_vrps.first[:vrp].restitution_geometry when 3 || 4 - assert_empty services_vrps.first[:vrp].restitution_geometry + assert_equal %i[polylines], services_vrps.first[:vrp].restitution_geometry when 7 assert_equal %i[partitions], services_vrps.first[:vrp].restitution_geometry end