mormon 0.0.2 → 1.0.0
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.
- data/lib/mormon/osm_loader.rb +82 -20
- data/lib/mormon/version.rb +1 -1
- data/spec/mormon_spec.rb +50 -16
- metadata +1 -1
data/lib/mormon/osm_loader.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
require 'nokogiri'
|
2
|
+
require 'tmpdir'
|
2
3
|
|
3
4
|
module Mormon
|
4
5
|
module OSM
|
5
6
|
class Loader
|
6
|
-
|
7
|
+
|
8
|
+
@route_types = [:cycle, :car, :train, :foot, :horse]
|
9
|
+
@cache_dir = File.join Dir.tmpdir, "mormon", "cache"
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_reader :route_types
|
13
|
+
attr_accessor :cache_dir
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :options, :routing, :nodes, :ways, :tiles, :routeable_nodes, :route_types,
|
17
|
+
:osm_filename
|
7
18
|
|
8
19
|
def initialize(filename, options = {})
|
9
20
|
@options = options
|
@@ -14,9 +25,8 @@ module Mormon
|
|
14
25
|
|
15
26
|
@routing = {}
|
16
27
|
@routeable_nodes = {}
|
17
|
-
@route_types = [:cycle, :car, :train, :foot, :horse]
|
18
28
|
|
19
|
-
|
29
|
+
Loader.route_types.each do |type|
|
20
30
|
@routing[type] = {}
|
21
31
|
@routeable_nodes[type] = {}
|
22
32
|
end
|
@@ -24,35 +34,87 @@ module Mormon
|
|
24
34
|
# @tilename = Mormon::Tile::Name.new
|
25
35
|
# @tiledata = Mormon::Tile::Data.new
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
def parse(filename)
|
31
|
-
puts "Loading %s.." % filename
|
32
|
-
|
33
|
-
if !File.exists?(filename)
|
34
|
-
print "No such data file %s" % filename
|
35
|
-
return false
|
36
|
-
end
|
37
|
-
|
38
|
-
osm = Nokogiri::XML(File.open(filename))
|
39
|
-
|
40
|
-
load_nodes osm
|
41
|
-
load_ways osm
|
37
|
+
@osm_filename = filename
|
38
|
+
@options[:cache] ? load_cached : parse
|
42
39
|
end
|
43
40
|
|
44
41
|
def report
|
45
42
|
report = "Loaded %d nodes,\n" % @nodes.keys.size
|
46
43
|
report += "%d ways, and...\n" % @ways.keys.size
|
47
44
|
|
48
|
-
|
45
|
+
Loader.route_types.each do |type|
|
49
46
|
report += " %d %s routes\n" % [@routing[type].keys.size, type]
|
50
47
|
end
|
51
48
|
|
52
49
|
report
|
53
50
|
end
|
54
51
|
|
52
|
+
def cache_filename
|
53
|
+
File.join Loader.cache_dir, File.basename(@osm_filename) + ".pstore"
|
54
|
+
end
|
55
|
+
|
55
56
|
private
|
57
|
+
def load_cached
|
58
|
+
require "pstore"
|
59
|
+
|
60
|
+
store_path = cache_filename
|
61
|
+
|
62
|
+
FileUtils.mkdir_p Loader.cache_dir
|
63
|
+
FileUtils.touch store_path
|
64
|
+
|
65
|
+
store = PStore.new store_path
|
66
|
+
|
67
|
+
if !File.zero? store_path
|
68
|
+
puts "Loading from cache %s..." % store.path
|
69
|
+
|
70
|
+
store.transaction(true) do
|
71
|
+
@tiles = store[:tiles]
|
72
|
+
@nodes = store[:nodes]
|
73
|
+
@ways = store[:ways]
|
74
|
+
@tiles = store[:tiles]
|
75
|
+
|
76
|
+
Loader.route_types.each do |type|
|
77
|
+
@routing[type] = store[:routing][type]
|
78
|
+
@routeable_nodes[type] = store[:routeable_nodes][type]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
else
|
83
|
+
puts "Parsing %s..." % @osm_filename
|
84
|
+
parse
|
85
|
+
|
86
|
+
puts "Creating cache %s..." % store.path
|
87
|
+
store.transaction do
|
88
|
+
store[:tiles] = @tiles
|
89
|
+
store[:nodes] = @nodes
|
90
|
+
store[:ways] = @ways
|
91
|
+
store[:tiles] = @tiles
|
92
|
+
|
93
|
+
store[:routing] = {}
|
94
|
+
store[:routeable_nodes] = {}
|
95
|
+
|
96
|
+
Loader.route_types.each do |type|
|
97
|
+
store[:routing][type] = @routing[type]
|
98
|
+
store[:routeable_nodes][type] = @routeable_nodes[type]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse
|
106
|
+
puts "Loading %s.." % @osm_filename
|
107
|
+
|
108
|
+
if !File.exists?(@osm_filename)
|
109
|
+
print "No such data file %s" % @osm_filename
|
110
|
+
return false
|
111
|
+
end
|
112
|
+
|
113
|
+
osm = Nokogiri::XML File.open(@osm_filename)
|
114
|
+
|
115
|
+
load_nodes osm
|
116
|
+
load_ways osm
|
117
|
+
end
|
56
118
|
|
57
119
|
def load_nodes(nokosm)
|
58
120
|
nokosm.css('node').each do |node|
|
@@ -121,7 +183,7 @@ module Mormon
|
|
121
183
|
last = -1
|
122
184
|
way[:nodes].each do |node|
|
123
185
|
if last != -1
|
124
|
-
|
186
|
+
Loader.route_types.each do |route_type|
|
125
187
|
if access[route_type]
|
126
188
|
weight = Mormon::Weight.get route_type, highway.to_sym
|
127
189
|
add_link(last, node, route_type, weight)
|
data/lib/mormon/version.rb
CHANGED
data/spec/mormon_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'mormon'
|
3
|
+
require 'benchmark'
|
3
4
|
|
4
5
|
describe Mormon::Weight do
|
5
6
|
it "get (transport, way type) must to return a value" do
|
@@ -36,31 +37,64 @@ describe Mormon::Tile::Name do
|
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
40
|
+
def spec_osm_file
|
41
|
+
File.join File.dirname(__FILE__), "spec.osm"
|
42
|
+
end
|
43
|
+
|
39
44
|
describe Mormon::OSM::Loader do
|
40
|
-
|
41
|
-
|
45
|
+
def common_specs(loader)
|
46
|
+
loader.nodes.keys.size.should eq 534
|
47
|
+
loader.ways.keys.size.should eq 135
|
48
|
+
|
49
|
+
loader.routing[:cycle].keys.size.should eq 240
|
50
|
+
loader.routing[:car].keys.size.should eq 240
|
51
|
+
loader.routing[:train].keys.size.should eq 0
|
52
|
+
loader.routing[:foot].keys.size.should eq 281
|
53
|
+
loader.routing[:horse].keys.size.should eq 216
|
42
54
|
end
|
43
55
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
56
|
+
describe "whitout cache" do
|
57
|
+
before :each do
|
58
|
+
@loader = Mormon::OSM::Loader.new spec_osm_file
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should load the correct data" do
|
62
|
+
common_specs @loader
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should has the correct nodes" do
|
66
|
+
map = { "448193026" => 1, "448193243" => 1, "448193220" => 1, "318099173" => 1 }
|
67
|
+
@loader.routing[:foot]["448193024"].should eq(map)
|
68
|
+
end
|
53
69
|
end
|
54
70
|
|
55
|
-
|
56
|
-
|
57
|
-
|
71
|
+
describe "with cache" do
|
72
|
+
it "should exists the cached version" do
|
73
|
+
@loader = Mormon::OSM::Loader.new spec_osm_file, :cache => true
|
74
|
+
File.exists?(@loader.cache_filename).should eq true
|
75
|
+
File.zero?(@loader.cache_filename).should eq false
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should have stored the same data" do
|
79
|
+
@without_cache = Mormon::OSM::Loader.new spec_osm_file
|
80
|
+
@with_cache = Mormon::OSM::Loader.new spec_osm_file, :cache => true
|
81
|
+
|
82
|
+
common_specs @without_cache
|
83
|
+
common_specs @with_cache
|
84
|
+
|
85
|
+
@without_cache.nodes.should eq @with_cache.nodes
|
86
|
+
@without_cache.ways.should eq @with_cache.ways
|
87
|
+
@without_cache.routing.should eq @with_cache.routing
|
88
|
+
@without_cache.routeable_nodes.should eq @with_cache.routeable_nodes
|
89
|
+
end
|
90
|
+
|
58
91
|
end
|
92
|
+
|
59
93
|
end
|
60
94
|
|
61
95
|
describe Mormon::OSM::Router do
|
62
96
|
before :each do
|
63
|
-
@loader = Mormon::OSM::Loader.new
|
97
|
+
@loader = Mormon::OSM::Loader.new spec_osm_file
|
64
98
|
@router = Mormon::OSM::Router.new @loader
|
65
99
|
end
|
66
100
|
|
@@ -81,7 +115,7 @@ describe Mormon::OSM::Router do
|
|
81
115
|
end
|
82
116
|
|
83
117
|
it "should find the route in tandil map" do
|
84
|
-
@loader = Mormon::OSM::Loader.new File.dirname(__FILE__)
|
118
|
+
@loader = Mormon::OSM::Loader.new File.join(File.dirname(__FILE__), "tandil.osm")
|
85
119
|
@router = Mormon::OSM::Router.new @loader
|
86
120
|
|
87
121
|
response, route = @router.find_route 1355012894, 1527759159, :car
|