Skip to content

Commit

Permalink
Merge pull request MongoEngine#263 from johnarnfield/dev
Browse files Browse the repository at this point in the history
Added polygon support and unit tests
  • Loading branch information
rozza committed Aug 17, 2011
2 parents 11621c6 + b037fb3 commit f549d8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion mongoengine/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def _transform_query(cls, _doc_cls=None, _field_operation=False, **query):
"""
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']
Expand Down Expand Up @@ -682,6 +682,8 @@ def _transform_query(cls, _doc_cls=None, _field_operation=False, **query):
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":
Expand Down
30 changes: 28 additions & 2 deletions tests/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
MultipleObjectsReturned, DoesNotExist,
QueryFieldList)
from mongoengine import *
from mongoengine.connection import _get_connection
from mongoengine.tests import query_counter


class QuerySetTest(unittest.TestCase):

def setUp(self):
connect(db='mongoenginetest')

class Person(Document):
name = StringField()
age = IntField()
Expand Down Expand Up @@ -2196,7 +2197,32 @@ def __unicode__(self):
events = Event.objects(location__within_box=box)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0].id, event2.id)


# check that polygon works for users who have a server >= 1.9
server_version = tuple(
_get_connection().server_info()['version'].split('.')
)
required_version = tuple("1.9.0".split("."))
if server_version >= required_version:
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):
Expand Down

0 comments on commit f549d8c

Please sign in to comment.