| 1 | [[TOC]] |
| 2 | = Database API = |
| 3 | |
| 4 | '''Note:''' The following database lookup types can only be used with on geographic fields with {{{filter()}}}. Filters on 'normal' fields (e.g. {{{CharField}}}) may be chained with those on geographic fields. Thus, geographic queries take the following form (assuming the {{{Zip}}} model used in the [GeoDjangoModelAPI GeoDjango Model API docs]: |
| 5 | {{{ |
| 6 | #!python |
| 7 | >>> qs = Zip.objects.filter(<geo field A>__<geo lookup type>=<geo string B>) |
| 8 | >>> qs = Zip.objects.exclude(...) |
| 9 | }}} |
| 10 | |
| 11 | |
| 12 | == Creating and Saving Geographic-Enabled Objects == |
| 13 | Here is an example of how to create a geometry object (assuming the {{{Zip}}} model example above): |
| 14 | |
| 15 | {{{ |
| 16 | #!python |
| 17 | >>> from zipcode.models import Zip |
| 18 | >>> z = Zip(code=77096, poly='POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))') |
| 19 | >>> z.save() |
| 20 | }}} |
| 21 | |
| 22 | Geometries are represented as '''strings''' in either of the formats WKT (Well Known Text) or HEXEWKB (PostGIS specific, essentially a WKB geometry in hexadecimal). For example: |
| 23 | * WKT Polygon: {{{'POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))'}}} |
| 24 | * ''See'' Open GIS Consortium, Inc., ''[http://www.opengis.org/docs/99-049.pdf OpenGIS Simple Feature Specification For SQL]'', Document 99-049 (May 5, 1999), at Ch. 3.2.5 (SQL Textual Representation of Geometry, pg. 53). |
| 25 | * HEXEWKB Polygon: '{{{0103000000010000000 ... 00000000000002440'}}} |
| 26 | * ''See'' [http://postgis.refractions.net/docs/ch04.html#id2904792 "PostGIS EWKB, EWKT and Canonical Forms"], PostGIS documentation at Ch. 4.1.2. |
| 27 | |
| 28 | == PostGIS Operator Field Lookup Types == |
| 29 | |
| 30 | * ''See generally'', [http://postgis.refractions.net/docs/ch06.html#id2854381 "Operators", PostGIS Documentation at Ch. 6.2.2] |
| 31 | * '''Note:''' This API is subject to some change -- we're open to suggestions. |
| 32 | * {{{overlaps_left}}} |
| 33 | * Returns true if A's bounding box overlaps or is to the left of B's bounding box. |
| 34 | * PostGIS equivalent "{{{&<}}}" |
| 35 | * {{{overlaps_right}}} |
| 36 | * Returns true if A's bounding box overlaps or is to the right of B's bounding box. |
| 37 | * PostGIS equivalent "{{{&>}}}" |
| 38 | * {{{left}}} |
| 39 | * Returns true if A's bounding box is strictly to the left of B's bounding box. |
| 40 | * PostGIS equivalent "{{{<<}}}" |
| 41 | * {{{right}}} |
| 42 | * Returns true if A's bounding box is strictly to the right of B's bounding box. |
| 43 | * PostGIS equivalent "{{{>>}}}" |
| 44 | * {{{overlaps_below}}} |
| 45 | * Returns true if A's bounding box overlaps or is below B's bounding box. |
| 46 | * PostGIS equivalent "{{{&<|}}}" |
| 47 | * {{{overlaps_above}}} |
| 48 | * Returns true if A's bounding box overlaps or is above B's bounding box. |
| 49 | * PostGIS equivalent "{{{|&>}}}" |
| 50 | * {{{strictly_below}}} |
| 51 | * Returns true if A's bounding box is strictly below B's bounding box. |
| 52 | * PostGIS equivalent "{{{<<|}}}" |
| 53 | * {{{strictly_above}}} |
| 54 | * Returns true if A's bounding box is strictly above B's bounding box. |
| 55 | * PostGIS equivalent "{{{|>>}}}" |
| 56 | * {{{same_as}}} or {{{exact}}} |
| 57 | * The "same as" operator. It tests actual geometric equality of two features. So if A and B are the same feature, vertex-by-vertex, the operator returns true. |
| 58 | * PostGIS equivalent "{{{~=}}}" |
| 59 | * {{{contained}}} |
| 60 | * Returns true if A's bounding box is completely contained by B's bounding box. |
| 61 | * PostGIS equivalent "{{{@}}}" |
| 62 | * {{{bbcontains}}} |
| 63 | * Returns true if A's bounding box completely contains B's bounding box. |
| 64 | * PostGIS equivalent "{{{~}}}" |
| 65 | * {{{bboverlaps}}} |
| 66 | * Returns true if A's bounding box overlaps B's bounding box. |
| 67 | * PostGIS equivalent "{{{&&}}}" |
| 68 | |
| 69 | == PostGIS GEOS Function Field Lookup Types == |
| 70 | * ''See generally'' [http://postgis.refractions.net/docs/ch06.html#id2615853 "Geometry Relationship Functions", PostGIS Documentation at Ch. 6.1.2]. |
| 71 | * This documentation will be updated completely with the content from the aforementioned PostGIS docs. |
| 72 | * {{{equals}}} |
| 73 | * Requires GEOS |
| 74 | * Returns 1 (TRUE) if the given Geometries are "spatially equal". |
| 75 | * Use this for a 'better' answer than '='. equals('LINESTRING(0 0, 10 10)','LINESTRING(0 0, 5 5, 10 10)') is true. |
| 76 | * PostGIS equivalent {{{Equals(geometry, geometry)}}}, OGC SPEC s2.1.1.2 |
| 77 | * {{{disjoint}}} |
| 78 | * Requires GEOS |
| 79 | * Returns 1 (TRUE) if the Geometries are "spatially disjoint". |
| 80 | * PostGIS equivalent {{{Disjoint(geometry, geometry)}}} |
| 81 | * {{{intersects}}} |
| 82 | * PostGIS equivalent {{{Intersects(geometry, geometry)}}} |
| 83 | * {{{touches}}} |
| 84 | * PostGIS equivalent {{{Touches(geometry, geometry)}}} |
| 85 | * {{{crosses}}} |
| 86 | * PostGIS equivalent {{{Crosses(geometry, geometry)}}} |
| 87 | * {{{overlaps}}} |
| 88 | * PostGIS equivalent {{{Overlaps(geometry, geometry)}}} |
| 89 | * {{{contains}}} |
| 90 | * PostGIS equivalent {{{Contains(geometry, geometry)}}} |
| 91 | * {{{intersects}}} |
| 92 | * PostGIS equivalent {{{Intersects(geometry, geometry)}}} |
| 93 | * {{{relate}}} |
| 94 | * PostGIS equivelent {{{Relate(geometry, geometry)}}} |
| 95 | |
| 96 | = Extra Instance Methods = |
| 97 | |
| 98 | A model with geometry fields will get the following methods, substitute {{{GEOM}}} with the name of the geometry field: |
| 99 | |
| 100 | == get_GEOM_ogr == |
| 101 | Returns a {{{OGRGeometry}}} instance for the geometry. |
| 102 | |
| 103 | == get_GEOM_geos == |
| 104 | Returns a {{{GEOSGeometry}}} instance for the geometry. For example (using the {{{District}}} model from above): |
| 105 | |
| 106 | {{{ |
| 107 | #!python |
| 108 | >>> from django.contrib.gis.geos import GEOSGeometry |
| 109 | >>> dist = District.objects.get(name='Houston ISD') |
| 110 | >>> geom = dist.get_poly_geos() |
| 111 | >>> print geom.centroid.wkt |
| 112 | POINT(-95.231713 29.723235) |
| 113 | >>> print geom.area |
| 114 | 0.08332 |
| 115 | >>> print geom.geom_type |
| 116 | Polygon |
| 117 | >>> print geom.centroid.geom_type |
| 118 | Point |
| 119 | >>> print geom.intersects(GEOSGeometry('POINT(-95.395223 29.798088)')) |
| 120 | False |
| 121 | }}} |
| 122 | |
| 123 | == get_GEOM_wkt == |
| 124 | |
| 125 | Returns the OGC WKT (Well Known Text) for the geometry. For example (using the {{{School}}} model from above): |
| 126 | |
| 127 | {{{ |
| 128 | #!python |
| 129 | >>> skool = School.objects.get(name='PSAS') |
| 130 | >>> print skool.get_point_wkt() |
| 131 | POINT(-95.460822 29.745463) |
| 132 | }}} |
| 133 | |
| 134 | == get_GEOM_centroid == |
| 135 | |
| 136 | This routine will return the centroid of the geometry. For example (using the {{{District}}} model from above): |
| 137 | |
| 138 | {{{ |
| 139 | #!python |
| 140 | >>> dist = District.objects.get(name='Houston ISD') |
| 141 | >>> print dist.get_poly_centroid() |
| 142 | POINT(-95.231713 29.723235) |
| 143 | }}} |
| 144 | |
| 145 | == get_GEOM_area == |
| 146 | |
| 147 | This routine will return the area of the geometry field. |
| 148 | |
| 149 | {{{ |
| 150 | #!python |
| 151 | >>> dist = District.objects.get(name='Houston ISD') |
| 152 | >>> print dist.get_poly_area() |
| 153 | 0.08332 |
| 154 | }}} |
| 155 | |
| 156 | '''Note''': Units are in the projected units of the coordinate system. In the example above, the units are in degrees since we're using WGS84. |