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 = XML::Document.new
49 doc.encoding = 'UTF-8'
50 root = XML::Node.new 'osm'
51 root['version'] = '0.4'
52 root['generator'] = 'OpenStreetMap server'
61 el1 = XML::Node.new 'way'
62 el1['id'] = self.id.to_s
63 el1['visible'] = self.visible.to_s
64 el1['timestamp'] = self.timestamp.xmlschema
66 # make sure segments are output in sequence_id order
\r
67 ordered_segments = []
\r
68 self.way_segments.each do |seg|
\r
69 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
71 ordered_segments.each do |seg_id|
\r
72 e = XML::Node.new 'seg'
\r
77 self.way_tags.each do |tag|
78 e = XML::Node.new 'tag'
88 @segs = Array.new unless @segs
93 @tags = Hash.new unless @tags
106 @segs = Array.new unless @segs
110 def add_tag_keyval(k, v)
111 @tags = Hash.new unless @tags
115 def save_with_history
120 WayTag.delete_all(['id = ?', self.id])
122 self.tags.each do |k,v|
130 WaySegment.delete_all(['id = ?', self.id])
133 self.segs.each do |n|
142 old_way = OldWay.from_way(self)
143 old_way.timestamp = t
144 old_way.save_with_dependencies
147 def preconditions_ok?
148 self.segs.each do |n|
149 segment = Segment.find(n)
150 unless segment and segment.visible and segment.preconditions_ok?