]> git.openstreetmap.org Git - rails.git/blob - app/models/changeset.rb
Adding some more unit test stubs that need filling out. Adding changeset tags fixture...
[rails.git] / app / models / changeset.rb
1 class Changeset < ActiveRecord::Base
2   require 'xml/libxml'
3
4   belongs_to :user
5
6   has_many :changeset_tags, :foreign_key => 'id'
7   
8   has_many :nodes
9   has_many :ways
10   has_many :relations
11   has_many :old_nodes
12   has_many :old_ways
13   has_many :old_relations
14   
15   validates_presence_of :user_id, :created_at
16   validates_inclusion_of :open, :in => [ true, false ]
17   
18   # over-expansion factor to use when updating the bounding box
19   EXPAND = 0.1
20
21   # Use a method like this, so that we can easily change how we
22   # determine whether a changeset is open, without breaking code in at 
23   # least 6 controllers
24   def is_open?
25     return open
26   end
27
28   def self.from_xml(xml, create=false)
29     begin
30       p = XML::Parser.new
31       p.string = xml
32       doc = p.parse
33
34       cs = Changeset.new
35
36       doc.find('//osm/changeset').each do |pt|
37         if create
38           cs.created_at = Time.now
39         end
40
41         pt.find('tag').each do |tag|
42           cs.add_tag_keyval(tag['k'], tag['v'])
43         end
44       end
45     rescue Exception => ex
46       cs = nil
47     end
48
49     return cs
50   end
51
52   ##
53   # returns the bounding box of the changeset. it is possible that some
54   # or all of the values will be nil, indicating that they are undefined.
55   def bbox
56     @bbox ||= [ min_lon, min_lat, max_lon, max_lat ]
57   end
58
59   ##
60   # expand the bounding box to include the given bounding box. also, 
61   # expand a little bit more in the direction of the expansion, so that
62   # further expansions may be unnecessary. this is an optimisation 
63   # suggested on the wiki page by kleptog.
64   def update_bbox!(array)
65     # ensure that bbox is cached and has no nils in it. if there are any
66     # nils, just use the bounding box update to write over them.
67     @bbox = bbox.zip(array).collect { |a, b| a.nil? ? b : a }
68
69     # FIXME - this looks nasty and violates DRY... is there any prettier 
70     # way to do this? 
71     @bbox[0] = array[0] + EXPAND * (@bbox[0] - @bbox[2]) if array[0] < @bbox[0]
72     @bbox[1] = array[1] + EXPAND * (@bbox[1] - @bbox[3]) if array[1] < @bbox[1]
73     @bbox[2] = array[2] + EXPAND * (@bbox[2] - @bbox[0]) if array[2] > @bbox[2]
74     @bbox[3] = array[3] + EXPAND * (@bbox[3] - @bbox[1]) if array[3] > @bbox[3]
75
76     # update active record. rails 2.1's dirty handling should take care of
77     # whether this object needs saving or not.
78     self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox
79   end
80
81   def tags_as_hash
82     return tags
83   end
84
85   def tags
86     unless @tags
87       @tags = {}
88       self.changeset_tags.each do |tag|
89         @tags[tag.k] = tag.v
90       end
91     end
92     @tags
93   end
94
95   def tags=(t)
96     @tags = t
97   end
98
99   def add_tag_keyval(k, v)
100     @tags = Hash.new unless @tags
101     @tags[k] = v
102   end
103
104   def save_with_tags!
105     t = Time.now
106
107     Changeset.transaction do
108       # fixme update modified_at time?
109       # FIXME there is no modified_at time, should it be added
110       self.save!
111     end
112
113     ChangesetTag.transaction do
114       tags = self.tags
115       ChangesetTag.delete_all(['id = ?', self.id])
116
117       tags.each do |k,v|
118         tag = ChangesetTag.new
119         tag.k = k
120         tag.v = v
121         tag.id = self.id
122         tag.save!
123       end
124     end
125   end
126   
127   def to_xml
128     doc = OSM::API.new.get_xml_doc
129     doc.root << to_xml_node()
130     return doc
131   end
132   
133   def to_xml_node(user_display_name_cache = nil)
134     el1 = XML::Node.new 'changeset'
135     el1['id'] = self.id.to_s
136
137     user_display_name_cache = {} if user_display_name_cache.nil?
138
139     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
140       # use the cache if available
141     elsif self.user.data_public?
142       user_display_name_cache[self.user_id] = self.user.display_name
143     else
144       user_display_name_cache[self.user_id] = nil
145     end
146
147     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
148     el1['uid'] = self.user_id.to_s if self.user.data_public?
149
150     self.tags.each do |k,v|
151       el2 = XML::Node.new('tag')
152       el2['k'] = k.to_s
153       el2['v'] = v.to_s
154       el1 << el2
155     end
156     
157     el1['created_at'] = self.created_at.xmlschema
158     el1['open'] = self.open.to_s
159
160     el1['min_lon'] = (bbox[0].to_f / GeoRecord::SCALE).to_s unless bbox[0].nil?
161     el1['min_lat'] = (bbox[1].to_f / GeoRecord::SCALE).to_s unless bbox[1].nil?
162     el1['max_lon'] = (bbox[2].to_f / GeoRecord::SCALE).to_s unless bbox[2].nil?
163     el1['max_lat'] = (bbox[3].to_f / GeoRecord::SCALE).to_s unless bbox[3].nil?
164     
165     # NOTE: changesets don't include the XML of the changes within them,
166     # they are just structures for tagging. to get the osmChange of a
167     # changeset, see the download method of the controller.
168
169     return el1
170   end
171 end