Author: jbronn
Date: 2007-10-20 23:27:22 -0500 (Sat, 20 Oct 2007)
New Revision: 6574
Modified:
django/branches/gis/django/contrib/gis/gdal/srs.py
django/branches/gis/django/contrib/gis/tests/test_gdal_srs.py
Log:
gis: Fixed #5779, thanks tlp; added type checking to
SpatialReference.__getitem__.
Modified: django/branches/gis/django/contrib/gis/gdal/srs.py
===================================================================
--- django/branches/gis/django/contrib/gis/gdal/srs.py 2007-10-20 15:36:05 UTC
(rev 6573)
+++ django/branches/gis/django/contrib/gis/gdal/srs.py 2007-10-21 04:27:22 UTC
(rev 6574)
@@ -70,8 +70,8 @@
class SpatialReference(object):
"""
A wrapper for the OGRSpatialReference object. According to the GDAL
website,
- the SpatialReference object 'provide[s] services to represent coordinate
- systems (projections and datums) and to transform between them.'
+ the SpatialReference object 'provide[s] services to represent coordinate
+ systems (projections and datums) and to transform between them.'
"""
# Well-Known Geographical Coordinate System Name
@@ -135,8 +135,25 @@
def __getitem__(self, target):
"""
Returns the value of the given string attribute node, None if the node
- doesn't exist. Can also take a tuple as a parameter, (target,
child),
- where child is the child index to get.
+ doesn't exist. Can also take a tuple as a parameter, (target, child),
+ where child is the index of the attribute in the WKT. For example:
+
+ >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ...
AUTHORITY["EPSG","4326"]]')
+ >>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326
+ >>> print srs['GEOGCS']
+ WGS 84
+ >>> print srs['DATUM']
+ WGS_1984
+ >>> print srs['AUTHORITY']
+ EPSG
+ >>> print srs['AUTHORITY', 1] # The authority value
+ 4326
+ >>> print srs['TOWGS84', 4] # the fourth value in this wkt
+ 0
+ >>> print srs['UNIT|AUTHORITY'] # For the units authority, have to use
the pipe symbole.
+ EPSG
+ >>> print srs['UNIT|AUTHORITY', 1] # The authority value for the untis
+ 9122
"""
if isinstance(target, TupleType):
return self.attr_value(*target)
@@ -158,7 +175,7 @@
def _string_ptr(self, ptr):
"""
Returns the string at the pointer if it is valid, None if the pointer
- is NULL.
+ is NULL.
"""
if not ptr: return None
else: return string_at(ptr)
@@ -177,8 +194,10 @@
def attr_value(self, target, index=0):
"""
The attribute value for the given target node (e.g. 'PROJCS'). The
index
- keyword specifies an index of the child node to return.
+ keyword specifies an index of the child node to return.
"""
+ if not isinstance(target, str):
+ raise TypeError('Attribute target must be a string')
ptr = lgdal.OSRGetAttrValue(self._srs, c_char_p(target), c_int(index))
return self._string_ptr(ptr)
@@ -200,13 +219,10 @@
@property
def srid(self):
- """
- Returns the EPSG SRID of this Spatial Reference, will be None if
- if undefined.
- """
+ "Returns the SRID of top-level authority, or None if undefined."
try:
return int(self.attr_value('AUTHORITY', 1))
- except ValueError:
+ except (TypeError, ValueError):
return None
#### Unit Properties ####
Modified: django/branches/gis/django/contrib/gis/tests/test_gdal_srs.py
===================================================================
--- django/branches/gis/django/contrib/gis/tests/test_gdal_srs.py
2007-10-20 15:36:05 UTC (rev 6573)
+++ django/branches/gis/django/contrib/gis/tests/test_gdal_srs.py
2007-10-21 04:27:22 UTC (rev 6574)
@@ -145,6 +145,18 @@
for s in srlist:
if s.proj:
ct = CoordTransform(SpatialReference(s.wkt), target)
+
+ def test13_attr_value(self):
+ "Testing the attr_value() method."
+ s1 = SpatialReference('WGS84')
+ self.assertRaises(TypeError, s1.__getitem__, 0)
+ self.assertRaises(TypeError, s1.__getitem__, ('GEOGCS', 'foo'))
+ self.assertEqual('WGS 84', s1['GEOGCS'])
+ self.assertEqual('WGS_1984', s1['DATUM'])
+ self.assertEqual('EPSG', s1['AUTHORITY'])
+ self.assertEqual(4326, int(s1['AUTHORITY', 1]))
+ for i in range(7): self.assertEqual(0, int(s1['TOWGS84', i]))
+ self.assertEqual(None, s1['FOOBAR'])
def suite():
s = unittest.TestSuite()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---