aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tom_tom/matrix.rb
blob: 985055dae0e5ca7120b687f91d5696f3f9ae2154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module TomTom
  class Matrix
    def initialize(connection)
      @connection = connection
    end

    def matrix(way_costs)
      points = points_from_way_costs(way_costs)
      points = points_as_params(points)

      response = @connection.post do |req|
        req.url '/routing/1/matrix/json'
        req.headers['Content-Type'] = 'application/json'

        req.params[:routeType] = 'shortest'
        req.params[:travelMode] = 'bus'

        req.body = {
          origins: points,
          destinations: points
        }.to_json
      end

      extract_costs_to_way_costs!(
        way_costs,
        points,
        JSON.parse(response.body)
      )
    end

    def points_from_way_costs(way_costs)
      points = Set.new

      way_costs.each do |way_cost|
        points.add(way_cost.departure)
        points.add(way_cost.arrival)
      end

      points
    end

    def points_as_params(points)
      points.map do |latlng|
        {
          point: {
            latitude: latlng.lat,
            longitude: latlng.lng
          }
        }
      end
    end

    # TODO: We actually need to create new WayCost objects to hold the new dot connections given to us by the matrix API.
    def extract_costs_to_way_costs!(way_costs, points, matrix_json)
      way_costs = []

      # `row` and `column` order is the same as `points`
      matrix_json['matrix'].each_with_index do |row, row_i|
        row.each_with_index do |column, column_i|
          next if column['statusCode'] != 200

          distance = column['response']['routeSummary']['lengthInMeters']

          # Ignore costs between a point and itself (e.g. from A to A)
          next if distance == 0

          departure = points[row_i]
          arrival = points[column_i]

          way_costs << WayCost.new(
            departure: Geokit::LatLng.new(
              departure[:point][:latitude],
              departure[:point][:longitude]
            ),
            arrival: Geokit::LatLng.new(
              arrival[:point][:latitude],
              arrival[:point][:longitude]
            ),
            distance: distance,
            time: column['response']['routeSummary']['travelTimeInSeconds']
            # id: 'TODO: figure out how to add combined stop IDs'
          )
        end
      end

      way_costs
    end
  end
end