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 relation = Relation.new
21 doc.find('//osm/relation').each do |pt|
22 if !create and pt['id'] != '0'
23 relation.id = pt['id'].to_i
27 relation.timestamp = Time.now
28 relation.visible = true
31 relation.timestamp = Time.parse(pt['timestamp'])
35 pt.find('tag').each do |tag|
36 relation.add_tag_keyval(tag['k'], tag['v'])
39 pt.find('member').each do |member|
40 relation.add_member(member['type'], member['ref'], member['role'])
51 doc = OSM::API.new.get_xml_doc
52 doc.root << to_xml_node()
56 def to_xml_node(user_display_name_cache = nil)
57 el1 = XML::Node.new 'relation'
58 el1['id'] = self.id.to_s
59 el1['visible'] = self.visible.to_s
60 el1['timestamp'] = self.timestamp.xmlschema
62 user_display_name_cache = {} if user_display_name_cache.nil?
64 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
65 # use the cache if available
66 elsif self.user.data_public?
67 user_display_name_cache[self.user_id] = self.user.display_name
69 user_display_name_cache[self.user_id] = nil
72 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
74 self.relation_members.each do |member|
77 # # if there is a list of visible members then use that to weed out deleted segments
78 # if visible_members[member.member_type][member.member_id]
82 # otherwise, manually go to the db to check things
83 if member.member.visible?
88 e = XML::Node.new 'member'
89 e['type'] = member.member_type
90 e['ref'] = member.member_id.to_s
91 e['role'] = member.member_role
96 self.relation_tags.each do |tag|
97 e = XML::Node.new 'tag'
106 # collect relationships. currently done in one big block at the end;
107 # may need to move this upwards if people want automatic completion of
108 # relationships, i.e. deliver referenced objects like we do with ways...
109 # FIXME: rip out the fucking SQL
110 def self.find_for_nodes_and_ways(node_ids, way_ids)
113 if node_ids.length > 0
114 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
116 "em.id = e.id and em.member_type='node' and em.member_id in (#{node_ids.join(',')})")
118 if way_ids.length > 0
119 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
121 "em.id = e.id and em.member_type='way' and em.member_id in (#{way_ids.join(',')})")
124 relations # if you don't do this then it returns nil and not []
128 # FIXME is this really needed?
132 self.relation_members.each do |member|
133 @members += [[member.member_type,member.member_id,member.member_role]]
142 self.relation_tags.each do |tag|
157 def add_member(type,id,role)
158 @members = Array.new unless @members
159 @members += [[type,id,role]]
162 def add_tag_keyval(k, v)
163 @tags = Hash.new unless @tags
167 def save_with_history!
168 Relation.transaction do
175 RelationTag.delete_all(['id = ?', self.id])
178 tag = RelationTag.new
185 members = self.members
187 RelationMember.delete_all(['id = ?', self.id])
190 mem = RelationMember.new
192 mem.member_type = n[0];
193 mem.member_id = n[1];
194 mem.member_role = n[2];
198 old_relation = OldRelation.from_relation(self)
199 old_relation.timestamp = t
200 old_relation.save_with_dependencies!
204 def preconditions_ok?
205 # These are hastables that store an id in the index of all
206 # the nodes/way/relations that have already been added.
207 # Once we know the id of the node/way/relation exists
208 # we check to see if it is already existing in the hashtable
209 # if it does, then we return false. Otherwise
210 # we add it to the relevant hash table, with the value true..
211 # Thus if you have nodes with the ids of 50 and 1 already in the
212 # relation, then the hash table nodes would contain:
213 # => {50=>true, 1=>true}
217 self.members.each do |m|
219 n = Node.find(:first, :conditions => ["id = ?", m[1]])
220 unless n and n.visible
228 elsif (m[0] == "way")
229 w = Way.find(:first, :conditions => ["id = ?", m[1]])
230 unless w and w.visible and w.preconditions_ok?
238 elsif (m[0] == "relation")
239 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
240 unless e and e.visible and e.preconditions_ok?
246 relations[m[1]] = true