From 2eeb84bd55796affde0e94bd5d1276e973178f8e Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Mon, 16 Jun 2008 16:58:12 +0000 Subject: [PATCH] gis: Improvements to `get_srid_info`, including raising no exception when the srid == -1 and making the error checking and message more robust; tweaked testing tolerance in `distapp` for those running older versions of GEOS. git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7666 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/models.py | 26 +++++++++++++++-------- django/contrib/gis/tests/distapp/tests.py | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/django/contrib/gis/models.py b/django/contrib/gis/models.py index 8bf8ef4964..3231f926ff 100644 --- a/django/contrib/gis/models.py +++ b/django/contrib/gis/models.py @@ -226,13 +226,17 @@ if _srid_info: function is used when it is not possible to use the ORM (for example, during field initialization). """ - from django.db import connection + # SRID=-1 is a common convention for indicating the geometry has no + # spatial reference information associated with it. Thus, we will + # return all None values without raising an exception. + if srid == -1: return None, None, None + # Getting the spatial reference WKT associated with the SRID from the - # `spatial_ref_sys` (or equivalent) spatial database table. - # - # The following doesn't work: SpatialRefSys.objects.get(srid=srid) - # Why? `syncdb` fails to recognize installed geographic models when there's - # an ORM query instantiated within a model field. + # `spatial_ref_sys` (or equivalent) spatial database table. This query + # cannot be executed using the ORM because this information is needed + # when the ORM cannot be used (e.g., during the initialization of + # `GeometryField`). + from django.db import connection cur = connection.cursor() qn = connection.ops.quote_name stmt = 'SELECT %(table)s.%(wkt_col)s FROM %(table)s WHERE (%(table)s.%(srid_col)s = %(srid)s)' @@ -242,9 +246,13 @@ if _srid_info: 'srid' : srid, } cur.execute(stmt) - srs_wkt = cur.fetchone()[0] - if srs_wkt is None: - raise ValueError('Failed to find Spatial Reference System entry corresponding to SRID=%s' % srid) + + # Fetching the WKT from the cursor; if the query failed raise an Exception. + fetched = cur.fetchone() + if not fetched: + raise ValueError('Failed to find spatial reference entry in "%s" corresponding to SRID=%s.' % + (SpatialRefSys._meta.db_table, srid)) + srs_wkt = fetched[0] # Getting metadata associated with the spatial reference system identifier. # Specifically, getting the unit information and spheroid information diff --git a/django/contrib/gis/tests/distapp/tests.py b/django/contrib/gis/tests/distapp/tests.py index 53bcf9d42d..b17e3d741c 100644 --- a/django/contrib/gis/tests/distapp/tests.py +++ b/django/contrib/gis/tests/distapp/tests.py @@ -273,7 +273,7 @@ class DistanceTest(unittest.TestCase): len_m = 473504.769553813 qs = Interstate.objects.length() if oracle: tol = 2 - else: tol = 7 + else: tol = 5 self.assertAlmostEqual(len_m, qs[0].length.m, tol) def test08_perimeter(self):