]> git.openstreetmap.org Git - rails.git/blob - app/models/way.rb
Added a bunch more tests on the API 0.6. Fixed node/way/relation from_xml code to...
[rails.git] / app / models / way.rb
1 class Way < ActiveRecord::Base
2   require 'xml/libxml'
3   
4   include ConsistencyValidations
5
6   set_table_name 'current_ways'
7
8   validates_presence_of :changeset_id, :timestamp
9   validates_inclusion_of :visible, :in => [ true, false ]
10   
11   belongs_to :changeset
12
13   has_many :old_ways, :foreign_key => 'id', :order => 'version'
14
15   has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
16   has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
17
18   has_many :way_tags, :foreign_key => 'id'
19
20   has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
21   has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
22
23   def self.from_xml(xml, create=false)
24     begin
25       p = XML::Parser.new
26       p.string = xml
27       doc = p.parse
28
29       doc.find('//osm/way').each do |pt|
30         return Way.from_xml_node(pt, create)
31       end
32     rescue
33       return nil
34     end
35   end
36
37   def self.from_xml_node(pt, create=false)
38     way = Way.new
39
40     if !create and pt['id'] != '0'
41       way.id = pt['id'].to_i
42     end
43     
44     way.version = pt['version']
45     way.changeset_id = pt['changeset']
46
47     if create
48       way.timestamp = Time.now
49       way.visible = true
50     else
51       if pt['timestamp']
52         way.timestamp = Time.parse(pt['timestamp'])
53       end
54     end
55
56     pt.find('tag').each do |tag|
57       way.add_tag_keyval(tag['k'], tag['v'])
58     end
59
60     pt.find('nd').each do |nd|
61       way.add_nd_num(nd['ref'])
62     end
63
64     return way
65   end
66
67   # Find a way given it's ID, and in a single SQL call also grab its nodes
68   #
69   
70   # 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
71   def self.find_eager(id)
72     way = Way.find(id, :include => {:way_nodes => :node})
73     #If waytag had a multipart key that was real, you could do this:
74     #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
75   end
76
77   # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
78   def to_xml
79     doc = OSM::API.new.get_xml_doc
80     doc.root << to_xml_node()
81     return doc
82   end
83
84   def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
85     el1 = XML::Node.new 'way'
86     el1['id'] = self.id.to_s
87     el1['visible'] = self.visible.to_s
88     el1['timestamp'] = self.timestamp.xmlschema
89     el1['version'] = self.version.to_s
90     el1['changeset'] = self.changeset_id.to_s
91
92     user_display_name_cache = {} if user_display_name_cache.nil?
93
94     if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
95       # use the cache if available
96     elsif self.changeset.user.data_public?
97       user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
98     else
99       user_display_name_cache[self.changeset.user_id] = nil
100     end
101
102     el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil?
103
104     # make sure nodes are output in sequence_id order
105     ordered_nodes = []
106     self.way_nodes.each do |nd|
107       if visible_nodes
108         # if there is a list of visible nodes then use that to weed out deleted nodes
109         if visible_nodes[nd.node_id]
110           ordered_nodes[nd.sequence_id] = nd.node_id.to_s
111         end
112       else
113         # otherwise, manually go to the db to check things
114         if nd.node.visible? and nd.node.visible?
115           ordered_nodes[nd.sequence_id] = nd.node_id.to_s
116         end
117       end
118     end
119
120     ordered_nodes.each do |nd_id|
121       if nd_id and nd_id != '0'
122         e = XML::Node.new 'nd'
123         e['ref'] = nd_id
124         el1 << e
125       end
126     end
127
128     self.way_tags.each do |tag|
129       e = XML::Node.new 'tag'
130       e['k'] = tag.k
131       e['v'] = tag.v
132       el1 << e
133     end
134     return el1
135   end 
136
137   def nds
138     unless @nds
139       @nds = Array.new
140       self.way_nodes.each do |nd|
141         @nds += [nd.node_id]
142       end
143     end
144     @nds
145   end
146
147   def tags
148     unless @tags
149       @tags = {}
150       self.way_tags.each do |tag|
151         @tags[tag.k] = tag.v
152       end
153     end
154     @tags
155   end
156
157   def nds=(s)
158     @nds = s
159   end
160
161   def tags=(t)
162     @tags = t
163   end
164
165   def add_nd_num(n)
166     @nds = Array.new unless @nds
167     @nds << n.to_i
168   end
169
170   def add_tag_keyval(k, v)
171     @tags = Hash.new unless @tags
172
173     # duplicate tags are now forbidden, so we can't allow values
174     # in the hash to be overwritten.
175     raise OSM::APIDuplicateTagsError.new if @tags.include? k
176
177     @tags[k] = v
178   end
179
180   def save_with_history!
181     t = Time.now
182
183     Way.transaction do
184       self.version += 1
185       self.timestamp = t
186       self.save!
187
188       tags = self.tags
189       WayTag.delete_all(['id = ?', self.id])
190       tags.each do |k,v|
191         tag = WayTag.new
192         tag.k = k
193         tag.v = v
194         tag.id = self.id
195         tag.save!
196       end
197
198       nds = self.nds
199       WayNode.delete_all(['id = ?', self.id])
200       sequence = 1
201       nds.each do |n|
202         nd = WayNode.new
203         nd.id = [self.id, sequence]
204         nd.node_id = n
205         nd.save!
206         sequence += 1
207       end
208
209       old_way = OldWay.from_way(self)
210       old_way.timestamp = t
211       old_way.save_with_dependencies!
212     end
213   end
214
215   def update_from(new_way, user)
216     check_consistency(self, new_way, user)
217     if !new_way.preconditions_ok?
218       raise OSM::APIPreconditionFailedError.new
219     end
220     self.changeset_id = new_way.changeset_id
221     self.tags = new_way.tags
222     self.nds = new_way.nds
223     self.visible = true
224     save_with_history!
225   end
226
227   def create_with_history(user)
228     check_create_consistency(self, user)
229     if !self.preconditions_ok?
230       raise OSM::APIPreconditionFailedError.new
231     end
232     self.version = 0
233     self.visible = true
234     save_with_history!
235   end
236
237   def preconditions_ok?
238     return false if self.nds.empty?
239     self.nds.each do |n|
240       node = Node.find(:first, :conditions => ["id = ?", n])
241       unless node and node.visible
242         return false
243       end
244     end
245     return true
246   end
247
248   def delete_with_history(new_way, user)
249     check_consistency(self, new_way, user)
250     if self.visible
251       # FIXME
252       # this should actually delete the relations,
253       # not just throw a PreconditionFailed if it's a member of a relation!!
254       # WHY?? The editor should decide whether the node is in the relation or not!
255
256       # FIXME: this should probably renamed to delete_with_history
257       if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
258                              :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
259         raise OSM::APIPreconditionFailedError
260       # end FIXME
261       else
262         self.changeset_id = new_way.changeset_id
263         self.tags = []
264         self.nds = []
265         self.visible = false
266         self.save_with_history!
267       end
268     else
269       raise OSM::APIAlreadyDeletedError
270     end
271   end
272
273   # delete a way and it's nodes that aren't part of other ways, with history
274
275   # FIXME: merge the potlatch code to delete the relations
276   def delete_with_relations_and_nodes_and_history(user)
277     # delete the nodes not used by other ways
278     self.unshared_node_ids.each do |node_id|
279       n = Node.find(node_id)
280       n.user_id = user.id
281       n.visible = false
282       n.save_with_history!
283     end
284     
285     # FIXME needs more information passed in so that the changeset can be updated
286     self.user_id = user.id
287
288     self.delete_with_history(user)
289   end
290
291   # Find nodes that belong to this way only
292   def unshared_node_ids
293     node_ids = self.nodes.collect { |node| node.id }
294
295     unless node_ids.empty?
296       way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
297       node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id }
298     end
299
300     return node_ids
301   end
302
303   # Temporary method to match interface to nodes
304   def tags_as_hash
305     return self.tags
306   end
307 end