]> git.openstreetmap.org Git - rails.git/blob - app/models/way.rb
The OSM layers use the 900913 projection, not 41001.
[rails.git] / app / models / way.rb
1 class Way < ActiveRecord::Base
2   require 'xml/libxml'
3
4   belongs_to :user
5
6   has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
7   has_many :way_tags, :foreign_key => 'id'
8
9   has_many :old_ways, :foreign_key => 'id', :order => 'version'
10
11   set_table_name 'current_ways'
12
13   def self.from_xml(xml, create=false)
14     begin
15       p = XML::Parser.new
16       p.string = xml
17       doc = p.parse
18
19       way = Way.new
20
21       doc.find('//osm/way').each do |pt|
22         if !create and pt['id'] != '0'
23           way.id = pt['id'].to_i
24         end
25
26         if create
27           way.timestamp = Time.now
28           way.visible = true
29         else
30           if pt['timestamp']
31             way.timestamp = Time.parse(pt['timestamp'])
32           end
33         end
34
35         pt.find('tag').each do |tag|
36           way.add_tag_keyval(tag['k'], tag['v'])
37         end
38
39         pt.find('nd').each do |nd|
40           way.add_nd_num(nd['ref'])
41         end
42       end
43     rescue
44       way = nil
45     end
46
47     return way
48   end
49
50   # Find a way given it's ID, and in a single SQL call also grab its nodes
51   #
52   
53   # You can't pull in all the tags too unless we put a sequence_id on the way_tags table and have a multipart key
54   def self.find_eager(id)
55     way = Way.find(id, :include => {:way_nodes => :node})
56     #If waytag had a multipart key that was real, you could do this:
57     #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
58   end
59
60   # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
61   def to_xml
62     doc = OSM::API.new.get_xml_doc
63     doc.root << to_xml_node()
64     return doc
65   end
66
67   def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
68     el1 = XML::Node.new 'way'
69     el1['id'] = self.id.to_s
70     el1['visible'] = self.visible.to_s
71     el1['timestamp'] = self.timestamp.xmlschema
72
73     user_display_name_cache = {} if user_display_name_cache.nil?
74
75     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
76       # use the cache if available
77     elsif self.user.data_public?
78       user_display_name_cache[self.user_id] = self.user.display_name
79     else
80       user_display_name_cache[self.user_id] = nil
81     end
82
83     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
84
85     # make sure nodes are output in sequence_id order
86     ordered_nodes = []
87     self.way_nodes.each do |nd|
88       if visible_nodes
89         # if there is a list of visible nodes then use that to weed out deleted nodes
90         if visible_nodes[nd.node_id]
91           ordered_nodes[nd.sequence_id] = nd.node_id.to_s
92         end
93       else
94         # otherwise, manually go to the db to check things
95         if nd.node.visible? and nd.node.visible?
96           ordered_nodes[nd.sequence_id] = nd.node_id.to_s
97         end
98       end
99     end
100
101     ordered_nodes.each do |nd_id|
102       if nd_id and nd_id != '0'
103         e = XML::Node.new 'nd'
104         e['ref'] = nd_id
105         el1 << e
106       end
107     end
108
109     self.way_tags.each do |tag|
110       e = XML::Node.new 'tag'
111       e['k'] = tag.k
112       e['v'] = tag.v
113       el1 << e
114     end
115     return el1
116   end 
117
118   def nds
119     unless @nds
120       @nds = Array.new
121       self.way_nodes.each do |nd|
122         @nds += [nd.node_id]
123       end
124     end
125     @nds
126   end
127
128   def tags
129     unless @tags
130       @tags = {}
131       self.way_tags.each do |tag|
132         @tags[tag.k] = tag.v
133       end
134     end
135     @tags
136   end
137
138   def nds=(s)
139     @nds = s
140   end
141
142   def tags=(t)
143     @tags = t
144   end
145
146   def add_nd_num(n)
147     @nds = Array.new unless @nds
148     @nds << n.to_i
149   end
150
151   def add_tag_keyval(k, v)
152     @tags = Hash.new unless @tags
153     @tags[k] = v
154   end
155
156   def save_with_history!
157     t = Time.now
158
159     Way.transaction do
160       self.timestamp = t
161       self.save!
162     end
163
164     WayTag.transaction do
165       tags = self.tags
166
167       WayTag.delete_all(['id = ?', self.id])
168
169       tags.each do |k,v|
170         tag = WayTag.new
171         tag.k = k
172         tag.v = v
173         tag.id = self.id
174         tag.save!
175       end
176     end
177
178     WayNode.transaction do
179       nds = self.nds
180
181       WayNode.delete_all(['id = ?', self.id])
182
183       sequence = 1
184       nds.each do |n|
185         nd = WayNode.new
186         nd.id = [self.id, sequence]
187         nd.node_id = n
188         nd.save!
189         sequence += 1
190       end
191     end
192
193     old_way = OldWay.from_way(self)
194     old_way.timestamp = t
195     old_way.save_with_dependencies!
196   end
197
198   def preconditions_ok?
199     return false if self.nds.empty?
200     self.nds.each do |n|
201       node = Node.find(:first, :conditions => ["id = ?", n])
202       unless node and node.visible
203         return false
204       end
205     end
206     return true
207   end
208
209   # Delete the way and it's relations, but don't really delete it - set its visibility to false and update the history etc to maintain wiki-like functionality.
210   def delete_with_relations_and_history(user)
211     if self.visible
212       # omg FIXME
213       if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
214                              :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
215         raise OSM::APIPreconditionFailedError
216       else
217         self.user_id = user.id
218         self.tags = []
219         self.nds = []
220         self.visible = false
221         self.save_with_history!
222       end
223     else
224       raise OSM::APIAlreadyDeletedError
225     end
226   end
227
228   # delete a way and it's nodes that aren't part of other ways, with history
229   # WARNING, INCOMPLETE - Read the code
230   def delete_with_relations_and_nodes_and_history(user)
231     return false # until complete, just return false
232     
233     node_ids = self.nodes.collect {|node| node.id }
234     node_ids_not_to_delete = []
235     way_nodes = WayNode.find(:all, :conditions => "node_id in [#{node_ids.join(',')}] and id not #{self.id}")
236     
237     node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id}
238
239     nodes_to_delete = node_ids - node_ids_not_to_delete
240
241     # update the visibility etc on the current nodes
242     update_time = Time.now()
243     
244     Node.update(node_ids_to_delete, {:user_id => user.id, :timestamp => update_time, :visibility => false})
245
246     # create old nodes
247
248     old_nodes_to_create = []
249     OldNode.find(node_ids_to_delete).each do |old_node|
250       old_nodes_to_create << {:id => old_node.id, :timestamp => update_time, :latitude => old_node.latitude, :longitude => old_node.longitude, :visible => false}
251     end
252
253     OldNode.create(old_nodes_to_create)
254
255     # FIXME - the old nodes need to have their tile updated and I have no idea how that works
256     # they also need their tags copied over, and that depends on normalising the tags out to their own table that nicks working on
257
258     # finally, delete the way
259     
260     self.delete_with_relations_and_history
261
262   end
263 end