1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetController < ApplicationController
7 before_filter :authorize, :only => [:create, :update, :delete, :upload, :include]
8 before_filter :check_write_availability, :only => [:create, :update, :delete, :upload, :include]
9 before_filter :check_read_availability, :except => [:create, :update, :delete, :upload, :download]
10 after_filter :compress_output
12 # Create a changeset from XML.
15 cs = Changeset.from_xml(request.raw_post, true)
20 render :text => cs.id.to_s, :content_type => "text/plain"
22 render :nothing => true, :status => :bad_request
25 render :nothing => true, :status => :method_not_allowed
31 changeset = Changeset.find(params[:id])
32 render :text => changeset.to_xml.to_s, :content_type => "text/xml"
33 rescue ActiveRecord::RecordNotFound
34 render :nothing => true, :status => :not_found
41 render :nothing => true, :status => :method_not_allowed
44 changeset = Changeset.find(params[:id])
45 changeset.open = false
47 render :nothing => true
48 rescue ActiveRecord::RecordNotFound
49 render :nothing => true, :status => :not_found
54 # insert a (set of) points into a changeset bounding box. this can only
55 # increase the size of the bounding box. this is a hint that clients can
56 # set either before uploading a large number of changes, or changes that
57 # the client (but not the server) knows will affect areas further away.
59 # only allow POST requests, because although this method is
60 # idempotent, there is no "document" to PUT really...
62 cs = Changeset.find(params[:id])
64 # keep an array of lons and lats
68 # the request is in pseudo-osm format... this is kind-of an
69 # abuse, maybe should change to some other format?
70 doc = XML::Parser.string(request.raw_post).parse
71 doc.find("//osm/node").each do |n|
72 lon << n['lon'].to_f * SCALE
73 lat << n['lat'].to_f * SCALE
76 # add the existing bounding box to the lon-lat array
77 lon << cs.min_lon unless cs.min_lon.nil?
78 lat << cs.min_lat unless cs.min_lat.nil?
79 lon << cs.max_lon unless cs.max_lon.nil?
80 lat << cs.max_lat unless cs.max_lat.nil?
82 # collapse the arrays to minimum and maximum
83 cs.min_lon, cs.min_lat, cs.max_lon, cs.max_lat =
84 lon.min, lat.min, lon.max, lat.max
86 # save the larger bounding box and return the changeset, which
87 # will include the bigger bounding box.
89 render :text => cs.to_xml.to_s, :content_type => "text/xml"
92 render :nothing => true, :status => :method_not_allowed
95 rescue ActiveRecord::RecordNotFound
96 render :nothing => true, :status => :not_found
97 rescue OSM::APIError => ex
102 # Upload a diff in a single transaction.
104 # This means that each change within the diff must succeed, i.e: that
105 # each version number mentioned is still current. Otherwise the entire
106 # transaction *must* be rolled back.
108 # Furthermore, each element in the diff can only reference the current
111 # Returns: a diffResult document, as described in
112 # http://wiki.openstreetmap.org/index.php/OSM_Protocol_Version_0.6
114 # only allow POST requests, as the upload method is most definitely
115 # not idempotent, as several uploads with placeholder IDs will have
116 # different side-effects.
117 # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
119 render :nothing => true, :status => :method_not_allowed
123 changeset = Changeset.find(params[:id])
125 diff_reader = DiffReader.new(request.raw_post, changeset)
126 Changeset.transaction do
127 result = diff_reader.commit
128 render :text => result.to_s, :content_type => "text/xml"
131 rescue ActiveRecord::RecordNotFound
132 render :nothing => true, :status => :not_found
133 rescue OSM::APIError => ex
134 render ex.render_opts
138 # download the changeset as an osmChange document.
140 # to make it easier to revert diffs it would be better if the osmChange
141 # format were reversible, i.e: contained both old and new versions of
142 # modified elements. but it doesn't at the moment...
144 # this method cannot order the database changes fully (i.e: timestamp and
145 # version number may be too coarse) so the resulting diff may not apply
146 # to a different database. however since changesets are not atomic this
147 # behaviour cannot be guaranteed anyway and is the result of a design
150 changeset = Changeset.find(params[:id])
152 # get all the elements in the changeset and stick them in a big array.
153 elements = [changeset.old_nodes,
155 changeset.old_relations].flatten
157 # sort the elements by timestamp and version number, as this is the
158 # almost sensible ordering available. this would be much nicer if
159 # global (SVN-style) versioning were used - then that would be
161 elements.sort! do |a, b|
162 if (a.timestamp == b.timestamp)
163 a.version <=> b.version
165 a.timestamp <=> b.timestamp
169 # create an osmChange document for the output
170 result = OSM::API.new.get_xml_doc
171 result.root.name = "osmChange"
173 # generate an output element for each operation. note: we avoid looking
174 # at the history because it is simpler - but it would be more correct to
175 # check these assertions.
176 elements.each do |elt|
178 if (elt.version == 1)
179 # first version, so it must be newly-created.
180 created = XML::Node.new "create"
181 created << elt.to_xml_node
183 # get the previous version from the element history
184 prev_elt = elt.class.find(:first, :conditions =>
185 ['id = ? and version = ?',
186 elt.id, elt.version])
188 # if the element isn't visible then it must have been deleted, so
189 # output the *previous* XML
190 deleted = XML::Node.new "delete"
191 deleted << prev_elt.to_xml_node
193 # must be a modify, for which we don't need the previous version
195 modified = XML::Node.new "modify"
196 modified << elt.to_xml_node
201 render :text => result.to_s, :content_type => "text/xml"
203 rescue ActiveRecord::RecordNotFound
204 render :nothing => true, :status => :not_found
205 rescue OSM::APIError => ex
206 render ex.render_opts