osrm-routing 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/osrm-routing.rb +145 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 350560acec3f47faafc19a93c011c614dafa134bbdfc020742edc6a0c057a3a4
|
4
|
+
data.tar.gz: 90ca7a491d4157b54789f4794a25599b3ccf1c177e5003d54fd6dca0fcf646b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 110723d6f12c37b3e27b855a31763382a26106ee20d9ceba64c913452ac3cfdb1b6caef687af0eb360b0f1c9ed20bcf2508dc08d0f24aea9714e946d261f26d5
|
7
|
+
data.tar.gz: dcfad525db4950ed29775f2200ab501b8544b05afad12a9883774c71faeda485abe453270ba6d2d7836726b015cc4c7f03fc320a641dd380d8479d35dcc8baa5
|
data/lib/osrm-routing.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
class OSRMRouting
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
attr_accessor :origin, :waypoints, :debug
|
7
|
+
@origin = {}
|
8
|
+
@waypoints = []
|
9
|
+
@debug = false
|
10
|
+
|
11
|
+
def initialize(origin = {}, waypoints = [], debug = false)
|
12
|
+
self.origin = origin
|
13
|
+
self.waypoints = waypoints
|
14
|
+
self.debug = debug
|
15
|
+
end
|
16
|
+
|
17
|
+
def optimize
|
18
|
+
response = {
|
19
|
+
status: 200,
|
20
|
+
errors: {},
|
21
|
+
result: nil
|
22
|
+
}
|
23
|
+
|
24
|
+
if self.origin.class != Hash
|
25
|
+
response[:status] = 400
|
26
|
+
response[:errors][:origin] = 'Invalid format'
|
27
|
+
end
|
28
|
+
|
29
|
+
if self.origin.keys.length == 0
|
30
|
+
if response[:status] == 200
|
31
|
+
response[:status] = 400
|
32
|
+
end
|
33
|
+
|
34
|
+
response[:errors][:origin] = 'Required parameter'
|
35
|
+
end
|
36
|
+
|
37
|
+
points = [
|
38
|
+
[
|
39
|
+
origin[:longitude],
|
40
|
+
origin[:latitude]
|
41
|
+
]
|
42
|
+
]
|
43
|
+
|
44
|
+
if self.waypoints.class != Array
|
45
|
+
if response[:status] == 200
|
46
|
+
response[:status] = 400
|
47
|
+
end
|
48
|
+
|
49
|
+
response[:errors][:waypoints] = 'Invalid format'
|
50
|
+
end
|
51
|
+
|
52
|
+
if self.waypoints.length == 0
|
53
|
+
if response[:status] == 200
|
54
|
+
response[:status] = 400
|
55
|
+
end
|
56
|
+
|
57
|
+
response[:errors][:waypoints] = 'Required parameter'
|
58
|
+
end
|
59
|
+
|
60
|
+
self.waypoints.each do |waypoint|
|
61
|
+
if waypoint.class != Hash
|
62
|
+
if response[:status] == 200
|
63
|
+
response[:status] = 400
|
64
|
+
end
|
65
|
+
|
66
|
+
response[:errors][:waypoints] = 'Invalid format'
|
67
|
+
end
|
68
|
+
|
69
|
+
if waypoint.keys.length == 0
|
70
|
+
if response[:status] == 200
|
71
|
+
response[:status] = 400
|
72
|
+
end
|
73
|
+
|
74
|
+
response[:errors][:waypoints] = 'Invalid format'
|
75
|
+
end
|
76
|
+
|
77
|
+
if response[:status] != 200
|
78
|
+
break
|
79
|
+
end
|
80
|
+
|
81
|
+
points.push([
|
82
|
+
waypoint[:longitude],
|
83
|
+
waypoint[:latitude]
|
84
|
+
])
|
85
|
+
end
|
86
|
+
|
87
|
+
route = "https://router.project-osrm.org/trip/v1/driving/#{points.map{ |point| point.join(",")}.join(";")}?roundtrip=true"
|
88
|
+
|
89
|
+
if self.debug
|
90
|
+
puts "=============================== Debug control logger ==============================="
|
91
|
+
puts "Query route: #{route}"
|
92
|
+
puts "===================================================================================="
|
93
|
+
end
|
94
|
+
|
95
|
+
uri = URI.parse(route)
|
96
|
+
osrm = Net::HTTP.get_response(uri)
|
97
|
+
|
98
|
+
response[:status] = osrm.code.to_i
|
99
|
+
|
100
|
+
if response[:status] != 200
|
101
|
+
response[:errors][:request] = "HTTP Error"
|
102
|
+
response[:result] = JSON.parse(osrm.body)
|
103
|
+
else
|
104
|
+
final_route = []
|
105
|
+
|
106
|
+
osrm = JSON.parse(osrm.body, :symbolize_names => true)
|
107
|
+
|
108
|
+
osrm[:waypoints].each do |waypoint|
|
109
|
+
final_route.push(waypoint[:waypoint_index])
|
110
|
+
end
|
111
|
+
|
112
|
+
final_route.push(0)
|
113
|
+
|
114
|
+
steps = []
|
115
|
+
|
116
|
+
osrm[:trips][0][:legs].each.with_index do |leg, i|
|
117
|
+
if i == (osrm[:trips][0][:legs].length - 1)
|
118
|
+
steps.push({
|
119
|
+
origin: final_route[i],
|
120
|
+
destination: final_route[0],
|
121
|
+
distance: leg[:distance],
|
122
|
+
duration: leg[:duration]
|
123
|
+
})
|
124
|
+
else
|
125
|
+
steps.push({
|
126
|
+
origin: final_route[i],
|
127
|
+
destination: final_route[i + 1],
|
128
|
+
distance: leg[:distance],
|
129
|
+
duration: leg[:duration]
|
130
|
+
})
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
response[:result] = {
|
136
|
+
optimized_route: final_route,
|
137
|
+
total_duration: osrm[:trips][0][:duration] / 60.0,
|
138
|
+
total_distance: osrm[:trips][0][:distance] / 1000.0,
|
139
|
+
steps: steps
|
140
|
+
}
|
141
|
+
end
|
142
|
+
|
143
|
+
return response
|
144
|
+
end
|
145
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: osrm-routing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Golden M Software
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: OSRM Project routing wrapper for Ruby
|
14
|
+
email: software@goldenmcorp.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/osrm-routing.rb
|
20
|
+
homepage: https://github.com/goldenm-software/osrm-routing-ruby
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.0.2
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: OSRM Project routing wrapper for Ruby
|
43
|
+
test_files: []
|