diff options
| author | Teddy Wing | 2018-03-12 11:36:07 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2018-03-14 16:38:39 +0100 | 
| commit | b55fcf26a4b71ad79737bc2a0d291f6ce6944dec (patch) | |
| tree | 33e3ca94dd86c115960357bbfa7027b18bd347f1 | |
| parent | d76fa905ede93b8943e2fff372f7afa93f22b524 (diff) | |
| download | chouette-core-b55fcf26a4b71ad79737bc2a0d291f6ce6944dec.tar.bz2 | |
TomTom: Add method for `calculateRoute` endpoint
A new method to provide access to the `/calculateRoute` endpoint, which
calculates costs for a single route through a bunch of waypoints.
This doesn't work currently because the API requires that I send
`supportingPoints` in the body of the request. Since I'm not too clear
on what those are, I'm going to cut off development of this method here
and just use `#batch` instead for my purposes.
My idea was to use this endpoint instead of `/batch` to allow us to
calculate a single route instead of having the API do `/calculateRoute`
for each of our point pairs. This seemed like it would be more
efficient, and give us our distance and time costs between each waypoint
all in one go, but since I'm not clear on how to use this API and
whether it will give us the correct data, I'm going to stick with
`/batch`. I'll probably be reverting this code. Just committing it now
in case it becomes useful in the future.
Refs #6095
| -rw-r--r-- | lib/tom_tom.rb | 36 | ||||
| -rw-r--r-- | spec/lib/tom_tom_spec.rb | 19 | 
2 files changed, 55 insertions, 0 deletions
| diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index 4a6a58df0..a2ebeb1a8 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -14,6 +14,19 @@ class TomTom      end    end +  def calculate_route(way_costs) +    params = URI.encode_www_form({ +      travelMode: 'bus', +      routeType: 'shortest' +    }) +    locations = convert_way_costs_for_calculate_route(way_costs) + +    response = @connection.post do |req| +      req.url "/routing/1/calculateRoute/#{locations}/json?#{params}" +      req.headers['Content-Type'] = 'application/json' +    end +  end +    def batch(way_costs)      params = URI.encode_www_form({        travelMode: 'bus', @@ -32,6 +45,16 @@ class TomTom          batchItems: batch_items        }.to_json      end + +    response = JSON.parse(response.body) + +    calculated_routes = response['batchItems'] + +    calculated_routes.each do |route| +      next if route['statusCode'] != 200 + +      distance = route['response']['routes'] +    end    end    def convert_way_costs_for_batch(way_costs) @@ -40,4 +63,17 @@ class TomTom        ":#{way_cost.arrival.lat},#{way_cost.arrival.lng}"      end    end + +  def convert_way_costs_for_calculate_route(way_costs) +    coordinates = [] + +    way_costs.map do |way_cost| +      coordinates << "#{way_cost.departure.lat},#{way_cost.departure.lng}" +      coordinates << "#{way_cost.arrival.lat},#{way_cost.arrival.lng}" +    end + +    coordinates +      .uniq +      .join(':') +  end  end diff --git a/spec/lib/tom_tom_spec.rb b/spec/lib/tom_tom_spec.rb index 57f22d26a..c6d387b9f 100644 --- a/spec/lib/tom_tom_spec.rb +++ b/spec/lib/tom_tom_spec.rb @@ -22,4 +22,23 @@ RSpec.describe TomTom do        ])      end    end + +  describe "#convert_way_costs_for_calculate_route" do +    it "turns WayCost points into a string of colon-separated coordinates" do +      way_costs = [ +        WayCost.new( +          departure: Geokit::LatLng.new(48, 2), +          arrival: Geokit::LatLng.new(46, 3) +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(46, 3), +          arrival: Geokit::LatLng.new(47.2, 3.9) +        ) +      ] + +      expect( +        tomtom.convert_way_costs_for_calculate_route(way_costs) +      ).to eq('48,2:46,3:47.2,3.9') +    end +  end  end | 
