1 class Way < ActiveRecord::Base
6 has_many :way_segments, :foreign_key => 'id'
7 has_many :way_tags, :foreign_key => 'id'
9 has_many :old_ways, :foreign_key => :id
11 set_table_name 'current_ways'
13 def self.from_xml(xml, create=false)
20 doc.find('//osm/way').each do |pt|
21 if !create and pt['id'] != '0'
22 way.id = pt['id'].to_i
26 way.timestamp = Time.now
30 way.timestamp = Time.parse(pt['timestamp'])
34 pt.find('tag').each do |tag|
35 way.add_tag_keyval(tag['k'], tag['v'])
38 pt.find('seg').each do |seg|
39 way.add_seg_num(seg['id'])
48 doc = OSM::API.new.get_xml_doc
49 doc.root << to_xml_node()
53 def to_xml_node(visible_segments = nil, user_display_name_cache = nil)
54 el1 = XML::Node.new 'way'
55 el1['id'] = self.id.to_s
56 el1['visible'] = self.visible.to_s
57 el1['timestamp'] = self.timestamp.xmlschema
59 if user_display_name_cache and user_display_name_cache[self.user_id]
60 # use the cache if available
62 user_display_name_cache[self.user_id] = self.user.display_name
65 #el1['user'] = self.user.display_name if self.user.data_public?
66 el1['user'] = user_display_name_cache[self.user_id]
68 # make sure segments are output in sequence_id order
70 self.way_segments.each do |seg|
72 # if there is a list of visible segments then use that to weed out deleted segments
73 if visible_segments[seg.segment_id]
74 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
77 # otherwise, manually go to the db to check things
78 if seg.segment.visible? and seg.segment.from_node.visible? and seg.segment.to_node.visible?
79 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
84 ordered_segments.each do |seg_id|
86 e = XML::Node.new 'seg'
92 self.way_tags.each do |tag|
93 e = XML::Node.new 'tag'
104 self.way_segments.each do |seg|
105 @segs += [seg.segment_id]
114 self.way_tags.each do |tag|
130 @segs = Array.new unless @segs
134 def add_tag_keyval(k, v)
135 @tags = Hash.new unless @tags
139 def save_with_history
144 WayTag.delete_all(['id = ?', self.id])
146 self.tags.each do |k,v|
154 WaySegment.delete_all(['id = ?', self.id])
157 self.segs.each do |n|
166 old_way = OldWay.from_way(self)
167 old_way.timestamp = t
168 old_way.save_with_dependencies
171 def preconditions_ok?
172 self.segs.each do |n|
173 segment = Segment.find(n)
174 unless segment and segment.visible and segment.preconditions_ok?