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 user_display_name_cache = {} if user_display_name_cache.nil?
61 if user_display_name_cache and user_display_name_cache[self.user_id]
62 # use the cache if available
64 user_display_name_cache[self.user_id] = self.user.display_name
67 #el1['user'] = self.user.display_name if self.user.data_public?
68 el1['user'] = user_display_name_cache[self.user_id]
70 # make sure segments are output in sequence_id order
72 self.way_segments.each do |seg|
74 # if there is a list of visible segments then use that to weed out deleted segments
75 if visible_segments[seg.segment_id]
76 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
79 # otherwise, manually go to the db to check things
80 if seg.segment.visible? and seg.segment.from_node.visible? and seg.segment.to_node.visible?
81 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
86 ordered_segments.each do |seg_id|
88 e = XML::Node.new 'seg'
94 self.way_tags.each do |tag|
95 e = XML::Node.new 'tag'
106 self.way_segments.each do |seg|
107 @segs += [seg.segment_id]
116 self.way_tags.each do |tag|
132 @segs = Array.new unless @segs
136 def add_tag_keyval(k, v)
137 @tags = Hash.new unless @tags
141 def save_with_history
146 WayTag.delete_all(['id = ?', self.id])
148 self.tags.each do |k,v|
156 WaySegment.delete_all(['id = ?', self.id])
159 self.segs.each do |n|
168 old_way = OldWay.from_way(self)
169 old_way.timestamp = t
170 old_way.save_with_dependencies
173 def preconditions_ok?
174 self.segs.each do |n|
175 segment = Segment.find(n)
176 unless segment and segment.visible and segment.preconditions_ok?