]> git.openstreetmap.org Git - rails.git/commitdiff
Added first attempt at bounding box support in changesets and tests for the same.
authorMatt Amos <zerebubuth@gmail.com>
Tue, 4 Nov 2008 15:52:22 +0000 (15:52 +0000)
committerMatt Amos <zerebubuth@gmail.com>
Tue, 4 Nov 2008 15:52:22 +0000 (15:52 +0000)
app/models/changeset.rb
app/models/node.rb
app/models/relation.rb
app/models/way.rb
lib/diff_reader.rb
test/functional/changeset_controller_test.rb
test/unit/way_test.rb [new file with mode: 0644]

index 446ca351de81cd4d574722630cc4785284ffd30f..b00dfa8af46600ae2158188d48ef39f0a30311da 100644 (file)
@@ -15,6 +15,9 @@ class Changeset < ActiveRecord::Base
   validates_presence_of :user_id, :created_at
   validates_inclusion_of :open, :in => [ true, false ]
   
   validates_presence_of :user_id, :created_at
   validates_inclusion_of :open, :in => [ true, false ]
   
+  # over-expansion factor to use when updating the bounding box
+  EXPAND = 0.1
+
   # Use a method like this, so that we can easily change how we
   # determine whether a changeset is open, without breaking code in at 
   # least 6 controllers
   # Use a method like this, so that we can easily change how we
   # determine whether a changeset is open, without breaking code in at 
   # least 6 controllers
@@ -46,6 +49,35 @@ class Changeset < ActiveRecord::Base
     return cs
   end
 
     return cs
   end
 
+  ##
+  # returns the bounding box of the changeset. it is possible that some
+  # or all of the values will be nil, indicating that they are undefined.
+  def bbox
+    @bbox ||= [ min_lon, min_lat, max_lon, max_lat ]
+  end
+
+  ##
+  # expand the bounding box to include the given bounding box. also, 
+  # expand a little bit more in the direction of the expansion, so that
+  # further expansions may be unnecessary. this is an optimisation 
+  # suggested on the wiki page by kleptog.
+  def update_bbox!(array)
+    # ensure that bbox is cached and has no nils in it. if there are any
+    # nils, just use the bounding box update to write over them.
+    @bbox = bbox.zip(array).collect { |a, b| a.nil? ? b : a }
+
+    # FIXME - this looks nasty and violates DRY... is there any prettier 
+    # way to do this? 
+    @bbox[0] = array[0] + EXPAND * (@bbox[0] - @bbox[2]) if array[0] < @bbox[0]
+    @bbox[1] = array[1] + EXPAND * (@bbox[1] - @bbox[3]) if array[1] < @bbox[1]
+    @bbox[2] = array[2] + EXPAND * (@bbox[2] - @bbox[0]) if array[2] > @bbox[2]
+    @bbox[3] = array[3] + EXPAND * (@bbox[3] - @bbox[1]) if array[3] > @bbox[3]
+
+    # update active record. rails 2.1's dirty handling should take care of
+    # whether this object needs saving or not.
+    self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox
+  end
+
   def tags_as_hash
     return tags
   end
   def tags_as_hash
     return tags
   end
@@ -124,9 +156,14 @@ class Changeset < ActiveRecord::Base
     el1['created_at'] = self.created_at.xmlschema
     el1['open'] = self.open.to_s
 
     el1['created_at'] = self.created_at.xmlschema
     el1['open'] = self.open.to_s
 
-    # FIXME FIXME FIXME: This does not include changes yet! There is 
-    # currently no changeset_id column in the tables as far as I can tell,
-    # so this is just a scaffold to build on, not a complete to_xml
+    el1['min_lon'] = (bbox[0] / SCALE).to_s unless bbox[0].nil?
+    el1['min_lat'] = (bbox[1] / SCALE).to_s unless bbox[1].nil?
+    el1['max_lon'] = (bbox[2] / SCALE).to_s unless bbox[2].nil?
+    el1['max_lat'] = (bbox[3] / SCALE).to_s unless bbox[3].nil?
+    
+    # NOTE: changesets don't include the XML of the changes within them,
+    # they are just structures for tagging. to get the osmChange of a
+    # changeset, see the download method of the controller.
 
     return el1
   end
 
     return el1
   end
index 67efeca2c1a471f2e78d3bc9e6b1ef0d7b3244d5..e58a1d8969cd7d39b3dc891e632e39df63cbde01 100644 (file)
@@ -112,6 +112,12 @@ class Node < ActiveRecord::Base
     return node
   end
 
     return node
   end
 
+  ##
+  # the bounding box around a node
+  def bbox
+    [ longitude, latitude, longitude, latitude ]
+  end
+
   def save_with_history!
     t = Time.now
     Node.transaction do
   def save_with_history!
     t = Time.now
     Node.transaction do
@@ -134,6 +140,9 @@ class Node < ActiveRecord::Base
       old_node = OldNode.from_node(self)
       old_node.timestamp = t
       old_node.save_with_dependencies!
       old_node = OldNode.from_node(self)
       old_node.timestamp = t
       old_node.save_with_dependencies!
+
+      # save the changeset in case of bounding box updates
+      changeset.save!
     end
   end
 
     end
   end
 
@@ -148,6 +157,10 @@ class Node < ActiveRecord::Base
       else
         self.changeset_id = new_node.changeset_id
         self.visible = 0
       else
         self.changeset_id = new_node.changeset_id
         self.visible = 0
+
+        # update the changeset with the deleted position
+        changeset.update_bbox!(bbox)
+
         save_with_history!
       end
     else
         save_with_history!
       end
     else
@@ -158,12 +171,19 @@ class Node < ActiveRecord::Base
   def update_from(new_node, user)
     check_consistency(self, new_node, user)
 
   def update_from(new_node, user)
     check_consistency(self, new_node, user)
 
+    # update changeset with *old* position first
+    changeset.update_bbox!(bbox);
+
     # FIXME logic needs to be double checked
     self.changeset_id = new_node.changeset_id
     self.latitude = new_node.latitude 
     self.longitude = new_node.longitude
     self.tags = new_node.tags
     self.visible = true
     # FIXME logic needs to be double checked
     self.changeset_id = new_node.changeset_id
     self.latitude = new_node.latitude 
     self.longitude = new_node.longitude
     self.tags = new_node.tags
     self.visible = true
+
+    # update changeset with *new* position
+    changeset.update_bbox!(bbox);
+
     save_with_history!
   end
   
     save_with_history!
   end
   
@@ -171,6 +191,10 @@ class Node < ActiveRecord::Base
     check_create_consistency(self, user)
     self.version = 0
     self.visible = true
     check_create_consistency(self, user)
     self.version = 0
     self.visible = true
+
+    # update the changeset to include the new location
+    changeset.update_bbox!(bbox)
+
     save_with_history!
   end
 
     save_with_history!
   end
 
index 81f17899739ce336a6d6b1151befc9bbd4d44ca6..9836ef4f1e29d9d65053f2cbacb833e4a7cb2bb2 100644 (file)
@@ -221,6 +221,9 @@ class Relation < ActiveRecord::Base
       old_relation = OldRelation.from_relation(self)
       old_relation.timestamp = t
       old_relation.save_with_dependencies!
       old_relation = OldRelation.from_relation(self)
       old_relation.timestamp = t
       old_relation.save_with_dependencies!
+
+      # update the bbox of the changeset and save it too.
+      # FIXME: what is the bounding box of a relation?
     end
   end
 
     end
   end
 
index 9d4d8ba87c0ac13c1ef635a612dfa191577e21e7..4143291c1f47eda44189fe803b855edd3e616612 100644 (file)
@@ -179,9 +179,24 @@ class Way < ActiveRecord::Base
     @tags[k] = v
   end
 
     @tags[k] = v
   end
 
+  ##
+  # the integer coords (i.e: unscaled) bounding box of the way, assuming
+  # straight line segments.
+  def bbox
+    lons = nodes.collect { |n| n.longitude }
+    lats = nodes.collect { |n| n.latitude }
+    [ lons.min, lats.min, lons.max, lats.max ]
+  end
+
   def save_with_history!
     t = Time.now
 
   def save_with_history!
     t = Time.now
 
+    # update the bounding box, but don't save it as the controller knows the 
+    # lifetime of the change better. note that this has to be done both before 
+    # and after the save, so that nodes from both versions are included in the 
+    # bbox.
+    changeset.update_bbox!(bbox) unless nodes.empty?
+
     Way.transaction do
       self.version += 1
       self.timestamp = t
     Way.transaction do
       self.version += 1
       self.timestamp = t
@@ -211,6 +226,11 @@ class Way < ActiveRecord::Base
       old_way = OldWay.from_way(self)
       old_way.timestamp = t
       old_way.save_with_dependencies!
       old_way = OldWay.from_way(self)
       old_way.timestamp = t
       old_way.save_with_dependencies!
+
+      # update and commit the bounding box, now that way nodes 
+      # have been updated and we're in a transaction.
+      changeset.update_bbox!(bbox) unless nodes.empty?
+      changeset.save!
     end
   end
 
     end
   end
 
index 165e30e20fa94840f70c2d3bff9365eee4f8faba..d793f63e7bfda199d1ecc73b8d7dd6e32c8aa69d 100644 (file)
@@ -99,9 +99,8 @@ class DiffReader
           # diff, so we must fix these before saving the element.
           new.fix_placeholders!(ids)
 
           # diff, so we must fix these before saving the element.
           new.fix_placeholders!(ids)
 
-          # set the initial version to zero and save (which increments it)
-          new.version = 0
-          new.save_with_history!
+          # create element given user
+          new.create_with_history(@changeset.user)
           
           # save placeholder => allocated ID map
           ids[model.to_s.downcase.to_sym][placeholder_id] = new.id
           
           # save placeholder => allocated ID map
           ids[model.to_s.downcase.to_sym][placeholder_id] = new.id
index 31ade9fce5e3f05000e2ae497f7c2d222be1ddda..b9c8c63d0f5249871012735306442f11bf3ae29c 100644 (file)
@@ -36,8 +36,16 @@ class ChangesetControllerTest < ActionController::TestCase
     assert_response :bad_request, "creating a invalid changeset should fail"
   end
 
     assert_response :bad_request, "creating a invalid changeset should fail"
   end
 
+  ##
+  # check that the changeset can be read and returns the correct
+  # document structure.
   def test_read
   def test_read
+    changeset_id = changesets(:normal_user_first_change).id
+    get :read, :id => changeset_id
+    assert_response :success, "cannot get first changeset"
     
     
+    assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
+    assert_select "osm>changeset[id=#{changeset_id}]", 1
   end
   
   def test_close
   end
   
   def test_close
@@ -487,5 +495,80 @@ EOF
     assert_select "osmChange>modify>node", 1
     assert_select "osmChange>modify>way", 1
   end
     assert_select "osmChange>modify>node", 1
     assert_select "osmChange>modify>way", 1
   end
-  
+
+  ##
+  # check that the bounding box of a changeset gets updated correctly
+  def test_changeset_bbox
+    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
+    
+    # add a single node to it
+    with_controller(NodeController.new) do
+      content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
+      put :create
+      assert_response :success, "Couldn't create node."
+    end
+
+    # get the bounding box back from the changeset
+    get :read, :id => changeset_id
+    assert_response :success, "Couldn't read back changeset."
+    assert_select "osm>changeset[min_lon=1]", 1
+    assert_select "osm>changeset[max_lon=1]", 1
+    assert_select "osm>changeset[min_lat=2]", 1
+    assert_select "osm>changeset[max_lat=2]", 1
+
+    # add another node to it
+    with_controller(NodeController.new) do
+      content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
+      put :create
+      assert_response :success, "Couldn't create second node."
+    end
+
+    # get the bounding box back from the changeset
+    get :read, :id => changeset_id
+    assert_response :success, "Couldn't read back changeset for the second time."
+    assert_select "osm>changeset[min_lon=1]", 1
+    assert_select "osm>changeset[max_lon=2]", 1
+    assert_select "osm>changeset[min_lat=1]", 1
+    assert_select "osm>changeset[max_lat=2]", 1
+
+    # add (delete) a way to it
+    with_controller(WayController.new) do
+      content update_changeset(current_ways(:visible_way).to_xml,
+                               changeset_id)
+      put :delete, :id => current_ways(:visible_way).id
+      assert_response :success, "Couldn't delete a way."
+    end
+
+    # get the bounding box back from the changeset
+    get :read, :id => changeset_id
+    assert_response :success, "Couldn't read back changeset for the third time."
+    assert_select "osm>changeset[min_lon=1]", 1
+    assert_select "osm>changeset[max_lon=3]", 1
+    assert_select "osm>changeset[min_lat=1]", 1
+    assert_select "osm>changeset[max_lat=3]", 1    
+  end
+
+  #------------------------------------------------------------
+  # utility functions
+  #------------------------------------------------------------
+
+  ##
+  # update the changeset_id of a way element
+  def update_changeset(xml, changeset_id)
+    xml_attr_rewrite(xml, 'changeset', changeset_id)
+  end
+
+  ##
+  # update an attribute in a way element
+  def xml_attr_rewrite(xml, name, value)
+    xml.find("//osm/way").first[name] = value.to_s
+    return xml
+  end
+
 end
 end
diff --git a/test/unit/way_test.rb b/test/unit/way_test.rb
new file mode 100644 (file)
index 0000000..cd565fd
--- /dev/null
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class WayTest < Test::Unit::TestCase
+  api_fixtures
+
+  def test_bbox
+    node = current_nodes(:used_node_1)
+    [ :visible_way,
+      :invisible_way,
+      :used_way ].each do |way_symbol|
+      way = current_ways(way_symbol)
+      assert_equal node.bbox, way.bbox
+    end
+  end
+  
+end