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