2008-08-05 18:13:06 +00:00
|
|
|
import os, unittest
|
|
|
|
from django.contrib.gis.geos import *
|
2009-07-11 20:34:27 +00:00
|
|
|
from django.contrib.gis.db.models import Collect, Count, Extent, F, Union
|
2009-12-22 15:18:51 +00:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry
|
|
|
|
from django.contrib.gis.tests.utils import mysql, oracle, postgis, spatialite, no_mysql, no_oracle, no_spatialite
|
2008-08-05 18:13:06 +00:00
|
|
|
from django.conf import settings
|
2010-07-20 19:05:46 +00:00
|
|
|
from models import City, Location, DirectoryEntry, Parcel, Book, Author, Article
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
cities = (('Aurora', 'TX', -97.516111, 33.058333),
|
|
|
|
('Roswell', 'NM', -104.528056, 33.387222),
|
|
|
|
('Kecksburg', 'PA', -79.460734, 40.18476),
|
|
|
|
)
|
|
|
|
|
|
|
|
class RelatedGeoModelTest(unittest.TestCase):
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def test01_setup(self):
|
|
|
|
"Setting up for related model tests."
|
|
|
|
for name, state, lon, lat in cities:
|
2009-03-03 22:10:15 +00:00
|
|
|
loc = Location.objects.create(point=Point(lon, lat))
|
|
|
|
c = City.objects.create(name=name, state=state, location=loc)
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def test02_select_related(self):
|
|
|
|
"Testing `select_related` on geographic models (see #7126)."
|
|
|
|
qs1 = City.objects.all()
|
|
|
|
qs2 = City.objects.select_related()
|
|
|
|
qs3 = City.objects.select_related('location')
|
|
|
|
|
|
|
|
for qs in (qs1, qs2, qs3):
|
|
|
|
for ref, c in zip(cities, qs):
|
|
|
|
nm, st, lon, lat = ref
|
|
|
|
self.assertEqual(nm, c.name)
|
|
|
|
self.assertEqual(st, c.state)
|
|
|
|
self.assertEqual(Point(lon, lat), c.location.point)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
@no_mysql
|
|
|
|
def test03_transform_related(self):
|
|
|
|
"Testing the `transform` GeoQuerySet method on related geographic models."
|
|
|
|
# All the transformations are to state plane coordinate systems using
|
|
|
|
# US Survey Feet (thus a tolerance of 0 implies error w/in 1 survey foot).
|
2009-07-27 04:21:31 +00:00
|
|
|
tol = 0
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def check_pnt(ref, pnt):
|
|
|
|
self.assertAlmostEqual(ref.x, pnt.x, tol)
|
|
|
|
self.assertAlmostEqual(ref.y, pnt.y, tol)
|
|
|
|
self.assertEqual(ref.srid, pnt.srid)
|
|
|
|
|
|
|
|
# Each city transformed to the SRID of their state plane coordinate system.
|
|
|
|
transformed = (('Kecksburg', 2272, 'POINT(1490553.98959621 314792.131023984)'),
|
|
|
|
('Roswell', 2257, 'POINT(481902.189077221 868477.766629735)'),
|
|
|
|
('Aurora', 2276, 'POINT(2269923.2484839 7069381.28722222)'),
|
|
|
|
)
|
|
|
|
|
|
|
|
for name, srid, wkt in transformed:
|
|
|
|
# Doing this implicitly sets `select_related` select the location.
|
2009-03-03 22:10:15 +00:00
|
|
|
# TODO: Fix why this breaks on Oracle.
|
2008-08-05 18:13:06 +00:00
|
|
|
qs = list(City.objects.filter(name=name).transform(srid, field_name='location__point'))
|
2009-03-30 17:15:49 +00:00
|
|
|
check_pnt(GEOSGeometry(wkt, srid), qs[0].location.point)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
@no_mysql
|
2009-03-30 17:15:49 +00:00
|
|
|
@no_spatialite
|
|
|
|
def test04a_related_extent_aggregate(self):
|
|
|
|
"Testing the `extent` GeoQuerySet aggregates on related geographic models."
|
2009-03-03 22:10:15 +00:00
|
|
|
# This combines the Extent and Union aggregates into one query
|
2009-03-30 17:15:49 +00:00
|
|
|
aggs = City.objects.aggregate(Extent('location__point'))
|
2009-03-03 22:10:15 +00:00
|
|
|
|
|
|
|
# One for all locations, one that excludes Roswell.
|
|
|
|
all_extent = (-104.528060913086, 33.0583305358887,-79.4607315063477, 40.1847610473633)
|
|
|
|
txpa_extent = (-97.51611328125, 33.0583305358887,-79.4607315063477, 40.1847610473633)
|
|
|
|
e1 = City.objects.extent(field_name='location__point')
|
|
|
|
e2 = City.objects.exclude(name='Roswell').extent(field_name='location__point')
|
|
|
|
e3 = aggs['location__point__extent']
|
|
|
|
|
|
|
|
# The tolerance value is to four decimal places because of differences
|
|
|
|
# between the Oracle and PostGIS spatial backends on the extent calculation.
|
|
|
|
tol = 4
|
|
|
|
for ref, e in [(all_extent, e1), (txpa_extent, e2), (all_extent, e3)]:
|
|
|
|
for ref_val, e_val in zip(ref, e): self.assertAlmostEqual(ref_val, e_val, tol)
|
|
|
|
|
2009-03-30 17:15:49 +00:00
|
|
|
@no_mysql
|
|
|
|
def test04b_related_union_aggregate(self):
|
|
|
|
"Testing the `unionagg` GeoQuerySet aggregates on related geographic models."
|
|
|
|
# This combines the Extent and Union aggregates into one query
|
|
|
|
aggs = City.objects.aggregate(Union('location__point'))
|
|
|
|
|
2009-03-03 22:10:15 +00:00
|
|
|
# These are the points that are components of the aggregate geographic
|
|
|
|
# union that is returned.
|
|
|
|
p1 = Point(-104.528056, 33.387222)
|
|
|
|
p2 = Point(-97.516111, 33.058333)
|
|
|
|
p3 = Point(-79.460734, 40.18476)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2009-03-03 22:10:15 +00:00
|
|
|
# Creating the reference union geometry depending on the spatial backend,
|
2009-03-30 17:15:49 +00:00
|
|
|
# as Oracle will have a different internal ordering of the component
|
2009-03-03 22:10:15 +00:00
|
|
|
# geometries than PostGIS. The second union aggregate is for a union
|
|
|
|
# query that includes limiting information in the WHERE clause (in other
|
|
|
|
# words a `.filter()` precedes the call to `.unionagg()`).
|
2009-12-22 15:18:51 +00:00
|
|
|
if oracle:
|
2009-03-03 22:10:15 +00:00
|
|
|
ref_u1 = MultiPoint(p3, p1, p2, srid=4326)
|
|
|
|
ref_u2 = MultiPoint(p3, p2, srid=4326)
|
|
|
|
else:
|
|
|
|
ref_u1 = MultiPoint(p1, p2, p3, srid=4326)
|
|
|
|
ref_u2 = MultiPoint(p2, p3, srid=4326)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
u1 = City.objects.unionagg(field_name='location__point')
|
|
|
|
u2 = City.objects.exclude(name='Roswell').unionagg(field_name='location__point')
|
2009-03-03 22:10:15 +00:00
|
|
|
u3 = aggs['location__point__union']
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(ref_u1, u1)
|
|
|
|
self.assertEqual(ref_u2, u2)
|
2009-03-03 22:10:15 +00:00
|
|
|
self.assertEqual(ref_u1, u3)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2008-12-06 01:52:14 +00:00
|
|
|
def test05_select_related_fk_to_subclass(self):
|
|
|
|
"Testing that calling select_related on a query over a model with an FK to a model subclass works"
|
|
|
|
# Regression test for #9752.
|
|
|
|
l = list(DirectoryEntry.objects.all().select_related())
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2009-04-01 16:01:50 +00:00
|
|
|
def test06_f_expressions(self):
|
2009-03-03 22:10:15 +00:00
|
|
|
"Testing F() expressions on GeometryFields."
|
|
|
|
# Constructing a dummy parcel border and getting the City instance for
|
|
|
|
# assigning the FK.
|
|
|
|
b1 = GEOSGeometry('POLYGON((-97.501205 33.052520,-97.501205 33.052576,-97.501150 33.052576,-97.501150 33.052520,-97.501205 33.052520))', srid=4326)
|
|
|
|
pcity = City.objects.get(name='Aurora')
|
|
|
|
|
|
|
|
# First parcel has incorrect center point that is equal to the City;
|
|
|
|
# it also has a second border that is different from the first as a
|
|
|
|
# 100ft buffer around the City.
|
|
|
|
c1 = pcity.location.point
|
|
|
|
c2 = c1.transform(2276, clone=True)
|
|
|
|
b2 = c2.buffer(100)
|
|
|
|
p1 = Parcel.objects.create(name='P1', city=pcity, center1=c1, center2=c2, border1=b1, border2=b2)
|
|
|
|
|
|
|
|
# Now creating a second Parcel where the borders are the same, just
|
|
|
|
# in different coordinate systems. The center points are also the
|
|
|
|
# the same (but in different coordinate systems), and this time they
|
|
|
|
# actually correspond to the centroid of the border.
|
|
|
|
c1 = b1.centroid
|
|
|
|
c2 = c1.transform(2276, clone=True)
|
|
|
|
p2 = Parcel.objects.create(name='P2', city=pcity, center1=c1, center2=c2, border1=b1, border2=b1)
|
|
|
|
|
|
|
|
# Should return the second Parcel, which has the center within the
|
|
|
|
# border.
|
|
|
|
qs = Parcel.objects.filter(center1__within=F('border1'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P2', qs[0].name)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
if not mysql:
|
2009-03-03 22:10:15 +00:00
|
|
|
# This time center2 is in a different coordinate system and needs
|
|
|
|
# to be wrapped in transformation SQL.
|
|
|
|
qs = Parcel.objects.filter(center2__within=F('border1'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P2', qs[0].name)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2009-03-03 22:10:15 +00:00
|
|
|
# Should return the first Parcel, which has the center point equal
|
|
|
|
# to the point in the City ForeignKey.
|
|
|
|
qs = Parcel.objects.filter(center1=F('city__location__point'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P1', qs[0].name)
|
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
if not mysql:
|
2009-03-03 22:10:15 +00:00
|
|
|
# This time the city column should be wrapped in transformation SQL.
|
|
|
|
qs = Parcel.objects.filter(border2__contains=F('city__location__point'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P1', qs[0].name)
|
|
|
|
|
2009-04-01 16:01:50 +00:00
|
|
|
def test07_values(self):
|
|
|
|
"Testing values() and values_list() and GeoQuerySets."
|
|
|
|
# GeoQuerySet and GeoValuesQuerySet, and GeoValuesListQuerySet respectively.
|
|
|
|
gqs = Location.objects.all()
|
|
|
|
gvqs = Location.objects.values()
|
|
|
|
gvlqs = Location.objects.values_list()
|
|
|
|
|
|
|
|
# Incrementing through each of the models, dictionaries, and tuples
|
|
|
|
# returned by the different types of GeoQuerySets.
|
|
|
|
for m, d, t in zip(gqs, gvqs, gvlqs):
|
|
|
|
# The values should be Geometry objects and not raw strings returned
|
|
|
|
# by the spatial database.
|
2009-12-22 15:18:51 +00:00
|
|
|
self.failUnless(isinstance(d['point'], Geometry))
|
|
|
|
self.failUnless(isinstance(t[1], Geometry))
|
2009-04-01 16:01:50 +00:00
|
|
|
self.assertEqual(m.point, d['point'])
|
|
|
|
self.assertEqual(m.point, t[1])
|
|
|
|
|
2009-04-04 03:21:31 +00:00
|
|
|
def test08_defer_only(self):
|
|
|
|
"Testing defer() and only() on Geographic models."
|
|
|
|
qs = Location.objects.all()
|
|
|
|
def_qs = Location.objects.defer('point')
|
|
|
|
for loc, def_loc in zip(qs, def_qs):
|
|
|
|
self.assertEqual(loc.point, def_loc.point)
|
2009-04-01 16:01:50 +00:00
|
|
|
|
2009-04-07 21:18:41 +00:00
|
|
|
def test09_pk_relations(self):
|
|
|
|
"Ensuring correct primary key column is selected across relations. See #10757."
|
|
|
|
# Adding two more cities, but this time making sure that their location
|
|
|
|
# ID values do not match their City ID values.
|
|
|
|
loc1 = Location.objects.create(point='POINT (-95.363151 29.763374)')
|
|
|
|
loc2 = Location.objects.create(point='POINT (-96.801611 32.782057)')
|
2009-06-03 04:49:16 +00:00
|
|
|
dallas = City.objects.create(name='Dallas', state='TX', location=loc2)
|
|
|
|
houston = City.objects.create(name='Houston', state='TX', location=loc1)
|
2009-04-07 21:18:41 +00:00
|
|
|
|
|
|
|
# The expected ID values -- notice the last two location IDs
|
|
|
|
# are out of order. We want to make sure that the related
|
|
|
|
# location ID column is selected instead of ID column for
|
|
|
|
# the city.
|
|
|
|
city_ids = (1, 2, 3, 4, 5)
|
|
|
|
loc_ids = (1, 2, 3, 5, 4)
|
|
|
|
ids_qs = City.objects.order_by('id').values('id', 'location__id')
|
|
|
|
for val_dict, c_id, l_id in zip(ids_qs, city_ids, loc_ids):
|
|
|
|
self.assertEqual(val_dict['id'], c_id)
|
|
|
|
self.assertEqual(val_dict['location__id'], l_id)
|
|
|
|
|
2009-04-14 19:14:32 +00:00
|
|
|
def test10_combine(self):
|
|
|
|
"Testing the combination of two GeoQuerySets. See #10807."
|
|
|
|
buf1 = City.objects.get(name='Aurora').location.point.buffer(0.1)
|
|
|
|
buf2 = City.objects.get(name='Kecksburg').location.point.buffer(0.1)
|
|
|
|
qs1 = City.objects.filter(location__point__within=buf1)
|
|
|
|
qs2 = City.objects.filter(location__point__within=buf2)
|
|
|
|
combined = qs1 | qs2
|
|
|
|
names = [c.name for c in combined]
|
|
|
|
self.assertEqual(2, len(names))
|
|
|
|
self.failUnless('Aurora' in names)
|
|
|
|
self.failUnless('Kecksburg' in names)
|
|
|
|
|
2009-04-21 22:35:04 +00:00
|
|
|
def test11_geoquery_pickle(self):
|
|
|
|
"Ensuring GeoQuery objects are unpickled correctly. See #10839."
|
|
|
|
import pickle
|
|
|
|
from django.contrib.gis.db.models.sql import GeoQuery
|
|
|
|
qs = City.objects.all()
|
|
|
|
q_str = pickle.dumps(qs.query)
|
|
|
|
q = pickle.loads(q_str)
|
|
|
|
self.assertEqual(GeoQuery, q.__class__)
|
|
|
|
|
2009-07-16 22:38:22 +00:00
|
|
|
# TODO: fix on Oracle -- get the following error because the SQL is ordered
|
|
|
|
# by a geometry object, which Oracle apparently doesn't like:
|
|
|
|
# ORA-22901: cannot compare nested table or VARRAY or LOB attributes of an object type
|
|
|
|
@no_oracle
|
|
|
|
def test12a_count(self):
|
|
|
|
"Testing `Count` aggregate use with the `GeoManager` on geo-fields."
|
2009-06-03 04:49:16 +00:00
|
|
|
# Creating a new City, 'Fort Worth', that uses the same location
|
|
|
|
# as Dallas.
|
|
|
|
dallas = City.objects.get(name='Dallas')
|
|
|
|
ftworth = City.objects.create(name='Fort Worth', state='TX', location=dallas.location)
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2009-06-03 04:49:16 +00:00
|
|
|
# Count annotation should be 2 for the Dallas location now.
|
|
|
|
loc = Location.objects.annotate(num_cities=Count('city')).get(id=dallas.location.id)
|
|
|
|
self.assertEqual(2, loc.num_cities)
|
|
|
|
|
2009-07-16 22:38:22 +00:00
|
|
|
def test12b_count(self):
|
|
|
|
"Testing `Count` aggregate use with the `GeoManager` on non geo-fields. See #11087."
|
2009-06-03 04:49:16 +00:00
|
|
|
# Creating some data for the Book/Author non-geo models that
|
|
|
|
# use GeoManager. See #11087.
|
|
|
|
tp = Author.objects.create(name='Trevor Paglen')
|
|
|
|
Book.objects.create(title='Torture Taxi', author=tp)
|
|
|
|
Book.objects.create(title='I Could Tell You But Then You Would Have to be Destroyed by Me', author=tp)
|
|
|
|
Book.objects.create(title='Blank Spots on the Map', author=tp)
|
|
|
|
wp = Author.objects.create(name='William Patry')
|
|
|
|
Book.objects.create(title='Patry on Copyright', author=wp)
|
2009-07-16 22:38:22 +00:00
|
|
|
|
2009-06-03 04:49:16 +00:00
|
|
|
# Should only be one author (Trevor Paglen) returned by this query, and
|
2009-07-16 22:38:22 +00:00
|
|
|
# the annotation should have 3 for the number of books. Also testing
|
|
|
|
# with a `GeoValuesQuerySet` (see #11489).
|
2009-06-03 04:49:16 +00:00
|
|
|
qs = Author.objects.annotate(num_books=Count('books')).filter(num_books__gt=1)
|
2009-07-16 22:38:22 +00:00
|
|
|
vqs = Author.objects.values('name').annotate(num_books=Count('books')).filter(num_books__gt=1)
|
2009-06-03 04:49:16 +00:00
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual(3, qs[0].num_books)
|
2009-07-16 22:38:22 +00:00
|
|
|
self.assertEqual(1, len(vqs))
|
|
|
|
self.assertEqual(3, vqs[0]['num_books'])
|
2009-06-03 04:49:16 +00:00
|
|
|
|
2009-07-16 22:38:22 +00:00
|
|
|
# TODO: The phantom model does appear on Oracle.
|
|
|
|
@no_oracle
|
2009-06-29 16:31:21 +00:00
|
|
|
def test13_select_related_null_fk(self):
|
|
|
|
"Testing `select_related` on a nullable ForeignKey via `GeoManager`. See #11381."
|
|
|
|
no_author = Book.objects.create(title='Without Author')
|
|
|
|
b = Book.objects.select_related('author').get(title='Without Author')
|
|
|
|
# Should be `None`, and not a 'dummy' model.
|
|
|
|
self.assertEqual(None, b.author)
|
|
|
|
|
2009-07-11 20:34:27 +00:00
|
|
|
@no_mysql
|
|
|
|
@no_oracle
|
|
|
|
@no_spatialite
|
|
|
|
def test14_collect(self):
|
|
|
|
"Testing the `collect` GeoQuerySet method and `Collect` aggregate."
|
|
|
|
# Reference query:
|
2009-12-22 15:18:51 +00:00
|
|
|
# SELECT AsText(ST_Collect("relatedapp_location"."point")) FROM "relatedapp_city" LEFT OUTER JOIN
|
|
|
|
# "relatedapp_location" ON ("relatedapp_city"."location_id" = "relatedapp_location"."id")
|
2009-07-11 20:34:27 +00:00
|
|
|
# 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)')
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2009-07-11 20:34:27 +00:00
|
|
|
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)
|
|
|
|
|
2010-07-20 19:05:46 +00:00
|
|
|
def test15_invalid_select_related(self):
|
|
|
|
"Testing doing select_related on the related name manager of a unique FK. See #13934."
|
|
|
|
qs = Article.objects.select_related('author__article')
|
|
|
|
# This triggers TypeError when `get_default_columns` has no `local_only`
|
|
|
|
# keyword. The TypeError is swallowed if QuerySet is actually
|
|
|
|
# evaluated as list generation swallows TypeError in CPython.
|
|
|
|
sql = str(qs.query)
|
|
|
|
|
2009-03-03 22:10:15 +00:00
|
|
|
# TODO: Related tests for KML, GML, and distance lookups.
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def suite():
|
|
|
|
s = unittest.TestSuite()
|
|
|
|
s.addTest(unittest.makeSuite(RelatedGeoModelTest))
|
|
|
|
return s
|