From 376ca717fa53bd010a834d1fbd30d535ec018529 Mon Sep 17 00:00:00 2001 From: John Arnfield Date: Sat, 30 Jul 2011 22:01:24 +0100 Subject: [PATCH 1/2] Added support for within_polygon for spatial queries --- mongoengine/queryset.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 0af8dead..5ceeea9d 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -481,7 +481,7 @@ class QuerySet(object): """ operators = ['ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'mod', 'all', 'size', 'exists', 'not'] - geo_operators = ['within_distance', 'within_spherical_distance', 'within_box', 'near', 'near_sphere'] + geo_operators = ['within_distance', 'within_spherical_distance', 'within_box', 'within_polygon', 'near', 'near_sphere'] match_operators = ['contains', 'icontains', 'startswith', 'istartswith', 'endswith', 'iendswith', 'exact', 'iexact'] @@ -527,6 +527,8 @@ class QuerySet(object): value = {'$within': {'$center': value}} elif op == "within_spherical_distance": value = {'$within': {'$centerSphere': value}} + elif op == "within_polygon": + value = {'$within': {'$polygon': value}} elif op == "near": value = {'$near': value} elif op == "near_sphere": From ca3b004921fd9e69ab0a3d3b6b62a435826c26a5 Mon Sep 17 00:00:00 2001 From: John Arnfield Date: Wed, 17 Aug 2011 20:04:38 +0100 Subject: [PATCH 2/2] Added tests for polygon queries --- tests/queryset.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/queryset.py b/tests/queryset.py index 72623b89..778d9317 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -1355,6 +1355,26 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(events.count(), 1) self.assertEqual(events[0].id, event2.id) + # check that polygon works + polygon = [ + (41.912114,-87.694445), + (41.919395,-87.69084), + (41.927186,-87.681742), + (41.911731,-87.654276), + (41.898061,-87.656164), + ] + events = Event.objects(location__within_polygon=polygon) + self.assertEqual(events.count(), 1) + self.assertEqual(events[0].id, event1.id) + + polygon2 = [ + (54.033586,-1.742249), + (52.792797,-1.225891), + (53.389881,-4.40094) + ] + events = Event.objects(location__within_polygon=polygon2) + self.assertEqual(events.count(), 0) + Event.drop_collection() def test_spherical_geospatial_operators(self):