tests.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. import tempfile
  2. from io import StringIO
  3. from django.contrib.gis import gdal
  4. from django.contrib.gis.db.models import Extent, MakeLine, Union, functions
  5. from django.contrib.gis.geos import (
  6. GeometryCollection, GEOSGeometry, LinearRing, LineString, MultiLineString,
  7. MultiPoint, MultiPolygon, Point, Polygon, fromstr,
  8. )
  9. from django.core.management import call_command
  10. from django.db import NotSupportedError, connection
  11. from django.test import TestCase, skipUnlessDBFeature
  12. from ..utils import (
  13. mysql, no_oracle, oracle, postgis, skipUnlessGISLookup, spatialite,
  14. )
  15. from .models import (
  16. City, Country, Feature, MinusOneSRID, NonConcreteModel, PennsylvaniaCity,
  17. State, Track,
  18. )
  19. class GeoModelTest(TestCase):
  20. fixtures = ['initial']
  21. def test_fixtures(self):
  22. "Testing geographic model initialization from fixtures."
  23. # Ensuring that data was loaded from initial data fixtures.
  24. self.assertEqual(2, Country.objects.count())
  25. self.assertEqual(8, City.objects.count())
  26. self.assertEqual(2, State.objects.count())
  27. def test_proxy(self):
  28. "Testing Lazy-Geometry support (using the GeometryProxy)."
  29. # Testing on a Point
  30. pnt = Point(0, 0)
  31. nullcity = City(name='NullCity', point=pnt)
  32. nullcity.save()
  33. # Making sure TypeError is thrown when trying to set with an
  34. # incompatible type.
  35. for bad in [5, 2.0, LineString((0, 0), (1, 1))]:
  36. with self.assertRaisesMessage(TypeError, 'Cannot set'):
  37. nullcity.point = bad
  38. # Now setting with a compatible GEOS Geometry, saving, and ensuring
  39. # the save took, notice no SRID is explicitly set.
  40. new = Point(5, 23)
  41. nullcity.point = new
  42. # Ensuring that the SRID is automatically set to that of the
  43. # field after assignment, but before saving.
  44. self.assertEqual(4326, nullcity.point.srid)
  45. nullcity.save()
  46. # Ensuring the point was saved correctly after saving
  47. self.assertEqual(new, City.objects.get(name='NullCity').point)
  48. # Setting the X and Y of the Point
  49. nullcity.point.x = 23
  50. nullcity.point.y = 5
  51. # Checking assignments pre & post-save.
  52. self.assertNotEqual(Point(23, 5, srid=4326), City.objects.get(name='NullCity').point)
  53. nullcity.save()
  54. self.assertEqual(Point(23, 5, srid=4326), City.objects.get(name='NullCity').point)
  55. nullcity.delete()
  56. # Testing on a Polygon
  57. shell = LinearRing((0, 0), (0, 90), (100, 90), (100, 0), (0, 0))
  58. inner = LinearRing((40, 40), (40, 60), (60, 60), (60, 40), (40, 40))
  59. # Creating a State object using a built Polygon
  60. ply = Polygon(shell, inner)
  61. nullstate = State(name='NullState', poly=ply)
  62. self.assertEqual(4326, nullstate.poly.srid) # SRID auto-set from None
  63. nullstate.save()
  64. ns = State.objects.get(name='NullState')
  65. self.assertEqual(ply, ns.poly)
  66. # Testing the `ogr` and `srs` lazy-geometry properties.
  67. self.assertIsInstance(ns.poly.ogr, gdal.OGRGeometry)
  68. self.assertEqual(ns.poly.wkb, ns.poly.ogr.wkb)
  69. self.assertIsInstance(ns.poly.srs, gdal.SpatialReference)
  70. self.assertEqual('WGS 84', ns.poly.srs.name)
  71. # Changing the interior ring on the poly attribute.
  72. new_inner = LinearRing((30, 30), (30, 70), (70, 70), (70, 30), (30, 30))
  73. ns.poly[1] = new_inner
  74. ply[1] = new_inner
  75. self.assertEqual(4326, ns.poly.srid)
  76. ns.save()
  77. self.assertEqual(ply, State.objects.get(name='NullState').poly)
  78. ns.delete()
  79. @skipUnlessDBFeature("supports_transform")
  80. def test_lookup_insert_transform(self):
  81. "Testing automatic transform for lookups and inserts."
  82. # San Antonio in 'WGS84' (SRID 4326)
  83. sa_4326 = 'POINT (-98.493183 29.424170)'
  84. wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
  85. # San Antonio in 'WGS 84 / Pseudo-Mercator' (SRID 3857)
  86. other_srid_pnt = wgs_pnt.transform(3857, clone=True)
  87. # Constructing & querying with a point from a different SRID. Oracle
  88. # `SDO_OVERLAPBDYINTERSECT` operates differently from
  89. # `ST_Intersects`, so contains is used instead.
  90. if oracle:
  91. tx = Country.objects.get(mpoly__contains=other_srid_pnt)
  92. else:
  93. tx = Country.objects.get(mpoly__intersects=other_srid_pnt)
  94. self.assertEqual('Texas', tx.name)
  95. # Creating San Antonio. Remember the Alamo.
  96. sa = City.objects.create(name='San Antonio', point=other_srid_pnt)
  97. # Now verifying that San Antonio was transformed correctly
  98. sa = City.objects.get(name='San Antonio')
  99. self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
  100. self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)
  101. # If the GeometryField SRID is -1, then we shouldn't perform any
  102. # transformation if the SRID of the input geometry is different.
  103. m1 = MinusOneSRID(geom=Point(17, 23, srid=4326))
  104. m1.save()
  105. self.assertEqual(-1, m1.geom.srid)
  106. def test_createnull(self):
  107. "Testing creating a model instance and the geometry being None"
  108. c = City()
  109. self.assertIsNone(c.point)
  110. def test_geometryfield(self):
  111. "Testing the general GeometryField."
  112. Feature(name='Point', geom=Point(1, 1)).save()
  113. Feature(name='LineString', geom=LineString((0, 0), (1, 1), (5, 5))).save()
  114. Feature(name='Polygon', geom=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)))).save()
  115. Feature(name='GeometryCollection',
  116. geom=GeometryCollection(Point(2, 2), LineString((0, 0), (2, 2)),
  117. Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))).save()
  118. f_1 = Feature.objects.get(name='Point')
  119. self.assertIsInstance(f_1.geom, Point)
  120. self.assertEqual((1.0, 1.0), f_1.geom.tuple)
  121. f_2 = Feature.objects.get(name='LineString')
  122. self.assertIsInstance(f_2.geom, LineString)
  123. self.assertEqual(((0.0, 0.0), (1.0, 1.0), (5.0, 5.0)), f_2.geom.tuple)
  124. f_3 = Feature.objects.get(name='Polygon')
  125. self.assertIsInstance(f_3.geom, Polygon)
  126. f_4 = Feature.objects.get(name='GeometryCollection')
  127. self.assertIsInstance(f_4.geom, GeometryCollection)
  128. self.assertEqual(f_3.geom, f_4.geom[2])
  129. # TODO: fix on Oracle: ORA-22901: cannot compare nested table or VARRAY or
  130. # LOB attributes of an object type.
  131. @no_oracle
  132. @skipUnlessDBFeature("supports_transform")
  133. def test_inherited_geofields(self):
  134. "Database functions on inherited Geometry fields."
  135. # Creating a Pennsylvanian city.
  136. PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)')
  137. # All transformation SQL will need to be performed on the
  138. # _parent_ table.
  139. qs = PennsylvaniaCity.objects.annotate(new_point=functions.Transform('point', srid=32128))
  140. self.assertEqual(1, qs.count())
  141. for pc in qs:
  142. self.assertEqual(32128, pc.new_point.srid)
  143. def test_raw_sql_query(self):
  144. "Testing raw SQL query."
  145. cities1 = City.objects.all()
  146. point_select = connection.ops.select % 'point'
  147. cities2 = list(City.objects.raw(
  148. 'select id, name, %s as point from geoapp_city' % point_select
  149. ))
  150. self.assertEqual(len(cities1), len(cities2))
  151. with self.assertNumQueries(0): # Ensure point isn't deferred.
  152. self.assertIsInstance(cities2[0].point, Point)
  153. def test_dumpdata_loaddata_cycle(self):
  154. """
  155. Test a dumpdata/loaddata cycle with geographic data.
  156. """
  157. out = StringIO()
  158. original_data = list(City.objects.all().order_by('name'))
  159. call_command('dumpdata', 'geoapp.City', stdout=out)
  160. result = out.getvalue()
  161. houston = City.objects.get(name='Houston')
  162. self.assertIn('"point": "%s"' % houston.point.ewkt, result)
  163. # Reload now dumped data
  164. with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as tmp:
  165. tmp.write(result)
  166. tmp.seek(0)
  167. call_command('loaddata', tmp.name, verbosity=0)
  168. self.assertEqual(original_data, list(City.objects.all().order_by('name')))
  169. @skipUnlessDBFeature("supports_empty_geometries")
  170. def test_empty_geometries(self):
  171. geometry_classes = [
  172. Point,
  173. LineString,
  174. LinearRing,
  175. Polygon,
  176. MultiPoint,
  177. MultiLineString,
  178. MultiPolygon,
  179. GeometryCollection,
  180. ]
  181. for klass in geometry_classes:
  182. g = klass(srid=4326)
  183. feature = Feature.objects.create(name='Empty %s' % klass.__name__, geom=g)
  184. feature.refresh_from_db()
  185. if klass is LinearRing:
  186. # LinearRing isn't representable in WKB, so GEOSGeomtry.wkb
  187. # uses LineString instead.
  188. g = LineString(srid=4326)
  189. self.assertEqual(feature.geom, g)
  190. self.assertEqual(feature.geom.srid, g.srid)
  191. class GeoLookupTest(TestCase):
  192. fixtures = ['initial']
  193. def test_disjoint_lookup(self):
  194. "Testing the `disjoint` lookup type."
  195. ptown = City.objects.get(name='Pueblo')
  196. qs1 = City.objects.filter(point__disjoint=ptown.point)
  197. self.assertEqual(7, qs1.count())
  198. if connection.features.supports_real_shape_operations:
  199. qs2 = State.objects.filter(poly__disjoint=ptown.point)
  200. self.assertEqual(1, qs2.count())
  201. self.assertEqual('Kansas', qs2[0].name)
  202. def test_contains_contained_lookups(self):
  203. "Testing the 'contained', 'contains', and 'bbcontains' lookup types."
  204. # Getting Texas, yes we were a country -- once ;)
  205. texas = Country.objects.get(name='Texas')
  206. # Seeing what cities are in Texas, should get Houston and Dallas,
  207. # and Oklahoma City because 'contained' only checks on the
  208. # _bounding box_ of the Geometries.
  209. if connection.features.supports_contained_lookup:
  210. qs = City.objects.filter(point__contained=texas.mpoly)
  211. self.assertEqual(3, qs.count())
  212. cities = ['Houston', 'Dallas', 'Oklahoma City']
  213. for c in qs:
  214. self.assertIn(c.name, cities)
  215. # Pulling out some cities.
  216. houston = City.objects.get(name='Houston')
  217. wellington = City.objects.get(name='Wellington')
  218. pueblo = City.objects.get(name='Pueblo')
  219. okcity = City.objects.get(name='Oklahoma City')
  220. lawrence = City.objects.get(name='Lawrence')
  221. # Now testing contains on the countries using the points for
  222. # Houston and Wellington.
  223. tx = Country.objects.get(mpoly__contains=houston.point) # Query w/GEOSGeometry
  224. nz = Country.objects.get(mpoly__contains=wellington.point.hex) # Query w/EWKBHEX
  225. self.assertEqual('Texas', tx.name)
  226. self.assertEqual('New Zealand', nz.name)
  227. # Testing `contains` on the states using the point for Lawrence.
  228. ks = State.objects.get(poly__contains=lawrence.point)
  229. self.assertEqual('Kansas', ks.name)
  230. # Pueblo and Oklahoma City (even though OK City is within the bounding box of Texas)
  231. # are not contained in Texas or New Zealand.
  232. self.assertEqual(len(Country.objects.filter(mpoly__contains=pueblo.point)), 0) # Query w/GEOSGeometry object
  233. self.assertEqual(len(Country.objects.filter(mpoly__contains=okcity.point.wkt)),
  234. 0 if connection.features.supports_real_shape_operations else 1) # Query w/WKT
  235. # OK City is contained w/in bounding box of Texas.
  236. if connection.features.supports_bbcontains_lookup:
  237. qs = Country.objects.filter(mpoly__bbcontains=okcity.point)
  238. self.assertEqual(1, len(qs))
  239. self.assertEqual('Texas', qs[0].name)
  240. @skipUnlessDBFeature("supports_crosses_lookup")
  241. def test_crosses_lookup(self):
  242. Track.objects.create(
  243. name='Line1',
  244. line=LineString([(-95, 29), (-60, 0)])
  245. )
  246. self.assertEqual(
  247. Track.objects.filter(line__crosses=LineString([(-95, 0), (-60, 29)])).count(),
  248. 1
  249. )
  250. self.assertEqual(
  251. Track.objects.filter(line__crosses=LineString([(-95, 30), (0, 30)])).count(),
  252. 0
  253. )
  254. @skipUnlessDBFeature("supports_isvalid_lookup")
  255. def test_isvalid_lookup(self):
  256. invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
  257. State.objects.create(name='invalid', poly=invalid_geom)
  258. qs = State.objects.all()
  259. if oracle or mysql:
  260. # Kansas has adjacent vertices with distance 6.99244813842e-12
  261. # which is smaller than the default Oracle tolerance.
  262. # It's invalid on MySQL too.
  263. qs = qs.exclude(name='Kansas')
  264. self.assertEqual(State.objects.filter(name='Kansas', poly__isvalid=False).count(), 1)
  265. self.assertEqual(qs.filter(poly__isvalid=False).count(), 1)
  266. self.assertEqual(qs.filter(poly__isvalid=True).count(), qs.count() - 1)
  267. @skipUnlessDBFeature("supports_left_right_lookups")
  268. def test_left_right_lookups(self):
  269. "Testing the 'left' and 'right' lookup types."
  270. # Left: A << B => true if xmax(A) < xmin(B)
  271. # Right: A >> B => true if xmin(A) > xmax(B)
  272. # See: BOX2D_left() and BOX2D_right() in lwgeom_box2dfloat4.c in PostGIS source.
  273. # Getting the borders for Colorado & Kansas
  274. co_border = State.objects.get(name='Colorado').poly
  275. ks_border = State.objects.get(name='Kansas').poly
  276. # Note: Wellington has an 'X' value of 174, so it will not be considered
  277. # to the left of CO.
  278. # These cities should be strictly to the right of the CO border.
  279. cities = ['Houston', 'Dallas', 'Oklahoma City',
  280. 'Lawrence', 'Chicago', 'Wellington']
  281. qs = City.objects.filter(point__right=co_border)
  282. self.assertEqual(6, len(qs))
  283. for c in qs:
  284. self.assertIn(c.name, cities)
  285. # These cities should be strictly to the right of the KS border.
  286. cities = ['Chicago', 'Wellington']
  287. qs = City.objects.filter(point__right=ks_border)
  288. self.assertEqual(2, len(qs))
  289. for c in qs:
  290. self.assertIn(c.name, cities)
  291. # Note: Wellington has an 'X' value of 174, so it will not be considered
  292. # to the left of CO.
  293. vic = City.objects.get(point__left=co_border)
  294. self.assertEqual('Victoria', vic.name)
  295. cities = ['Pueblo', 'Victoria']
  296. qs = City.objects.filter(point__left=ks_border)
  297. self.assertEqual(2, len(qs))
  298. for c in qs:
  299. self.assertIn(c.name, cities)
  300. @skipUnlessGISLookup("strictly_above", "strictly_below")
  301. def test_strictly_above_below_lookups(self):
  302. dallas = City.objects.get(name='Dallas')
  303. self.assertQuerysetEqual(
  304. City.objects.filter(point__strictly_above=dallas.point).order_by('name'),
  305. ['Chicago', 'Lawrence', 'Oklahoma City', 'Pueblo', 'Victoria'],
  306. lambda b: b.name
  307. )
  308. self.assertQuerysetEqual(
  309. City.objects.filter(point__strictly_below=dallas.point).order_by('name'),
  310. ['Houston', 'Wellington'],
  311. lambda b: b.name
  312. )
  313. def test_equals_lookups(self):
  314. "Testing the 'same_as' and 'equals' lookup types."
  315. pnt = fromstr('POINT (-95.363151 29.763374)', srid=4326)
  316. c1 = City.objects.get(point=pnt)
  317. c2 = City.objects.get(point__same_as=pnt)
  318. c3 = City.objects.get(point__equals=pnt)
  319. for c in [c1, c2, c3]:
  320. self.assertEqual('Houston', c.name)
  321. @skipUnlessDBFeature("supports_null_geometries")
  322. def test_null_geometries(self):
  323. "Testing NULL geometry support, and the `isnull` lookup type."
  324. # Creating a state with a NULL boundary.
  325. State.objects.create(name='Puerto Rico')
  326. # Querying for both NULL and Non-NULL values.
  327. nullqs = State.objects.filter(poly__isnull=True)
  328. validqs = State.objects.filter(poly__isnull=False)
  329. # Puerto Rico should be NULL (it's a commonwealth unincorporated territory)
  330. self.assertEqual(1, len(nullqs))
  331. self.assertEqual('Puerto Rico', nullqs[0].name)
  332. # GeometryField=None is an alias for __isnull=True.
  333. self.assertCountEqual(State.objects.filter(poly=None), nullqs)
  334. self.assertCountEqual(State.objects.exclude(poly=None), validqs)
  335. # The valid states should be Colorado & Kansas
  336. self.assertEqual(2, len(validqs))
  337. state_names = [s.name for s in validqs]
  338. self.assertIn('Colorado', state_names)
  339. self.assertIn('Kansas', state_names)
  340. # Saving another commonwealth w/a NULL geometry.
  341. nmi = State.objects.create(name='Northern Mariana Islands', poly=None)
  342. self.assertIsNone(nmi.poly)
  343. # Assigning a geometry and saving -- then UPDATE back to NULL.
  344. nmi.poly = 'POLYGON((0 0,1 0,1 1,1 0,0 0))'
  345. nmi.save()
  346. State.objects.filter(name='Northern Mariana Islands').update(poly=None)
  347. self.assertIsNone(State.objects.get(name='Northern Mariana Islands').poly)
  348. @skipUnlessDBFeature('supports_null_geometries', 'supports_crosses_lookup', 'supports_relate_lookup')
  349. def test_null_geometries_excluded_in_lookups(self):
  350. """NULL features are excluded in spatial lookup functions."""
  351. null = State.objects.create(name='NULL', poly=None)
  352. queries = [
  353. ('equals', Point(1, 1)),
  354. ('disjoint', Point(1, 1)),
  355. ('touches', Point(1, 1)),
  356. ('crosses', LineString((0, 0), (1, 1), (5, 5))),
  357. ('within', Point(1, 1)),
  358. ('overlaps', LineString((0, 0), (1, 1), (5, 5))),
  359. ('contains', LineString((0, 0), (1, 1), (5, 5))),
  360. ('intersects', LineString((0, 0), (1, 1), (5, 5))),
  361. ('relate', (Point(1, 1), 'T*T***FF*')),
  362. ('same_as', Point(1, 1)),
  363. ('exact', Point(1, 1)),
  364. ('coveredby', Point(1, 1)),
  365. ('covers', Point(1, 1)),
  366. ]
  367. for lookup, geom in queries:
  368. with self.subTest(lookup=lookup):
  369. self.assertNotIn(null, State.objects.filter(**{'poly__%s' % lookup: geom}))
  370. @skipUnlessDBFeature("supports_relate_lookup")
  371. def test_relate_lookup(self):
  372. "Testing the 'relate' lookup type."
  373. # To make things more interesting, we will have our Texas reference point in
  374. # different SRIDs.
  375. pnt1 = fromstr('POINT (649287.0363174 4177429.4494686)', srid=2847)
  376. pnt2 = fromstr('POINT(-98.4919715741052 29.4333344025053)', srid=4326)
  377. # Not passing in a geometry as first param raises a TypeError when
  378. # initializing the QuerySet.
  379. with self.assertRaises(ValueError):
  380. Country.objects.filter(mpoly__relate=(23, 'foo'))
  381. # Making sure the right exception is raised for the given
  382. # bad arguments.
  383. for bad_args, e in [((pnt1, 0), ValueError), ((pnt2, 'T*T***FF*', 0), ValueError)]:
  384. qs = Country.objects.filter(mpoly__relate=bad_args)
  385. with self.assertRaises(e):
  386. qs.count()
  387. # Relate works differently for the different backends.
  388. if postgis or spatialite:
  389. contains_mask = 'T*T***FF*'
  390. within_mask = 'T*F**F***'
  391. intersects_mask = 'T********'
  392. elif oracle:
  393. contains_mask = 'contains'
  394. within_mask = 'inside'
  395. # TODO: This is not quite the same as the PostGIS mask above
  396. intersects_mask = 'overlapbdyintersect'
  397. # Testing contains relation mask.
  398. self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt1, contains_mask)).name)
  399. self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt2, contains_mask)).name)
  400. # Testing within relation mask.
  401. ks = State.objects.get(name='Kansas')
  402. self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, within_mask)).name)
  403. # Testing intersection relation mask.
  404. if not oracle:
  405. self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt1, intersects_mask)).name)
  406. self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt2, intersects_mask)).name)
  407. self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, intersects_mask)).name)
  408. # With a complex geometry expression
  409. mask = 'anyinteract' if oracle else within_mask
  410. self.assertFalse(City.objects.exclude(point__relate=(functions.Union('point', 'point'), mask)))
  411. def test_gis_lookups_with_complex_expressions(self):
  412. multiple_arg_lookups = {'dwithin', 'relate'} # These lookups are tested elsewhere.
  413. lookups = connection.ops.gis_operators.keys() - multiple_arg_lookups
  414. self.assertTrue(lookups, 'No lookups found')
  415. for lookup in lookups:
  416. with self.subTest(lookup):
  417. City.objects.filter(**{'point__' + lookup: functions.Union('point', 'point')}).exists()
  418. class GeoQuerySetTest(TestCase):
  419. # TODO: GeoQuerySet is removed, organize these test better.
  420. fixtures = ['initial']
  421. @skipUnlessDBFeature("supports_extent_aggr")
  422. def test_extent(self):
  423. """
  424. Testing the `Extent` aggregate.
  425. """
  426. # Reference query:
  427. # `SELECT ST_extent(point) FROM geoapp_city WHERE (name='Houston' or name='Dallas');`
  428. # => BOX(-96.8016128540039 29.7633724212646,-95.3631439208984 32.7820587158203)
  429. expected = (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
  430. qs = City.objects.filter(name__in=('Houston', 'Dallas'))
  431. extent = qs.aggregate(Extent('point'))['point__extent']
  432. for val, exp in zip(extent, expected):
  433. self.assertAlmostEqual(exp, val, 4)
  434. self.assertIsNone(City.objects.filter(name=('Smalltown')).aggregate(Extent('point'))['point__extent'])
  435. @skipUnlessDBFeature("supports_extent_aggr")
  436. def test_extent_with_limit(self):
  437. """
  438. Testing if extent supports limit.
  439. """
  440. extent1 = City.objects.all().aggregate(Extent('point'))['point__extent']
  441. extent2 = City.objects.all()[:3].aggregate(Extent('point'))['point__extent']
  442. self.assertNotEqual(extent1, extent2)
  443. def test_make_line(self):
  444. """
  445. Testing the `MakeLine` aggregate.
  446. """
  447. if not connection.features.supports_make_line_aggr:
  448. with self.assertRaises(NotSupportedError):
  449. City.objects.all().aggregate(MakeLine('point'))
  450. return
  451. # MakeLine on an inappropriate field returns simply None
  452. self.assertIsNone(State.objects.aggregate(MakeLine('poly'))['poly__makeline'])
  453. # Reference query:
  454. # SELECT AsText(ST_MakeLine(geoapp_city.point)) FROM geoapp_city;
  455. ref_line = GEOSGeometry(
  456. 'LINESTRING(-95.363151 29.763374,-96.801611 32.782057,'
  457. '-97.521157 34.464642,174.783117 -41.315268,-104.609252 38.255001,'
  458. '-95.23506 38.971823,-87.650175 41.850385,-123.305196 48.462611)',
  459. srid=4326
  460. )
  461. # We check for equality with a tolerance of 10e-5 which is a lower bound
  462. # of the precisions of ref_line coordinates
  463. line = City.objects.aggregate(MakeLine('point'))['point__makeline']
  464. self.assertTrue(
  465. ref_line.equals_exact(line, tolerance=10e-5),
  466. "%s != %s" % (ref_line, line)
  467. )
  468. @skipUnlessDBFeature('supports_union_aggr')
  469. def test_unionagg(self):
  470. """
  471. Testing the `Union` aggregate.
  472. """
  473. tx = Country.objects.get(name='Texas').mpoly
  474. # Houston, Dallas -- Ordering may differ depending on backend or GEOS version.
  475. union = GEOSGeometry('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)')
  476. qs = City.objects.filter(point__within=tx)
  477. with self.assertRaises(ValueError):
  478. qs.aggregate(Union('name'))
  479. # Using `field_name` keyword argument in one query and specifying an
  480. # order in the other (which should not be used because this is
  481. # an aggregate method on a spatial column)
  482. u1 = qs.aggregate(Union('point'))['point__union']
  483. u2 = qs.order_by('name').aggregate(Union('point'))['point__union']
  484. self.assertTrue(union.equals(u1))
  485. self.assertTrue(union.equals(u2))
  486. qs = City.objects.filter(name='NotACity')
  487. self.assertIsNone(qs.aggregate(Union('point'))['point__union'])
  488. def test_within_subquery(self):
  489. """
  490. Using a queryset inside a geo lookup is working (using a subquery)
  491. (#14483).
  492. """
  493. tex_cities = City.objects.filter(
  494. point__within=Country.objects.filter(name='Texas').values('mpoly')).order_by('name')
  495. expected = ['Dallas', 'Houston']
  496. if not connection.features.supports_real_shape_operations:
  497. expected.append('Oklahoma City')
  498. self.assertEqual(
  499. list(tex_cities.values_list('name', flat=True)),
  500. expected
  501. )
  502. def test_non_concrete_field(self):
  503. NonConcreteModel.objects.create(point=Point(0, 0), name='name')
  504. list(NonConcreteModel.objects.all())
  505. def test_values_srid(self):
  506. for c, v in zip(City.objects.all(), City.objects.values()):
  507. self.assertEqual(c.point.srid, v['point'].srid)