From 120aae2209a50641f51e1f6bc4fe383fb42757fb Mon Sep 17 00:00:00 2001
From: Justin Bronn <jbronn@gmail.com>
Date: Tue, 12 Oct 2010 17:13:27 +0000
Subject: [PATCH] Enabled area calculations for geography columns.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14189 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/contrib/gis/db/models/query.py      | 5 ++++-
 django/contrib/gis/tests/geogapp/models.py | 2 +-
 django/contrib/gis/tests/geogapp/tests.py  | 9 +++++++++
 docs/ref/contrib/gis/model-api.txt         | 1 -
 4 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/django/contrib/gis/db/models/query.py b/django/contrib/gis/db/models/query.py
index 79eed853f9..4df1a3ab78 100644
--- a/django/contrib/gis/db/models/query.py
+++ b/django/contrib/gis/db/models/query.py
@@ -48,7 +48,10 @@ class GeoQuerySet(QuerySet):
             s['procedure_args']['tolerance'] = tolerance
             s['select_field'] = AreaField('sq_m') # Oracle returns area in units of meters.
         elif backend.postgis or backend.spatialite:
-            if not geo_field.geodetic(connection):
+            if backend.geography:
+                # Geography fields support area calculation, returns square meters.
+                s['select_field'] = AreaField('sq_m')
+            elif not geo_field.geodetic(connection):
                 # Getting the area units of the geographic field.
                 s['select_field'] = AreaField(Area.unit_attname(geo_field.units_name(connection)))
             else:
diff --git a/django/contrib/gis/tests/geogapp/models.py b/django/contrib/gis/tests/geogapp/models.py
index 3198fbd36d..3696ba2ff4 100644
--- a/django/contrib/gis/tests/geogapp/models.py
+++ b/django/contrib/gis/tests/geogapp/models.py
@@ -10,7 +10,7 @@ class Zipcode(models.Model):
     code = models.CharField(max_length=10)
     poly = models.PolygonField(geography=True)
     objects = models.GeoManager()
-    def __unicode__(self): return self.name
+    def __unicode__(self): return self.code
 
 class County(models.Model):
     name = models.CharField(max_length=25)
diff --git a/django/contrib/gis/tests/geogapp/tests.py b/django/contrib/gis/tests/geogapp/tests.py
index 3dea930afd..cb69ce94e1 100644
--- a/django/contrib/gis/tests/geogapp/tests.py
+++ b/django/contrib/gis/tests/geogapp/tests.py
@@ -76,3 +76,12 @@ class GeographyTest(TestCase):
             self.assertEqual(num_poly, len(c.mpoly))
             self.assertEqual(name, c.name)
             self.assertEqual(state, c.state)
+
+    def test06_geography_area(self):
+        "Testing that Area calculations work on geography columns."
+        from django.contrib.gis.measure import A
+        # SELECT ST_Area(poly) FROM geogapp_zipcode WHERE code='77002';
+        ref_area = 5439084.70637573
+        tol = 5
+        z = Zipcode.objects.area().get(code='77002')
+        self.assertAlmostEqual(z.area.sq_m, ref_area, tol)
diff --git a/docs/ref/contrib/gis/model-api.txt b/docs/ref/contrib/gis/model-api.txt
index b6d92dd24c..6b50cf363e 100644
--- a/docs/ref/contrib/gis/model-api.txt
+++ b/docs/ref/contrib/gis/model-api.txt
@@ -216,7 +216,6 @@ only the following additional :ref:`spatial lookups <spatial-lookups>` are
 available for geography columns:
 
 * :lookup:`bboverlaps`
-* :lookup:`exact`, and :lookup:`same_as`
 * :lookup:`coveredby`
 * :lookup:`covers`
 * :lookup:`intersects`