public
Description: A simple ruby script for extracting specific data from OSM files.
Clone URL: git://github.com/karouf/osm-cherrypick.git
Implemented relation support

Reorganised the code to do that.
karouf (author)
Mon Jul 14 12:40:03 -0700 2008
commit  ef3cea92b3322d867e7097b02601ab092ed86d65
tree    a10267661eaed89ac90657dc58d891cf57b6996f
parent  5b17b2fc66d04da1a513eeb85dd67db31c112b5f
...
31
32
33
34
 
35
36
37
38
 
39
40
41
42
43
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
46
47
...
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
...
83
84
85
86
 
...
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
...
144
145
146
 
 
 
 
 
147
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
150
 
151
152
153
154
...
157
158
159
 
160
0
@@ -31,17 +31,110 @@ features = ARGV
0
 
0
 
0
 # Open OSM file
0
-input_doc = Document.file(input)
0
+$input_doc = Document.file(input)
0
 
0
 
0
 # Create new XML doc for output
0
-output_doc = Document.new
0
+$output_doc = Document.new
0
 #output_doc << input_doc.xml_decl
0
 #output_doc.root = input_doc.root
0
 #output_doc.root['generator'] = "osm-cherrypick"
0
-output_doc.root = Node.new('osm')
0
-output_doc.root['version'] = '0.5'
0
-output_doc.root['generator'] = 'osm-cherrypick'
0
+$output_doc.root = Node.new('osm')
0
+$output_doc.root['version'] = '0.5'
0
+$output_doc.root['generator'] = 'osm-cherrypick'
0
+
0
+def extract_node(id)
0
+ $input_doc.find("node[@id='#{id}']").each do |node|
0
+ #puts "Node id: #{the_node['id']}"
0
+ # Add the feature to the output doc
0
+ $output_doc.root.child_add(node)
0
+ end
0
+end
0
+
0
+def extract_way(id)
0
+ $input_doc.find("way[@id='#{id}']").each do |way|
0
+ #puts "Node id: #{the_node['id']}"
0
+ # Add the feature to the output doc
0
+ way.find("nd").each do |node|
0
+ #puts "Node ref in way: #{node['ref']}"
0
+ # Get the feature from the file
0
+ extract_node(node['ref'])
0
+ #$input_doc.find("node[@id='#{node['ref']}']").each do |the_node|
0
+ #puts "Node id: #{the_node['id']}"
0
+ # Add the feature to the output doc
0
+ #$output_doc.root.child_add(the_node)
0
+ #end
0
+ end
0
+ $output_doc.root.child_add(way)
0
+ end
0
+end
0
+
0
+def extract_relation(id)
0
+ $input_doc.find("relation[@id='#{id}']").each do |relation|
0
+ relation.find("member").each do |member|
0
+ case member['type']
0
+ when "node"
0
+ extract_node(member['ref'])
0
+ when "way"
0
+ extract_way(member['ref'])
0
+ when "relation"
0
+ extract_relation(member['ref'])
0
+ else
0
+ puts "Bad shit happened!"
0
+ raise
0
+ end
0
+ end
0
+ $output_doc.root.child_add(relation)
0
+ end
0
+end
0
+
0
+def extract_nodes(key, value)
0
+ $input_doc.find("/osm/node/tag[@k='#{key}' and @v='#{value}']/..").each do |node|
0
+ # Add the feature to the output doc
0
+ #extract_node(node['id'])
0
+ $output_doc.root.child_add(node)
0
+ #puts "Node id: #{node['id']}"
0
+ end
0
+end
0
+
0
+def extract_ways(key, value)
0
+ # Get the feature from the file
0
+ $input_doc.find("/osm/way/tag[@k='#{key}' and @v='#{value}']/..").each do |way|
0
+ #puts "Way id: #{way['id']}"
0
+ # Add the feature to the output doc
0
+ way.find("nd").each do |node|
0
+ #puts "Node ref in way: #{node['ref']}"
0
+ # Get the feature from the file
0
+ extract_node(node['ref'])
0
+ #$input_doc.find("node[@id='#{node['ref']}']").each do |the_node|
0
+ #puts "Node id: #{the_node['id']}"
0
+ # Add the feature to the output doc
0
+ #$output_doc.root.child_add(the_node)
0
+ #end
0
+ end
0
+ $output_doc.root.child_add(way)
0
+ end
0
+end
0
+
0
+def extract_relations(key, value)
0
+ $input_doc.find("/osm/relation/tag[@k='#{key}' and @v='#{value}']/..").each do |relation|
0
+ relation.find("member").each do |member|
0
+ case member['type']
0
+ when "node"
0
+ extract_node(member['ref'])
0
+ when "way"
0
+ extract_way(member['ref'])
0
+ when "relation"
0
+ extract_relation(member['ref'])
0
+ else
0
+ puts "Bad shit happened!"
0
+ raise
0
+ end
0
+ end
0
+ $output_doc.root.child_add(relation)
0
+ end
0
+end
0
+
0
 
0
 # For each feature
0
 for feature in features do
0
@@ -51,30 +144,11 @@ for feature in features do
0
   case type
0
   when "node"
0
     # Get the feature from the file
0
- input_doc.find("/osm/#{type}/tag[@k='#{key}' and @v='#{value}']/..").each do |node|
0
- # Add the feature to the output doc
0
- output_doc.root.child_add(node)
0
- #puts "Node id: #{node['id']}"
0
- end
0
+ extract_nodes(key, value)
0
   when "way"
0
- #puts "Ways are not supported yet!"
0
- # Get the feature from the file
0
- input_doc.find("#{type}/tag[@k='#{key}' and @v='#{value}']/..").each do |way|
0
- #puts "Way id: #{way['id']}"
0
- # Add the feature to the output doc
0
- way.find("nd").each do |node|
0
- #puts "Node ref in way: #{node['ref']}"
0
- # Get the feature from the file
0
- input_doc.find("node[@id='#{node['ref']}']").each do |the_node|
0
- #puts "Node id: #{the_node['id']}"
0
- # Add the feature to the output doc
0
- output_doc.root.child_add(the_node)
0
- end
0
- end
0
- output_doc.root.child_add(way)
0
- end
0
+ extract_ways(key, value)
0
   when "relation"
0
- puts "Relations are not supported yet!"
0
+ extract_relations(key, value)
0
   else
0
     puts "Wrong type of object: #{feature}"
0
   end
0
@@ -83,4 +157,4 @@ end
0
 
0
 # Write XML doc to the output file
0
 format = true
0
-output_doc.save(output, format)
0
+$output_doc.save(output, format)

Comments

    No one has commented yet.