2015-02-21 18:18:54 +00:00
|
|
|
import tempfile
|
2017-01-07 11:11:46 +00:00
|
|
|
from io import StringIO
|
2011-10-17 18:45:22 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
from django.contrib.gis import gdal
|
2016-11-07 12:00:40 +00:00
|
|
|
from django.contrib.gis.db.models import Extent, MakeLine, Union, functions
|
2015-04-24 15:24:07 +00:00
|
|
|
from django.contrib.gis.geos import (
|
2016-06-17 15:03:50 +00:00
|
|
|
GeometryCollection,
|
|
|
|
GEOSGeometry,
|
|
|
|
LinearRing,
|
|
|
|
LineString,
|
|
|
|
MultiLineString,
|
|
|
|
MultiPoint,
|
|
|
|
MultiPolygon,
|
|
|
|
Point,
|
|
|
|
Polygon,
|
|
|
|
fromstr,
|
2015-04-24 15:24:07 +00:00
|
|
|
)
|
2014-10-01 19:24:10 +00:00
|
|
|
from django.core.management import call_command
|
2020-02-24 13:46:28 +00:00
|
|
|
from django.db import DatabaseError, NotSupportedError, connection
|
2019-11-24 15:21:32 +00:00
|
|
|
from django.db.models import F, OuterRef, Subquery
|
2016-11-07 12:00:40 +00:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2020-11-03 21:50:10 +00:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2020-11-14 14:08:30 +00:00
|
|
|
from ..utils import skipUnlessGISLookup
|
2015-04-24 15:24:07 +00:00
|
|
|
from .models import (
|
2019-11-24 15:21:32 +00:00
|
|
|
City,
|
|
|
|
Country,
|
|
|
|
Feature,
|
|
|
|
MinusOneSRID,
|
|
|
|
MultiFields,
|
|
|
|
NonConcreteModel,
|
|
|
|
PennsylvaniaCity,
|
|
|
|
State,
|
|
|
|
Track,
|
2015-04-24 15:24:07 +00:00
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
class GeoModelTest(TestCase):
|
2014-07-26 17:15:54 +00:00
|
|
|
fixtures = ["initial"]
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_fixtures(self):
|
2009-12-22 15:18:51 +00:00
|
|
|
"Testing geographic model initialization from fixtures."
|
|
|
|
# Ensuring that data was loaded from initial data fixtures.
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(2, Country.objects.count())
|
|
|
|
self.assertEqual(8, City.objects.count())
|
2009-12-22 15:18:51 +00:00
|
|
|
self.assertEqual(2, State.objects.count())
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_proxy(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing Lazy-Geometry support (using the GeometryProxy)."
|
2015-02-05 18:25:34 +00:00
|
|
|
# Testing on a Point
|
2008-08-05 18:13:06 +00:00
|
|
|
pnt = Point(0, 0)
|
|
|
|
nullcity = City(name="NullCity", point=pnt)
|
|
|
|
nullcity.save()
|
|
|
|
|
|
|
|
# Making sure TypeError is thrown when trying to set with an
|
|
|
|
# incompatible type.
|
|
|
|
for bad in [5, 2.0, LineString((0, 0), (1, 1))]:
|
2016-06-28 15:21:26 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, "Cannot set"):
|
2008-08-05 18:13:06 +00:00
|
|
|
nullcity.point = bad
|
|
|
|
|
|
|
|
# Now setting with a compatible GEOS Geometry, saving, and ensuring
|
|
|
|
# the save took, notice no SRID is explicitly set.
|
|
|
|
new = Point(5, 23)
|
|
|
|
nullcity.point = new
|
|
|
|
|
2009-03-09 00:03:03 +00:00
|
|
|
# Ensuring that the SRID is automatically set to that of the
|
2008-08-05 18:13:06 +00:00
|
|
|
# field after assignment, but before saving.
|
|
|
|
self.assertEqual(4326, nullcity.point.srid)
|
|
|
|
nullcity.save()
|
|
|
|
|
|
|
|
# Ensuring the point was saved correctly after saving
|
|
|
|
self.assertEqual(new, City.objects.get(name="NullCity").point)
|
|
|
|
|
|
|
|
# Setting the X and Y of the Point
|
|
|
|
nullcity.point.x = 23
|
|
|
|
nullcity.point.y = 5
|
|
|
|
# Checking assignments pre & post-save.
|
2016-11-23 08:23:06 +00:00
|
|
|
self.assertNotEqual(
|
|
|
|
Point(23, 5, srid=4326), City.objects.get(name="NullCity").point
|
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
nullcity.save()
|
2016-11-23 08:23:06 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Point(23, 5, srid=4326), City.objects.get(name="NullCity").point
|
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
nullcity.delete()
|
|
|
|
|
2015-02-05 18:25:34 +00:00
|
|
|
# Testing on a Polygon
|
2017-05-30 12:22:40 +00:00
|
|
|
shell = LinearRing((0, 0), (0, 90), (100, 90), (100, 0), (0, 0))
|
2008-08-05 18:13:06 +00:00
|
|
|
inner = LinearRing((40, 40), (40, 60), (60, 60), (60, 40), (40, 40))
|
|
|
|
|
|
|
|
# Creating a State object using a built Polygon
|
|
|
|
ply = Polygon(shell, inner)
|
|
|
|
nullstate = State(name="NullState", poly=ply)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(4326, nullstate.poly.srid) # SRID auto-set from None
|
2008-08-05 18:13:06 +00:00
|
|
|
nullstate.save()
|
|
|
|
|
|
|
|
ns = State.objects.get(name="NullState")
|
2020-10-22 00:20:26 +00:00
|
|
|
self.assertEqual(connection.ops.Adapter._fix_polygon(ply), ns.poly)
|
2009-03-09 00:03:03 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Testing the `ogr` and `srs` lazy-geometry properties.
|
2017-05-03 01:27:11 +00:00
|
|
|
self.assertIsInstance(ns.poly.ogr, gdal.OGRGeometry)
|
|
|
|
self.assertEqual(ns.poly.wkb, ns.poly.ogr.wkb)
|
|
|
|
self.assertIsInstance(ns.poly.srs, gdal.SpatialReference)
|
|
|
|
self.assertEqual("WGS 84", ns.poly.srs.name)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Changing the interior ring on the poly attribute.
|
|
|
|
new_inner = LinearRing((30, 30), (30, 70), (70, 70), (70, 30), (30, 30))
|
|
|
|
ns.poly[1] = new_inner
|
|
|
|
ply[1] = new_inner
|
|
|
|
self.assertEqual(4326, ns.poly.srid)
|
|
|
|
ns.save()
|
2020-10-22 00:20:26 +00:00
|
|
|
self.assertEqual(
|
|
|
|
connection.ops.Adapter._fix_polygon(ply),
|
|
|
|
State.objects.get(name="NullState").poly,
|
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
ns.delete()
|
|
|
|
|
2014-08-21 12:26:49 +00:00
|
|
|
@skipUnlessDBFeature("supports_transform")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_lookup_insert_transform(self):
|
|
|
|
"Testing automatic transform for lookups and inserts."
|
|
|
|
# San Antonio in 'WGS84' (SRID 4326)
|
|
|
|
sa_4326 = "POINT (-98.493183 29.424170)"
|
2013-11-02 21:02:56 +00:00
|
|
|
wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
|
2016-12-15 14:32:12 +00:00
|
|
|
# San Antonio in 'WGS 84 / Pseudo-Mercator' (SRID 3857)
|
|
|
|
other_srid_pnt = wgs_pnt.transform(3857, clone=True)
|
2012-08-03 08:47:27 +00:00
|
|
|
# Constructing & querying with a point from a different SRID. Oracle
|
|
|
|
# `SDO_OVERLAPBDYINTERSECT` operates differently from
|
|
|
|
# `ST_Intersects`, so contains is used instead.
|
2020-11-14 14:08:30 +00:00
|
|
|
if connection.ops.oracle:
|
2016-12-15 14:32:12 +00:00
|
|
|
tx = Country.objects.get(mpoly__contains=other_srid_pnt)
|
2009-07-27 04:21:31 +00:00
|
|
|
else:
|
2016-12-15 14:32:12 +00:00
|
|
|
tx = Country.objects.get(mpoly__intersects=other_srid_pnt)
|
2012-08-03 08:47:27 +00:00
|
|
|
self.assertEqual("Texas", tx.name)
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# Creating San Antonio. Remember the Alamo.
|
2016-12-15 14:32:12 +00:00
|
|
|
sa = City.objects.create(name="San Antonio", point=other_srid_pnt)
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# Now verifying that San Antonio was transformed correctly
|
|
|
|
sa = City.objects.get(name="San Antonio")
|
|
|
|
self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
|
|
|
|
self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)
|
2009-04-02 16:50:44 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# If the GeometryField SRID is -1, then we shouldn't perform any
|
|
|
|
# transformation if the SRID of the input geometry is different.
|
2014-08-21 16:47:57 +00:00
|
|
|
m1 = MinusOneSRID(geom=Point(17, 23, srid=4326))
|
|
|
|
m1.save()
|
|
|
|
self.assertEqual(-1, m1.geom.srid)
|
2009-04-02 16:50:44 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_createnull(self):
|
|
|
|
"Testing creating a model instance and the geometry being None"
|
|
|
|
c = City()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(c.point)
|
2009-04-02 16:50:44 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_geometryfield(self):
|
|
|
|
"Testing the general GeometryField."
|
|
|
|
Feature(name="Point", geom=Point(1, 1)).save()
|
|
|
|
Feature(name="LineString", geom=LineString((0, 0), (1, 1), (5, 5))).save()
|
|
|
|
Feature(
|
|
|
|
name="Polygon",
|
|
|
|
geom=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))),
|
|
|
|
).save()
|
|
|
|
Feature(
|
|
|
|
name="GeometryCollection",
|
|
|
|
geom=GeometryCollection(
|
|
|
|
Point(2, 2),
|
|
|
|
LineString((0, 0), (2, 2)),
|
|
|
|
Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2012-08-03 08:47:27 +00:00
|
|
|
).save()
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
f_1 = Feature.objects.get(name="Point")
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIsInstance(f_1.geom, Point)
|
2012-08-03 08:47:27 +00:00
|
|
|
self.assertEqual((1.0, 1.0), f_1.geom.tuple)
|
|
|
|
f_2 = Feature.objects.get(name="LineString")
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIsInstance(f_2.geom, LineString)
|
2012-08-03 08:47:27 +00:00
|
|
|
self.assertEqual(((0.0, 0.0), (1.0, 1.0), (5.0, 5.0)), f_2.geom.tuple)
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
f_3 = Feature.objects.get(name="Polygon")
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIsInstance(f_3.geom, Polygon)
|
2012-08-03 08:47:27 +00:00
|
|
|
f_4 = Feature.objects.get(name="GeometryCollection")
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIsInstance(f_4.geom, GeometryCollection)
|
2012-08-03 08:47:27 +00:00
|
|
|
self.assertEqual(f_3.geom, f_4.geom[2])
|
2009-04-07 20:12:08 +00:00
|
|
|
|
2014-08-21 12:26:49 +00:00
|
|
|
@skipUnlessDBFeature("supports_transform")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_inherited_geofields(self):
|
2016-11-07 12:00:40 +00:00
|
|
|
"Database functions on inherited Geometry fields."
|
2012-08-03 08:47:27 +00:00
|
|
|
# Creating a Pennsylvanian city.
|
2013-09-08 15:05:16 +00:00
|
|
|
PennsylvaniaCity.objects.create(
|
|
|
|
name="Mansfield", county="Tioga", point="POINT(-77.071445 41.823881)"
|
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# All transformation SQL will need to be performed on the
|
|
|
|
# _parent_ table.
|
2016-11-07 12:00:40 +00:00
|
|
|
qs = PennsylvaniaCity.objects.annotate(
|
|
|
|
new_point=functions.Transform("point", srid=32128)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
self.assertEqual(1, qs.count())
|
2013-10-17 08:17:41 +00:00
|
|
|
for pc in qs:
|
2016-11-07 12:00:40 +00:00
|
|
|
self.assertEqual(32128, pc.new_point.srid)
|
2012-08-25 08:52:57 +00:00
|
|
|
|
|
|
|
def test_raw_sql_query(self):
|
|
|
|
"Testing raw SQL query."
|
|
|
|
cities1 = City.objects.all()
|
2017-10-29 18:25:57 +00:00
|
|
|
point_select = connection.ops.select % "point"
|
|
|
|
cities2 = list(
|
|
|
|
City.objects.raw(
|
|
|
|
"select id, name, %s as point from geoapp_city" % point_select
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2017-10-29 18:25:57 +00:00
|
|
|
self.assertEqual(len(cities1), len(cities2))
|
|
|
|
with self.assertNumQueries(0): # Ensure point isn't deferred.
|
|
|
|
self.assertIsInstance(cities2[0].point, Point)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2022-06-21 07:53:44 +00:00
|
|
|
def test_gis_query_as_string(self):
|
|
|
|
"""GIS queries can be represented as strings."""
|
|
|
|
query = City.objects.filter(point__within=Polygon.from_bbox((0, 0, 2, 2)))
|
|
|
|
self.assertIn(
|
|
|
|
connection.ops.quote_name(City._meta.db_table),
|
|
|
|
str(query.query),
|
|
|
|
)
|
|
|
|
|
2014-10-01 19:24:10 +00:00
|
|
|
def test_dumpdata_loaddata_cycle(self):
|
|
|
|
"""
|
|
|
|
Test a dumpdata/loaddata cycle with geographic data.
|
|
|
|
"""
|
2017-01-07 11:11:46 +00:00
|
|
|
out = StringIO()
|
2022-02-22 09:29:38 +00:00
|
|
|
original_data = list(City.objects.order_by("name"))
|
2014-10-01 19:24:10 +00:00
|
|
|
call_command("dumpdata", "geoapp.City", stdout=out)
|
|
|
|
result = out.getvalue()
|
|
|
|
houston = City.objects.get(name="Houston")
|
2014-10-11 19:39:38 +00:00
|
|
|
self.assertIn('"point": "%s"' % houston.point.ewkt, result)
|
2014-10-01 19:24:10 +00:00
|
|
|
|
|
|
|
# Reload now dumped data
|
2015-02-21 18:18:54 +00:00
|
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json") as tmp:
|
|
|
|
tmp.write(result)
|
|
|
|
tmp.seek(0)
|
|
|
|
call_command("loaddata", tmp.name, verbosity=0)
|
2022-02-22 09:29:38 +00:00
|
|
|
self.assertEqual(original_data, list(City.objects.order_by("name")))
|
2014-10-01 19:24:10 +00:00
|
|
|
|
2016-06-17 15:03:50 +00:00
|
|
|
@skipUnlessDBFeature("supports_empty_geometries")
|
|
|
|
def test_empty_geometries(self):
|
|
|
|
geometry_classes = [
|
|
|
|
Point,
|
|
|
|
LineString,
|
|
|
|
LinearRing,
|
|
|
|
Polygon,
|
|
|
|
MultiPoint,
|
|
|
|
MultiLineString,
|
|
|
|
MultiPolygon,
|
|
|
|
GeometryCollection,
|
|
|
|
]
|
|
|
|
for klass in geometry_classes:
|
|
|
|
g = klass(srid=4326)
|
|
|
|
feature = Feature.objects.create(name="Empty %s" % klass.__name__, geom=g)
|
|
|
|
feature.refresh_from_db()
|
|
|
|
if klass is LinearRing:
|
|
|
|
# LinearRing isn't representable in WKB, so GEOSGeomtry.wkb
|
|
|
|
# uses LineString instead.
|
|
|
|
g = LineString(srid=4326)
|
|
|
|
self.assertEqual(feature.geom, g)
|
|
|
|
self.assertEqual(feature.geom.srid, g.srid)
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
class GeoLookupTest(TestCase):
|
2014-07-26 17:15:54 +00:00
|
|
|
fixtures = ["initial"]
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_disjoint_lookup(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing the `disjoint` lookup type."
|
|
|
|
ptown = City.objects.get(name="Pueblo")
|
|
|
|
qs1 = City.objects.filter(point__disjoint=ptown.point)
|
|
|
|
self.assertEqual(7, qs1.count())
|
2019-01-19 14:28:42 +00:00
|
|
|
qs2 = State.objects.filter(poly__disjoint=ptown.point)
|
|
|
|
self.assertEqual(1, qs2.count())
|
|
|
|
self.assertEqual("Kansas", qs2[0].name)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_contains_contained_lookups(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing the 'contained', 'contains', and 'bbcontains' lookup types."
|
|
|
|
# Getting Texas, yes we were a country -- once ;)
|
|
|
|
texas = Country.objects.get(name="Texas")
|
2009-03-09 00:03:03 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Seeing what cities are in Texas, should get Houston and Dallas,
|
|
|
|
# and Oklahoma City because 'contained' only checks on the
|
|
|
|
# _bounding box_ of the Geometries.
|
2014-08-21 16:47:57 +00:00
|
|
|
if connection.features.supports_contained_lookup:
|
2008-08-05 18:13:06 +00:00
|
|
|
qs = City.objects.filter(point__contained=texas.mpoly)
|
|
|
|
self.assertEqual(3, qs.count())
|
|
|
|
cities = ["Houston", "Dallas", "Oklahoma City"]
|
2013-10-17 08:17:41 +00:00
|
|
|
for c in qs:
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIn(c.name, cities)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Pulling out some cities.
|
|
|
|
houston = City.objects.get(name="Houston")
|
|
|
|
wellington = City.objects.get(name="Wellington")
|
|
|
|
pueblo = City.objects.get(name="Pueblo")
|
|
|
|
okcity = City.objects.get(name="Oklahoma City")
|
|
|
|
lawrence = City.objects.get(name="Lawrence")
|
|
|
|
|
|
|
|
# Now testing contains on the countries using the points for
|
|
|
|
# Houston and Wellington.
|
2013-11-02 21:02:56 +00:00
|
|
|
tx = Country.objects.get(mpoly__contains=houston.point) # Query w/GEOSGeometry
|
|
|
|
nz = Country.objects.get(
|
|
|
|
mpoly__contains=wellington.point.hex
|
|
|
|
) # Query w/EWKBHEX
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual("Texas", tx.name)
|
|
|
|
self.assertEqual("New Zealand", nz.name)
|
2009-03-30 17:15:49 +00:00
|
|
|
|
2015-10-30 19:04:00 +00:00
|
|
|
# Testing `contains` on the states using the point for Lawrence.
|
|
|
|
ks = State.objects.get(poly__contains=lawrence.point)
|
|
|
|
self.assertEqual("Kansas", ks.name)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Pueblo and Oklahoma City (even though OK City is within the bounding
|
2009-12-22 15:18:51 +00:00
|
|
|
# box of Texas) are not contained in Texas or New Zealand.
|
2014-08-21 16:47:57 +00:00
|
|
|
self.assertEqual(
|
|
|
|
len(Country.objects.filter(mpoly__contains=pueblo.point)), 0
|
|
|
|
) # Query w/GEOSGeometry object
|
2019-01-19 14:28:42 +00:00
|
|
|
self.assertEqual(
|
|
|
|
len(Country.objects.filter(mpoly__contains=okcity.point.wkt)), 0
|
|
|
|
) # Query w/WKT
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# OK City is contained w/in bounding box of Texas.
|
2014-08-21 16:47:57 +00:00
|
|
|
if connection.features.supports_bbcontains_lookup:
|
2008-08-05 18:13:06 +00:00
|
|
|
qs = Country.objects.filter(mpoly__bbcontains=okcity.point)
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual("Texas", qs[0].name)
|
|
|
|
|
2015-01-17 14:43:02 +00:00
|
|
|
@skipUnlessDBFeature("supports_crosses_lookup")
|
|
|
|
def test_crosses_lookup(self):
|
|
|
|
Track.objects.create(name="Line1", line=LineString([(-95, 29), (-60, 0)]))
|
|
|
|
self.assertEqual(
|
|
|
|
Track.objects.filter(
|
|
|
|
line__crosses=LineString([(-95, 0), (-60, 29)])
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Track.objects.filter(
|
|
|
|
line__crosses=LineString([(-95, 30), (0, 30)])
|
|
|
|
).count(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
2016-04-06 11:50:25 +00:00
|
|
|
@skipUnlessDBFeature("supports_isvalid_lookup")
|
|
|
|
def test_isvalid_lookup(self):
|
|
|
|
invalid_geom = fromstr("POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))")
|
|
|
|
State.objects.create(name="invalid", poly=invalid_geom)
|
2016-11-30 16:22:56 +00:00
|
|
|
qs = State.objects.all()
|
2022-07-08 11:30:12 +00:00
|
|
|
if connection.ops.oracle:
|
2016-11-30 16:22:56 +00:00
|
|
|
# Kansas has adjacent vertices with distance 6.99244813842e-12
|
|
|
|
# which is smaller than the default Oracle tolerance.
|
|
|
|
qs = qs.exclude(name="Kansas")
|
|
|
|
self.assertEqual(
|
|
|
|
State.objects.filter(name="Kansas", poly__isvalid=False).count(), 1
|
|
|
|
)
|
|
|
|
self.assertEqual(qs.filter(poly__isvalid=False).count(), 1)
|
|
|
|
self.assertEqual(qs.filter(poly__isvalid=True).count(), qs.count() - 1)
|
2016-04-06 11:50:25 +00:00
|
|
|
|
2021-01-02 20:39:48 +00:00
|
|
|
@skipUnlessGISLookup("left", "right")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_left_right_lookups(self):
|
|
|
|
"Testing the 'left' and 'right' lookup types."
|
|
|
|
# Left: A << B => true if xmax(A) < xmin(B)
|
|
|
|
# Right: A >> B => true if xmin(A) > xmax(B)
|
|
|
|
# See: BOX2D_left() and BOX2D_right() in lwgeom_box2dfloat4.c in PostGIS source.
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# Getting the borders for Colorado & Kansas
|
|
|
|
co_border = State.objects.get(name="Colorado").poly
|
|
|
|
ks_border = State.objects.get(name="Kansas").poly
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# Note: Wellington has an 'X' value of 174, so it will not be considered
|
|
|
|
# to the left of CO.
|
2009-03-09 00:03:03 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# These cities should be strictly to the right of the CO border.
|
|
|
|
cities = [
|
|
|
|
"Houston",
|
|
|
|
"Dallas",
|
|
|
|
"Oklahoma City",
|
|
|
|
"Lawrence",
|
|
|
|
"Chicago",
|
|
|
|
"Wellington",
|
|
|
|
]
|
|
|
|
qs = City.objects.filter(point__right=co_border)
|
|
|
|
self.assertEqual(6, len(qs))
|
2013-10-17 08:17:41 +00:00
|
|
|
for c in qs:
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIn(c.name, cities)
|
2009-03-09 00:03:03 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# These cities should be strictly to the right of the KS border.
|
|
|
|
cities = ["Chicago", "Wellington"]
|
|
|
|
qs = City.objects.filter(point__right=ks_border)
|
|
|
|
self.assertEqual(2, len(qs))
|
2013-10-17 08:17:41 +00:00
|
|
|
for c in qs:
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIn(c.name, cities)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
# Note: Wellington has an 'X' value of 174, so it will not be considered
|
|
|
|
# to the left of CO.
|
|
|
|
vic = City.objects.get(point__left=co_border)
|
|
|
|
self.assertEqual("Victoria", vic.name)
|
|
|
|
|
|
|
|
cities = ["Pueblo", "Victoria"]
|
|
|
|
qs = City.objects.filter(point__left=ks_border)
|
|
|
|
self.assertEqual(2, len(qs))
|
2013-10-17 08:17:41 +00:00
|
|
|
for c in qs:
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIn(c.name, cities)
|
2012-08-03 08:47:27 +00:00
|
|
|
|
2015-10-22 12:42:18 +00:00
|
|
|
@skipUnlessGISLookup("strictly_above", "strictly_below")
|
|
|
|
def test_strictly_above_below_lookups(self):
|
|
|
|
dallas = City.objects.get(name="Dallas")
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2015-10-22 12:42:18 +00:00
|
|
|
City.objects.filter(point__strictly_above=dallas.point).order_by("name"),
|
|
|
|
["Chicago", "Lawrence", "Oklahoma City", "Pueblo", "Victoria"],
|
|
|
|
lambda b: b.name,
|
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2015-10-22 12:42:18 +00:00
|
|
|
City.objects.filter(point__strictly_below=dallas.point).order_by("name"),
|
|
|
|
["Houston", "Wellington"],
|
|
|
|
lambda b: b.name,
|
|
|
|
)
|
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_equals_lookups(self):
|
|
|
|
"Testing the 'same_as' and 'equals' lookup types."
|
|
|
|
pnt = fromstr("POINT (-95.363151 29.763374)", srid=4326)
|
|
|
|
c1 = City.objects.get(point=pnt)
|
|
|
|
c2 = City.objects.get(point__same_as=pnt)
|
|
|
|
c3 = City.objects.get(point__equals=pnt)
|
2013-10-17 08:17:41 +00:00
|
|
|
for c in [c1, c2, c3]:
|
|
|
|
self.assertEqual("Houston", c.name)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2014-08-21 12:26:49 +00:00
|
|
|
@skipUnlessDBFeature("supports_null_geometries")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_null_geometries(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing NULL geometry support, and the `isnull` lookup type."
|
2009-12-22 15:18:51 +00:00
|
|
|
# Creating a state with a NULL boundary.
|
|
|
|
State.objects.create(name="Puerto Rico")
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Querying for both NULL and Non-NULL values.
|
|
|
|
nullqs = State.objects.filter(poly__isnull=True)
|
|
|
|
validqs = State.objects.filter(poly__isnull=False)
|
|
|
|
|
|
|
|
# Puerto Rico should be NULL (it's a commonwealth unincorporated territory)
|
|
|
|
self.assertEqual(1, len(nullqs))
|
|
|
|
self.assertEqual("Puerto Rico", nullqs[0].name)
|
2017-12-06 09:59:30 +00:00
|
|
|
# GeometryField=None is an alias for __isnull=True.
|
|
|
|
self.assertCountEqual(State.objects.filter(poly=None), nullqs)
|
|
|
|
self.assertCountEqual(State.objects.exclude(poly=None), validqs)
|
2009-03-09 00:03:03 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# The valid states should be Colorado & Kansas
|
|
|
|
self.assertEqual(2, len(validqs))
|
|
|
|
state_names = [s.name for s in validqs]
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIn("Colorado", state_names)
|
|
|
|
self.assertIn("Kansas", state_names)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Saving another commonwealth w/a NULL geometry.
|
2009-04-24 20:24:50 +00:00
|
|
|
nmi = State.objects.create(name="Northern Mariana Islands", poly=None)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(nmi.poly)
|
2009-04-24 20:24:50 +00:00
|
|
|
|
2014-03-02 14:25:53 +00:00
|
|
|
# Assigning a geometry and saving -- then UPDATE back to NULL.
|
2009-04-24 20:24:50 +00:00
|
|
|
nmi.poly = "POLYGON((0 0,1 0,1 1,1 0,0 0))"
|
|
|
|
nmi.save()
|
|
|
|
State.objects.filter(name="Northern Mariana Islands").update(poly=None)
|
2014-09-24 16:44:58 +00:00
|
|
|
self.assertIsNone(State.objects.get(name="Northern Mariana Islands").poly)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2017-07-10 14:53:29 +00:00
|
|
|
@skipUnlessDBFeature(
|
|
|
|
"supports_null_geometries", "supports_crosses_lookup", "supports_relate_lookup"
|
|
|
|
)
|
|
|
|
def test_null_geometries_excluded_in_lookups(self):
|
|
|
|
"""NULL features are excluded in spatial lookup functions."""
|
|
|
|
null = State.objects.create(name="NULL", poly=None)
|
|
|
|
queries = [
|
|
|
|
("equals", Point(1, 1)),
|
|
|
|
("disjoint", Point(1, 1)),
|
|
|
|
("touches", Point(1, 1)),
|
|
|
|
("crosses", LineString((0, 0), (1, 1), (5, 5))),
|
|
|
|
("within", Point(1, 1)),
|
|
|
|
("overlaps", LineString((0, 0), (1, 1), (5, 5))),
|
|
|
|
("contains", LineString((0, 0), (1, 1), (5, 5))),
|
|
|
|
("intersects", LineString((0, 0), (1, 1), (5, 5))),
|
|
|
|
("relate", (Point(1, 1), "T*T***FF*")),
|
|
|
|
("same_as", Point(1, 1)),
|
|
|
|
("exact", Point(1, 1)),
|
2018-06-21 13:25:31 +00:00
|
|
|
("coveredby", Point(1, 1)),
|
|
|
|
("covers", Point(1, 1)),
|
2017-07-10 14:53:29 +00:00
|
|
|
]
|
|
|
|
for lookup, geom in queries:
|
|
|
|
with self.subTest(lookup=lookup):
|
|
|
|
self.assertNotIn(
|
|
|
|
null, State.objects.filter(**{"poly__%s" % lookup: geom})
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2017-07-10 14:53:29 +00:00
|
|
|
|
2020-01-16 13:34:54 +00:00
|
|
|
def test_wkt_string_in_lookup(self):
|
|
|
|
# Valid WKT strings don't emit error logs.
|
2021-01-07 16:54:40 +00:00
|
|
|
with self.assertNoLogs("django.contrib.gis", "ERROR"):
|
|
|
|
State.objects.filter(poly__intersects="LINESTRING(0 0, 1 1, 5 5)")
|
2020-01-16 13:34:54 +00:00
|
|
|
|
2014-08-21 12:26:49 +00:00
|
|
|
@skipUnlessDBFeature("supports_relate_lookup")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_relate_lookup(self):
|
2008-08-05 18:13:06 +00:00
|
|
|
"Testing the 'relate' lookup type."
|
2009-03-09 00:03:03 +00:00
|
|
|
# To make things more interesting, we will have our Texas reference point in
|
2008-08-05 18:13:06 +00:00
|
|
|
# different SRIDs.
|
|
|
|
pnt1 = fromstr("POINT (649287.0363174 4177429.4494686)", srid=2847)
|
|
|
|
pnt2 = fromstr("POINT(-98.4919715741052 29.4333344025053)", srid=4326)
|
|
|
|
|
2016-11-07 12:00:40 +00:00
|
|
|
# Not passing in a geometry as first param raises a TypeError when
|
|
|
|
# initializing the QuerySet.
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
Country.objects.filter(mpoly__relate=(23, "foo"))
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Making sure the right exception is raised for the given
|
|
|
|
# bad arguments.
|
2009-12-22 15:18:51 +00:00
|
|
|
for bad_args, e in [
|
|
|
|
((pnt1, 0), ValueError),
|
|
|
|
((pnt2, "T*T***FF*", 0), ValueError),
|
|
|
|
]:
|
2008-08-05 18:13:06 +00:00
|
|
|
qs = Country.objects.filter(mpoly__relate=bad_args)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(e):
|
|
|
|
qs.count()
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2020-11-14 14:08:30 +00:00
|
|
|
contains_mask = "T*T***FF*"
|
|
|
|
within_mask = "T*F**F***"
|
|
|
|
intersects_mask = "T********"
|
|
|
|
# Relate works differently on Oracle.
|
|
|
|
if connection.ops.oracle:
|
2008-08-05 18:13:06 +00:00
|
|
|
contains_mask = "contains"
|
|
|
|
within_mask = "inside"
|
|
|
|
# TODO: This is not quite the same as the PostGIS mask above
|
|
|
|
intersects_mask = "overlapbdyintersect"
|
|
|
|
|
|
|
|
# Testing contains relation mask.
|
2019-10-17 12:02:37 +00:00
|
|
|
if connection.features.supports_transform:
|
|
|
|
self.assertEqual(
|
|
|
|
Country.objects.get(mpoly__relate=(pnt1, contains_mask)).name,
|
|
|
|
"Texas",
|
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(
|
|
|
|
"Texas", Country.objects.get(mpoly__relate=(pnt2, contains_mask)).name
|
|
|
|
)
|
|
|
|
|
|
|
|
# Testing within relation mask.
|
|
|
|
ks = State.objects.get(name="Kansas")
|
|
|
|
self.assertEqual(
|
|
|
|
"Lawrence", City.objects.get(point__relate=(ks.poly, within_mask)).name
|
|
|
|
)
|
|
|
|
|
|
|
|
# Testing intersection relation mask.
|
2020-11-14 14:08:30 +00:00
|
|
|
if not connection.ops.oracle:
|
2019-10-17 12:02:37 +00:00
|
|
|
if connection.features.supports_transform:
|
|
|
|
self.assertEqual(
|
|
|
|
Country.objects.get(mpoly__relate=(pnt1, intersects_mask)).name,
|
|
|
|
"Texas",
|
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
self.assertEqual(
|
|
|
|
"Texas", Country.objects.get(mpoly__relate=(pnt2, intersects_mask)).name
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
"Lawrence",
|
|
|
|
City.objects.get(point__relate=(ks.poly, intersects_mask)).name,
|
|
|
|
)
|
|
|
|
|
2017-07-26 10:49:20 +00:00
|
|
|
# With a complex geometry expression
|
2020-11-14 14:08:30 +00:00
|
|
|
mask = "anyinteract" if connection.ops.oracle else within_mask
|
2017-07-26 10:49:20 +00:00
|
|
|
self.assertFalse(
|
|
|
|
City.objects.exclude(
|
|
|
|
point__relate=(functions.Union("point", "point"), mask)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
|
|
|
)
|
2017-07-26 10:49:20 +00:00
|
|
|
|
|
|
|
def test_gis_lookups_with_complex_expressions(self):
|
|
|
|
multiple_arg_lookups = {
|
|
|
|
"dwithin",
|
|
|
|
"relate",
|
|
|
|
} # These lookups are tested elsewhere.
|
|
|
|
lookups = connection.ops.gis_operators.keys() - multiple_arg_lookups
|
|
|
|
self.assertTrue(lookups, "No lookups found")
|
|
|
|
for lookup in lookups:
|
|
|
|
with self.subTest(lookup):
|
|
|
|
City.objects.filter(
|
|
|
|
**{"point__" + lookup: functions.Union("point", "point")}
|
|
|
|
).exists()
|
|
|
|
|
2019-11-24 15:21:32 +00:00
|
|
|
def test_subquery_annotation(self):
|
|
|
|
multifields = MultiFields.objects.create(
|
|
|
|
city=City.objects.create(point=Point(1, 1)),
|
|
|
|
point=Point(2, 2),
|
|
|
|
poly=Polygon.from_bbox((0, 0, 2, 2)),
|
|
|
|
)
|
|
|
|
qs = MultiFields.objects.annotate(
|
|
|
|
city_point=Subquery(
|
|
|
|
City.objects.filter(
|
|
|
|
id=OuterRef("city"),
|
|
|
|
).values("point")
|
|
|
|
),
|
|
|
|
).filter(
|
|
|
|
city_point__within=F("poly"),
|
|
|
|
)
|
|
|
|
self.assertEqual(qs.get(), multifields)
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
class GeoQuerySetTest(TestCase):
|
2016-11-07 12:00:40 +00:00
|
|
|
# TODO: GeoQuerySet is removed, organize these test better.
|
2014-07-26 17:15:54 +00:00
|
|
|
fixtures = ["initial"]
|
|
|
|
|
2014-08-19 16:47:23 +00:00
|
|
|
@skipUnlessDBFeature("supports_extent_aggr")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_extent(self):
|
2015-01-14 19:48:55 +00:00
|
|
|
"""
|
2015-09-02 23:55:55 +00:00
|
|
|
Testing the `Extent` aggregate.
|
2015-01-14 19:48:55 +00:00
|
|
|
"""
|
2012-08-03 08:47:27 +00:00
|
|
|
# Reference query:
|
|
|
|
# SELECT ST_extent(point)
|
|
|
|
# FROM geoapp_city
|
|
|
|
# WHERE (name='Houston' or name='Dallas');`
|
|
|
|
# => BOX(-96.8016128540039 29.7633724212646,-95.3631439208984 32.7820587158203)
|
|
|
|
expected = (
|
|
|
|
-96.8016128540039,
|
|
|
|
29.7633724212646,
|
|
|
|
-95.3631439208984,
|
|
|
|
32.782058715820,
|
|
|
|
)
|
|
|
|
|
|
|
|
qs = City.objects.filter(name__in=("Houston", "Dallas"))
|
2015-09-02 23:55:55 +00:00
|
|
|
extent = qs.aggregate(Extent("point"))["point__extent"]
|
|
|
|
for val, exp in zip(extent, expected):
|
|
|
|
self.assertAlmostEqual(exp, val, 4)
|
2015-01-14 19:48:55 +00:00
|
|
|
self.assertIsNone(
|
|
|
|
City.objects.filter(name=("Smalltown")).aggregate(Extent("point"))[
|
|
|
|
"point__extent"
|
2022-02-03 19:24:19 +00:00
|
|
|
]
|
2015-01-14 19:48:55 +00:00
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2015-01-16 19:10:25 +00:00
|
|
|
@skipUnlessDBFeature("supports_extent_aggr")
|
|
|
|
def test_extent_with_limit(self):
|
|
|
|
"""
|
|
|
|
Testing if extent supports limit.
|
|
|
|
"""
|
2022-02-22 09:29:38 +00:00
|
|
|
extent1 = City.objects.aggregate(Extent("point"))["point__extent"]
|
2015-01-16 19:10:25 +00:00
|
|
|
extent2 = City.objects.all()[:3].aggregate(Extent("point"))["point__extent"]
|
|
|
|
self.assertNotEqual(extent1, extent2)
|
|
|
|
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_make_line(self):
|
2015-01-14 19:48:55 +00:00
|
|
|
"""
|
2015-09-02 23:55:55 +00:00
|
|
|
Testing the `MakeLine` aggregate.
|
2015-01-14 19:48:55 +00:00
|
|
|
"""
|
2015-01-17 09:01:17 +00:00
|
|
|
if not connection.features.supports_make_line_aggr:
|
2017-10-06 16:47:08 +00:00
|
|
|
with self.assertRaises(NotSupportedError):
|
2022-02-22 09:29:38 +00:00
|
|
|
City.objects.aggregate(MakeLine("point"))
|
2015-01-17 09:01:17 +00:00
|
|
|
return
|
|
|
|
|
2015-01-14 19:48:55 +00:00
|
|
|
# MakeLine on an inappropriate field returns simply None
|
|
|
|
self.assertIsNone(State.objects.aggregate(MakeLine("poly"))["poly__makeline"])
|
2012-08-03 08:47:27 +00:00
|
|
|
# Reference query:
|
|
|
|
# SELECT AsText(ST_MakeLine(geoapp_city.point)) FROM geoapp_city;
|
2015-09-02 23:55:55 +00:00
|
|
|
line = City.objects.aggregate(MakeLine("point"))["point__makeline"]
|
2023-01-03 04:46:40 +00:00
|
|
|
ref_points = City.objects.values_list("point", flat=True)
|
|
|
|
self.assertIsInstance(line, LineString)
|
|
|
|
self.assertEqual(len(line), ref_points.count())
|
|
|
|
# Compare pairs of manually sorted points, as the default ordering is
|
|
|
|
# flaky.
|
|
|
|
for point, ref_city in zip(sorted(line), sorted(ref_points)):
|
|
|
|
point_x, point_y = point
|
|
|
|
self.assertAlmostEqual(point_x, ref_city.x, 5),
|
|
|
|
self.assertAlmostEqual(point_y, ref_city.y, 5),
|
2012-08-03 08:47:27 +00:00
|
|
|
|
2017-01-03 14:49:00 +00:00
|
|
|
@skipUnlessDBFeature("supports_union_aggr")
|
2012-08-03 08:47:27 +00:00
|
|
|
def test_unionagg(self):
|
2015-01-14 19:48:55 +00:00
|
|
|
"""
|
2015-09-02 23:55:55 +00:00
|
|
|
Testing the `Union` aggregate.
|
2015-01-14 19:48:55 +00:00
|
|
|
"""
|
2012-08-03 08:47:27 +00:00
|
|
|
tx = Country.objects.get(name="Texas").mpoly
|
2014-03-27 21:26:20 +00:00
|
|
|
# Houston, Dallas -- Ordering may differ depending on backend or GEOS version.
|
2016-11-30 12:49:05 +00:00
|
|
|
union = GEOSGeometry("MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)")
|
2012-08-03 08:47:27 +00:00
|
|
|
qs = City.objects.filter(point__within=tx)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
qs.aggregate(Union("name"))
|
2012-08-03 08:47:27 +00:00
|
|
|
# Using `field_name` keyword argument in one query and specifying an
|
|
|
|
# order in the other (which should not be used because this is
|
|
|
|
# an aggregate method on a spatial column)
|
2015-09-02 23:55:55 +00:00
|
|
|
u1 = qs.aggregate(Union("point"))["point__union"]
|
|
|
|
u2 = qs.order_by("name").aggregate(Union("point"))["point__union"]
|
2016-11-30 12:49:05 +00:00
|
|
|
self.assertTrue(union.equals(u1))
|
|
|
|
self.assertTrue(union.equals(u2))
|
2012-08-03 08:47:27 +00:00
|
|
|
qs = City.objects.filter(name="NotACity")
|
2015-01-14 19:48:55 +00:00
|
|
|
self.assertIsNone(qs.aggregate(Union("point"))["point__union"])
|
2014-03-29 05:58:25 +00:00
|
|
|
|
2020-11-03 21:50:10 +00:00
|
|
|
@skipUnlessDBFeature("supports_union_aggr")
|
|
|
|
def test_geoagg_subquery(self):
|
2020-11-06 07:48:11 +00:00
|
|
|
tx = Country.objects.get(name="Texas")
|
|
|
|
union = GEOSGeometry("MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)")
|
2020-11-03 21:50:10 +00:00
|
|
|
# Use distinct() to force the usage of a subquery for aggregation.
|
|
|
|
with CaptureQueriesContext(connection) as ctx:
|
|
|
|
self.assertIs(
|
|
|
|
union.equals(
|
2020-11-06 07:48:11 +00:00
|
|
|
City.objects.filter(point__within=tx.mpoly)
|
|
|
|
.distinct()
|
|
|
|
.aggregate(
|
2020-11-03 21:50:10 +00:00
|
|
|
Union("point"),
|
|
|
|
)["point__union"],
|
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
self.assertIn("subquery", ctx.captured_queries[0]["sql"])
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2020-11-30 06:29:00 +00:00
|
|
|
@skipUnlessDBFeature("supports_tolerance_parameter")
|
2020-02-24 13:46:28 +00:00
|
|
|
def test_unionagg_tolerance(self):
|
|
|
|
City.objects.create(
|
|
|
|
point=fromstr("POINT(-96.467222 32.751389)", srid=4326),
|
|
|
|
name="Forney",
|
|
|
|
)
|
|
|
|
tx = Country.objects.get(name="Texas").mpoly
|
|
|
|
# Tolerance is greater than distance between Forney and Dallas, that's
|
|
|
|
# why Dallas is ignored.
|
|
|
|
forney_houston = GEOSGeometry(
|
|
|
|
"MULTIPOINT(-95.363151 29.763374, -96.467222 32.751389)",
|
|
|
|
srid=4326,
|
|
|
|
)
|
|
|
|
self.assertIs(
|
2020-03-05 07:55:34 +00:00
|
|
|
forney_houston.equals_exact(
|
2020-02-24 13:46:28 +00:00
|
|
|
City.objects.filter(point__within=tx).aggregate(
|
|
|
|
Union("point", tolerance=32000),
|
|
|
|
)["point__union"],
|
2020-03-05 07:55:34 +00:00
|
|
|
tolerance=10e-6,
|
2020-02-24 13:46:28 +00:00
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
2020-11-30 06:29:00 +00:00
|
|
|
@skipUnlessDBFeature("supports_tolerance_parameter")
|
2020-02-24 13:46:28 +00:00
|
|
|
def test_unionagg_tolerance_escaping(self):
|
|
|
|
tx = Country.objects.get(name="Texas").mpoly
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
City.objects.filter(point__within=tx).aggregate(
|
|
|
|
Union("point", tolerance="0.05))), (((1"),
|
|
|
|
)
|
|
|
|
|
2015-01-16 23:17:54 +00:00
|
|
|
def test_within_subquery(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Using a queryset inside a geo lookup is working (using a subquery)
|
2015-01-16 23:17:54 +00:00
|
|
|
(#14483).
|
|
|
|
"""
|
|
|
|
tex_cities = City.objects.filter(
|
|
|
|
point__within=Country.objects.filter(name="Texas").values("mpoly")
|
|
|
|
).order_by("name")
|
2019-01-19 14:28:42 +00:00
|
|
|
self.assertEqual(
|
|
|
|
list(tex_cities.values_list("name", flat=True)), ["Dallas", "Houston"]
|
|
|
|
)
|
2015-01-16 23:17:54 +00:00
|
|
|
|
2014-03-29 05:58:25 +00:00
|
|
|
def test_non_concrete_field(self):
|
2014-08-12 12:08:40 +00:00
|
|
|
NonConcreteModel.objects.create(point=Point(0, 0), name="name")
|
|
|
|
list(NonConcreteModel.objects.all())
|
2015-11-04 06:30:05 +00:00
|
|
|
|
|
|
|
def test_values_srid(self):
|
|
|
|
for c, v in zip(City.objects.all(), City.objects.values()):
|
|
|
|
self.assertEqual(c.point.srid, v["point"].srid)
|