| 1 |
============================= |
| 2 |
Geographic Database Functions |
| 3 |
============================= |
| 4 |
|
| 5 |
.. module:: django.contrib.gis.db.models.functions |
| 6 |
:synopsis: Geographic Database Functions |
| 7 |
|
| 8 |
The functions documented on this page allow users to access geographic database |
| 9 |
functions to be used in annotations, aggregations, or filters in Django. |
| 10 |
|
| 11 |
Example: |
| 12 |
|
| 13 |
.. code-block:: pycon |
| 14 |
|
| 15 |
>>> from django.contrib.gis.db.models.functions import Length |
| 16 |
>>> Track.objects.annotate(length=Length("line")).filter(length__gt=100) |
| 17 |
|
| 18 |
Not all backends support all functions, so refer to the documentation of each |
| 19 |
function to see if your database backend supports the function you want to use. |
| 20 |
If you call a geographic function on a backend that doesn't support it, you'll |
| 21 |
get a ``NotImplementedError`` exception. |
| 22 |
|
| 23 |
Measurements |
| 24 |
============ |
| 25 |
|
| 26 |
``Area`` |
| 27 |
-------- |
| 28 |
|
| 29 |
.. class:: Area(expression, **extra) |
| 30 |
|
| 31 |
*Availability*: MariaDB, `MySQL |
| 32 |
<https://dev.mysql.com/doc/refman/en/gis-polygon-property-functions.html#function_st-area>`_, |
| 33 |
Oracle, `PostGIS <https://postgis.net/docs/ST_Area.html>`__, SpatiaLite |
| 34 |
|
| 35 |
Accepts a single geographic field or expression and returns the area of the |
| 36 |
field as an :class:`~django.contrib.gis.measure.Area` measure. |
| 37 |
|
| 38 |
MySQL and SpatiaLite without LWGEOM/RTTOPO don't support area calculations on |
| 39 |
geographic SRSes. |
| 40 |
|
| 41 |
``Distance`` |
| 42 |
------------ |
| 43 |
|
| 44 |
.. class:: Distance(expr1, expr2, spheroid=None, **extra) |
| 45 |
|
| 46 |
*Availability*: MariaDB, `MySQL |
| 47 |
<https://dev.mysql.com/doc/refman/en/spatial-relation-functions-object-shapes.html#function_st-distance>`__, |
| 48 |
`PostGIS <https://postgis.net/docs/ST_Distance.html>`__, Oracle, SpatiaLite |
| 49 |
|
| 50 |
Accepts two geographic fields or expressions and returns the distance between |
| 51 |
them, as a :class:`~django.contrib.gis.measure.Distance` object. On MySQL, a raw |
| 52 |
float value is returned when the coordinates are geodetic. |
| 53 |
|
| 54 |
On backends that support distance calculation on geodetic coordinates, the |
| 55 |
proper backend function is automatically chosen depending on the SRID value of |
| 56 |
the geometries (e.g. `ST_DistanceSphere |
| 57 |
<https://postgis.net/docs/ST_DistanceSphere.html>`__ on PostGIS). |
| 58 |
|
| 59 |
When distances are calculated with geodetic (angular) coordinates, as is the |
| 60 |
case with the default WGS84 (4326) SRID, you can set the ``spheroid`` keyword |
| 61 |
argument to decide if the calculation should be based on a simple sphere (less |
| 62 |
accurate, less resource-intensive) or on a spheroid (more accurate, more |
| 63 |
resource-intensive). |
| 64 |
|
| 65 |
In the following example, the distance from the city of Hobart to every other |
| 66 |
:class:`~django.contrib.gis.db.models.PointField` in the ``AustraliaCity`` |
| 67 |
queryset is calculated: |
| 68 |
|
| 69 |
.. code-block:: pycon |
| 70 |
|
| 71 |
>>> from django.contrib.gis.db.models.functions import Distance |
| 72 |
>>> pnt = AustraliaCity.objects.get(name="Hobart").point |
| 73 |
>>> for city in AustraliaCity.objects.annotate(distance=Distance("point", pnt)): |
| 74 |
... print(city.name, city.distance) |
| 75 |
... |
| 76 |
Wollongong 990071.220408 m |
| 77 |
Shellharbour 972804.613941 m |
| 78 |
Thirroul 1002334.36351 m |
| 79 |
... |
| 80 |
|
| 81 |
.. note:: |
| 82 |
|
| 83 |
Because the ``distance`` attribute is a |
| 84 |
:class:`~django.contrib.gis.measure.Distance` object, you can easily express |
| 85 |
the value in the units of your choice. For example, ``city.distance.mi`` is |
| 86 |
the distance value in miles and ``city.distance.km`` is the distance value |
| 87 |
in kilometers. See :doc:`measure` for usage details and the list of |
| 88 |
:ref:`supported_units`. |
| 89 |
|
| 90 |
``GeometryDistance`` |
| 91 |
-------------------- |
| 92 |
|
| 93 |
.. class:: GeometryDistance(expr1, expr2, **extra) |
| 94 |
|
| 95 |
*Availability*: `PostGIS <https://postgis.net/docs/geometry_distance_knn.html>`__ |
| 96 |
|
| 97 |
Accepts two geographic fields or expressions and returns the distance between |
| 98 |
them. When used in an :meth:`~django.db.models.query.QuerySet.order_by` clause, |
| 99 |
it provides index-assisted nearest-neighbor result sets. |
| 100 |
|
| 101 |
``Length`` |
| 102 |
---------- |
| 103 |
|
| 104 |
.. class:: Length(expression, spheroid=True, **extra) |
| 105 |
|
| 106 |
*Availability*: MariaDB, `MySQL |
| 107 |
<https://dev.mysql.com/doc/refman/en/gis-linestring-property-functions.html#function_st-length>`__, |
| 108 |
Oracle, `PostGIS <https://postgis.net/docs/ST_Length.html>`__, SpatiaLite |
| 109 |
|
| 110 |
Accepts a single geographic linestring or multilinestring field or expression |
| 111 |
and returns its length as a :class:`~django.contrib.gis.measure.Distance` |
| 112 |
measure. |
| 113 |
|
| 114 |
On PostGIS and SpatiaLite, when the coordinates are geodetic (angular), you can |
| 115 |
specify if the calculation should be based on a simple sphere (less |
| 116 |
accurate, less resource-intensive) or on a spheroid (more accurate, more |
| 117 |
resource-intensive) with the ``spheroid`` keyword argument. |
| 118 |
|
| 119 |
MySQL doesn't support length calculations on geographic SRSes. |
| 120 |
|
| 121 |
``Perimeter`` |
| 122 |
------------- |
| 123 |
|
| 124 |
.. class:: Perimeter(expression, **extra) |
| 125 |
|
| 126 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_Perimeter.html>`__, |
| 127 |
Oracle, SpatiaLite |
| 128 |
|
| 129 |
Accepts a single geographic field or expression and returns the perimeter of the |
| 130 |
geometry field as a :class:`~django.contrib.gis.measure.Distance` object. |
| 131 |
|
| 132 |
Relationships |
| 133 |
============= |
| 134 |
|
| 135 |
``Azimuth`` |
| 136 |
----------- |
| 137 |
|
| 138 |
.. class:: Azimuth(point_a, point_b, **extra) |
| 139 |
|
| 140 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_Azimuth.html>`__, |
| 141 |
SpatiaLite (LWGEOM/RTTOPO) |
| 142 |
|
| 143 |
Returns the azimuth in radians of the segment defined by the given point |
| 144 |
geometries, or ``None`` if the two points are coincident. The azimuth is angle |
| 145 |
referenced from north and is positive clockwise: north = ``0``; east = ``π/2``; |
| 146 |
south = ``π``; west = ``3π/2``. |
| 147 |
|
| 148 |
``BoundingCircle`` |
| 149 |
------------------ |
| 150 |
|
| 151 |
.. class:: BoundingCircle(expression, num_seg=48, **extra) |
| 152 |
|
| 153 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_MinimumBoundingCircle.html>`__, |
| 154 |
`Oracle <https://docs.oracle.com/en/database/oracle/oracle-database/21/spatl/ |
| 155 |
SDO_GEOM-reference.html#GUID-82A61626-BB64-4793-B53D-A0DBEC91831A>`_, |
| 156 |
SpatiaLite 5.1+ |
| 157 |
|
| 158 |
Accepts a single geographic field or expression and returns the smallest circle |
| 159 |
polygon that can fully contain the geometry. |
| 160 |
|
| 161 |
The ``num_seg`` parameter is used only on PostGIS. |
| 162 |
|
| 163 |
.. versionchanged:: 5.1 |
| 164 |
|
| 165 |
SpatiaLite 5.1+ support was added. |
| 166 |
|
| 167 |
``Centroid`` |
| 168 |
------------ |
| 169 |
|
| 170 |
.. class:: Centroid(expression, **extra) |
| 171 |
|
| 172 |
*Availability*: MariaDB, `MySQL |
| 173 |
<https://dev.mysql.com/doc/refman/en/gis-polygon-property-functions.html#function_st-centroid>`__, |
| 174 |
`PostGIS <https://postgis.net/docs/ST_Centroid.html>`__, Oracle, SpatiaLite |
| 175 |
|
| 176 |
Accepts a single geographic field or expression and returns the ``centroid`` |
| 177 |
value of the geometry. |
| 178 |
|
| 179 |
``ClosestPoint`` |
| 180 |
---------------- |
| 181 |
|
| 182 |
.. class:: ClosestPoint(expr1, expr2, **extra) |
| 183 |
|
| 184 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_ClosestPoint.html>`__, |
| 185 |
SpatiaLite |
| 186 |
|
| 187 |
Accepts two geographic fields or expressions and returns the 2-dimensional |
| 188 |
point on geometry A that is closest to geometry B. |
| 189 |
|
| 190 |
``Envelope`` |
| 191 |
------------ |
| 192 |
|
| 193 |
.. class:: Envelope(expression, **extra) |
| 194 |
|
| 195 |
*Availability*: MariaDB, `MySQL |
| 196 |
<https://dev.mysql.com/doc/refman/en/gis-general-property-functions.html#function_st-envelope>`__, |
| 197 |
`Oracle <https://docs.oracle.com/en/database/oracle/oracle-database/21/spatl/ |
| 198 |
spatial-operators-reference.html#GUID-ACED800F-3435-44AA-9606-D40934A23ED0>`__, |
| 199 |
`PostGIS <https://postgis.net/docs/ST_Envelope.html>`__, SpatiaLite |
| 200 |
|
| 201 |
Accepts a single geographic field or expression and returns the geometry |
| 202 |
representing the bounding box of the geometry. |
| 203 |
|
| 204 |
``LineLocatePoint`` |
| 205 |
------------------- |
| 206 |
|
| 207 |
.. class:: LineLocatePoint(linestring, point, **extra) |
| 208 |
|
| 209 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_LineLocatePoint.html>`__, |
| 210 |
SpatiaLite |
| 211 |
|
| 212 |
Returns a float between 0 and 1 representing the location of the closest point on |
| 213 |
``linestring`` to the given ``point``, as a fraction of the 2D line length. |
| 214 |
|
| 215 |
``PointOnSurface`` |
| 216 |
------------------ |
| 217 |
|
| 218 |
.. class:: PointOnSurface(expression, **extra) |
| 219 |
|
| 220 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_PointOnSurface.html>`__, |
| 221 |
MariaDB, Oracle, SpatiaLite |
| 222 |
|
| 223 |
Accepts a single geographic field or expression and returns a ``Point`` geometry |
| 224 |
guaranteed to lie on the surface of the field; otherwise returns ``None``. |
| 225 |
|
| 226 |
Operations |
| 227 |
========== |
| 228 |
|
| 229 |
``Difference`` |
| 230 |
-------------- |
| 231 |
|
| 232 |
.. class:: Difference(expr1, expr2, **extra) |
| 233 |
|
| 234 |
*Availability*: MariaDB, `MySQL |
| 235 |
<https://dev.mysql.com/doc/refman/en/spatial-operator-functions.html#function_st-difference>`__, |
| 236 |
`PostGIS <https://postgis.net/docs/ST_Difference.html>`__, Oracle, SpatiaLite |
| 237 |
|
| 238 |
Accepts two geographic fields or expressions and returns the geometric |
| 239 |
difference, that is the part of geometry A that does not intersect with |
| 240 |
geometry B. |
| 241 |
|
| 242 |
``Intersection`` |
| 243 |
---------------- |
| 244 |
|
| 245 |
.. class:: Intersection(expr1, expr2, **extra) |
| 246 |
|
| 247 |
*Availability*: MariaDB, `MySQL |
| 248 |
<https://dev.mysql.com/doc/refman/en/spatial-operator-functions.html#function_st-intersection>`__, |
| 249 |
`PostGIS <https://postgis.net/docs/ST_Intersection.html>`__, Oracle, SpatiaLite |
| 250 |
|
| 251 |
Accepts two geographic fields or expressions and returns the geometric |
| 252 |
intersection between them. |
| 253 |
|
| 254 |
``SymDifference`` |
| 255 |
----------------- |
| 256 |
|
| 257 |
.. class:: SymDifference(expr1, expr2, **extra) |
| 258 |
|
| 259 |
*Availability*: MariaDB, `MySQL |
| 260 |
<https://dev.mysql.com/doc/refman/en/spatial-operator-functions.html#function_st-symdifference>`__, |
| 261 |
`PostGIS <https://postgis.net/docs/ST_SymDifference.html>`__, Oracle, |
| 262 |
SpatiaLite |
| 263 |
|
| 264 |
Accepts two geographic fields or expressions and returns the geometric |
| 265 |
symmetric difference (union without the intersection) between the given |
| 266 |
parameters. |
| 267 |
|
| 268 |
``Union`` |
| 269 |
--------- |
| 270 |
|
| 271 |
.. class:: Union(expr1, expr2, **extra) |
| 272 |
|
| 273 |
*Availability*: MariaDB, `MySQL |
| 274 |
<https://dev.mysql.com/doc/refman/en/spatial-operator-functions.html#function_st-union>`__, |
| 275 |
`PostGIS <https://postgis.net/docs/ST_Union.html>`__, Oracle, SpatiaLite |
| 276 |
|
| 277 |
Accepts two geographic fields or expressions and returns the union of both |
| 278 |
geometries. |
| 279 |
|
| 280 |
.. _`Well-known binary (WKB)`: https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary |
| 281 |
.. _`Well-known text (WKT)`: https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry |
| 282 |
|
| 283 |
Editors |
| 284 |
======= |
| 285 |
|
| 286 |
``ForcePolygonCW`` |
| 287 |
------------------ |
| 288 |
|
| 289 |
.. class:: ForcePolygonCW(expression, **extra) |
| 290 |
|
| 291 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_ForcePolygonCW.html>`__, |
| 292 |
SpatiaLite |
| 293 |
|
| 294 |
Accepts a single geographic field or expression and returns a modified version |
| 295 |
of the polygon/multipolygon in which all exterior rings are oriented clockwise |
| 296 |
and all interior rings are oriented counterclockwise. Non-polygonal geometries |
| 297 |
are returned unchanged. |
| 298 |
|
| 299 |
``MakeValid`` |
| 300 |
------------- |
| 301 |
|
| 302 |
.. class:: MakeValid(expr) |
| 303 |
|
| 304 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_MakeValid.html>`__, |
| 305 |
SpatiaLite (LWGEOM/RTTOPO) |
| 306 |
|
| 307 |
Accepts a geographic field or expression and attempts to convert the value into |
| 308 |
a valid geometry without losing any of the input vertices. Geometries that are |
| 309 |
already valid are returned without changes. Simple polygons might become a |
| 310 |
multipolygon and the result might be of lower dimension than the input. |
| 311 |
|
| 312 |
``Reverse`` |
| 313 |
----------- |
| 314 |
|
| 315 |
.. class:: Reverse(expression, **extra) |
| 316 |
|
| 317 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_Reverse.html>`__, Oracle, |
| 318 |
SpatiaLite |
| 319 |
|
| 320 |
Accepts a single geographic field or expression and returns a geometry with |
| 321 |
reversed coordinates. |
| 322 |
|
| 323 |
``Scale`` |
| 324 |
--------- |
| 325 |
|
| 326 |
.. class:: Scale(expression, x, y, z=0.0, **extra) |
| 327 |
|
| 328 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_Scale.html>`__, SpatiaLite |
| 329 |
|
| 330 |
Accepts a single geographic field or expression and returns a geometry with |
| 331 |
scaled coordinates by multiplying them with the ``x``, ``y``, and optionally |
| 332 |
``z`` parameters. |
| 333 |
|
| 334 |
``SnapToGrid`` |
| 335 |
-------------- |
| 336 |
|
| 337 |
.. class:: SnapToGrid(expression, *args, **extra) |
| 338 |
|
| 339 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_SnapToGrid.html>`__, |
| 340 |
SpatiaLite |
| 341 |
|
| 342 |
Accepts a single geographic field or expression and returns a geometry with all |
| 343 |
points snapped to the given grid. How the geometry is snapped to the grid |
| 344 |
depends on how many numeric (either float, integer, or long) arguments are |
| 345 |
given. |
| 346 |
|
| 347 |
=================== ===================================================== |
| 348 |
Number of Arguments Description |
| 349 |
=================== ===================================================== |
| 350 |
1 A single size to snap both the X and Y grids to. |
| 351 |
2 X and Y sizes to snap the grid to. |
| 352 |
4 X, Y sizes and the corresponding X, Y origins. |
| 353 |
=================== ===================================================== |
| 354 |
|
| 355 |
``Transform`` |
| 356 |
------------- |
| 357 |
|
| 358 |
.. class:: Transform(expression, srid, **extra) |
| 359 |
|
| 360 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_Transform.html>`__, |
| 361 |
Oracle, SpatiaLite |
| 362 |
|
| 363 |
Accepts a geographic field or expression and a SRID integer code, and returns |
| 364 |
the transformed geometry to the spatial reference system specified by the |
| 365 |
``srid`` parameter. |
| 366 |
|
| 367 |
.. note:: |
| 368 |
|
| 369 |
What spatial reference system an integer SRID corresponds to may depend on |
| 370 |
the spatial database used. In other words, the SRID numbers used for Oracle |
| 371 |
are not necessarily the same as those used by PostGIS. |
| 372 |
|
| 373 |
``Translate`` |
| 374 |
------------- |
| 375 |
|
| 376 |
.. class:: Translate(expression, x, y, z=0.0, **extra) |
| 377 |
|
| 378 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_Translate.html>`__, |
| 379 |
SpatiaLite |
| 380 |
|
| 381 |
Accepts a single geographic field or expression and returns a geometry with |
| 382 |
its coordinates offset by the ``x``, ``y``, and optionally ``z`` numeric |
| 383 |
parameters. |
| 384 |
|
| 385 |
Input format |
| 386 |
============ |
| 387 |
|
| 388 |
``FromWKB`` |
| 389 |
----------- |
| 390 |
|
| 391 |
.. class:: FromWKB(expression, srid=0, **extra) |
| 392 |
|
| 393 |
*Availability*: MariaDB, `MySQL |
| 394 |
<https://dev.mysql.com/doc/refman/en/gis-wkb-functions.html#function_st-geomfromwkb>`__, |
| 395 |
Oracle, `PostGIS <https://postgis.net/docs/ST_GeomFromWKB.html>`__, SpatiaLite |
| 396 |
|
| 397 |
Creates geometry from `Well-known binary (WKB)`_ representation. The optional |
| 398 |
``srid`` argument allows to specify the SRID of the resulting geometry. |
| 399 |
``srid`` is ignored on Oracle. |
| 400 |
|
| 401 |
.. versionchanged:: 5.1 |
| 402 |
|
| 403 |
The ``srid`` argument was added. |
| 404 |
|
| 405 |
``FromWKT`` |
| 406 |
----------- |
| 407 |
|
| 408 |
.. class:: FromWKT(expression, srid=0, **extra) |
| 409 |
|
| 410 |
*Availability*: MariaDB, `MySQL |
| 411 |
<https://dev.mysql.com/doc/refman/en/gis-wkt-functions.html#function_st-geomfromtext>`__, |
| 412 |
Oracle, `PostGIS <https://postgis.net/docs/ST_GeomFromText.html>`__, SpatiaLite |
| 413 |
|
| 414 |
Creates geometry from `Well-known text (WKT)`_ representation. The optional |
| 415 |
``srid`` argument allows to specify the SRID of the resulting geometry. |
| 416 |
``srid`` is ignored on Oracle. |
| 417 |
|
| 418 |
.. versionchanged:: 5.1 |
| 419 |
|
| 420 |
The ``srid`` argument was added. |
| 421 |
|
| 422 |
Output format |
| 423 |
============= |
| 424 |
|
| 425 |
``AsGeoJSON`` |
| 426 |
------------- |
| 427 |
|
| 428 |
.. class:: AsGeoJSON(expression, bbox=False, crs=False, precision=8, **extra) |
| 429 |
|
| 430 |
*Availability*: MariaDB, `MySQL |
| 431 |
<https://dev.mysql.com/doc/refman/en/spatial-geojson-functions.html#function_st-asgeojson>`__, |
| 432 |
Oracle, `PostGIS <https://postgis.net/docs/ST_AsGeoJSON.html>`__, SpatiaLite |
| 433 |
|
| 434 |
Accepts a single geographic field or expression and returns a `GeoJSON |
| 435 |
<https://geojson.org/>`_ representation of the geometry. Note that the result |
| 436 |
is not a complete GeoJSON structure but only the ``geometry`` key content of a |
| 437 |
GeoJSON structure. See also :doc:`/ref/contrib/gis/serializers`. |
| 438 |
|
| 439 |
Example: |
| 440 |
|
| 441 |
.. code-block:: pycon |
| 442 |
|
| 443 |
>>> City.objects.annotate(json=AsGeoJSON("point")).get(name="Chicago").json |
| 444 |
{"type":"Point","coordinates":[-87.65018,41.85039]} |
| 445 |
|
| 446 |
===================== ===================================================== |
| 447 |
Keyword Argument Description |
| 448 |
===================== ===================================================== |
| 449 |
``bbox`` Set this to ``True`` if you want the bounding box |
| 450 |
to be included in the returned GeoJSON. Ignored on |
| 451 |
Oracle. |
| 452 |
|
| 453 |
``crs`` Set this to ``True`` if you want the coordinate |
| 454 |
reference system to be included in the returned |
| 455 |
GeoJSON. Ignored on MySQL and Oracle. |
| 456 |
|
| 457 |
``precision`` It may be used to specify the number of significant |
| 458 |
digits for the coordinates in the GeoJSON |
| 459 |
representation -- the default value is 8. Ignored on |
| 460 |
Oracle. |
| 461 |
===================== ===================================================== |
| 462 |
|
| 463 |
``AsGML`` |
| 464 |
--------- |
| 465 |
|
| 466 |
.. class:: AsGML(expression, version=2, precision=8, **extra) |
| 467 |
|
| 468 |
*Availability*: Oracle, `PostGIS <https://postgis.net/docs/ST_AsGML.html>`__, |
| 469 |
SpatiaLite |
| 470 |
|
| 471 |
Accepts a single geographic field or expression and returns a `Geographic Markup |
| 472 |
Language (GML)`__ representation of the geometry. |
| 473 |
|
| 474 |
Example: |
| 475 |
|
| 476 |
.. code-block:: pycon |
| 477 |
|
| 478 |
>>> qs = Zipcode.objects.annotate(gml=AsGML("poly")) |
| 479 |
>>> print(qs[0].gml) |
| 480 |
<gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70.245363 ... |
| 481 |
-147.78711,70.245363</gml:OuterBoundaryIs></gml:Polygon> |
| 482 |
|
| 483 |
===================== ===================================================== |
| 484 |
Keyword Argument Description |
| 485 |
===================== ===================================================== |
| 486 |
``precision`` Specifies the number of significant digits for the |
| 487 |
coordinates in the GML representation -- the default |
| 488 |
value is 8. Ignored on Oracle. |
| 489 |
|
| 490 |
``version`` Specifies the GML version to use: 2 (default) or 3. |
| 491 |
===================== ===================================================== |
| 492 |
|
| 493 |
__ https://en.wikipedia.org/wiki/Geography_Markup_Language |
| 494 |
|
| 495 |
``AsKML`` |
| 496 |
--------- |
| 497 |
|
| 498 |
.. class:: AsKML(expression, precision=8, **extra) |
| 499 |
|
| 500 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_AsKML.html>`__, SpatiaLite |
| 501 |
|
| 502 |
Accepts a single geographic field or expression and returns a `Keyhole Markup |
| 503 |
Language (KML)`__ representation of the geometry. |
| 504 |
|
| 505 |
Example: |
| 506 |
|
| 507 |
.. code-block:: pycon |
| 508 |
|
| 509 |
>>> qs = Zipcode.objects.annotate(kml=AsKML("poly")) |
| 510 |
>>> print(qs[0].kml) |
| 511 |
<Polygon><outerBoundaryIs><LinearRing><coordinates>-103.04135,36.217596,0 ... |
| 512 |
-103.04135,36.217596,0</coordinates></LinearRing></outerBoundaryIs></Polygon> |
| 513 |
|
| 514 |
===================== ===================================================== |
| 515 |
Keyword Argument Description |
| 516 |
===================== ===================================================== |
| 517 |
``precision`` This keyword may be used to specify the number of |
| 518 |
significant digits for the coordinates in the KML |
| 519 |
representation -- the default value is 8. |
| 520 |
===================== ===================================================== |
| 521 |
|
| 522 |
__ https://developers.google.com/kml/documentation/ |
| 523 |
|
| 524 |
``AsSVG`` |
| 525 |
--------- |
| 526 |
|
| 527 |
.. class:: AsSVG(expression, relative=False, precision=8, **extra) |
| 528 |
|
| 529 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_AsSVG.html>`__, SpatiaLite |
| 530 |
|
| 531 |
Accepts a single geographic field or expression and returns a `Scalable Vector |
| 532 |
Graphics (SVG)`__ representation of the geometry. |
| 533 |
|
| 534 |
===================== ===================================================== |
| 535 |
Keyword Argument Description |
| 536 |
===================== ===================================================== |
| 537 |
``relative`` If set to ``True``, the path data will be implemented |
| 538 |
in terms of relative moves. Defaults to ``False``, |
| 539 |
meaning that absolute moves are used instead. |
| 540 |
|
| 541 |
``precision`` This keyword may be used to specify the number of |
| 542 |
significant digits for the coordinates in the SVG |
| 543 |
representation -- the default value is 8. |
| 544 |
===================== ===================================================== |
| 545 |
|
| 546 |
__ https://www.w3.org/Graphics/SVG/ |
| 547 |
|
| 548 |
``AsWKB`` |
| 549 |
--------- |
| 550 |
|
| 551 |
.. class:: AsWKB(expression, **extra) |
| 552 |
|
| 553 |
*Availability*: MariaDB, `MySQL |
| 554 |
<https://dev.mysql.com/doc/refman/en/gis-format-conversion-functions.html#function_st-asbinary>`__, |
| 555 |
Oracle, `PostGIS <https://postgis.net/docs/ST_AsBinary.html>`__, SpatiaLite |
| 556 |
|
| 557 |
Accepts a single geographic field or expression and returns a `Well-known |
| 558 |
binary (WKB)`_ representation of the geometry. |
| 559 |
|
| 560 |
Example: |
| 561 |
|
| 562 |
.. code-block:: pycon |
| 563 |
|
| 564 |
>>> bytes(City.objects.annotate(wkb=AsWKB("point")).get(name="Chelyabinsk").wkb) |
| 565 |
b'\x01\x01\x00\x00\x00]3\xf9f\x9b\x91K@\x00X\x1d9\xd2\xb9N@' |
| 566 |
|
| 567 |
``AsWKT`` |
| 568 |
--------- |
| 569 |
|
| 570 |
.. class:: AsWKT(expression, **extra) |
| 571 |
|
| 572 |
*Availability*: MariaDB, `MySQL |
| 573 |
<https://dev.mysql.com/doc/refman/en/gis-format-conversion-functions.html#function_st-astext>`__, |
| 574 |
Oracle, `PostGIS <https://postgis.net/docs/ST_AsText.html>`__, SpatiaLite |
| 575 |
|
| 576 |
Accepts a single geographic field or expression and returns a `Well-known text |
| 577 |
(WKT)`_ representation of the geometry. |
| 578 |
|
| 579 |
Example: |
| 580 |
|
| 581 |
.. code-block:: pycon |
| 582 |
|
| 583 |
>>> City.objects.annotate(wkt=AsWKT("point")).get(name="Chelyabinsk").wkt |
| 584 |
'POINT (55.137555 61.451728)' |
| 585 |
|
| 586 |
``GeoHash`` |
| 587 |
----------- |
| 588 |
|
| 589 |
.. class:: GeoHash(expression, precision=None, **extra) |
| 590 |
|
| 591 |
*Availability*: `MySQL |
| 592 |
<https://dev.mysql.com/doc/refman/en/spatial-geohash-functions.html#function_st-geohash>`__, |
| 593 |
`PostGIS <https://postgis.net/docs/ST_GeoHash.html>`__, SpatiaLite |
| 594 |
(LWGEOM/RTTOPO) |
| 595 |
|
| 596 |
Accepts a single geographic field or expression and returns a `GeoHash`__ |
| 597 |
representation of the geometry. |
| 598 |
|
| 599 |
The ``precision`` keyword argument controls the number of characters in the |
| 600 |
result. |
| 601 |
|
| 602 |
__ https://en.wikipedia.org/wiki/Geohash |
| 603 |
|
| 604 |
Miscellaneous |
| 605 |
============= |
| 606 |
|
| 607 |
``IsEmpty`` |
| 608 |
----------- |
| 609 |
|
| 610 |
.. class:: IsEmpty(expr) |
| 611 |
|
| 612 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_IsEmpty.html>`__ |
| 613 |
|
| 614 |
Accepts a geographic field or expression and tests if the value is an empty |
| 615 |
geometry. Returns ``True`` if its value is empty and ``False`` otherwise. |
| 616 |
|
| 617 |
``IsValid`` |
| 618 |
----------- |
| 619 |
|
| 620 |
.. class:: IsValid(expr) |
| 621 |
|
| 622 |
*Availability*: `MySQL |
| 623 |
<https://dev.mysql.com/doc/refman/en/spatial-convenience-functions.html#function_st-isvalid>`__, |
| 624 |
`PostGIS <https://postgis.net/docs/ST_IsValid.html>`__, Oracle, SpatiaLite |
| 625 |
|
| 626 |
Accepts a geographic field or expression and tests if the value is well formed. |
| 627 |
Returns ``True`` if its value is a valid geometry and ``False`` otherwise. |
| 628 |
|
| 629 |
``MemSize`` |
| 630 |
----------- |
| 631 |
|
| 632 |
.. class:: MemSize(expression, **extra) |
| 633 |
|
| 634 |
*Availability*: `PostGIS <https://postgis.net/docs/ST_MemSize.html>`__ |
| 635 |
|
| 636 |
Accepts a single geographic field or expression and returns the memory size |
| 637 |
(number of bytes) that the geometry field takes. |
| 638 |
|
| 639 |
``NumGeometries`` |
| 640 |
----------------- |
| 641 |
|
| 642 |
.. class:: NumGeometries(expression, **extra) |
| 643 |
|
| 644 |
*Availability*: MariaDB, `MySQL |
| 645 |
<https://dev.mysql.com/doc/refman/en/gis-geometrycollection-property-functions.html#function_st-numgeometries>`__, |
| 646 |
`PostGIS <https://postgis.net/docs/ST_NumGeometries.html>`__, Oracle, |
| 647 |
SpatiaLite |
| 648 |
|
| 649 |
Accepts a single geographic field or expression and returns the number of |
| 650 |
geometries if the geometry field is a collection (e.g., a ``GEOMETRYCOLLECTION`` |
| 651 |
or ``MULTI*`` field). Returns 1 for single geometries. |
| 652 |
|
| 653 |
On MySQL, returns ``None`` for single geometries. |
| 654 |
|
| 655 |
``NumPoints`` |
| 656 |
------------- |
| 657 |
|
| 658 |
.. class:: NumPoints(expression, **extra) |
| 659 |
|
| 660 |
*Availability*: MariaDB, `MySQL |
| 661 |
<https://dev.mysql.com/doc/refman/en/gis-linestring-property-functions.html#function_st-numpoints>`__, |
| 662 |
`PostGIS <https://postgis.net/docs/ST_NPoints.html>`__, Oracle, SpatiaLite |
| 663 |
|
| 664 |
Accepts a single geographic field or expression and returns the number of points |
| 665 |
in a geometry. |
| 666 |
|
| 667 |
On MySQL, returns ``None`` for any non-``LINESTRING`` geometry. |