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
23 # over-expansion factor to use when updating the bounding box
26 # maximum number of elements allowed in a changeset
29 # maximum time a changeset is allowed to be open for.
32 # idle timeout increment, one hour seems reasonable.
35 # Use a method like this, so that we can easily change how we
36 # determine whether a changeset is open, without breaking code in at
39 # a changeset is open (that is, it will accept further changes) when
40 # it has not yet run out of time and its capacity is small enough.
41 # note that this may not be a hard limit - due to timing changes and
42 # concurrency it is possible that some changesets may be slightly
43 # longer than strictly allowed or have slightly more changes in them.
44 return ((closed_at > Time.now.getutc) and (num_changes <= MAX_ELEMENTS))
47 def set_closed_time_now
49 self.closed_at = Time.now.getutc
53 def self.from_xml(xml, create=false)
55 p = XML::Parser.string(xml)
58 doc.find('//osm/changeset').each do |pt|
59 return Changeset.from_xml_node(pt, create)
61 raise OSM::APIBadXMLError.new("changeset", xml, "XML doesn't contain an osm/changeset element.")
62 rescue LibXML::XML::Error, ArgumentError => ex
63 raise OSM::APIBadXMLError.new("changeset", xml, ex.message)
67 def self.from_xml_node(pt, create=false)
70 cs.created_at = Time.now.getutc
71 # initial close time is 1h ahead, but will be increased on each
73 cs.closed_at = cs.created_at + IDLE_TIMEOUT
74 # initially we have no changes in a changeset
78 pt.find('tag').each do |tag|
79 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing key") if tag['k'].nil?
80 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing value") if tag['v'].nil?
81 cs.add_tag_keyval(tag['k'], tag['v'])
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 ]
99 # returns area of the changset bbox as a rough comparitive quantity for use of changset displays
102 (max_lon - min_lon) * (max_lat - min_lat)
109 # expand the bounding box to include the given bounding box. also,
110 # expand a little bit more in the direction of the expansion, so that
111 # further expansions may be unnecessary. this is an optimisation
112 # suggested on the wiki page by kleptog.
113 def update_bbox!(array)
114 # ensure that bbox is cached and has no nils in it. if there are any
115 # nils, just use the bounding box update to write over them.
116 @bbox = bbox.zip(array).collect { |a, b| a.nil? ? b : a }
118 # only try to update the bbox if there is a value for every coordinate
119 # which there will be from the previous line as long as both array and
120 # bbox are all non-nil.
121 if has_valid_bbox? and array.all?
122 # FIXME - this looks nasty and violates DRY... is there any prettier
124 @bbox[0] = [-180 * GeoRecord::SCALE, array[0] + EXPAND * (@bbox[0] - @bbox[2])].max if array[0] < @bbox[0]
125 @bbox[1] = [ -90 * GeoRecord::SCALE, array[1] + EXPAND * (@bbox[1] - @bbox[3])].max if array[1] < @bbox[1]
126 @bbox[2] = [ 180 * GeoRecord::SCALE, array[2] + EXPAND * (@bbox[2] - @bbox[0])].min if array[2] > @bbox[2]
127 @bbox[3] = [ 90 * GeoRecord::SCALE, array[3] + EXPAND * (@bbox[3] - @bbox[1])].min if array[3] > @bbox[3]
129 # update active record. rails 2.1's dirty handling should take care of
130 # whether this object needs saving or not.
131 self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox
136 # the number of elements is also passed in so that we can ensure that
137 # a single changeset doesn't contain too many elements. this, of course,
138 # destroys the optimisation described in the bbox method above.
139 def add_changes!(elements)
140 self.num_changes += elements
150 self.changeset_tags.each do |tag|
161 def add_tag_keyval(k, v)
162 @tags = Hash.new unless @tags
164 # duplicate tags are now forbidden, so we can't allow values
165 # in the hash to be overwritten.
166 raise OSM::APIDuplicateTagsError.new("changeset", self.id, k) if @tags.include? k
172 # do the changeset update and the changeset tags update in the
173 # same transaction to ensure consistency.
174 Changeset.transaction do
178 ChangesetTag.delete_all(['id = ?', self.id])
181 tag = ChangesetTag.new
191 # set the auto-close time to be one hour in the future unless
192 # that would make it more than 24h long, in which case clip to
193 # 24h, as this has been decided is a reasonable time limit.
196 if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
197 self.closed_at = created_at + MAX_TIME_OPEN
199 self.closed_at = Time.now.getutc + IDLE_TIMEOUT
205 doc = OSM::API.new.get_xml_doc
206 doc.root << to_xml_node()
210 def to_xml_node(user_display_name_cache = nil)
211 el1 = XML::Node.new 'changeset'
212 el1['id'] = self.id.to_s
214 user_display_name_cache = {} if user_display_name_cache.nil?
216 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
217 # use the cache if available
218 elsif self.user.data_public?
219 user_display_name_cache[self.user_id] = self.user.display_name
221 user_display_name_cache[self.user_id] = nil
224 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
225 el1['uid'] = self.user_id.to_s if self.user.data_public?
227 self.tags.each do |k,v|
228 el2 = XML::Node.new('tag')
234 el1['created_at'] = self.created_at.xmlschema
235 el1['closed_at'] = self.closed_at.xmlschema unless is_open?
236 el1['open'] = is_open?.to_s
238 el1['min_lon'] = (bbox[0].to_f / GeoRecord::SCALE).to_s unless bbox[0].nil?
239 el1['min_lat'] = (bbox[1].to_f / GeoRecord::SCALE).to_s unless bbox[1].nil?
240 el1['max_lon'] = (bbox[2].to_f / GeoRecord::SCALE).to_s unless bbox[2].nil?
241 el1['max_lat'] = (bbox[3].to_f / GeoRecord::SCALE).to_s unless bbox[3].nil?
243 # NOTE: changesets don't include the XML of the changes within them,
244 # they are just structures for tagging. to get the osmChange of a
245 # changeset, see the download method of the controller.
251 # update this instance from another instance given and the user who is
252 # doing the updating. note that this method is not for updating the
253 # bounding box, only the tags of the changeset.
254 def update_from(other, user)
255 # ensure that only the user who opened the changeset may modify it.
256 unless user.id == self.user_id
257 raise OSM::APIUserChangesetMismatchError.new
260 # can't change a closed changeset
262 raise OSM::APIChangesetAlreadyClosedError.new(self)
265 # copy the other's tags
266 self.tags = other.tags