1 class Relation < ActiveRecord::Base
4 set_table_name 'current_relations'
8 has_many :old_relations, :foreign_key => 'id', :order => 'version'
10 has_many :relation_members, :foreign_key => 'id'
11 has_many :relation_tags, :foreign_key => 'id'
13 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
14 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
16 def self.from_xml(xml, create=false)
22 relation = Relation.new
24 doc.find('//osm/relation').each do |pt|
25 if !create and pt['id'] != '0'
26 relation.id = pt['id'].to_i
30 relation.timestamp = Time.now
31 relation.visible = true
34 relation.timestamp = Time.parse(pt['timestamp'])
38 pt.find('tag').each do |tag|
39 relation.add_tag_keyval(tag['k'], tag['v'])
42 pt.find('member').each do |member|
43 relation.add_member(member['type'], member['ref'], member['role'])
54 doc = OSM::API.new.get_xml_doc
55 doc.root << to_xml_node()
59 def to_xml_node(user_display_name_cache = nil)
60 el1 = XML::Node.new 'relation'
61 el1['id'] = self.id.to_s
62 el1['visible'] = self.visible.to_s
63 el1['timestamp'] = self.timestamp.xmlschema
65 user_display_name_cache = {} if user_display_name_cache.nil?
67 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
68 # use the cache if available
69 elsif self.user.data_public?
70 user_display_name_cache[self.user_id] = self.user.display_name
72 user_display_name_cache[self.user_id] = nil
75 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
77 self.relation_members.each do |member|
80 # # if there is a list of visible members then use that to weed out deleted segments
81 # if visible_members[member.member_type][member.member_id]
85 # otherwise, manually go to the db to check things
86 if member.member.visible?
91 e = XML::Node.new 'member'
92 e['type'] = member.member_type
93 e['ref'] = member.member_id.to_s
94 e['role'] = member.member_role
99 self.relation_tags.each do |tag|
100 e = XML::Node.new 'tag'
108 # FIXME is this really needed?
112 self.relation_members.each do |member|
113 @members += [[member.member_type,member.member_id,member.member_role]]
122 self.relation_tags.each do |tag|
137 def add_member(type,id,role)
138 @members = Array.new unless @members
139 @members += [[type,id,role]]
142 def add_tag_keyval(k, v)
143 @tags = Hash.new unless @tags
147 def save_with_history!
148 Relation.transaction do
155 RelationTag.delete_all(['id = ?', self.id])
158 tag = RelationTag.new
165 members = self.members
167 RelationMember.delete_all(['id = ?', self.id])
170 mem = RelationMember.new
172 mem.member_type = n[0];
173 mem.member_id = n[1];
174 mem.member_role = n[2];
178 old_relation = OldRelation.from_relation(self)
179 old_relation.timestamp = t
180 old_relation.save_with_dependencies!
184 def preconditions_ok?
185 # These are hastables that store an id in the index of all
186 # the nodes/way/relations that have already been added.
187 # Once we know the id of the node/way/relation exists
188 # we check to see if it is already existing in the hashtable
189 # if it does, then we return false. Otherwise
190 # we add it to the relevant hash table, with the value true..
191 # Thus if you have nodes with the ids of 50 and 1 already in the
192 # relation, then the hash table nodes would contain:
193 # => {50=>true, 1=>true}
197 self.members.each do |m|
199 n = Node.find(:first, :conditions => ["id = ?", m[1]])
200 unless n and n.visible
208 elsif (m[0] == "way")
209 w = Way.find(:first, :conditions => ["id = ?", m[1]])
210 unless w and w.visible and w.preconditions_ok?
218 elsif (m[0] == "relation")
219 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
220 unless e and e.visible and e.preconditions_ok?
226 relations[m[1]] = true
237 # Temporary method to match interface to nodes