]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
Updating the cycle map to use all zoom levels as per Andy Allan's message.
[rails.git] / app / models / old_way.rb
1 class OldWay < ActiveRecord::Base
2   set_table_name 'ways'
3
4   belongs_to :user
5
6   def self.from_way(way)
7     old_way = OldWay.new
8     old_way.visible = way.visible
9     old_way.user_id = way.user_id
10     old_way.timestamp = way.timestamp
11     old_way.id = way.id
12     old_way.nds = way.nds
13     old_way.tags = way.tags
14     return old_way
15   end
16
17   def save_with_dependencies!
18
19     # dont touch this unless you really have figured out why it's called
20     # (Rails doesn't deal well with the old ways table (called 'ways') because
21     # it doesn't have a unique key. It knows how to insert and auto_increment
22     # id and get it back but we have that and we want to get the 'version' back
23     # we could add another column but thats a lot of data. No, set_primary_key
24     # doesn't work either.
25     save!
26     clear_aggregation_cache
27     clear_association_cache
28     @attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
29
30     # ok, you can touch from here on
31
32     self.tags.each do |k,v|
33       tag = OldWayTag.new
34       tag.k = k
35       tag.v = v
36       tag.id = self.id
37       tag.version = self.version
38       tag.save!
39     end
40
41     sequence = 1
42     self.nds.each do |n|
43       nd = OldWayNode.new
44       nd.id = [self.id, self.version, sequence]
45       nd.node_id = n
46       nd.save!
47       sequence += 1
48     end
49   end
50
51   def nds
52     unless @nds
53         @nds = Array.new
54         OldWayNode.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |nd|
55             @nds += [nd.node_id]
56         end
57     end
58     @nds
59   end
60
61   def tags
62     unless @tags
63         @tags = Hash.new
64         OldWayTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
65             @tags[tag.k] = tag.v
66         end
67     end
68     @tags = Hash.new unless @tags
69     @tags
70   end
71
72   def nds=(s)
73     @nds = s
74   end
75
76   def tags=(t)
77     @tags = t
78   end
79
80 #  has_many :way_nodes, :class_name => 'OldWayNode', :foreign_key => 'id'
81 #  has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
82
83   def old_nodes
84     OldWayNode.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
85   end
86
87   def old_tags
88     OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
89   end
90
91   def to_xml_node
92     el1 = XML::Node.new 'way'
93     el1['id'] = self.id.to_s
94     el1['visible'] = self.visible.to_s
95     el1['timestamp'] = self.timestamp.xmlschema
96     el1['user'] = self.user.display_name if self.user.data_public?
97     
98     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
99       e = XML::Node.new 'nd'
100       e['ref'] = nd.node_id.to_s
101       el1 << e
102     end
103  
104     self.old_tags.each do |tag|
105       e = XML::Node.new 'tag'
106       e['k'] = tag.k
107       e['v'] = tag.v
108       el1 << e
109     end
110     return el1
111   end
112
113   # Read full version of old way
114   # For get_nodes_undelete, uses same nodes, even if they've moved since
115   # For get_nodes_revert,   allocates new ids 
116   # Currently returns Potlatch-style array
117   
118   def get_nodes_undelete
119         points = []
120         self.nds.each do |n|
121           node=Node.find(n)
122           points << [node.lon, node.lat, n, node.visible ? 1 : 0, node.tags_as_hash]
123     end
124         points
125   end
126   
127   def get_nodes_revert
128     points=[]
129     self.nds.each do |n|
130       oldnode=OldNode.find(:first, :conditions=>['id=? AND timestamp<=?',n,self.timestamp], :order=>"timestamp DESC")
131       curnode=Node.find(n)
132       id=n; v=curnode.visible ? 1 : 0
133       if oldnode.lat!=curnode.lat or oldnode.lon!=curnode.lon or oldnode.tags!=curnode.tags then
134         # node has changed: if it's in other ways, give it a new id
135         if curnode.ways-[self.id] then id=-1; v=nil end
136       end
137       points << [oldnode.lon, oldnode.lat, id, v, oldnode.tags_as_hash]
138     end
139     points
140   end
141
142   # Temporary method to match interface to nodes
143   def tags_as_hash
144     return self.tags
145   end
146
147   # Temporary method to match interface to ways
148   def way_nodes
149     return self.old_nodes
150   end
151
152   # Pretend we're not in any relations
153   def containing_relation_members
154     return []
155   end
156 end