1 class Relation < ActiveRecord::Base
6 has_many :relation_members, :foreign_key => 'id'
7 has_many :relation_tags, :foreign_key => 'id'
9 has_many :old_relations, :foreign_key => 'id', :order => 'version'
11 set_table_name 'current_relations'
13 def self.from_xml(xml, create=false)
19 doc.find('//osm/relation').each do |pt|
20 relation = Relation.from_xml_node pt, create
29 def self.from_xml_node(pt, create=false)
30 relation = Relation.new
32 if !create and pt['id'] != '0'
33 relation.id = pt['id'].to_i
37 relation.timestamp = Time.now
38 relation.visible = true
41 relation.timestamp = Time.parse(pt['timestamp'])
45 pt.find('tag').each do |tag|
46 relation.add_tag_keyval(tag['k'], tag['v'])
49 pt.find('member').each do |member|
50 relation.add_member(member['type'], member['ref'], member['role'])
57 doc = OSM::API.new.get_xml_doc
58 doc.root << to_xml_node()
62 def to_xml_node(user_display_name_cache = nil)
63 el1 = XML::Node.new 'relation'
64 el1['id'] = self.id.to_s
65 el1['visible'] = self.visible.to_s
66 el1['timestamp'] = self.timestamp.xmlschema
67 el1['version'] = self.version.to_s
69 user_display_name_cache = {} if user_display_name_cache.nil?
71 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
72 # use the cache if available
73 elsif self.user.data_public?
74 user_display_name_cache[self.user_id] = self.user.display_name
76 user_display_name_cache[self.user_id] = nil
79 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
81 self.relation_members.each do |member|
84 # # if there is a list of visible members then use that to weed out deleted segments
85 # if visible_members[member.member_type][member.member_id]
89 # otherwise, manually go to the db to check things
90 if member.member.visible?
95 e = XML::Node.new 'member'
96 e['type'] = member.member_type
97 e['ref'] = member.member_id.to_s
98 e['role'] = member.member_role
103 self.relation_tags.each do |tag|
104 e = XML::Node.new 'tag'
113 # collect relationships. currently done in one big block at the end;
114 # may need to move this upwards if people want automatic completion of
115 # relationships, i.e. deliver referenced objects like we do with ways...
116 # FIXME: rip out the fucking SQL
117 def self.find_for_nodes_and_ways(node_ids, way_ids)
120 if node_ids.length > 0
121 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
123 "em.id = e.id and em.member_type='node' and em.member_id in (#{node_ids.join(',')})")
125 if way_ids.length > 0
126 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
128 "em.id = e.id and em.member_type='way' and em.member_id in (#{way_ids.join(',')})")
131 relations # if you don't do this then it returns nil and not []
135 # FIXME is this really needed?
139 self.relation_members.each do |member|
140 @members += [[member.member_type,member.member_id,member.member_role]]
149 self.relation_tags.each do |tag|
164 def add_member(type,id,role)
165 @members = Array.new unless @members
166 @members += [[type,id,role]]
169 def add_tag_keyval(k, v)
170 @tags = Hash.new unless @tags
174 def save_with_history!
175 Relation.transaction do
182 RelationTag.delete_all(['id = ?', self.id])
184 tag = RelationTag.new
191 members = self.members
192 RelationMember.delete_all(['id = ?', self.id])
194 mem = RelationMember.new
196 mem.member_type = n[0];
197 mem.member_id = n[1];
198 mem.member_role = n[2];
202 old_relation = OldRelation.from_relation(self)
203 old_relation.timestamp = t
204 old_relation.save_with_dependencies!
208 def preconditions_ok?
209 self.members.each do |m|
211 n = Node.find(:first, :conditions => ["id = ?", m[1]])
212 unless n and n.visible
215 elsif (m[0] == "way")
216 w = Way.find(:first, :conditions => ["id = ?", m[1]])
217 unless w and w.visible and w.preconditions_ok?
220 elsif (m[0] == "relation")
221 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
222 unless e and e.visible and e.preconditions_ok?