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 def self.find_for_nodes(ids, options = {})
112 self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'node' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
113 return self.find(:all, options)
118 def self.find_for_ways(ids, options = {})
122 self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'way' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
123 return self.find(:all, options)
128 def self.find_for_relations(ids, options = {})
132 self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'relation' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
133 return self.find(:all, options)
138 # FIXME is this really needed?
142 self.relation_members.each do |member|
143 @members += [[member.member_type,member.member_id,member.member_role]]
152 self.relation_tags.each do |tag|
167 def add_member(type,id,role)
168 @members = Array.new unless @members
169 @members += [[type,id,role]]
172 def add_tag_keyval(k, v)
173 @tags = Hash.new unless @tags
177 def save_with_history!
178 Relation.transaction do
185 RelationTag.delete_all(['id = ?', self.id])
188 tag = RelationTag.new
195 members = self.members
197 RelationMember.delete_all(['id = ?', self.id])
200 mem = RelationMember.new
202 mem.member_type = n[0];
203 mem.member_id = n[1];
204 mem.member_role = n[2];
208 old_relation = OldRelation.from_relation(self)
209 old_relation.timestamp = t
210 old_relation.save_with_dependencies!
214 def preconditions_ok?
215 # These are hastables that store an id in the index of all
216 # the nodes/way/relations that have already been added.
217 # Once we know the id of the node/way/relation exists
218 # we check to see if it is already existing in the hashtable
219 # if it does, then we return false. Otherwise
220 # we add it to the relevant hash table, with the value true..
221 # Thus if you have nodes with the ids of 50 and 1 already in the
222 # relation, then the hash table nodes would contain:
223 # => {50=>true, 1=>true}
227 self.members.each do |m|
229 n = Node.find(:first, :conditions => ["id = ?", m[1]])
230 unless n and n.visible
238 elsif (m[0] == "way")
239 w = Way.find(:first, :conditions => ["id = ?", m[1]])
240 unless w and w.visible and w.preconditions_ok?
248 elsif (m[0] == "relation")
249 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
250 unless e and e.visible and e.preconditions_ok?
256 relations[m[1]] = true
267 # Temporary method to match interface to nodes