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