1 class Way < ActiveRecord::Base
6 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
7 has_many :way_tags, :foreign_key => 'id'
9 has_many :old_ways, :foreign_key => 'id', :order => 'version'
11 set_table_name 'current_ways'
13 def self.from_xml(xml, create=false)
21 doc.find('//osm/way').each do |pt|
22 if !create and pt['id'] != '0'
23 way.id = pt['id'].to_i
27 way.timestamp = Time.now
31 way.timestamp = Time.parse(pt['timestamp'])
35 pt.find('tag').each do |tag|
36 way.add_tag_keyval(tag['k'], tag['v'])
39 pt.find('nd').each do |nd|
40 way.add_nd_num(nd['id'])
51 doc = OSM::API.new.get_xml_doc
52 doc.root << to_xml_node()
56 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
57 el1 = XML::Node.new 'way'
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 # make sure nodes are output in sequence_id order
76 self.way_nodes.each do |nd|
78 # if there is a list of visible nodes then use that to weed out deleted nodes
79 if visible_nodes[nd.node_id]
80 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
83 # otherwise, manually go to the db to check things
84 if nd.node.visible? and nd.node.visible?
85 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
90 ordered_nodes.each do |nd_id|
91 if nd_id and nd_id != '0'
92 e = XML::Node.new 'nd'
98 self.way_tags.each do |tag|
99 e = XML::Node.new 'tag'
110 self.way_nodes.each do |nd|
120 self.way_tags.each do |tag|
136 @nds = Array.new unless @nds
140 def add_tag_keyval(k, v)
141 @tags = Hash.new unless @tags
145 def save_with_history
154 WayTag.delete_all(['id = ?', self.id])
166 WayNode.delete_all(['id = ?', self.id])
178 old_way = OldWay.from_way(self)
179 old_way.timestamp = t
180 old_way.save_with_dependencies!
190 def preconditions_ok?
191 return false if self.nds.empty?
193 node = Node.find(:first, :conditions => ["id = ?", n])
194 unless node and node.visible