Skip to content

# smtlaissezfaire/geokit

### Subversion checkout URL

You can clone with HTTPS or Subversion.

added bounds test and automatic bounding box on distance query

`git-svn-id: http://geokit.rubyforge.org/svn/trunk@39 9265c765-0211-4c68-b2df-6d1bd6e20c4d`
• Loading branch information...
commit 05c685ccbdf6e9fa3d3a9f5b344d2b59f0309895 1 parent 6fa95ad
lewisac authored
14 lib/geo_kit/acts_as_mappable.rb
 @@ -204,6 +204,8 @@ def prepare_for_find_or_count(action, args) units = extract_units_from_options(options) formula = extract_formula_from_options(options) bounds = extract_bounds_from_options(options) + # if no explicit bounds were given, try formulating them from the point and distance given + bounds = formulate_bounds_from_distance(options, origin, units) unless bounds # Apply select adjustments based upon action. add_distance_to_select(options, origin, units, formula) if origin && action == :find # Apply the conditions for a bounding rectangle if applicable @@ -245,6 +247,18 @@ def apply_find_scope(args, options) end end + # If it's a :within query, add a bounding box to improve performance. + # This only gets called if a :bounds argument is not otherwise supplied. + def formulate_bounds_from_distance(options, origin, units) + distance = options[:within] if options.has_key?(:within) + distance = options[:range].last-(options[:range].exclude_end?? 1 : 0) if options.has_key?(:range) + if distance + res=GeoKit::Bounds.from_point_and_radius(origin,distance,:units=>units) + else + nil + end + end + # Replace :within, :beyond and :range distance tokens with the appropriate distance # where clauses. Removes these tokens from the options hash. def apply_distance_scope(options)
1  lib/geo_kit/mappable.rb
 @@ -399,6 +399,7 @@ class <
6 test/bounds_test.rb
 @@ -65,7 +65,11 @@ def test_center_cross_meridian end def test_creation_from_circle - #TODO -- add this test + bounds=GeoKit::Bounds.from_point_and_radius([32.939829, -96.951176],2.5) + inside=GeoKit::LatLng.new 32.9695270000,-96.9901590000 + outside=GeoKit::LatLng.new 32.8951550000,-96.9584440000 + assert bounds.contains?(inside) + assert !bounds.contains?(outside) end end
Please sign in to comment.
Something went wrong with that request. Please try again.