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 Changeset.transaction do
108 # fixme update modified_at time?
109 # FIXME there is no modified_at time, should it be added
113 ChangesetTag.transaction do
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?
149 self.tags.each do |k,v|
150 el2 = XML::Node.new('tag')
156 el1['created_at'] = self.created_at.xmlschema
157 el1['open'] = self.open.to_s
159 el1['min_lon'] = (bbox[0].to_f / GeoRecord::SCALE).to_s unless bbox[0].nil?
160 el1['min_lat'] = (bbox[1].to_f / GeoRecord::SCALE).to_s unless bbox[1].nil?
161 el1['max_lon'] = (bbox[2].to_f / GeoRecord::SCALE).to_s unless bbox[2].nil?
162 el1['max_lat'] = (bbox[3].to_f / GeoRecord::SCALE).to_s unless bbox[3].nil?
164 # NOTE: changesets don't include the XML of the changes within them,
165 # they are just structures for tagging. to get the osmChange of a
166 # changeset, see the download method of the controller.