1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

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
This commit is contained in:
Justin Bronn 2008-06-16 16:58:12 +00:00
parent 6fdd9254e6
commit 2eeb84bd55
2 changed files with 18 additions and 10 deletions

View File

@ -226,13 +226,17 @@ if _srid_info:
function is used when it is not possible to use the ORM (for example, function is used when it is not possible to use the ORM (for example,
during field initialization). 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 # Getting the spatial reference WKT associated with the SRID from the
# `spatial_ref_sys` (or equivalent) spatial database table. # `spatial_ref_sys` (or equivalent) spatial database table. This query
# # cannot be executed using the ORM because this information is needed
# The following doesn't work: SpatialRefSys.objects.get(srid=srid) # when the ORM cannot be used (e.g., during the initialization of
# Why? `syncdb` fails to recognize installed geographic models when there's # `GeometryField`).
# an ORM query instantiated within a model field. from django.db import connection
cur = connection.cursor() cur = connection.cursor()
qn = connection.ops.quote_name qn = connection.ops.quote_name
stmt = 'SELECT %(table)s.%(wkt_col)s FROM %(table)s WHERE (%(table)s.%(srid_col)s = %(srid)s)' 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, 'srid' : srid,
} }
cur.execute(stmt) cur.execute(stmt)
srs_wkt = cur.fetchone()[0]
if srs_wkt is None: # Fetching the WKT from the cursor; if the query failed raise an Exception.
raise ValueError('Failed to find Spatial Reference System entry corresponding to SRID=%s' % srid) 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. # Getting metadata associated with the spatial reference system identifier.
# Specifically, getting the unit information and spheroid information # Specifically, getting the unit information and spheroid information

View File

@ -273,7 +273,7 @@ class DistanceTest(unittest.TestCase):
len_m = 473504.769553813 len_m = 473504.769553813
qs = Interstate.objects.length() qs = Interstate.objects.length()
if oracle: tol = 2 if oracle: tol = 2
else: tol = 7 else: tol = 5
self.assertAlmostEqual(len_m, qs[0].length.m, tol) self.assertAlmostEqual(len_m, qs[0].length.m, tol)
def test08_perimeter(self): def test08_perimeter(self):