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, :as => :member
14 has_many :containing_relations, :through => :containing_relation_members
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'
109 # collect relationships. currently done in one big block at the end;
110 # may need to move this upwards if people want automatic completion of
111 # relationships, i.e. deliver referenced objects like we do with ways...
112 # FIXME: rip out the fucking SQL
113 def self.find_for_nodes_and_ways(node_ids, way_ids)
116 if node_ids.length > 0
117 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
119 "em.id = e.id and em.member_type='node' and em.member_id in (#{node_ids.join(',')})")
121 if way_ids.length > 0
122 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
124 "em.id = e.id and em.member_type='way' and em.member_id in (#{way_ids.join(',')})")
127 relations # if you don't do this then it returns nil and not []
131 # FIXME is this really needed?
135 self.relation_members.each do |member|
136 @members += [[member.member_type,member.member_id,member.member_role]]
145 self.relation_tags.each do |tag|
160 def add_member(type,id,role)
161 @members = Array.new unless @members
162 @members += [[type,id,role]]
165 def add_tag_keyval(k, v)
166 @tags = Hash.new unless @tags
170 def save_with_history!
171 Relation.transaction do
178 RelationTag.delete_all(['id = ?', self.id])
181 tag = RelationTag.new
188 members = self.members
190 RelationMember.delete_all(['id = ?', self.id])
193 mem = RelationMember.new
195 mem.member_type = n[0];
196 mem.member_id = n[1];
197 mem.member_role = n[2];
201 old_relation = OldRelation.from_relation(self)
202 old_relation.timestamp = t
203 old_relation.save_with_dependencies!
207 def preconditions_ok?
208 # These are hastables that store an id in the index of all
209 # the nodes/way/relations that have already been added.
210 # Once we know the id of the node/way/relation exists
211 # we check to see if it is already existing in the hashtable
212 # if it does, then we return false. Otherwise
213 # we add it to the relevant hash table, with the value true..
214 # Thus if you have nodes with the ids of 50 and 1 already in the
215 # relation, then the hash table nodes would contain:
216 # => {50=>true, 1=>true}
220 self.members.each do |m|
222 n = Node.find(:first, :conditions => ["id = ?", m[1]])
223 unless n and n.visible
231 elsif (m[0] == "way")
232 w = Way.find(:first, :conditions => ["id = ?", m[1]])
233 unless w and w.visible and w.preconditions_ok?
241 elsif (m[0] == "relation")
242 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
243 unless e and e.visible and e.preconditions_ok?
249 relations[m[1]] = true