mormon 2.0.0 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +3 -1
- data/Gemfile.lock +1 -10
- data/README.md +31 -17
- data/lib/mormon/osm_distance_optimizer.rb +128 -0
- data/lib/mormon/osm_loader.rb +32 -44
- data/lib/mormon/osm_router.rb +22 -22
- data/lib/mormon/version.rb +1 -1
- data/lib/mormon/weight.rb +1 -0
- data/lib/mormon.rb +2 -0
- data/mormon.gemspec +4 -5
- data/spec/distance_optimizer_spec.rb +124 -0
- data/spec/map.osm +18216 -0
- metadata +24 -43
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bc716b3e1ce51d7d6bc2da6aebd4015438b5998cfce3d08abcbfccbdf02e467f
|
4
|
+
data.tar.gz: 1fc718d5ec92bdc2ecb824f1c96c27e2fb2e611810ee1c12d5cb82fb69292d54
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8da4d1df4f858b633fcc476c042dceba19ccfde4c70ed689eb49a18f1f02b15027755084c21b1e8bb1f30ecc9875c9e1b299a5067902cfe113fbd2cd4669d03
|
7
|
+
data.tar.gz: 357cdddb94aff94615e355a5c6a5ce04d268e3695288d83c2f97d027b404cadf08489d8041b9548072240d09df91d57491ec4c5e8afb882fdaf07ce0eadd07f2
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,20 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mormon (
|
4
|
+
mormon (2.0.1)
|
5
5
|
nokogiri
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
columnize (0.3.6)
|
11
|
-
debugger (1.2.0)
|
12
|
-
columnize (>= 0.3.1)
|
13
|
-
debugger-linecache (~> 1.1.1)
|
14
|
-
debugger-ruby_core_source (~> 1.1.3)
|
15
|
-
debugger-linecache (1.1.2)
|
16
|
-
debugger-ruby_core_source (>= 1.1.1)
|
17
|
-
debugger-ruby_core_source (1.1.3)
|
18
10
|
diff-lcs (1.1.3)
|
19
11
|
nokogiri (1.5.5)
|
20
12
|
rspec (2.11.0)
|
@@ -30,6 +22,5 @@ PLATFORMS
|
|
30
22
|
ruby
|
31
23
|
|
32
24
|
DEPENDENCIES
|
33
|
-
debugger
|
34
25
|
mormon!
|
35
26
|
rspec
|
data/README.md
CHANGED
@@ -3,26 +3,44 @@ Mormon
|
|
3
3
|
|
4
4
|
OSM Routing in Ruby, based on pyroutelib2
|
5
5
|
|
6
|
-
Usage
|
7
|
-
|
6
|
+
Usage of OSM route
|
7
|
+
==================
|
8
8
|
|
9
9
|
osm_loader = Mormon::OSM::Loader.new "path/to/file.osm"
|
10
10
|
osm_router = Mormon::OSM::Router.new osm_loader
|
11
11
|
osm_router.find_route node_start, node_end, transport
|
12
12
|
|
13
13
|
- Notes:
|
14
|
-
-
|
15
|
-
- node_start and node_end must be node ids
|
14
|
+
- transports: must be one in [:cycle, :car, :train, :foot, :horse]
|
15
|
+
- node_start and node_end must be node ids associated with ways in osm (``<way ...> <nd ref="node_id"/</way>``)
|
16
|
+
|
17
|
+
Usage of OSM distance optimizer
|
18
|
+
================================
|
19
|
+
|
20
|
+
osm_loader = Mormon::OSM::Loader.new "path/to/file.osm"
|
21
|
+
osm_router = Mormon::OSM::Router.new osm_loader
|
22
|
+
route = osm_distance_optimizer.route_planer(*Array of Mormon::OSM::StopNode where at least node_id has value*, osm_loader)
|
23
|
+
|
24
|
+
- The route is a sorted array where each node is succeeded by the closest (not already visited). The stop nodes all have updated the distance value to represent the distance to the predecessor.
|
25
|
+
- The total distance of the route can be calculated by summing up all the distances
|
26
|
+
|
27
|
+
- Notes
|
28
|
+
- All distances are measures in meters
|
29
|
+
- The calculation is not accurate:
|
30
|
+
- The calculation of latitude and longitude degrees are done by the algorithms from [http://en.wikipedia.org/wiki/Latitude](http://en.wikipedia.org/wiki/Latitude)
|
31
|
+
- The length of one degree is calculated as the average between latitude and longitude for simplicity as heading is not included. Which can be misleading in areas where the differences between these are high (close to the poles)
|
32
|
+
- In Denmark (11.000000,56.000000) this results in a margin of < 10 meters and together with gps-fixes this would be a estimated total of +/- 10%
|
33
|
+
- A route is considered an array of stops. The distance between stops (paths) is summed up by the distance between the OSM nodes.
|
16
34
|
|
17
35
|
Caching
|
18
36
|
=======
|
19
37
|
|
20
38
|
You probably wants to cache the parsed osm file:
|
21
|
-
|
22
|
-
osm_loader = Mormon::OSM::Loader.new "path/to/filename.osm", :cache => true
|
39
|
+
|
40
|
+
osm_loader = Mormon::OSM::Loader.new "path/to/filename.osm", :cache => true
|
23
41
|
|
24
42
|
The previous code generate a filename.pstore file and it's stored in Dir.tmpdir, "mormon", "cache" depending of your so, if you need to change the cache dir try ie:
|
25
|
-
|
43
|
+
|
26
44
|
cache_dir = File.join File.dirname(__FILE__), "cache"
|
27
45
|
Mormon::OSM::Loader.cache_dir = cache_dir
|
28
46
|
osm_loader = Mormon::OSM::Loader.new "path/to/file.osm", :cache => true
|
@@ -37,15 +55,17 @@ The default algorithm is A* with weights, but a Random algorithm is available, i
|
|
37
55
|
osm_router = Mormon::OSM::Router.new osm_loader, :algorithm => :random
|
38
56
|
osm_router.find_route node_start, node_end, transport
|
39
57
|
|
40
|
-
|
58
|
+
**KNOWN ISSUE**: the actual random algorithm has problems when the different between start and end is enough far, i'm working to fix it.
|
59
|
+
|
60
|
+
Breadth in random algorithm
|
41
61
|
-----
|
42
62
|
|
43
63
|
The programming technique to find a random route is Backtracking, so in order to don't loop forever we need to narrow the search in neighbors, for that reason i use a breadth value combined with distance between start and end nodes:
|
44
|
-
|
64
|
+
|
45
65
|
max_amplitude = @breadth * distance(start, end)
|
46
66
|
|
47
67
|
The default breadth value is 2 but you could change it doing one of:
|
48
|
-
|
68
|
+
|
49
69
|
osm_router = Mormon::OSM::Router.new osm_loader, :algorithm => :random, :breadth => 1.5
|
50
70
|
or
|
51
71
|
osm_router.algorithm.breath = 2.5
|
@@ -53,10 +73,4 @@ The default breadth value is 2 but you could change it doing one of:
|
|
53
73
|
License
|
54
74
|
=======
|
55
75
|
|
56
|
-
|
57
|
-
|
58
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
59
|
-
|
60
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
61
|
-
|
62
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
76
|
+
LICENSE.txt
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module Mormon
|
2
|
+
module OSM
|
3
|
+
class StopNode
|
4
|
+
attr_accessor :node_id, :distance, :road, :house_number, :zip_code
|
5
|
+
end
|
6
|
+
|
7
|
+
class DistanceOptimizer
|
8
|
+
# encoding: UTF-8
|
9
|
+
|
10
|
+
# Takes an array of stops and computes an optimal route, so the stops are succeeded by the closest of the remaining.
|
11
|
+
def self.route_planer(stops, osm_loader)
|
12
|
+
@osm_loader = osm_loader
|
13
|
+
|
14
|
+
route = [stops]
|
15
|
+
route[0] = stops[0]
|
16
|
+
i = 0
|
17
|
+
|
18
|
+
# Iterates through the stops, adds the closest to the route and sets the stops with the start and the remaining stops
|
19
|
+
length = stops.length
|
20
|
+
while (i < length - 1)
|
21
|
+
stop, remaining_stops = find_next_stop(stops)
|
22
|
+
route.push stop
|
23
|
+
stops = remaining_stops
|
24
|
+
i = i + 1
|
25
|
+
end
|
26
|
+
|
27
|
+
return route
|
28
|
+
end
|
29
|
+
|
30
|
+
# Take an array of stops and return the closest stop with its distance in meters from the first way node in the array
|
31
|
+
def self.find_next_stop(stops)
|
32
|
+
sorted_stops = []
|
33
|
+
i = 0
|
34
|
+
|
35
|
+
# Sets the distance attribute of the stop_objects in the array
|
36
|
+
while (i < stops.length)
|
37
|
+
if (i == 0)
|
38
|
+
stops[i].distance = 0.0
|
39
|
+
else
|
40
|
+
stops[i].distance = route(stops[0].node_id,stops[i].node_id)
|
41
|
+
end
|
42
|
+
sorted_stops[i] = stops[i]
|
43
|
+
i = i + 1
|
44
|
+
end
|
45
|
+
|
46
|
+
sorted_stops.sort! { |a,b| a.distance <=> b.distance }
|
47
|
+
|
48
|
+
next_stop = sorted_stops[1]
|
49
|
+
sorted_stops.delete_at(1)
|
50
|
+
|
51
|
+
return next_stop, sorted_stops
|
52
|
+
end
|
53
|
+
|
54
|
+
# Calculates the distance of one part between two stops by using roads with a car
|
55
|
+
def self.route(from, to)
|
56
|
+
osm_router = Mormon::OSM::Router.new(@osm_loader)
|
57
|
+
route = osm_router.find_route(from, to, :car)
|
58
|
+
osm_loader ="" # Clears memory for osm data
|
59
|
+
|
60
|
+
# Puts error from finding way nodes
|
61
|
+
if (route[0] != "success")
|
62
|
+
puts route[0]
|
63
|
+
end
|
64
|
+
|
65
|
+
return total_distance_calc(route[1]).round(2)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Calculates the distance for this part by adding the distances between the way nodes in it
|
69
|
+
def self.total_distance_calc(distance_Array)
|
70
|
+
total_distance = 0.0
|
71
|
+
i = 0
|
72
|
+
latitude_1 = longitude_1 = latitude_2 = longitude_2 = 0.0
|
73
|
+
|
74
|
+
distance_Array.each {| point |
|
75
|
+
if (i == 0)
|
76
|
+
longitude_1 = point[0]
|
77
|
+
latitude_1 = point[1]
|
78
|
+
i = i + 1
|
79
|
+
elsif (i == 1)
|
80
|
+
longitude_2 = point[0]
|
81
|
+
latitude_2 = point[1]
|
82
|
+
total_distance = total_distance + distance_calc(latitude_1, longitude_1, latitude_2, longitude_2)
|
83
|
+
i = i + 1
|
84
|
+
elsif (i > 1)
|
85
|
+
longitude_1 = longitude_2
|
86
|
+
latitude_1 = latitude_2
|
87
|
+
longitude_2 = point[0]
|
88
|
+
latitude_2 = point[1]
|
89
|
+
total_distance = total_distance + distance_calc(latitude_1, longitude_1, latitude_2, longitude_2)
|
90
|
+
i = i + 1
|
91
|
+
end
|
92
|
+
}
|
93
|
+
return total_distance
|
94
|
+
end
|
95
|
+
|
96
|
+
# Calculates the distance between the coordinates of two way nodes in meters
|
97
|
+
def self.distance_calc(latitude_1, longitude_1, latitude_2, longitude_2)
|
98
|
+
|
99
|
+
difference_latitude = latitude_2 - latitude_1
|
100
|
+
difference_longitude = longitude_2 - longitude_1
|
101
|
+
|
102
|
+
result = Math.sqrt(difference_latitude**2 + difference_longitude**2)
|
103
|
+
latitude_longitude_length = latitude_longitude_length(difference_latitude, difference_longitude)
|
104
|
+
return result * latitude_longitude_length[2]
|
105
|
+
end
|
106
|
+
|
107
|
+
# Calculates the length in meters of one degree and returns an array of the results [0] = latitude degree, [1] = longitude degree, [2] average of these two
|
108
|
+
# The argument should represent the latitude the result shall reflect
|
109
|
+
# Inspired ny: http://en.wikipedia.org/wiki/Latitude :)
|
110
|
+
def self.latitude_longitude_length(latitude, longitude)
|
111
|
+
lat = latitude * ((2.0 * Math::PI)/360.0)
|
112
|
+
lon = longitude * ((2.0 * Math::PI)/360.0)
|
113
|
+
|
114
|
+
constant_1 = 111132.954
|
115
|
+
constant_2 = -559.822
|
116
|
+
constant_3 = 1.175
|
117
|
+
a = 6378137.0 # Earth radius
|
118
|
+
b = 6356752.3142 # Earth radius
|
119
|
+
|
120
|
+
#Calculate the length of a degree of latitude and longitude in meters
|
121
|
+
latitude_length = constant_1 + (constant_2 * Math.cos(2 * lat)) + (constant_3 * Math.cos(4 * lat))
|
122
|
+
longitude_length = Math::PI * a * Math.cos(lon) / 180 * (1 - ((a - b) / a) ** 2 * Math.sin(lon) ** 2)**0.5
|
123
|
+
|
124
|
+
return latitude_length, longitude_length, (latitude_length + longitude_length)/2
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/mormon/osm_loader.rb
CHANGED
@@ -4,15 +4,15 @@ require 'tmpdir'
|
|
4
4
|
module Mormon
|
5
5
|
module OSM
|
6
6
|
class Loader
|
7
|
-
|
7
|
+
|
8
8
|
@route_types = [:cycle, :car, :train, :foot, :horse]
|
9
9
|
@cache_dir = File.join Dir.tmpdir, "mormon", "cache"
|
10
|
-
|
10
|
+
|
11
11
|
class << self
|
12
12
|
attr_reader :route_types
|
13
13
|
attr_accessor :cache_dir
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
attr_reader :options, :routing, :nodes, :ways, :tiles, :routeable_nodes, :route_types,
|
17
17
|
:osm_filename
|
18
18
|
|
@@ -22,7 +22,7 @@ module Mormon
|
|
22
22
|
@tiles = {}
|
23
23
|
@nodes = {}
|
24
24
|
@ways = {}
|
25
|
-
|
25
|
+
|
26
26
|
@routing = {}
|
27
27
|
@routeable_nodes = {}
|
28
28
|
|
@@ -41,7 +41,7 @@ module Mormon
|
|
41
41
|
def report
|
42
42
|
report = "Loaded %d nodes,\n" % @nodes.keys.size
|
43
43
|
report += "%d ways, and...\n" % @ways.keys.size
|
44
|
-
|
44
|
+
|
45
45
|
Loader.route_types.each do |type|
|
46
46
|
report += " %d %s routes\n" % [@routing[type].keys.size, type]
|
47
47
|
end
|
@@ -52,18 +52,18 @@ module Mormon
|
|
52
52
|
def cache_filename
|
53
53
|
File.join Loader.cache_dir, File.basename(@osm_filename) + ".pstore"
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
private
|
57
57
|
def load_cached
|
58
58
|
require "pstore"
|
59
|
-
|
59
|
+
|
60
60
|
store_path = cache_filename
|
61
61
|
|
62
62
|
FileUtils.mkdir_p Loader.cache_dir
|
63
63
|
FileUtils.touch store_path
|
64
|
-
|
64
|
+
|
65
65
|
store = PStore.new store_path
|
66
|
-
|
66
|
+
|
67
67
|
if !File.zero? store_path
|
68
68
|
puts "Loading from cache %s..." % store.path
|
69
69
|
|
@@ -78,7 +78,7 @@ module Mormon
|
|
78
78
|
@routeable_nodes[type] = store[:routeable_nodes][type]
|
79
79
|
end
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
else
|
83
83
|
puts "Parsing %s..." % @osm_filename
|
84
84
|
parse
|
@@ -87,7 +87,7 @@ module Mormon
|
|
87
87
|
store.transaction do
|
88
88
|
store[:tiles] = @tiles
|
89
89
|
store[:nodes] = @nodes
|
90
|
-
store[:ways] = @ways
|
90
|
+
store[:ways] = @ways
|
91
91
|
store[:tiles] = @tiles
|
92
92
|
|
93
93
|
store[:routing] = {}
|
@@ -104,12 +104,12 @@ module Mormon
|
|
104
104
|
|
105
105
|
def parse
|
106
106
|
puts "Loading %s.." % @osm_filename
|
107
|
-
|
107
|
+
|
108
108
|
if !File.exists?(@osm_filename)
|
109
109
|
print "No such data file %s" % @osm_filename
|
110
110
|
return false
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
osm = Nokogiri::XML File.open(@osm_filename)
|
114
114
|
|
115
115
|
load_nodes osm
|
@@ -119,13 +119,13 @@ module Mormon
|
|
119
119
|
def load_nodes(nokosm)
|
120
120
|
nokosm.css('node').each do |node|
|
121
121
|
node_id = node[:id]
|
122
|
-
|
122
|
+
|
123
123
|
@nodes[node_id] = {
|
124
124
|
lat: node[:lat].to_f,
|
125
125
|
lon: node[:lon].to_f,
|
126
126
|
tags: {}
|
127
127
|
}
|
128
|
-
|
128
|
+
|
129
129
|
node.css('tag').each do |t|
|
130
130
|
k,v = t[:k].to_sym, t[:v]
|
131
131
|
@nodes[node_id][:tags][k] = v unless useless_tags.include?(k)
|
@@ -136,12 +136,12 @@ module Mormon
|
|
136
136
|
def load_ways(nokosm)
|
137
137
|
nokosm.css('way').each do |way|
|
138
138
|
way_id = way[:id]
|
139
|
-
|
139
|
+
|
140
140
|
@ways[way_id] = {
|
141
141
|
nodes: way.css('nd').map { |nd| nd[:ref] },
|
142
142
|
tags: {}
|
143
143
|
}
|
144
|
-
|
144
|
+
|
145
145
|
way.css('tag').each do |t|
|
146
146
|
k,v = t[:k].to_sym, t[:v]
|
147
147
|
@ways[way_id][:tags][k] = v unless useless_tags.include?(k)
|
@@ -154,31 +154,31 @@ module Mormon
|
|
154
154
|
def useless_tags
|
155
155
|
[:created_by]
|
156
156
|
end
|
157
|
-
|
157
|
+
|
158
158
|
def way_access(highway, railway)
|
159
159
|
access = {}
|
160
|
-
access[:cycle] = [:primary, :secondary, :tertiary, :unclassified, :minor, :cycleway,
|
161
|
-
:residential, :track, :service].include?
|
162
|
-
|
163
|
-
access[:car] = [:motorway, :trunk, :primary, :secondary, :tertiary,
|
164
|
-
:unclassified, :minor, :residential, :service].include?
|
165
|
-
|
166
|
-
access[:train] = [:rail, :light_rail, :subway].include?
|
167
|
-
access[:foot] = access[:cycle] || [:footway, :steps].include?(highway.to_sym)
|
168
|
-
access[:horse] = [:track, :unclassified, :bridleway].include?
|
160
|
+
access[:cycle] = [:primary, :secondary, :tertiary, :unclassified, :minor, :cycleway,
|
161
|
+
:residential, :track, :service].include?(highway.to_sym)
|
162
|
+
|
163
|
+
access[:car] = [:motorway, :trunk, :primary, :secondary, :tertiary,
|
164
|
+
:unclassified, :minor, :residential, :service].include?(highway.to_sym)
|
165
|
+
|
166
|
+
access[:train] = [:rail, :light_rail, :subway].include?(railway.to_sym)
|
167
|
+
access[:foot] = access[:cycle] || [:footway, :steps, :path].include?(highway.to_sym)
|
168
|
+
access[:horse] = [:track, :unclassified, :bridleway].include?(highway.to_sym)
|
169
169
|
access
|
170
170
|
end
|
171
171
|
|
172
172
|
def store_way(way)
|
173
173
|
tags = way[:tags]
|
174
|
-
|
174
|
+
|
175
175
|
highway = equivalent tags.fetch(:highway, "")
|
176
176
|
railway = equivalent tags.fetch(:railway, "")
|
177
177
|
oneway = tags.fetch(:oneway, "")
|
178
178
|
reversible = !['yes','true','1'].include?(oneway)
|
179
|
-
|
179
|
+
|
180
180
|
access = way_access highway, railway
|
181
|
-
|
181
|
+
|
182
182
|
# Store routing information
|
183
183
|
last = -1
|
184
184
|
way[:nodes].each do |node|
|
@@ -188,7 +188,7 @@ module Mormon
|
|
188
188
|
weight = Mormon::Weight.get route_type, highway.to_sym
|
189
189
|
add_link(last, node, route_type, weight)
|
190
190
|
add_link(node, last, route_type, weight) if reversible || route_type == :foot
|
191
|
-
end
|
191
|
+
end
|
192
192
|
end
|
193
193
|
end
|
194
194
|
last = node
|
@@ -207,17 +207,8 @@ module Mormon
|
|
207
207
|
@routing[route_type][fr] = { to => weight }
|
208
208
|
end
|
209
209
|
|
210
|
-
def way_type(tags)
|
211
|
-
# Look for a variety of tags (priority order - first one found is used)
|
212
|
-
[:highway, :railway, :waterway, :natural].each do |type|
|
213
|
-
value = tags.fetch(type, '')
|
214
|
-
return equivalent(value) if value
|
215
|
-
end
|
216
|
-
nil
|
217
|
-
end
|
218
|
-
|
219
210
|
def equivalent(tag)
|
220
|
-
{
|
211
|
+
{
|
221
212
|
primary_link: "primary",
|
222
213
|
trunk: "primary",
|
223
214
|
trunk_link: "primary",
|
@@ -230,7 +221,6 @@ module Mormon
|
|
230
221
|
driveway: "service",
|
231
222
|
pedestrian: "footway",
|
232
223
|
bridleway: "cycleway",
|
233
|
-
track: "cycleway",
|
234
224
|
arcade: "footway",
|
235
225
|
canal: "river",
|
236
226
|
riverbank: "river",
|
@@ -239,8 +229,6 @@ module Mormon
|
|
239
229
|
living_street: "unclassified"
|
240
230
|
}[tag.to_sym] || tag
|
241
231
|
end
|
242
|
-
|
243
232
|
end
|
244
233
|
end
|
245
234
|
end
|
246
|
-
|
data/lib/mormon/osm_router.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Mormon
|
2
2
|
module OSM
|
3
|
-
|
4
3
|
module Algorithm
|
5
4
|
class Base
|
6
5
|
def initialize(router, options = {})
|
@@ -24,7 +23,7 @@ module Mormon
|
|
24
23
|
|
25
24
|
lat1, lon1 = node1[:lat], node1[:lon]
|
26
25
|
lat2, lon2 = node2[:lat], node2[:lon]
|
27
|
-
|
26
|
+
|
28
27
|
dlat = lat2 - lat1
|
29
28
|
dlon = lon2 - lon1
|
30
29
|
|
@@ -45,7 +44,7 @@ module Mormon
|
|
45
44
|
end
|
46
45
|
|
47
46
|
closed = [node_start]
|
48
|
-
|
47
|
+
|
49
48
|
# Limit for how long it will search
|
50
49
|
(0..10000).each do
|
51
50
|
next_item = @queue.shift
|
@@ -53,7 +52,7 @@ module Mormon
|
|
53
52
|
|
54
53
|
x = next_item[:node_end]
|
55
54
|
next if closed.include?(x)
|
56
|
-
|
55
|
+
|
57
56
|
# Found the end node - success
|
58
57
|
return ['success', next_item[:nodes]] if x == node_end
|
59
58
|
|
@@ -66,18 +65,18 @@ module Mormon
|
|
66
65
|
|
67
66
|
'gave_up'
|
68
67
|
end
|
69
|
-
|
68
|
+
|
70
69
|
def enqueue(node_start, node_end, node_finish, current_queue, weight = 1)
|
71
70
|
# Add another potential route to the queue
|
72
|
-
|
71
|
+
|
73
72
|
# if already in queue
|
74
73
|
return if @queue.any? { |other| other[:node_end] == node_end }
|
75
74
|
|
76
75
|
distance = distance(node_start, node_end)
|
77
|
-
|
76
|
+
|
78
77
|
return if weight.zero?
|
79
78
|
distance = distance / weight
|
80
|
-
|
79
|
+
|
81
80
|
# Create a hash for all the route's attributes
|
82
81
|
current_distance = current_queue[:distance]
|
83
82
|
nodes = current_queue[:nodes].dup
|
@@ -89,7 +88,7 @@ module Mormon
|
|
89
88
|
nodes: nodes,
|
90
89
|
node_end: node_end
|
91
90
|
}
|
92
|
-
|
91
|
+
|
93
92
|
# Try to insert, keeping the queue ordered by decreasing worst-case distance
|
94
93
|
ix = @queue.find_index { |other| other[:max_distance] > queue_item[:max_distance] } || -1
|
95
94
|
@queue.insert(ix, queue_item)
|
@@ -124,17 +123,17 @@ module Mormon
|
|
124
123
|
if neighbors
|
125
124
|
neighbors = neighbors.keys.map(&:to_i)
|
126
125
|
not_visited = neighbors - (neighbors & visited)
|
127
|
-
|
126
|
+
|
128
127
|
# random sort in order to not take the same order for neighbors every time.
|
129
128
|
not_visited.sort_by { rand }.each do |neighbor|
|
130
129
|
# limit the width of the route go further more than max_distance the distance between start and end
|
131
|
-
next if distance(neighbor, node_end) > max_amplitude
|
130
|
+
next if distance(neighbor, node_end) > max_amplitude
|
132
131
|
current_route << neighbor
|
133
132
|
enqueue neighbor, node_end, transport, current_route, visited, max_amplitude
|
134
133
|
current_route.delete neighbor
|
135
134
|
end
|
136
135
|
end
|
137
|
-
|
136
|
+
|
138
137
|
visited.delete node_start
|
139
138
|
end
|
140
139
|
end
|
@@ -144,21 +143,23 @@ module Mormon
|
|
144
143
|
|
145
144
|
|
146
145
|
class Router
|
147
|
-
attr_reader :loader
|
146
|
+
attr_reader :loader
|
148
147
|
|
149
148
|
def initialize(loader, options = {})
|
150
|
-
algorithm = options.delete(:algorithm) || :astar
|
151
|
-
algorithm_class = "Mormon::OSM::Algorithm::#{algorithm.to_s.capitalize}".constantize
|
152
|
-
|
153
149
|
@loader = loader
|
154
|
-
@
|
150
|
+
@options = options
|
151
|
+
|
152
|
+
algorithm = @options.delete(:algorithm) || :astar
|
153
|
+
@algorithm_class = "Mormon::OSM::Algorithm::#{algorithm.to_s.capitalize}".constantize
|
155
154
|
end
|
156
155
|
|
157
156
|
def find_route(node_start, node_end, transport)
|
158
|
-
|
159
|
-
|
157
|
+
algorithm = @algorithm_class.new(self, @options)
|
158
|
+
|
159
|
+
result, nodes = algorithm.route(node_start.to_i, node_end.to_i, transport.to_sym)
|
160
|
+
|
160
161
|
return [result,[]] if result != 'success'
|
161
|
-
|
162
|
+
|
162
163
|
nodes.map! do |node|
|
163
164
|
data = @loader.nodes[node.to_s]
|
164
165
|
[data[:lat], data[:lon]]
|
@@ -166,7 +167,6 @@ module Mormon
|
|
166
167
|
|
167
168
|
[result, nodes]
|
168
169
|
end
|
169
|
-
|
170
170
|
end
|
171
171
|
end
|
172
|
-
end
|
172
|
+
end
|
data/lib/mormon/version.rb
CHANGED
data/lib/mormon/weight.rb
CHANGED
@@ -15,6 +15,7 @@ module Mormon
|
|
15
15
|
service: { cycle: 1, car: 1, foot: 1, horse: 1 },
|
16
16
|
bridleway: { cycle: 0.8, foot: 1, horse: 10, mtb: 3 },
|
17
17
|
footway: { cycle: 0.2, foot: 1 },
|
18
|
+
path: { foot: 1 },
|
18
19
|
steps: { foot: 1, cycle: 0.3 },
|
19
20
|
rail: { train: 1 },
|
20
21
|
light_rail: { train: 1 },
|
data/lib/mormon.rb
CHANGED
data/mormon.gemspec
CHANGED
@@ -6,10 +6,10 @@ require 'mormon/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "mormon"
|
8
8
|
gem.version = Mormon::VERSION
|
9
|
-
gem.
|
10
|
-
gem.email =
|
9
|
+
gem.author = "Geronimo Diaz"
|
10
|
+
gem.email = "geronimod@gmail.com"
|
11
11
|
gem.description = %q{ OSM Router }
|
12
|
-
gem.summary = %q{ OSM Routing with some extra features: reset tiles cache and
|
12
|
+
gem.summary = %q{ OSM Routing with some extra features: reset tiles cache, random routes and distance optimizer for routes with several stops. It's based on Pyroute library. }
|
13
13
|
gem.homepage = "https://github.com/geronimod/mormon"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
@@ -18,8 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
20
|
gem.rubyforge_project = "mormon"
|
21
|
-
|
21
|
+
|
22
22
|
gem.add_dependency "nokogiri"
|
23
23
|
gem.add_development_dependency "rspec"
|
24
|
-
gem.add_development_dependency "debugger"
|
25
24
|
end
|