Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Forgot to add the `collect` GeoQuerySet method to correspond to the `…

…Collect` aggregate object; also added tests for this operation.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11227 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 33ea28c2b2e62d492e99258f5eb19683119448d4 1 parent ebce1b9
@jbronn jbronn authored
View
8 django/contrib/gis/db/models/query.py
@@ -62,6 +62,14 @@ def centroid(self, **kwargs):
"""
return self._geom_attribute('centroid', **kwargs)
+ def collect(self, **kwargs):
+ """
+ Performs an aggregate collect operation on the given geometry field.
+ This is analagous to a union operation, but much faster because
+ boundaries are not dissolved.
+ """
+ return self._spatial_aggregate(aggregates.Collect, **kwargs)
+
def difference(self, geom, **kwargs):
"""
Returns the spatial difference of the geographic field in a `difference`
View
22 django/contrib/gis/tests/relatedapp/tests.py
@@ -1,7 +1,7 @@
import os, unittest
from django.contrib.gis.geos import *
from django.contrib.gis.db.backend import SpatialBackend
-from django.contrib.gis.db.models import Count, Extent, F, Union
+from django.contrib.gis.db.models import Collect, Count, Extent, F, Union
from django.contrib.gis.tests.utils import no_mysql, no_oracle, no_spatialite
from django.conf import settings
from models import City, Location, DirectoryEntry, Parcel, Book, Author
@@ -264,6 +264,26 @@ def test13_select_related_null_fk(self):
# Should be `None`, and not a 'dummy' model.
self.assertEqual(None, b.author)
+ @no_mysql
+ @no_oracle
+ @no_spatialite
+ def test14_collect(self):
+ "Testing the `collect` GeoQuerySet method and `Collect` aggregate."
+ # Reference query:
+ # SELECT AsText(ST_Collect("relatedapp_location"."point")) FROM "relatedapp_city" LEFT OUTER JOIN
+ # "relatedapp_location" ON ("relatedapp_city"."location_id" = "relatedapp_location"."id")
+ # WHERE "relatedapp_city"."state" = 'TX';
+ ref_geom = fromstr('MULTIPOINT(-97.516111 33.058333,-96.801611 32.782057,-95.363151 29.763374,-96.801611 32.782057)')
+
+ c1 = City.objects.filter(state='TX').collect(field_name='location__point')
+ c2 = City.objects.filter(state='TX').aggregate(Collect('location__point'))['location__point__collect']
+
+ for coll in (c1, c2):
+ # Even though Dallas and Ft. Worth share same point, Collect doesn't
+ # consolidate -- that's why 4 points in MultiPoint.
+ self.assertEqual(4, len(coll))
+ self.assertEqual(ref_geom, coll)
+
# TODO: Related tests for KML, GML, and distance lookups.
def suite():
Please sign in to comment.
Something went wrong with that request. Please try again.