1 class Relation < ActiveRecord::Base
4 include ConsistencyValidations
6 set_table_name 'current_relations'
10 has_many :old_relations, :foreign_key => 'id', :order => 'version'
12 has_many :relation_members, :foreign_key => 'id'
13 has_many :relation_tags, :foreign_key => 'id'
15 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
16 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
18 def self.from_xml(xml, create=false)
24 doc.find('//osm/relation').each do |pt|
25 return Relation.from_xml_node(pt, create)
32 def self.from_xml_node(pt, create=false)
33 relation = Relation.new
35 if !create and pt['id'] != '0'
36 relation.id = pt['id'].to_i
39 relation.version = pt['version']
40 relation.changeset_id = pt['changeset']
43 relation.timestamp = Time.now
44 relation.visible = true
47 relation.timestamp = Time.parse(pt['timestamp'])
51 pt.find('tag').each do |tag|
52 relation.add_tag_keyval(tag['k'], tag['v'])
55 pt.find('member').each do |member|
56 relation.add_member(member['type'], member['ref'], member['role'])
63 doc = OSM::API.new.get_xml_doc
64 doc.root << to_xml_node()
68 def to_xml_node(user_display_name_cache = nil)
69 el1 = XML::Node.new 'relation'
70 el1['id'] = self.id.to_s
71 el1['visible'] = self.visible.to_s
72 el1['timestamp'] = self.timestamp.xmlschema
73 el1['version'] = self.version.to_s
74 el1['changeset'] = self.changeset_id.to_s
76 user_display_name_cache = {} if user_display_name_cache.nil?
78 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
79 # use the cache if available
80 elsif self.changeset.user.data_public?
81 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
83 user_display_name_cache[self.changeset.user_id] = nil
86 el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil?
88 self.relation_members.each do |member|
91 # # if there is a list of visible members then use that to weed out deleted segments
92 # if visible_members[member.member_type][member.member_id]
96 # otherwise, manually go to the db to check things
97 if member.member.visible?
102 e = XML::Node.new 'member'
103 e['type'] = member.member_type
104 e['ref'] = member.member_id.to_s
105 e['role'] = member.member_role
110 self.relation_tags.each do |tag|
111 e = XML::Node.new 'tag'
119 def self.find_for_nodes(ids, options = {})
123 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
124 return self.find(:all, options)
129 def self.find_for_ways(ids, options = {})
133 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
134 return self.find(:all, options)
139 def self.find_for_relations(ids, options = {})
143 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
144 return self.find(:all, options)
149 # FIXME is this really needed?
153 self.relation_members.each do |member|
154 @members += [[member.member_type,member.member_id,member.member_role]]
163 self.relation_tags.each do |tag|
178 def add_member(type,id,role)
179 @members = Array.new unless @members
180 @members += [[type,id,role]]
183 def add_tag_keyval(k, v)
184 @tags = Hash.new unless @tags
186 # duplicate tags are now forbidden, so we can't allow values
187 # in the hash to be overwritten.
188 raise OSM::APIDuplicateTagsError.new if @tags.include? k
193 def save_with_history!
194 Relation.transaction do
201 RelationTag.delete_all(['id = ?', self.id])
203 tag = RelationTag.new
210 members = self.members
211 RelationMember.delete_all(['id = ?', self.id])
213 mem = RelationMember.new
215 mem.member_type = n[0];
216 mem.member_id = n[1];
217 mem.member_role = n[2];
221 old_relation = OldRelation.from_relation(self)
222 old_relation.timestamp = t
223 old_relation.save_with_dependencies!
227 def delete_with_history!(new_relation, user)
229 check_consistency(self, new_relation, user)
230 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='relation' and member_id=? ", self.id ])
231 raise OSM::APIPreconditionFailedError.new
233 self.changeset_id = new_relation.changeset_id
240 raise OSM::APIAlreadyDeletedError.new
244 def update_from(new_relation, user)
245 check_consistency(self, new_relation, user)
246 if !new_relation.preconditions_ok?
247 raise OSM::APIPreconditionFailedError.new
249 self.changeset_id = new_relation.changeset_id
250 self.tags = new_relation.tags
251 self.members = new_relation.members
256 def create_with_history(user)
257 check_create_consistency(self, user)
258 if !self.preconditions_ok?
259 raise OSM::APIPreconditionFailedError.new
266 def preconditions_ok?
267 # These are hastables that store an id in the index of all
268 # the nodes/way/relations that have already been added.
269 # Once we know the id of the node/way/relation exists
270 # we check to see if it is already existing in the hashtable
271 # if it does, then we return false. Otherwise
272 # we add it to the relevant hash table, with the value true..
273 # Thus if you have nodes with the ids of 50 and 1 already in the
274 # relation, then the hash table nodes would contain:
275 # => {50=>true, 1=>true}
279 self.members.each do |m|
281 n = Node.find(:first, :conditions => ["id = ?", m[1]])
282 unless n and n.visible
290 elsif (m[0] == "way")
291 w = Way.find(:first, :conditions => ["id = ?", m[1]])
292 unless w and w.visible and w.preconditions_ok?
300 elsif (m[0] == "relation")
301 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
302 unless e and e.visible and e.preconditions_ok?
308 relations[m[1]] = true
319 # Temporary method to match interface to nodes