Skip to content

Commit

Permalink
Merge dc52dc3 into 93b7153
Browse files Browse the repository at this point in the history
  • Loading branch information
denisahearn authored Sep 19, 2018
2 parents 93b7153 + dc52dc3 commit c13fd59
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ If you are displaying points on a map, you probably need to query for whatever f
Store.in_bounds([sw_point,ne_point]).all
```

If you want the query to return things that are located on the rectangular bounds, specify the `inclusive` option set to true:

```ruby
Store.in_bounds([sw_point,ne_point], :inclusive => true).all
```

The input to `bounds` can be an array with the two points or a Bounds object. However you provide them, the order should always be the southwest corner, northeast corner of the rectangle. Typically, you will be getting the sw\_point and ne\_point from a map that is displayed on a web page.

If you need to calculate the bounding box from a point and radius, you can do that:
Expand Down
18 changes: 13 additions & 5 deletions lib/geokit-rails/acts_as_mappable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ def in_range(range, options = {})
end

def in_bounds(bounds, options = {})
inclusive = options.delete(:inclusive) || false
options[:bounds] = bounds
#geo_scope(options)
#where(distance_conditions(options))
bounds = extract_bounds_from_options(options)
where(bound_conditions(bounds))
where(bound_conditions(bounds, inclusive))
end

def by_distance(options = {})
Expand Down Expand Up @@ -263,15 +264,22 @@ def distance_conditions(options)
end
end

def bound_conditions(bounds)
def bound_conditions(bounds, inclusive = false)
return nil unless bounds
if inclusive
lt_operator = :lteq
gt_operator = :gteq
else
lt_operator = :lt
gt_operator = :gt
end
sw,ne = bounds.sw, bounds.ne
lat, lng = Arel.sql(qualified_lat_column_name), Arel.sql(qualified_lng_column_name)
lat.gt(sw.lat).and(lat.lt(ne.lat)).and(
lat.send(gt_operator, sw.lat).and(lat.send(lt_operator, ne.lat)).and(
if bounds.crosses_meridian?
lng.lt(ne.lng).or(lng.gt(sw.lng))
lng.send(lt_operator, ne.lng).or(lng.send(gt_operator, sw.lng))
else
lng.gt(sw.lng).and(lng.lt(ne.lng))
lng.send(gt_operator, sw.lng).and(lng.send(lt_operator, ne.lng))
end
)
end
Expand Down
10 changes: 10 additions & 0 deletions test/acts_as_mappable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,16 @@ def test_find_within_bounds
assert_equal 2, locations.count
end

def test_find_within_bounds_with_inclusive
sw = Geokit::LatLng.new(@loc_a.lat,@loc_a.lng)
ne = sw
bounds = [sw,ne]
locations = Location.in_bounds(bounds, :inclusive => false)
assert_equal 0, locations.count
locations = Location.in_bounds(bounds, :inclusive => true)
assert_equal 1, locations.count
end

def test_find_within_bounds_ordered_by_distance
#locations = Location.in_bounds([@sw,@ne], :origin=>@bounds_center).order('distance asc')
locations = Location.in_bounds([@sw,@ne]).by_distance(:origin => @bounds_center)
Expand Down

0 comments on commit c13fd59

Please sign in to comment.