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