forked from zerebubuth/openstreetmap-license-change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm.rb
183 lines (148 loc) · 4.44 KB
/
osm.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module OSM
module Taggable
attr_accessor :tags
private
def init_tags(tags)
@tags = tags
end
end
module Element
attr_accessor :element_id, :changeset_id, :timestamp, :visible, :version
private
def init_attrs(attrs)
@element_id = attrs[:id]
@changeset_id = attrs[:changeset]
@timestamp = attrs[:timestamp]
@visible = attrs.has_key?(:visible) ? attrs[:visible] : true
@version = attrs[:version]
end
def self.parse_options(options)
groups = options.group_by {|k, v| k.instance_of? String and v.instance_of? String}
tags = groups[true].nil? ? Hash.new : Hash[groups[true]]
attrs = groups[false].nil? ? Hash.new : Hash[groups[false]]
return tags, attrs
end
def compare(o, arg, *args)
cmp = self.send(arg) <=> o.send(arg)
if cmp != 0 or args.empty?
cmp
else
compare(o, *args)
end
end
end
class Node
include Taggable, Element, Comparable
attr_accessor :position
def self.[](pos, options = {})
tags, attrs = Element.parse_options(options)
Node.new(attrs, pos, tags)
end
def <=>(o)
compare(o, :position, :tags, :element_id, :changeset_id, :timestamp, :visible, :version)
end
def to_s
self.class.to_s + "[" +
@position.inspect + "," +
[:element_id, :changeset_id, :timestamp, :visible, :version].collect {|attr| "#{attr.inspect}=>#{self.send(attr)}"}.join(",") + "," +
@tags.to_a.collect {|k,v| "#{k.inspect}=>#{v.inspect}"}.join(",") + "]"
end
def version_zero_geom
# there's no real "version zero" geometry for a node - it's
# not possible to have one where the geometry is not a valid
# lon/lat pair. so here we choose something which can't
# happen in the real data model.
nil
end
def geom
position
end
private
def initialize(attrs, pos, tags)
@position = pos
init_attrs(attrs)
init_tags(tags)
end
end
class Way
include Taggable, Element, Comparable
attr_accessor :nodes
def self.[](nodes, options = {})
tags, attrs = Element.parse_options(options)
Way.new(attrs, nodes, tags)
end
def <=>(o)
compare(o, :nodes, :tags, :element_id, :changeset_id, :timestamp, :visible, :version)
end
def to_s
self.class.to_s + "[" +
@nodes.inspect + "," +
[:element_id, :changeset_id, :timestamp, :visible, :version].collect {|attr| "#{attr.inspect}=>#{self.send(attr)}"}.join(",") + "," +
@tags.to_a.collect {|k,v| "#{k.inspect}=>#{v.inspect}"}.join(",") + "]"
end
def version_zero_geom
# according to the "version zero" proposal, we should start
# by assuming a zeroth version which is simply empty.
[]
end
def geom
nodes
end
private
def initialize(attrs, nodes, tags)
@nodes = nodes
init_attrs(attrs)
init_tags(tags)
end
end
class Relation
include Taggable, Element, Comparable
class Member
include Comparable
attr_accessor :type, :ref, :role
def self.[](type, ref, role = "")
Member.new(type, ref, role)
end
def <=>(o)
return @type <=> o.type if @type != o.type
return @ref <=> o.ref if @ref != o.ref
@role <=> o.role
end
def to_s
"Member[#{@type.inspect},#{@ref},#{@role.inspect}]"
end
private
def initialize(type, ref, role)
@type, @ref, @role = type, ref, role
end
end
attr_accessor :members
def self.[](members, options = {})
tags, attrs = Element.parse_options(options)
Relation.new(attrs, members, tags)
end
def <=>(o)
compare(o, :members, :tags, :element_id, :changeset_id, :timestamp, :visible, :version)
end
def to_s
self.class.to_s + "[" +
@members.inspect + "," +
[:element_id, :changeset_id, :timestamp, :visible, :version].collect {|attr| "#{attr.inspect}=>#{self.send(attr)}"}.join(",") + "," +
@tags.to_a.collect {|k,v| "#{k.inspect}=>#{v.inspect}"}.join(",") + "]"
end
def version_zero_geom
# according to the "version zero" proposal, we should start
# by assuming a zeroth version which is simply empty.
return []
end
def geom
members
end
private
def initialize(attrs, members, tags)
@members = members
init_attrs(attrs)
init_tags(tags)
end
end
end