Skip to content

Commit

Permalink
bounding box and within spatial search fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
jasisz committed Jan 17, 2013
1 parent 34fcd5a commit 9f1d19d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
4 changes: 1 addition & 3 deletions docs/spatial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ finding results within an area.

``within`` is a bounding box comparison. A bounding box is a rectangular area
within which to search. It's composed of a bottom-left point & a top-right
point, though provided you give two opposing corners in either order, Haystack
will determine the right coordinates. It is faster but slighty sloppier than
its counterpart.
point. It is faster but slighty sloppier than its counterpart.

Examples::

Expand Down
10 changes: 5 additions & 5 deletions haystack/backends/elasticsearch_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,17 @@ def build_search_kwargs(self, query_string, sort_by=None, start_offset=0, end_of
if within is not None:
from haystack.utils.geo import generate_bounding_box

((min_lat, min_lng), (max_lat, max_lng)) = generate_bounding_box(within['point_1'], within['point_2'])
((south, west), (north, east)) = generate_bounding_box(within['point_1'], within['point_2'])
within_filter = {
"geo_bounding_box": {
within['field']: {
"top_left": {
"lat": max_lat,
"lon": min_lng
"lat": north,
"lon": west
},
"bottom_right": {
"lat": min_lat,
"lon": max_lng
"lat": south,
"lon": east
}
}
},
Expand Down
11 changes: 5 additions & 6 deletions haystack/utils/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ def ensure_distance(dist):
return dist


def generate_bounding_box(point_1, point_2):
def generate_bounding_box(bottom_left, top_right):
"""
Takes two opposite corners of a bounding box (in any order) & generates
Takes two opposite corners of a bounding box (order matters!) & generates
a two-tuple of the correct coordinates for the bounding box.
The two-tuple is in the form ``((min_lat, min_lng), (max_lat, max_lng))``.
"""
lng_1, lat_1 = point_1.get_coords()
lng_2, lat_2 = point_2.get_coords()
west, lat_1 = bottom_left.get_coords()
east, lat_2 = top_right.get_coords()
min_lat, max_lat = min(lat_1, lat_2), max(lat_1, lat_2)
min_lng, max_lng = min(lng_1, lng_2), max(lng_1, lng_2)
return ((min_lat, min_lng), (max_lat, max_lng))
return ((min_lat, west), (max_lat, east))
9 changes: 9 additions & 0 deletions tests/spatial/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def test_generate_bounding_box(self):
self.assertEqual(max_lat, 38.973081081164715)
self.assertEqual(max_lng, -95.23362278938293)

def test_generate_bounding_box_crossing_line_date(self):
downtown_bottom_left = Point(95.23947, 38.9637903)
downtown_top_right = Point(-95.23362278938293, 38.973081081164715)
((south, west), (north, east)) = generate_bounding_box(downtown_bottom_left, downtown_top_right)
self.assertEqual(south, 38.9637903)
self.assertEqual(west, 95.23947)
self.assertEqual(north, 38.973081081164715)
self.assertEqual(east, -95.23362278938293)


class SpatialSolrNoDistanceTestCase(TestCase):
fixtures = ['sample_spatial_data.json']
Expand Down
2 changes: 1 addition & 1 deletion tests/spatial_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'solr_native_distance': {
# Solr 4.X+
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': 'http://localhost:9003/solr/',
'URL': 'http://localhost:8983/solr/',
# See ``haystack/backends/solr_backend.py`` for details on why not.
# 'DISTANCE_AVAILABLE': True,
},
Expand Down

0 comments on commit 9f1d19d

Please sign in to comment.