]> git.openstreetmap.org Git - rails.git/commitdiff
Added a changeset/#id/include method to POST to to expand the bounding box.
authorMatt Amos <zerebubuth@gmail.com>
Thu, 6 Nov 2008 18:42:06 +0000 (18:42 +0000)
committerMatt Amos <zerebubuth@gmail.com>
Thu, 6 Nov 2008 18:42:06 +0000 (18:42 +0000)
app/controllers/changeset_controller.rb
test/functional/changeset_controller_test.rb

index 7ac4eb91a81dc378331a443d7adb3ae2258b2425..c56e15c0186a74d2f66ea28075da65710c4ca0a5 100644 (file)
@@ -4,9 +4,9 @@ class ChangesetController < ApplicationController
   require 'xml/libxml'
   require 'diff_reader'
 
-  before_filter :authorize, :only => [:create, :update, :delete, :upload]
-  before_filter :check_write_availability, :only => [:create, :update, :delete, :upload]
-  before_filter :check_read_availability, :except => [:create, :update, :delete, :upload]
+  before_filter :authorize, :only => [:create, :update, :delete, :upload, :include]
+  before_filter :check_write_availability, :only => [:create, :update, :delete, :upload, :include]
+  before_filter :check_read_availability, :except => [:create, :update, :delete, :upload, :download]
   after_filter :compress_output
 
   # Create a changeset from XML.
@@ -26,27 +26,6 @@ class ChangesetController < ApplicationController
     end
   end
 
-  def create_prim(ids, prim, nd)
-    prim.version = 0
-    prim.user_id = @user.id
-    prim.visible = true
-    prim.save_with_history!
-
-    ids[nd['id'].to_i] = prim.id
-  end
-
-  def fix_way(w, node_ids)
-    w.nds = w.instance_eval { @nds }.
-      map { |nd| node_ids[nd] || nd }
-    return w
-  end
-
-  def fix_rel(r, ids)
-    r.members = r.instance_eval { @members }.
-      map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
-    return r
-  end
-  
   def read
     begin
       changeset = Changeset.find(params[:id])
@@ -71,6 +50,54 @@ class ChangesetController < ApplicationController
     end
   end
 
+  ##
+  # insert a (set of) points into a changeset bounding box. this can only
+  # increase the size of the bounding box. this is a hint that clients can
+  # set either before uploading a large number of changes, or changes that
+  # the client (but not the server) knows will affect areas further away.
+  def include
+    # only allow POST requests, because although this method is
+    # idempotent, there is no "document" to PUT really...
+    if request.post?
+      cs = Changeset.find(params[:id])
+
+      # keep an array of lons and lats
+      lon = Array.new
+      lat = Array.new
+
+      # the request is in pseudo-osm format... this is kind-of an
+      # abuse, maybe should change to some other format?
+      doc = XML::Parser.string(request.raw_post).parse
+      doc.find("//osm/node").each do |n|
+        lon << n['lon'].to_f * SCALE
+        lat << n['lat'].to_f * SCALE
+      end
+
+      # add the existing bounding box to the lon-lat array
+      lon << cs.min_lon unless cs.min_lon.nil?
+      lat << cs.min_lat unless cs.min_lat.nil?
+      lon << cs.max_lon unless cs.max_lon.nil?
+      lat << cs.max_lat unless cs.max_lat.nil?
+
+      # collapse the arrays to minimum and maximum
+      cs.min_lon, cs.min_lat, cs.max_lon, cs.max_lat = 
+        lon.min, lat.min, lon.max, lat.max
+
+      # save the larger bounding box and return the changeset, which
+      # will include the bigger bounding box.
+      cs.save!
+      render :text => cs.to_xml.to_s, :content_type => "text/xml"
+
+    else
+      render :nothing => true, :status => :method_not_allowed
+    end
+
+  rescue ActiveRecord::RecordNotFound
+    render :nothing => true, :status => :not_found
+  rescue OSM::APIError => ex
+    render ex.render_opts
+  end
+
   ##
   # Upload a diff in a single transaction.
   #
index b9c8c63d0f5249871012735306442f11bf3ae29c..150e53dae946aa511f077231accbebcb35e8963a 100644 (file)
@@ -49,7 +49,7 @@ class ChangesetControllerTest < ActionController::TestCase
   end
   
   def test_close
-    
+    # FIXME FIXME FIXME!
   end
 
   ##
@@ -554,10 +554,52 @@ EOF
     assert_select "osm>changeset[max_lat=3]", 1    
   end
 
+  ##
+  # test that the changeset :include method works as it should
+  def test_changeset_include
+    basic_authorization "test@openstreetmap.org", "test"
+
+    # create a new changeset
+    content "<osm><changeset/></osm>"
+    put :create
+    assert_response :success, "Creating of changeset failed."
+    changeset_id = @response.body.to_i
+
+    # NOTE: the include method doesn't over-expand, like inserting
+    # a real method does. this is because we expect the client to 
+    # know what it is doing!
+    check_after_include(changeset_id,  1,  1, [ 1,  1,  1,  1])
+    check_after_include(changeset_id,  3,  3, [ 1,  1,  3,  3])
+    check_after_include(changeset_id,  4,  2, [ 1,  1,  4,  3])
+    check_after_include(changeset_id,  2,  2, [ 1,  1,  4,  3])
+    check_after_include(changeset_id, -1, -1, [-1, -1,  4,  3])
+    check_after_include(changeset_id, -2,  5, [-2, -1,  4,  5])
+  end
+
   #------------------------------------------------------------
   # utility functions
   #------------------------------------------------------------
 
+  ##
+  # call the include method and assert properties of the bbox
+  def check_after_include(changeset_id, lon, lat, bbox)
+    content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
+    post :include, :id => changeset_id
+    assert_response :success, "Setting include of changeset failed: #{@response.body}"
+
+    # check exactly one changeset
+    assert_select "osm>changeset", 1
+    assert_select "osm>changeset[id=#{changeset_id}]", 1
+
+    # check the bbox
+    doc = XML::Parser.string(@response.body).parse
+    changeset = doc.find("//osm/changeset").first
+    assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
+    assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
+    assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
+    assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
+  end
+
   ##
   # update the changeset_id of a way element
   def update_changeset(xml, changeset_id)