1 class Changeset < ActiveRecord::Base
6 has_many :changeset_tags, :foreign_key => 'id'
13 has_many :old_relations
15 validates_presence_of :user_id, :created_at
16 validates_inclusion_of :open, :in => [ true, false ]
18 # over-expansion factor to use when updating the bounding box
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
28 def self.from_xml(xml, create=false)
36 doc.find('//osm/changeset').each do |pt|
38 cs.created_at = Time.now
41 pt.find('tag').each do |tag|
42 cs.add_tag_keyval(tag['k'], tag['v'])
45 rescue Exception => ex
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.
56 @bbox ||= [ min_lon, min_lat, max_lon, max_lat ]
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 }
69 # FIXME - this looks nasty and violates DRY... is there any prettier
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]
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
88 self.changeset_tags.each do |tag|
99 def add_tag_keyval(k, v)
100 @tags = Hash.new unless @tags
107 # do the changeset update and the changeset tags update in the
108 # same transaction to ensure consistency.
109 Changeset.transaction do
110 # fixme update modified_at time?
111 # FIXME there is no modified_at time, should it be added
115 ChangesetTag.delete_all(['id = ?', self.id])
118 tag = ChangesetTag.new
128 doc = OSM::API.new.get_xml_doc
129 doc.root << to_xml_node()
133 def to_xml_node(user_display_name_cache = nil)
134 el1 = XML::Node.new 'changeset'
135 el1['id'] = self.id.to_s
137 user_display_name_cache = {} if user_display_name_cache.nil?
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
144 user_display_name_cache[self.user_id] = nil
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?
150 self.tags.each do |k,v|
151 el2 = XML::Node.new('tag')
157 el1['created_at'] = self.created_at.xmlschema
158 el1['open'] = self.open.to_s
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?
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.
173 # update this instance from another instance given and the user who is
174 # doing the updating. note that this method is not for updating the
175 # bounding box, only the tags of the changeset.
176 def update_from(other, user)
177 # ensure that only the user who opened the changeset may modify it.
178 unless user.id == self.user_id
179 raise OSM::APIUserChangesetMismatchError
182 # can't change a closed changeset
184 raise OSM::APIChangesetAlreadyClosedError
187 # copy the other's tags
188 self.tags = other.tags