1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

gis: Fixed #5779, thanks tlp; added type checking to SpatialReference.__getitem__.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6574 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2007-10-21 04:27:22 +00:00
parent 712df76528
commit aed1db8266
2 changed files with 39 additions and 11 deletions

View File

@ -70,8 +70,8 @@ angular_units = units_func(lgdal.OSRGetAngularUnits)
class SpatialReference(object): class SpatialReference(object):
""" """
A wrapper for the OGRSpatialReference object. According to the GDAL website, A wrapper for the OGRSpatialReference object. According to the GDAL website,
the SpatialReference object 'provide[s] services to represent coordinate the SpatialReference object 'provide[s] services to represent coordinate
systems (projections and datums) and to transform between them.' systems (projections and datums) and to transform between them.'
""" """
# Well-Known Geographical Coordinate System Name # Well-Known Geographical Coordinate System Name
@ -135,8 +135,25 @@ class SpatialReference(object):
def __getitem__(self, target): def __getitem__(self, target):
""" """
Returns the value of the given string attribute node, None if the node 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), doesn't exist. Can also take a tuple as a parameter, (target, child),
where child is the child index to get. 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): if isinstance(target, TupleType):
return self.attr_value(*target) return self.attr_value(*target)
@ -158,7 +175,7 @@ class SpatialReference(object):
def _string_ptr(self, ptr): def _string_ptr(self, ptr):
""" """
Returns the string at the pointer if it is valid, None if the pointer Returns the string at the pointer if it is valid, None if the pointer
is NULL. is NULL.
""" """
if not ptr: return None if not ptr: return None
else: return string_at(ptr) else: return string_at(ptr)
@ -177,8 +194,10 @@ class SpatialReference(object):
def attr_value(self, target, index=0): def attr_value(self, target, index=0):
""" """
The attribute value for the given target node (e.g. 'PROJCS'). The index 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)) ptr = lgdal.OSRGetAttrValue(self._srs, c_char_p(target), c_int(index))
return self._string_ptr(ptr) return self._string_ptr(ptr)
@ -200,13 +219,10 @@ class SpatialReference(object):
@property @property
def srid(self): def srid(self):
""" "Returns the SRID of top-level authority, or None if undefined."
Returns the EPSG SRID of this Spatial Reference, will be None if
if undefined.
"""
try: try:
return int(self.attr_value('AUTHORITY', 1)) return int(self.attr_value('AUTHORITY', 1))
except ValueError: except (TypeError, ValueError):
return None return None
#### Unit Properties #### #### Unit Properties ####

View File

@ -146,6 +146,18 @@ class SpatialRefTest(unittest.TestCase):
if s.proj: if s.proj:
ct = CoordTransform(SpatialReference(s.wkt), target) 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(): def suite():
s = unittest.TestSuite() s = unittest.TestSuite()
s.addTest(unittest.makeSuite(SpatialRefTest)) s.addTest(unittest.makeSuite(SpatialRefTest))