Added first attempt at bounding box support in changesets and tests for the same.

This commit is contained in:
Matt Amos 2008-11-04 15:52:22 +00:00
parent 0ca779e5bd
commit 02fbcf5f06
7 changed files with 189 additions and 7 deletions

View file

@ -15,6 +15,9 @@ class Changeset < ActiveRecord::Base
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
@ -46,6 +49,35 @@ class Changeset < ActiveRecord::Base
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
@ -124,9 +156,14 @@ class Changeset < ActiveRecord::Base
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

View file

@ -112,6 +112,12 @@ class Node < ActiveRecord::Base
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
@ -134,6 +140,9 @@ class Node < ActiveRecord::Base
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
@ -148,6 +157,10 @@ class Node < ActiveRecord::Base
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
@ -158,12 +171,19 @@ class Node < ActiveRecord::Base
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
# update changeset with *new* position
changeset.update_bbox!(bbox);
save_with_history!
end
@ -171,6 +191,10 @@ class Node < ActiveRecord::Base
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

View file

@ -221,6 +221,9 @@ class Relation < ActiveRecord::Base
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

View file

@ -179,9 +179,24 @@ class Way < ActiveRecord::Base
@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
# 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
@ -211,6 +226,11 @@ class Way < ActiveRecord::Base
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