Class: OSRMRouting

Inherits:
Object
  • Object
show all
Defined in:
lib/osrm-routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin = {}, waypoints = [], debug = false) ⇒ OSRMRouting

Returns a new instance of OSRMRouting.



11
12
13
14
15
# File 'lib/osrm-routing.rb', line 11

def initialize(origin = {}, waypoints = [], debug = false)
  self.origin = origin
  self.waypoints = waypoints
  self.debug = debug
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



6
7
8
# File 'lib/osrm-routing.rb', line 6

def debug
  @debug
end

#originObject

Returns the value of attribute origin.



6
7
8
# File 'lib/osrm-routing.rb', line 6

def origin
  @origin
end

#waypointsObject

Returns the value of attribute waypoints.



6
7
8
# File 'lib/osrm-routing.rb', line 6

def waypoints
  @waypoints
end

Instance Method Details

#optimizeObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/osrm-routing.rb', line 17

def optimize
  response = {
    status: 200,
    errors: {},
    result: nil
  }

  if self.origin.class != Hash
    response[:status] = 400
    response[:errors][:origin] = 'Invalid format'
  end

  if self.origin.keys.length == 0
    if response[:status] == 200
      response[:status] = 400
    end

    response[:errors][:origin] = 'Required parameter'
  end

  points = [
    [
      origin[:longitude],
      origin[:latitude]
    ]
  ]

  if self.waypoints.class != Array
    if response[:status] == 200
      response[:status] = 400
    end

    response[:errors][:waypoints] = 'Invalid format'
  end

  if self.waypoints.length == 0
    if response[:status] == 200
      response[:status] = 400
    end

    response[:errors][:waypoints] = 'Required parameter'
  end

  self.waypoints.each do |waypoint|
    if waypoint.class != Hash
      if response[:status] == 200
        response[:status] = 400
      end

      response[:errors][:waypoints] = 'Invalid format'
    end

    if waypoint.keys.length == 0
      if response[:status] == 200
        response[:status] = 400
      end

      response[:errors][:waypoints] = 'Invalid format'
    end

    if response[:status] != 200
      break
    end

    points.push([
      waypoint[:longitude],
      waypoint[:latitude]
    ])
  end

  route = "https://router.project-osrm.org/trip/v1/driving/#{points.map{ |point| point.join(",")}.join(";")}?roundtrip=true"

  if self.debug
    puts "=============================== Debug control logger ==============================="
    puts "Query route: #{route}"
    puts "===================================================================================="
  end

  uri = URI.parse(route)
  osrm = Net::HTTP.get_response(uri)

  response[:status] = osrm.code.to_i

  if response[:status] != 200
    response[:errors][:request] = "HTTP Error"
    response[:result] = JSON.parse(osrm.body)
  else
    final_route = []

    osrm = JSON.parse(osrm.body, :symbolize_names => true)
    
    osrm[:waypoints].each do |waypoint|
      final_route.push(waypoint[:waypoint_index])
    end

    final_route.push(0)

    steps = []

    osrm[:trips][0][:legs].each.with_index do |leg, i|
      if i == (osrm[:trips][0][:legs].length - 1)
        steps.push({
          origin: final_route[i],
          destination: final_route[0],
          distance: leg[:distance],
          duration: leg[:duration]
        })
      else
        steps.push({
          origin: final_route[i],
          destination: final_route[i + 1],
          distance: leg[:distance],
          duration: leg[:duration]
        })
      end
    end


    response[:result] = {
      optimized_route: final_route,
      total_duration: osrm[:trips][0][:duration] / 60.0,
      total_distance: osrm[:trips][0][:distance] / 1000.0,
      steps: steps
    }
  end

  return response
end