File: geoquerysets.txt

package info (click to toggle)
python-django 1%3A1.11.29-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,428 kB
  • sloc: python: 220,776; javascript: 13,523; makefile: 209; xml: 201; sh: 64
file content (1462 lines) | stat: -rw-r--r-- 45,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
=========================
GeoQuerySet API Reference
=========================

.. currentmodule:: django.contrib.gis.db.models

.. class:: GeoQuerySet(model=None)

.. _spatial-lookups:

Spatial Lookups
===============

The spatial lookups in this section are available for :class:`GeometryField`
and :class:`RasterField`.

For an introduction, see the :ref:`spatial lookups introduction
<spatial-lookups-intro>`.  For an overview of what lookups are
compatible with a particular spatial backend, refer to the
:ref:`spatial lookup compatibility table <spatial-lookup-compatibility>`.

.. versionchanged:: 1.10

    Spatial lookups now support raster input.

Lookups with rasters
--------------------

All examples in the reference below are given for geometry fields and inputs,
but the lookups can be used the same way with rasters on both sides. Whenever
a lookup doesn't support raster input, the input is automatically
converted to a geometry where necessary using the `ST_Polygon
<https://postgis.net/docs/RT_ST_Polygon.html>`_ function. See also the
:ref:`introduction to raster lookups <spatial-lookup-raster>`.

The database operators used by the lookups can be divided into three categories:

- Native raster support ``N``: the operator accepts rasters natively on both
  sides of the lookup, and raster input can be mixed with geometry inputs.

- Bilateral raster support ``B``: the operator supports rasters only if both
  sides of the lookup receive raster inputs. Raster data is automatically
  converted to geometries for mixed lookups.

- Geometry conversion support ``C``. The lookup does not have native raster
  support, all raster data is automatically converted to geometries.

The examples below show the SQL equivalent for the lookups in the different
types of raster support. The same pattern applies to all spatial lookups.

==== ============================== =======================================================
Case Lookup                         SQL Equivalent
==== ============================== =======================================================
N, B ``rast__contains=rst``         ``ST_Contains(rast, rst)``
N, B ``rast__1__contains=(rst, 2)`` ``ST_Contains(rast, 1, rst, 2)``
B, C ``rast__contains=geom``        ``ST_Contains(ST_Polygon(rast), geom)``
B, C ``rast__1__contains=geom``     ``ST_Contains(ST_Polygon(rast, 1), geom)``
B, C ``poly__contains=rst``         ``ST_Contains(poly, ST_Polygon(rst))``
B, C ``poly__contains=(rst, 1)``    ``ST_Contains(poly, ST_Polygon(rst, 1))``
C    ``rast__crosses=rst``          ``ST_Crosses(ST_Polygon(rast), ST_Polygon(rst))``
C    ``rast__1__crosses=(rst, 2)``  ``ST_Crosses(ST_Polygon(rast, 1), ST_Polygon(rst, 2))``
C    ``rast__crosses=geom``         ``ST_Crosses(ST_Polygon(rast), geom)``
C    ``poly__crosses=rst``          ``ST_Crosses(poly, ST_Polygon(rst))``
==== ============================== =======================================================

Spatial lookups with rasters are only supported for PostGIS backends
(denominated as PGRaster in this section).

.. fieldlookup:: bbcontains

``bbcontains``
--------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Contain.html>`__,
MySQL, SpatiaLite, PGRaster (Native)

Tests if the geometry or raster field's bounding box completely contains the
lookup geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__bbcontains=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``poly ~ geom``
MySQL       ``MBRContains(poly, geom)``
SpatiaLite  ``MbrContains(poly, geom)``
==========  ==========================

.. fieldlookup:: bboverlaps

``bboverlaps``
--------------

*Availability*: `PostGIS <https://postgis.net/docs/geometry_overlaps.html>`__,
MySQL, SpatiaLite, PGRaster (Native)

Tests if the geometry field's bounding box overlaps the lookup geometry's
bounding box.

Example::

    Zipcode.objects.filter(poly__bboverlaps=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``poly && geom``
MySQL       ``MBROverlaps(poly, geom)``
SpatiaLite  ``MbrOverlaps(poly, geom)``
==========  ==========================

.. fieldlookup:: contained

``contained``
-------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Contained.html>`__,
MySQL, SpatiaLite, PGRaster (Native)

Tests if the geometry field's bounding box is completely contained by the
lookup geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__contained=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``poly @ geom``
MySQL       ``MBRWithin(poly, geom)``
SpatiaLite  ``MbrWithin(poly, geom)``
==========  ==========================

.. fieldlookup:: gis-contains

``contains``
------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Contains.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)

Tests if the geometry field spatially contains the lookup geometry.

Example::

    Zipcode.objects.filter(poly__contains=geom)

==========  ============================
Backend     SQL Equivalent
==========  ============================
PostGIS     ``ST_Contains(poly, geom)``
Oracle      ``SDO_CONTAINS(poly, geom)``
MySQL       ``MBRContains(poly, geom)``
SpatiaLite  ``Contains(poly, geom)``
==========  ============================

.. fieldlookup:: contains_properly

``contains_properly``
---------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_ContainsProperly.html>`__,
PGRaster (Bilateral)

Returns true if the lookup geometry intersects the interior of the
geometry field, but not the boundary (or exterior).

Example::

    Zipcode.objects.filter(poly__contains_properly=geom)

==========  ===================================
Backend     SQL Equivalent
==========  ===================================
PostGIS     ``ST_ContainsProperly(poly, geom)``
==========  ===================================

.. fieldlookup:: coveredby

``coveredby``
-------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_CoveredBy.html>`__,
Oracle, PGRaster (Bilateral)

Tests if no point in the geometry field is outside the lookup geometry.
[#fncovers]_

Example::

    Zipcode.objects.filter(poly__coveredby=geom)

==========  =============================
Backend     SQL Equivalent
==========  =============================
PostGIS     ``ST_CoveredBy(poly, geom)``
Oracle      ``SDO_COVEREDBY(poly, geom)``
==========  =============================

.. fieldlookup:: covers

``covers``
----------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Covers.html>`__,
Oracle, PGRaster (Bilateral)

Tests if no point in the lookup geometry is outside the geometry field.
[#fncovers]_

Example::

    Zipcode.objects.filter(poly__covers=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``ST_Covers(poly, geom)``
Oracle      ``SDO_COVERS(poly, geom)``
==========  ==========================

.. fieldlookup:: crosses

``crosses``
-----------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Crosses.html>`__,
SpatiaLite, PGRaster (Conversion)

Tests if the geometry field spatially crosses the lookup geometry.

Example::

    Zipcode.objects.filter(poly__crosses=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``ST_Crosses(poly, geom)``
SpatiaLite  ``Crosses(poly, geom)``
==========  ==========================

.. fieldlookup:: disjoint

``disjoint``
------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Disjoint.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)

Tests if the geometry field is spatially disjoint from the lookup geometry.

Example::

    Zipcode.objects.filter(poly__disjoint=geom)

==========  =================================================
Backend     SQL Equivalent
==========  =================================================
PostGIS     ``ST_Disjoint(poly, geom)``
Oracle      ``SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)``
MySQL       ``MBRDisjoint(poly, geom)``
SpatiaLite  ``Disjoint(poly, geom)``
==========  =================================================

.. fieldlookup:: equals

``equals``
----------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Equals.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Conversion)

.. fieldlookup:: exact
.. fieldlookup:: same_as

``exact``, ``same_as``
----------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Same.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)

.. fieldlookup:: intersects

``intersects``
--------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Intersects.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)

Tests if the geometry field spatially intersects the lookup geometry.

Example::

    Zipcode.objects.filter(poly__intersects=geom)

==========  =================================================
Backend     SQL Equivalent
==========  =================================================
PostGIS     ``ST_Intersects(poly, geom)``
Oracle      ``SDO_OVERLAPBDYINTERSECT(poly, geom)``
MySQL       ``MBRIntersects(poly, geom)``
SpatiaLite  ``Intersects(poly, geom)``
==========  =================================================

.. fieldlookup:: isvalid

``isvalid``
-----------

.. versionadded:: 1.10

*Availability*: `PostGIS <https://postgis.net/docs/ST_IsValid.html>`__,
Oracle, SpatiaLite

Tests if the geometry is valid.

Example::

    Zipcode.objects.filter(poly__isvalid=True)

===================  ================================================================
Backend              SQL Equivalent
===================  ================================================================
PostGIS, SpatiaLite  ``ST_IsValid(poly)``
Oracle               ``SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'``
===================  ================================================================

.. versionchanged:: 1.11

    Oracle and SpatiaLite support was added.

.. fieldlookup:: overlaps

``overlaps``
------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Overlaps.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)

.. fieldlookup:: relate

``relate``
----------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Relate.html>`__,
Oracle, SpatiaLite, PGRaster (Conversion)

Tests if the geometry field is spatially related to the lookup geometry by
the values given in the given pattern.  This lookup requires a tuple parameter,
``(geom, pattern)``; the form of ``pattern`` will depend on the spatial backend:

PostGIS & SpatiaLite
~~~~~~~~~~~~~~~~~~~~
On these spatial backends the intersection pattern is a string comprising
nine characters, which  define intersections between  the interior, boundary,
and exterior of the geometry field and the lookup geometry.
The intersection pattern matrix may only use the following characters:
``1``, ``2``, ``T``, ``F``, or ``*``.  This lookup type allows users to "fine tune"
a specific geometric relationship consistent with the DE-9IM model. [#fnde9im]_

Geometry example::

    # A tuple lookup parameter is used to specify the geometry and
    # the intersection pattern (the pattern here is for 'contains').
    Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))

PostGIS SQL equivalent::

    SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')

SpatiaLite SQL equivalent::

    SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')

Raster example::

    Zipcode.objects.filter(poly__relate=(rast, 1, 'T*T***FF*'))
    Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))

PostGIS SQL equivalent::

    SELECT ... WHERE ST_Relate(poly, ST_Polygon(rast, 1), 'T*T***FF*')
    SELECT ... WHERE ST_Relate(ST_Polygon(rast, 2), ST_Polygon(rast, 1), 'T*T***FF*')

Oracle
~~~~~~

Here the relation pattern is comprised of at least one of the nine relation
strings: ``TOUCH``, ``OVERLAPBDYDISJOINT``, ``OVERLAPBDYINTERSECT``,
``EQUAL``, ``INSIDE``, ``COVEREDBY``, ``CONTAINS``, ``COVERS``, ``ON``, and
``ANYINTERACT``.   Multiple strings may be combined with the logical Boolean
operator OR, for example, ``'inside+touch'``. [#fnsdorelate]_  The relation
strings are case-insensitive.

Example::

    Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))

Oracle SQL equivalent::

    SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')

.. fieldlookup:: touches

``touches``
-----------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Touches.html>`__,
Oracle, MySQL, SpatiaLite

Tests if the geometry field spatially touches the lookup geometry.

Example::

    Zipcode.objects.filter(poly__touches=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``ST_Touches(poly, geom)``
MySQL       ``MBRTouches(poly, geom)``
Oracle      ``SDO_TOUCH(poly, geom)``
SpatiaLite  ``Touches(poly, geom)``
==========  ==========================

.. fieldlookup:: within

``within``
----------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Within.html>`__,
Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)

Tests if the geometry field is spatially within the lookup geometry.

Example::

    Zipcode.objects.filter(poly__within=geom)

==========  ==========================
Backend     SQL Equivalent
==========  ==========================
PostGIS     ``ST_Within(poly, geom)``
MySQL       ``MBRWithin(poly, geom)``
Oracle      ``SDO_INSIDE(poly, geom)``
SpatiaLite  ``Within(poly, geom)``
==========  ==========================

.. fieldlookup:: left

``left``
--------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Left.html>`__,
PGRaster (Conversion)

Tests if the geometry field's bounding box is strictly to the left of the
lookup geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__left=geom)

PostGIS equivalent::

    SELECT ... WHERE poly << geom

.. fieldlookup:: right

``right``
---------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Right.html>`__,
PGRaster (Conversion)

Tests if the geometry field's bounding box is strictly to the right of the
lookup geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__right=geom)

PostGIS equivalent::

    SELECT ... WHERE poly >> geom

.. fieldlookup:: overlaps_left

``overlaps_left``
-----------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overleft.html>`__,
PGRaster (Bilateral)

Tests if the geometry field's bounding box overlaps or is to the left of the lookup
geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__overlaps_left=geom)

PostGIS equivalent::

    SELECT ... WHERE poly &< geom


.. fieldlookup:: overlaps_right

``overlaps_right``
------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overright.html>`__,
PGRaster (Bilateral)

Tests if the geometry field's bounding box overlaps or is to the right of the lookup
geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__overlaps_right=geom)

PostGIS equivalent::

    SELECT ... WHERE poly &> geom

.. fieldlookup:: overlaps_above

``overlaps_above``
------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overabove.html>`__,
PGRaster (Conversion)

Tests if the geometry field's bounding box overlaps or is above the lookup
geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__overlaps_above=geom)

PostGIS equivalent::

    SELECT ... WHERE poly |&> geom

.. fieldlookup:: overlaps_below

``overlaps_below``
------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overbelow.html>`__,
PGRaster (Conversion)

Tests if the geometry field's bounding box overlaps or is below the lookup
geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__overlaps_below=geom)

PostGIS equivalent::

    SELECT ... WHERE poly &<| geom

.. fieldlookup:: strictly_above

``strictly_above``
------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Above.html>`__,
PGRaster (Conversion)

Tests if the geometry field's bounding box is strictly above the lookup
geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__strictly_above=geom)

PostGIS equivalent::

    SELECT ... WHERE poly |>> geom

.. fieldlookup:: strictly_below

``strictly_below``
------------------

*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Below.html>`__,
PGRaster (Conversion)

Tests if the geometry field's bounding box is strictly below the lookup
geometry's bounding box.

Example::

    Zipcode.objects.filter(poly__strictly_below=geom)

PostGIS equivalent::

    SELECT ... WHERE poly <<| geom


.. _distance-lookups:

Distance Lookups
================

*Availability*: PostGIS, Oracle, SpatiaLite, PGRaster (Native)

For an overview on performing distance queries, please refer to
the :ref:`distance queries introduction <distance-queries>`.

Distance lookups take the following form::

    <field>__<distance lookup>=(<geometry/raster>, <distance value>[, 'spheroid'])
    <field>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
    <field>__<band_index>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])

The value passed into a distance lookup is a tuple; the first two
values are mandatory, and are the geometry to calculate distances to,
and a distance value (either a number in units of the field, a
:class:`~django.contrib.gis.measure.Distance` object, or a `query expression
<ref/models/expressions>`). To pass a band index to the lookup, use a 3-tuple
where the second entry is the band index.

On every distance lookup except :lookup:`dwithin`, an optional element,
``'spheroid'``, may be included to use the more accurate spheroid distance
calculation functions on fields with a geodetic coordinate system.

On PostgreSQL, the ``'spheroid'`` option uses `ST_DistanceSpheroid
<https://postgis.net/docs/ST_Distance_Spheroid.html>`__ instead of
`ST_DistanceSphere <https://postgis.net/docs/ST_DistanceSphere.html>`__. The
simpler `ST_Distance <https://postgis.net/docs/ST_Distance.html>`__ function is
used with projected coordinate systems. Rasters are converted to geometries for
spheroid based lookups.

.. versionadded:: 1.10

    The ability to pass an expression as the distance value was added.

.. versionadded:: 1.11

    Support for the ``'spheroid'`` option on SQLite was added.

.. fieldlookup:: distance_gt

``distance_gt``
---------------

Returns models where the distance to the geometry field from the lookup
geometry is greater than the given distance value.

Example::

    Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))

==========  ==================================================
Backend     SQL Equivalent
==========  ==================================================
PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) > 5``
Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
SpatiaLite  ``Distance(poly, geom) > 5``
==========  ==================================================

.. fieldlookup:: distance_gte

``distance_gte``
----------------

Returns models where the distance to the geometry field from the lookup
geometry is greater than or equal to the given distance value.

Example::

    Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))

==========  ===================================================
Backend     SQL Equivalent
==========  ===================================================
PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) >= 5``
Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
SpatiaLite  ``Distance(poly, geom) >= 5``
==========  ===================================================

.. fieldlookup:: distance_lt

``distance_lt``
---------------

Returns models where the distance to the geometry field from the lookup
geometry is less than the given distance value.

Example::

    Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))

==========  ==================================================
Backend     SQL Equivalent
==========  ==================================================
PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) < 5``
Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
SpatiaLite  ``Distance(poly, geom) < 5``
==========  ==================================================

.. fieldlookup:: distance_lte

``distance_lte``
----------------

Returns models where the distance to the geometry field from the lookup
geometry is less than or equal to the given distance value.

Example::

    Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))

==========  ===================================================
Backend     SQL Equivalent
==========  ===================================================
PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) <= 5``
Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
SpatiaLite  ``Distance(poly, geom) <= 5``
==========  ===================================================

.. fieldlookup:: dwithin

``dwithin``
-----------

Returns models where the distance to the geometry field from the lookup
geometry are within the given distance from one another. Note that you can only
provide :class:`~django.contrib.gis.measure.Distance` objects if the targeted
geometries are in a projected system. For geographic geometries, you should use
units of the geometry field (e.g. degrees for ``WGS84``) .

Example::

    Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))

==========  ======================================
Backend     SQL Equivalent
==========  ======================================
PostGIS     ``ST_DWithin(poly, geom, 5)``
Oracle      ``SDO_WITHIN_DISTANCE(poly, geom, 5)``
SpatiaLite  ``PtDistWithin(poly, geom, 5)``
==========  ======================================

.. versionchanged:: 1.11

    SpatiaLite support was added.

.. _geoqueryset-methods:

``GeoQuerySet`` Methods
=======================

.. deprecated:: 1.9

    Using ``GeoQuerySet`` methods is now deprecated in favor of the new
    :doc:`functions`. Albeit a little more verbose, they are much more powerful
    in how it is possible to combine them to build more complex queries.

``GeoQuerySet`` methods specify that a spatial operation be performed
on each spatial operation on each geographic
field in the queryset and store its output in a new attribute on the model
(which is generally the name of the ``GeoQuerySet`` method).

There are also aggregate ``GeoQuerySet`` methods which return a single value
instead of a queryset.  This section will describe the API and availability
of every ``GeoQuerySet`` method available in GeoDjango.

.. note::

    What methods are available depend on your spatial backend.  See
    the :ref:`compatibility table <database-functions-compatibility>`
    for more details.

With a few exceptions, the following keyword arguments may be used with all
``GeoQuerySet`` methods:

=====================  =====================================================
Keyword Argument       Description
=====================  =====================================================
``field_name``         By default, ``GeoQuerySet`` methods use the first
                       geographic field encountered in the model.  This
                       keyword should be used to specify another
                       geographic field (e.g., ``field_name='point2'``)
                       when there are multiple geographic fields in a model.

                       On PostGIS, the ``field_name`` keyword may also be
                       used on geometry fields in models that are related
                       via a ``ForeignKey`` relation (e.g.,
                       ``field_name='related__point'``).

``model_att``          By default, ``GeoQuerySet`` methods typically attach
                       their output in an attribute with the same name as
                       the ``GeoQuerySet`` method.  Setting this keyword
                       with the desired attribute name will override this
                       default behavior.  For example,
                       ``qs = Zipcode.objects.centroid(model_att='c')`` will
                       attach the centroid of the ``Zipcode`` geometry field
                       in a ``c`` attribute on every model rather than in a
                       ``centroid`` attribute.

                       This keyword is required if
                       a method name clashes with an existing
                       ``GeoQuerySet`` method -- if you wanted to use the
                       ``area()`` method on model with a ``PolygonField``
                       named ``area``, for example.
=====================  =====================================================

Measurement
-----------
*Availability*: PostGIS, Oracle, SpatiaLite

``area``
~~~~~~~~

.. method:: GeoQuerySet.area(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Area` function
    instead.

Returns the area of the geographic field in an ``area`` attribute on
each element of this GeoQuerySet.

``distance``
~~~~~~~~~~~~

.. method:: GeoQuerySet.distance(geom, **kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Distance` function
    instead.

This method takes a geometry as a parameter, and attaches a ``distance``
attribute to every model in the returned queryset that contains the
distance (as a :class:`~django.contrib.gis.measure.Distance` object) to the given geometry.

In the following example (taken from the `GeoDjango distance tests`__),
the distance from the `Tasmanian`__ city of Hobart to every other
:class:`PointField` in the ``AustraliaCity`` queryset is calculated::

    >>> pnt = AustraliaCity.objects.get(name='Hobart').point
    >>> for city in AustraliaCity.objects.distance(pnt): print(city.name, city.distance)
    Wollongong 990071.220408 m
    Shellharbour 972804.613941 m
    Thirroul 1002334.36351 m
    Mittagong 975691.632637 m
    Batemans Bay 834342.185561 m
    Canberra 598140.268959 m
    Melbourne 575337.765042 m
    Sydney 1056978.87363 m
    Hobart 0.0 m
    Adelaide 1162031.83522 m
    Hillsdale 1049200.46122 m

.. note::

    Because the ``distance`` attribute is a
    :class:`~django.contrib.gis.measure.Distance` object, you can easily express
    the value in the units of your choice.  For example, ``city.distance.mi`` is
    the distance value in miles and ``city.distance.km`` is the distance value
    in kilometers.  See :doc:`measure` for usage details and the list of
    :ref:`supported_units`.

__ https://github.com/django/django/blob/master/tests/gis_tests/distapp/models.py
__ https://en.wikipedia.org/wiki/Tasmania

``length``
~~~~~~~~~~

.. method:: GeoQuerySet.length(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Length` function
    instead.

Returns the length of the geometry field in a ``length`` attribute
(a :class:`~django.contrib.gis.measure.Distance` object) on each model in
the queryset.

``perimeter``
~~~~~~~~~~~~~

.. method:: GeoQuerySet.perimeter(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Perimeter` function
    instead.

Returns the perimeter of the geometry field in a ``perimeter`` attribute
(a :class:`~django.contrib.gis.measure.Distance` object) on each model in
the queryset.

Geometry Relationships
----------------------

The following methods take no arguments, and attach geometry objects
each element of the :class:`GeoQuerySet` that is the result of relationship
function evaluated on the geometry field.

``centroid``
~~~~~~~~~~~~

.. method:: GeoQuerySet.centroid(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Centroid` function
    instead.

*Availability*: PostGIS, Oracle, SpatiaLite

Returns the ``centroid`` value for the geographic field in a ``centroid``
attribute on each element of the ``GeoQuerySet``.

``envelope``
~~~~~~~~~~~~

.. method:: GeoQuerySet.envelope(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Envelope` function
    instead.

*Availability*: PostGIS, SpatiaLite

Returns a geometry representing the bounding box of the geometry field in
an ``envelope`` attribute on each element of the ``GeoQuerySet``.

``point_on_surface``
~~~~~~~~~~~~~~~~~~~~

.. method:: GeoQuerySet.point_on_surface(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.PointOnSurface`
    function instead.

*Availability*: PostGIS, Oracle, SpatiaLite

Returns a Point geometry guaranteed to lie on the surface of the
geometry field in a ``point_on_surface`` attribute on each element
of the queryset; otherwise sets with None.

Geometry Editors
----------------

``force_rhr``
~~~~~~~~~~~~~

.. method:: GeoQuerySet.force_rhr(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.ForceRHR` function
    instead.

*Availability*: PostGIS

Returns a modified version of the polygon/multipolygon in which all
of the vertices follow the Right-Hand-Rule, and attaches as a
``force_rhr`` attribute on each element of the queryset.

``reverse_geom``
~~~~~~~~~~~~~~~~

.. method:: GeoQuerySet.reverse_geom(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Reverse` function
    instead.

*Availability*: PostGIS, Oracle

Reverse the coordinate order of the geometry field, and attaches as a
``reverse`` attribute on each element of the queryset.

``scale``
~~~~~~~~~

.. method:: GeoQuerySet.scale(x, y, z=0.0, **kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Scale` function
    instead.

*Availability*: PostGIS, SpatiaLite

``snap_to_grid``
~~~~~~~~~~~~~~~~

.. method:: GeoQuerySet.snap_to_grid(*args, **kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.SnapToGrid` function
    instead.

Snap all points of the input geometry to the grid.  How the
geometry is snapped to the grid depends on how many numeric
(either float, integer, or long) arguments are given.

===================  =====================================================
Number of Arguments  Description
===================  =====================================================
1                    A single size to snap bot the X and Y grids to.
2                    X and Y sizes to snap the grid to.
4                    X, Y sizes and the corresponding X, Y origins.
===================  =====================================================

``transform``
~~~~~~~~~~~~~

.. method:: GeoQuerySet.transform(srid=4326, **kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Transform` function
    instead.

*Availability*: PostGIS, Oracle, SpatiaLite

The ``transform`` method transforms the geometry field of a model to the spatial
reference system specified by the ``srid`` parameter.  If no ``srid`` is given,
then 4326 (WGS84) is used by default.

.. note::

    Unlike other ``GeoQuerySet`` methods, ``transform`` stores its output
    "in-place".  In other words, no new attribute for the transformed
    geometry is placed on the models.

.. note::

    What spatial reference system an integer SRID corresponds to may depend on
    the spatial database used.  In other words, the SRID numbers used for Oracle
    are not necessarily the same as those used by PostGIS.

Example::

    >>> qs = Zipcode.objects.all().transform() # Transforms to WGS84
    >>> qs = Zipcode.objects.all().transform(32140) # Transforming to "NAD83 / Texas South Central"
    >>> print(qs[0].poly.srid)
    32140
    >>> print(qs[0].poly)
    POLYGON ((234055.1698884720099159 4937796.9232223574072123 ...

``translate``
~~~~~~~~~~~~~
.. method:: GeoQuerySet.translate(x, y, z=0.0, **kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Translate` function
    instead.

*Availability*: PostGIS, SpatiaLite

Translates the geometry field to a new location using the given numeric
parameters as offsets.

Geometry Operations
-------------------
*Availability*: PostGIS, Oracle, SpatiaLite

The following methods all take a geometry as a parameter and attach a geometry
to each element of the ``GeoQuerySet`` that is the result of the operation.

``difference``
~~~~~~~~~~~~~~

.. method:: GeoQuerySet.difference(geom)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Difference` function
    instead.

Returns the spatial difference of the geographic field with the given
geometry in a ``difference`` attribute on each element of the
``GeoQuerySet``.


``intersection``
~~~~~~~~~~~~~~~~

.. method:: GeoQuerySet.intersection(geom)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Intersection`
    function instead.

Returns the spatial intersection of the geographic field with the
given geometry in an ``intersection`` attribute on each element of the
``GeoQuerySet``.

``sym_difference``
~~~~~~~~~~~~~~~~~~

.. method:: GeoQuerySet.sym_difference(geom)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.SymDifference`
    function instead.

Returns the symmetric difference of the geographic field with the
given geometry in a ``sym_difference`` attribute on each element of the
``GeoQuerySet``.

``union``
~~~~~~~~~

.. method:: GeoQuerySet.union(geom)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.Union` function
    instead.

Returns the union of the geographic field with the given
geometry in an ``union`` attribute on each element of the
``GeoQuerySet``.

Geometry Output
---------------

The following ``GeoQuerySet`` methods will return an attribute that has the value
of the geometry field in each model converted to the requested output format.

``geohash``
~~~~~~~~~~~

.. method:: GeoQuerySet.geohash(precision=20, **kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.GeoHash` function
    instead.

Attaches a ``geohash`` attribute to every model the queryset
containing the `GeoHash`__ representation of the geometry.

__ http://geohash.org/

``geojson``
~~~~~~~~~~~

.. method:: GeoQuerySet.geojson(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` function
    instead.

*Availability*: PostGIS, SpatiaLite

Attaches a ``geojson`` attribute to every model in the queryset that contains the
`GeoJSON`__ representation of the geometry.

=====================  =====================================================
Keyword Argument       Description
=====================  =====================================================
``precision``          It may be used to specify the number of significant
                       digits for the coordinates in the GeoJSON
                       representation -- the default value is 8.

``crs``                Set this to ``True`` if you want the coordinate
                       reference system to be included in the returned
                       GeoJSON.

``bbox``               Set this to ``True`` if you want the bounding box
                       to be included in the returned GeoJSON.
=====================  =====================================================

__ http://geojson.org/

``gml``
~~~~~~~

.. method:: GeoQuerySet.gml(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.AsGML` function
    instead.

*Availability*: PostGIS, Oracle, SpatiaLite

Attaches a ``gml`` attribute to every model in the queryset that contains the
`Geographic Markup Language (GML)`__ representation of the geometry.

Example::

    >>> qs = Zipcode.objects.all().gml()
    >>> print(qs[0].gml)
    <gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70.245363 ...  -147.78711,70.245363</gml:OuterBoundaryIs></gml:Polygon>

=====================  =====================================================
Keyword Argument       Description
=====================  =====================================================
``precision``          This keyword is for PostGIS only.  It may be used
                       to specify the number of significant digits for the
                       coordinates in the GML representation -- the default
                       value is 8.

``version``            This keyword is for PostGIS only.  It may be used to
                       specify the GML version used, and may only be values
                       of 2 or 3.  The default value is 2.
=====================  =====================================================

__ https://en.wikipedia.org/wiki/Geography_Markup_Language

``kml``
~~~~~~~

.. method:: GeoQuerySet.kml(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.AsKML` function
    instead.

*Availability*: PostGIS, SpatiaLite

Attaches a ``kml`` attribute to every model in the queryset that contains the
`Keyhole Markup Language (KML)`__ representation of the geometry fields. It
should be noted that the contents of the KML are transformed to WGS84 if
necessary.

Example::

    >>> qs = Zipcode.objects.all().kml()
    >>> print(qs[0].kml)
    <Polygon><outerBoundaryIs><LinearRing><coordinates>-103.04135,36.217596,0 ... -103.04135,36.217596,0</coordinates></LinearRing></outerBoundaryIs></Polygon>

=====================  =====================================================
Keyword Argument       Description
=====================  =====================================================
``precision``          This keyword may be used to specify the number of
                       significant digits for the coordinates in the KML
                       representation -- the default value is 8.
=====================  =====================================================

__ https://developers.google.com/kml/documentation/

``svg``
~~~~~~~

.. method:: GeoQuerySet.svg(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.AsSVG` function
    instead.

*Availability*: PostGIS, SpatiaLite

Attaches a ``svg`` attribute to every model in the queryset that contains
the `Scalable Vector Graphics (SVG)`__ path data of the geometry fields.

=====================  =====================================================
Keyword Argument       Description
=====================  =====================================================
``relative``           If set to ``True``, the path data will be implemented
                       in terms of relative moves.  Defaults to ``False``,
                       meaning that absolute moves are used instead.

``precision``          This keyword may be used to specify the number of
                       significant digits for the coordinates in the SVG
                       representation -- the default value is 8.
=====================  =====================================================

__ http://www.w3.org/Graphics/SVG/

Miscellaneous
-------------

``mem_size``
~~~~~~~~~~~~

.. method:: GeoQuerySet.mem_size(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.MemSize` function
    instead.

*Availability*: PostGIS

Returns the memory size (number of bytes) that the geometry field takes
in a ``mem_size`` attribute  on each element of the ``GeoQuerySet``.

``num_geom``
~~~~~~~~~~~~

.. method:: GeoQuerySet.num_geom(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.NumGeometries`
    function instead.

*Availability*: PostGIS, Oracle, SpatiaLite

Returns the number of geometries in a ``num_geom`` attribute on
each element of the ``GeoQuerySet`` if the geometry field is a
collection (e.g., a ``GEOMETRYCOLLECTION`` or ``MULTI*`` field);
otherwise sets with ``None``.

``num_points``
~~~~~~~~~~~~~~

.. method:: GeoQuerySet.num_points(**kwargs)

.. deprecated:: 1.9

    Use the :class:`~django.contrib.gis.db.models.functions.NumPoints` function
    instead.

*Availability*: PostGIS, Oracle, SpatiaLite

Returns the number of points in the first linestring in the
geometry field in a ``num_points`` attribute on each element of
the ``GeoQuerySet``; otherwise sets with ``None``.

Aggregate Functions
-------------------

Django provides some GIS-specific aggregate functions. For details on how to
use these aggregate functions, see :doc:`the topic guide on aggregation
</topics/db/aggregation>`.

=====================  =====================================================
Keyword Argument       Description
=====================  =====================================================
``tolerance``          This keyword is for Oracle only.  It is for the
                       tolerance value used by the ``SDOAGGRTYPE``
                       procedure; the  `Oracle documentation`__ has more
                       details.
=====================  =====================================================

__ https://docs.oracle.com/database/121/SPATL/GUID-3BD00273-E74F-4830-9444-A3BB15AA0AC4.htm#SPATL466

Example::

    >>> from django.contrib.gis.db.models import Extent, Union
    >>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))

``Collect``
~~~~~~~~~~~

.. class:: Collect(geo_field)

*Availability*: `PostGIS <https://postgis.net/docs/ST_Collect.html>`__,
SpatiaLite

Returns a ``GEOMETRYCOLLECTION`` or a ``MULTI`` geometry object from the geometry
column. This is analogous to a simplified version of the :class:`Union`
aggregate, except it can be several orders of magnitude faster than performing
a union because it simply rolls up geometries into a collection or multi object,
not caring about dissolving boundaries.

``Extent``
~~~~~~~~~~

.. class:: Extent(geo_field)

*Availability*: `PostGIS <https://postgis.net/docs/ST_Extent.html>`__,
Oracle, SpatiaLite

Returns the extent of all ``geo_field`` in the ``QuerySet`` as a four-tuple,
comprising the lower left coordinate and the upper right coordinate.

Example::

    >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
    >>> print(qs['poly__extent'])
    (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)

``Extent3D``
~~~~~~~~~~~~

.. class:: Extent3D(geo_field)

*Availability*: `PostGIS <https://postgis.net/docs/ST_3DExtent.html>`__

Returns the 3D extent of all ``geo_field`` in the ``QuerySet`` as a six-tuple,
comprising the lower left coordinate and upper right coordinate (each with x, y,
and z coordinates).

Example::

    >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly'))
    >>> print(qs['poly__extent3d'])
    (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)

``MakeLine``
~~~~~~~~~~~~

.. class:: MakeLine(geo_field)

*Availability*: `PostGIS <https://postgis.net/docs/ST_MakeLine.html>`__,
SpatiaLite

Returns a ``LineString`` constructed from the point field geometries in the
``QuerySet``. Currently, ordering the queryset has no effect.

.. versionchanged:: 1.10

    SpatiaLite support was added.

Example::

    >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly'))
    >>> print(qs['poly__makeline'])
    LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)

``Union``
~~~~~~~~~

.. class:: Union(geo_field)

*Availability*: `PostGIS <https://postgis.net/docs/ST_Union.html>`__,
Oracle, SpatiaLite

This method returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
comprising the union of every geometry in the queryset. Please note that use of
``Union`` is processor intensive and may take a significant amount of time on
large querysets.

.. note::

    If the computation time for using this method is too expensive, consider
    using :class:`Collect` instead.

Example::

    >>> u = Zipcode.objects.aggregate(Union(poly))  # This may take a long time.
    >>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly))  # A more sensible approach.

.. rubric:: Footnotes
.. [#fnde9im] *See* `OpenGIS Simple Feature Specification For SQL <http://www.opengis.org/docs/99-049.pdf>`_, at Ch. 2.1.13.2, p. 2-13 (The Dimensionally Extended Nine-Intersection Model).
.. [#fnsdorelate] *See* `SDO_RELATE documentation <https://docs.oracle.com/database/121/SPATL/sdo_relate.htm#SPATL1039>`_, from the Oracle Spatial and Graph Developer's Guide.
.. [#fncovers] For an explanation of this routine, read `Quirks of the "Contains" Spatial Predicate <http://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html>`_ by Martin Davis (a PostGIS developer).