Django

Code

Changeset 6412

Show
Ignore:
Timestamp:
09/24/07 15:28:23 (1 year ago)
Author:
jbronn
Message:

gis: gdal: Changed name of OGRGeometryIndexError to OGRIndexError and moved to errors mdoule; added HAS_GDAL flag to module; improved docstrings in srs.

Files:

Legend:

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

    r6059 r6412  
    88class OGRException(Exception): pass 
    99class SRSException(Exception): pass 
     10class OGRIndexError(OGRException, KeyError): 
     11    """ 
     12    This exception is raised when an invalid index is encountered, and has 
     13    the 'silent_variable_feature' attribute set to true.  This ensures that 
     14    django's templates proceed to use the next lookup type gracefully when 
     15    an Exception is raised.  Fixes ticket #4740. 
     16    """ 
     17    silent_variable_failure = True 
    1018 
    1119# OGR Error Codes 
  • django/branches/gis/django/contrib/gis/gdal/geometries.py

    r5754 r6412  
    66from django.contrib.gis.gdal.libgdal import lgdal 
    77from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope 
    8 from django.contrib.gis.gdal.error import check_err, OGRException 
     8from django.contrib.gis.gdal.error import check_err, OGRException, OGRIndexError 
    99from django.contrib.gis.gdal.geomtype import OGRGeomType 
    1010from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform 
     
    7373 
    7474#### OGRGeometry Class #### 
    75 class OGRGeometryIndexError(OGRException, KeyError): 
    76     """This exception is raised when an invalid index is encountered, and has 
    77     the 'silent_variable_feature' attribute set to true.  This ensures that 
    78     django's templates proceed to use the next lookup type gracefully when 
    79     an Exception is raised.  Fixes ticket #4740. 
    80     """ 
    81     silent_variable_failure = True 
    82  
    8375class OGRGeometry(object): 
    8476    "Generally encapsulates an OGR geometry." 
     
    8880 
    8981        self._g = 0 # Initially NULL 
     82        self._init_srs(srs) 
    9083 
    9184        if isinstance(input, StringType): 
    92             # Getting the spatial reference 
    93             self._init_srs(srs) 
    94  
    9585            # First, trying the input as WKT 
    9686            buf = c_char_p(input) 
     
    10696                    raise OGRException, 'Could not initialize on WKT "%s"' % input 
    10797        elif isinstance(input, OGRGeomType): 
    108             self._init_srs(srs) 
    10998            g = lgdal.OGR_G_CreateGeometry(input.num) 
    11099            lgdal.OGR_G_AssignSpatialReference(g, self._s._srs) 
     
    238227        # Closing the open rings. 
    239228        lgdal.OGR_G_CloseRings(self._g) 
    240         # This "fixes" a GDAL bug. See http://trac.osgeo.org/gdal/ticket/1673 
    241         foo = self.wkt 
    242229 
    243230    def transform(self, coord_trans): 
     
    382369                return (x.value, y.value, z.value) 
    383370        else: 
    384             raise OGRGeometryIndexError, 'index out of range: %s' % str(index) 
     371            raise OGRIndexError, 'index out of range: %s' % str(index) 
    385372 
    386373    def __iter__(self): 
     
    415402        "Gets the ring at the specified index." 
    416403        if index < 0 or index >= self.geom_count: 
    417             raise OGRGeometryIndexError, 'index out of range: %s' % str(index) 
     404            raise OGRIndexError, 'index out of range: %s' % str(index) 
    418405        else: 
    419406            return OGRGeometry(lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index)))) 
     
    451438        "Gets the Geometry at the specified index." 
    452439        if index < 0 or index >= self.geom_count: 
    453             raise OGRGeometryIndexError, 'index out of range: %s' % str(index) 
     440            raise OGRIndexError, 'index out of range: %s' % str(index) 
    454441        else: 
    455442            return OGRGeometry(lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index)))) 
  • django/branches/gis/django/contrib/gis/gdal/__init__.py

    r6059 r6412  
    1 from django.contrib.gis.gdal.driver import Driver 
    2 from django.contrib.gis.gdal.envelope import Envelope 
    3 from django.contrib.gis.gdal.datasource import DataSource 
    4 from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform 
    5 from django.contrib.gis.gdal.geometries import OGRGeometry 
    6 from django.contrib.gis.gdal.geomtype import OGRGeomType 
    7 from django.contrib.gis.gdal.error import check_err, OGRException, SRSException 
     1try: 
     2    from django.contrib.gis.gdal.driver import Driver 
     3    from django.contrib.gis.gdal.envelope import Envelope 
     4    from django.contrib.gis.gdal.datasource import DataSource 
     5    from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform 
     6    from django.contrib.gis.gdal.geometries import OGRGeometry 
     7    from django.contrib.gis.gdal.geomtype import OGRGeomType 
     8    from django.contrib.gis.gdal.error import check_err, OGRException, SRSException 
     9    HAS_GDAL = True 
     10except: 
     11    HAS_GDAL = False 
    812 
  • django/branches/gis/django/contrib/gis/gdal/srs.py

    r6018 r6412  
    1 # Getting what we need from ctypes 
    21import re 
    32from types import StringType, UnicodeType, TupleType 
     
    4039  NAD83 / Texas South Central 
    4140""" 
    42  
    43  
    4441#### ctypes function prototypes #### 
    4542def ellipsis_func(f): 
    46     """Creates a ctypes function prototype for OSR ellipsis property functions, 
    47     e.g., OSRGetSemiMajor, OSRGetSemiMinor, OSRGetInvFlattening.""" 
     43    """ 
     44    Creates a ctypes function prototype for OSR ellipsis property functions, e.g.,  
     45     OSRGetSemiMajor, OSRGetSemiMinor, OSRGetInvFlattening. 
     46    """ 
    4847    f.restype = c_double 
    4948    f.argtypes = [c_void_p, POINTER(c_int)] 
     
    5655 
    5756def units_func(f): 
    58     """Creates a ctypes function prototype for OSR units functions, 
    59     e.g., OSRGetAngularUnits, OSRGetLinearUnits.""" 
     57    """ 
     58    Creates a ctypes function prototype for OSR units functions, e.g.,  
     59     OSRGetAngularUnits, OSRGetLinearUnits. 
     60    """ 
    6061    f.restype = c_double 
    6162    f.argtypes = [c_void_p, POINTER(c_char_p)] 
     
    6869#### Spatial Reference class. #### 
    6970class SpatialReference(object): 
    70     """A wrapper for the OGRSpatialReference object.  According to the GDAL website, 
    71     the SpatialReference object 'provide[s] services to represent coordinate systems 
    72     (projections and datums) and to transform between them.'""" 
     71    """ 
     72    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.' 
     75    """ 
    7376 
    7477    # Well-Known Geographical Coordinate System Name 
     
    130133 
    131134    def __getitem__(self, target): 
    132         """Returns the value of the given string attribute node, None if the node doesn't exist. 
    133         Can also take a tuple as a parameter, (target, child), where child is the child index to get.""" 
     135        """ 
     136        Returns the value of the given string attribute node, None if the node  
     137         doesn't exist.  Can also take a tuple as a parameter, (target, child),  
     138         where child is the child index to get. 
     139        """ 
    134140        if isinstance(target, TupleType): 
    135141            return self.attr_value(*target) 
     
    142148 
    143149    def _string_ptr(self, ptr): 
    144         "Returns the string at the pointer if it is valid, None if the pointer is NULL." 
     150        """ 
     151        Returns the string at the pointer if it is valid, None if the pointer 
     152         is NULL. 
     153        """ 
    145154        if not ptr: return None 
    146155        else: return string_at(ptr) 
     
    158167 
    159168    def attr_value(self, target, index=0): 
    160         """The attribute value for the given target node (e.g. 'PROJCS'). The index keyword 
    161         specifies an index of the child node to return.""" 
     169        """ 
     170        The attribute value for the given target node (e.g. 'PROJCS'). The index 
     171         keyword specifies an index of the child node to return. 
     172        """ 
    162173        ptr = lgdal.OSRGetAttrValue(self._srs, c_char_p(target), c_int(index)) 
    163174        return self._string_ptr(ptr) 
     
    221232    @property 
    222233    def ellipsoid(self): 
    223         """Returns a tuple of the ellipsoid parameters: 
    224         (semimajor axis, semiminor axis, and inverse flattening).""" 
     234        """ 
     235        Returns a tuple of the ellipsoid parameters: 
     236         (semimajor axis, semiminor axis, and inverse flattening) 
     237        """ 
    225238        return (self.semi_major, self.semi_minor, self.inverse_flattening) 
    226239 
    227240    @property 
    228241    def semi_major(self): 
    229         "Gets the Semi Major Axis for this Spatial Reference." 
     242        "Returns the Semi Major Axis for this Spatial Reference." 
    230243        err = c_int(0) 
    231244        sm = semi_major(self._srs, byref(err)) 
     
    235248    @property 
    236249    def semi_minor(self): 
    237         "Gets the Semi Minor Axis for this Spatial Reference." 
     250        "Returns the Semi Minor Axis for this Spatial Reference." 
    238251        err = c_int() 
    239252        sm = semi_minor(self._srs, byref(err)) 
     
    243256    @property 
    244257    def inverse_flattening(self): 
    245         "Gets the Inverse Flattening for this Spatial Reference." 
     258        "Returns the Inverse Flattening for this Spatial Reference." 
    246259        err = c_int() 
    247260        inv_flat = invflattening(self._srs, byref(err)) 
     
    252265    @property 
    253266    def geographic(self): 
    254         "Returns True if this SpatialReference is geographic (root node is GEOGCS)." 
     267        """ 
     268        Returns True if this SpatialReference is geographic  
     269         (root node is GEOGCS). 
     270        """ 
    255271        if lgdal.OSRIsGeographic(self._srs): return True 
    256272        else: return False 
     
    264280    @property 
    265281    def projected(self): 
    266         "Returns True if this SpatialReference is a projected coordinate system (root node is PROJCS)." 
     282        """ 
     283        Returns True if this SpatialReference is a projected coordinate system  
     284         (root node is PROJCS). 
     285        """ 
    267286        if lgdal.OSRIsProjected(self._srs): return True 
    268287        else: return False 
     
    341360    def __str__(self): 
    342361        return 'Transform from "%s" to "%s"' % (str(self._srs1_name), str(self._srs2_name)) 
    343