Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and reactivate polyline generation #297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/v01/entities/vrp_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
braktar marked this conversation as resolved.
Show resolved Hide resolved
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')
Expand Down
11 changes: 5 additions & 6 deletions lib/output_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -459,21 +458,21 @@ 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]
},
geometry: {
type: 'LineString',
coordinates: route[:geometry][0]
coordinates: route[:geometry].flatten(1)
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions models/types/geometry_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ 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|
unless ALL_TYPES.include?(geometry_type.to_sym)
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
Expand Down
2 changes: 1 addition & 1 deletion optimizer_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test/api/v01/output_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' \
Expand Down
10 changes: 4 additions & 6 deletions test/api/v01/vrp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down