1 class Changeset < ActiveRecord::Base
6 has_many :changeset_tags, :foreign_key => 'id'
13 has_many :old_relations
15 validates_presence_of :id, :on => :update
16 validates_presence_of :user_id, :created_at, :closed_at, :num_changes
17 validates_uniqueness_of :id
18 validates_numericality_of :id, :on => :update, :integer_only => true
19 validates_numericality_of :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true, :integer_only => true
20 validates_numericality_of :user_id, :integer_only => true
21 validates_numericality_of :num_changes, :integer_only => true, :greater_than_or_equal_to => 0
22 validates_associated :user
24 # over-expansion factor to use when updating the bounding box
27 # maximum number of elements allowed in a changeset
30 # maximum time a changeset is allowed to be open for (note that this
31 # is in days - so one hour is Rational(1,24)).
34 # idle timeout increment, one hour as a rational number of days.
35 # NOTE: DO NOT CHANGE THIS TO 1.hour! when this was done the idle
36 # timeout changed to 1 second, which meant all changesets closed
38 IDLE_TIMEOUT = Rational(1,24)
40 # Use a method like this, so that we can easily change how we
41 # determine whether a changeset is open, without breaking code in at
44 # a changeset is open (that is, it will accept further changes) when
45 # it has not yet run out of time and its capacity is small enough.
46 # note that this may not be a hard limit - due to timing changes and
47 # concurrency it is possible that some changesets may be slightly
48 # longer than strictly allowed or have slightly more changes in them.
49 return ((closed_at > DateTime.now) and (num_changes <= MAX_ELEMENTS))
52 def set_closed_time_now
54 self.closed_at = DateTime.now
58 def self.from_xml(xml, create=false)
66 doc.find('//osm/changeset').each do |pt|
68 cs.created_at = Time.now
69 # initial close time is 1h ahead, but will be increased on each
71 cs.closed_at = Time.now + IDLE_TIMEOUT
72 # initially we have no changes in a changeset
76 pt.find('tag').each do |tag|
77 cs.add_tag_keyval(tag['k'], tag['v'])
80 rescue Exception => ex
88 # returns the bounding box of the changeset. it is possible that some
89 # or all of the values will be nil, indicating that they are undefined.
91 @bbox ||= [ min_lon, min_lat, max_lon, max_lat ]
95 # expand the bounding box to include the given bounding box. also,
96 # expand a little bit more in the direction of the expansion, so that
97 # further expansions may be unnecessary. this is an optimisation
98 # suggested on the wiki page by kleptog.
99 def update_bbox!(array)
100 # ensure that bbox is cached and has no nils in it. if there are any
101 # nils, just use the bounding box update to write over them.
102 @bbox = bbox.zip(array).collect { |a, b| a.nil? ? b : a }
104 # FIXME - this looks nasty and violates DRY... is there any prettier
106 @bbox[0] = array[0] + EXPAND * (@bbox[0] - @bbox[2]) if array[0] < @bbox[0]
107 @bbox[1] = array[1] + EXPAND * (@bbox[1] - @bbox[3]) if array[1] < @bbox[1]
108 @bbox[2] = array[2] + EXPAND * (@bbox[2] - @bbox[0]) if array[2] > @bbox[2]
109 @bbox[3] = array[3] + EXPAND * (@bbox[3] - @bbox[1]) if array[3] > @bbox[3]
111 # update active record. rails 2.1's dirty handling should take care of
112 # whether this object needs saving or not.
113 self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox
117 # the number of elements is also passed in so that we can ensure that
118 # a single changeset doesn't contain too many elements. this, of course,
119 # destroys the optimisation described in the bbox method above.
120 def add_changes!(elements)
121 self.num_changes += elements
131 self.changeset_tags.each do |tag|
142 def add_tag_keyval(k, v)
143 @tags = Hash.new unless @tags
150 # do the changeset update and the changeset tags update in the
151 # same transaction to ensure consistency.
152 Changeset.transaction do
153 # set the auto-close time to be one hour in the future unless
154 # that would make it more than 24h long, in which case clip to
155 # 24h, as this has been decided is a reasonable time limit.
156 if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
157 self.closed_at = created_at + MAX_TIME_OPEN
159 self.closed_at = DateTime.now + IDLE_TIMEOUT
164 ChangesetTag.delete_all(['id = ?', self.id])
167 tag = ChangesetTag.new
177 doc = OSM::API.new.get_xml_doc
178 doc.root << to_xml_node()
182 def to_xml_node(user_display_name_cache = nil)
183 el1 = XML::Node.new 'changeset'
184 el1['id'] = self.id.to_s
186 user_display_name_cache = {} if user_display_name_cache.nil?
188 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
189 # use the cache if available
190 elsif self.user.data_public?
191 user_display_name_cache[self.user_id] = self.user.display_name
193 user_display_name_cache[self.user_id] = nil
196 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
197 el1['uid'] = self.user_id.to_s if self.user.data_public?
199 self.tags.each do |k,v|
200 el2 = XML::Node.new('tag')
206 el1['created_at'] = self.created_at.xmlschema
207 el1['closed_at'] = self.closed_at.xmlschema unless is_open?
208 el1['open'] = is_open?.to_s
210 el1['min_lon'] = (bbox[0].to_f / GeoRecord::SCALE).to_s unless bbox[0].nil?
211 el1['min_lat'] = (bbox[1].to_f / GeoRecord::SCALE).to_s unless bbox[1].nil?
212 el1['max_lon'] = (bbox[2].to_f / GeoRecord::SCALE).to_s unless bbox[2].nil?
213 el1['max_lat'] = (bbox[3].to_f / GeoRecord::SCALE).to_s unless bbox[3].nil?
215 # NOTE: changesets don't include the XML of the changes within them,
216 # they are just structures for tagging. to get the osmChange of a
217 # changeset, see the download method of the controller.
223 # update this instance from another instance given and the user who is
224 # doing the updating. note that this method is not for updating the
225 # bounding box, only the tags of the changeset.
226 def update_from(other, user)
227 # ensure that only the user who opened the changeset may modify it.
228 unless user.id == self.user_id
229 raise OSM::APIUserChangesetMismatchError
232 # can't change a closed changeset
234 raise OSM::APIChangesetAlreadyClosedError.new(self)
237 # copy the other's tags
238 self.tags = other.tags