Django

Code

Changeset 6574

Show
Ignore:
Timestamp:
10/20/07 23:27:22 (1 year ago)
Author:
jbronn
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/gdal/srs.py

    r6460 r6574  
    7171    """ 
    7272    A wrapper for the OGRSpatialReference object.  According to the GDAL website, 
    73     the SpatialReference object 'provide[s] services to represent coordinate  
    74     systems (projections and datums) and to transform between them.' 
     73    the SpatialReference object 'provide[s] services to represent coordinate  
     74    systems (projections and datums) and to transform between them.' 
    7575    """ 
    7676 
     
    136136        """ 
    137137        Returns the value of the given string attribute node, None if the node  
    138          doesn't exist.  Can also take a tuple as a parameter, (target, child),  
    139          where child is the child index to get. 
     138        doesn't exist.  Can also take a tuple as a parameter, (target, child),  
     139        where child is the index of the attribute in the WKT.  For example: 
     140 
     141        >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]') 
     142        >>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326 
     143        >>> print srs['GEOGCS'] 
     144        WGS 84 
     145        >>> print srs['DATUM'] 
     146        WGS_1984 
     147        >>> print srs['AUTHORITY'] 
     148        EPSG 
     149        >>> print srs['AUTHORITY', 1] # The authority value 
     150        4326 
     151        >>> print srs['TOWGS84', 4] # the fourth value in this wkt 
     152        0 
     153        >>> print srs['UNIT|AUTHORITY'] # For the units authority, have to use the pipe symbole. 
     154        EPSG 
     155        >>> print srs['UNIT|AUTHORITY', 1] # The authority value for the untis 
     156        9122 
    140157        """ 
    141158        if isinstance(target, TupleType): 
     
    159176        """ 
    160177        Returns the string at the pointer if it is valid, None if the pointer 
    161         is NULL. 
     178        is NULL. 
    162179        """ 
    163180        if not ptr: return None 
     
    178195        """ 
    179196        The attribute value for the given target node (e.g. 'PROJCS'). The index 
    180          keyword specifies an index of the child node to return. 
    181         """ 
     197        keyword specifies an index of the child node to return. 
     198        """ 
     199        if not isinstance(target, str): 
     200            raise TypeError('Attribute target must be a string') 
    182201        ptr = lgdal.OSRGetAttrValue(self._srs, c_char_p(target), c_int(index)) 
    183202        return self._string_ptr(ptr) 
     
    201220    @property 
    202221    def srid(self): 
    203         """ 
    204         Returns the EPSG SRID of this Spatial Reference, will be None if 
    205         if undefined. 
    206         """ 
     222        "Returns the SRID of top-level authority, or None if undefined." 
    207223        try: 
    208224            return int(self.attr_value('AUTHORITY', 1)) 
    209         except ValueError
     225        except (TypeError, ValueError)
    210226            return None 
    211227         
  • django/branches/gis/django/contrib/gis/tests/test_gdal_srs.py

    r5748 r6574  
    146146            if s.proj: 
    147147                ct = CoordTransform(SpatialReference(s.wkt), target) 
     148 
     149    def test13_attr_value(self): 
     150        "Testing the attr_value() method." 
     151        s1 = SpatialReference('WGS84') 
     152        self.assertRaises(TypeError, s1.__getitem__, 0) 
     153        self.assertRaises(TypeError, s1.__getitem__, ('GEOGCS', 'foo')) 
     154        self.assertEqual('WGS 84', s1['GEOGCS']) 
     155        self.assertEqual('WGS_1984', s1['DATUM']) 
     156        self.assertEqual('EPSG', s1['AUTHORITY']) 
     157        self.assertEqual(4326, int(s1['AUTHORITY', 1])) 
     158        for i in range(7): self.assertEqual(0, int(s1['TOWGS84', i])) 
     159        self.assertEqual(None, s1['FOOBAR']) 
    148160     
    149161def suite():