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)
18 p = XML::Parser.string(xml)
21 relation = Relation.new
23 doc.find('//osm/relation').each do |pt|
24 if !create and pt['id'] != '0'
25 relation.id = pt['id'].to_i
29 relation.timestamp = Time.now
30 relation.visible = true
33 relation.timestamp = Time.parse(pt['timestamp'])
37 pt.find('tag').each do |tag|
38 relation.add_tag_keyval(tag['k'], tag['v'])
41 pt.find('member').each do |member|
42 relation.add_member(member['type'], member['ref'], member['role'])
53 doc = OSM::API.new.get_xml_doc
54 doc.root << to_xml_node()
58 def to_xml_node(user_display_name_cache = nil)
59 el1 = XML::Node.new 'relation'
60 el1['id'] = self.id.to_s
61 el1['visible'] = self.visible.to_s
62 el1['timestamp'] = self.timestamp.xmlschema
64 user_display_name_cache = {} if user_display_name_cache.nil?
66 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
67 # use the cache if available
68 elsif self.user.data_public?
69 user_display_name_cache[self.user_id] = self.user.display_name
71 user_display_name_cache[self.user_id] = nil
74 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
76 self.relation_members.each do |member|
79 # # if there is a list of visible members then use that to weed out deleted segments
80 # if visible_members[member.member_type][member.member_id]
84 # otherwise, manually go to the db to check things
85 if member.member.visible?
90 e = XML::Node.new 'member'
91 e['type'] = member.member_type
92 e['ref'] = member.member_id.to_s
93 e['role'] = member.member_role
98 self.relation_tags.each do |tag|
99 e = XML::Node.new 'tag'
107 def self.find_for_nodes(ids, options = {})
111 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
112 return self.find(:all, options)
117 def self.find_for_ways(ids, options = {})
121 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
122 return self.find(:all, options)
127 def self.find_for_relations(ids, options = {})
131 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
132 return self.find(:all, options)
137 # FIXME is this really needed?
141 self.relation_members.each do |member|
142 @members += [[member.member_type,member.member_id,member.member_role]]
151 self.relation_tags.each do |tag|
166 def add_member(type,id,role)
167 @members = Array.new unless @members
168 @members += [[type,id,role]]
171 def add_tag_keyval(k, v)
172 @tags = Hash.new unless @tags
176 def save_with_history!
177 Relation.transaction do
184 RelationTag.delete_all(['id = ?', self.id])
187 tag = RelationTag.new
194 members = self.members
196 RelationMember.delete_all(['id = ?', self.id])
199 mem = RelationMember.new
201 mem.member_type = n[0];
202 mem.member_id = n[1];
203 mem.member_role = n[2];
207 old_relation = OldRelation.from_relation(self)
208 old_relation.timestamp = t
209 old_relation.save_with_dependencies!
213 def preconditions_ok?
214 # These are hastables that store an id in the index of all
215 # the nodes/way/relations that have already been added.
216 # Once we know the id of the node/way/relation exists
217 # we check to see if it is already existing in the hashtable
218 # if it does, then we return false. Otherwise
219 # we add it to the relevant hash table, with the value true..
220 # Thus if you have nodes with the ids of 50 and 1 already in the
221 # relation, then the hash table nodes would contain:
222 # => {50=>true, 1=>true}
226 self.members.each do |m|
228 n = Node.find(:first, :conditions => ["id = ?", m[1]])
229 unless n and n.visible
237 elsif (m[0] == "way")
238 w = Way.find(:first, :conditions => ["id = ?", m[1]])
239 unless w and w.visible and w.preconditions_ok?
247 elsif (m[0] == "relation")
248 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
249 unless e and e.visible and e.preconditions_ok?
255 relations[m[1]] = true
266 # Temporary method to match interface to nodes