Django

Code

Changeset 5587

Show
Ignore:
Timestamp:
07/02/07 18:47:40 (1 year ago)
Author:
jbronn
Message:

gis: fixed ticket 4740 with the addition of new exceptions; updated tests for prev change in Field; added get() method to Feature; fixed bug in Layer for geometries w/o srs; SpatialRefSys? now uses ellipsoid

Files:

Legend:

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

    r5478 r5587  
    9494     
    9595    #### Feature Methods #### 
     96    def get(self, field_name): 
     97        "Returns the value of the field, instead of an instance of the Field object." 
     98        return self.__getitem__(field_name).value 
     99 
    96100    def index(self, field_name): 
    97101        "Returns the index of the given field name." 
  • django/branches/gis/django/contrib/gis/gdal/Layer.py

    r5527 r5587  
    88from django.contrib.gis.gdal.Envelope import Envelope, OGREnvelope 
    99from django.contrib.gis.gdal.Feature import Feature 
     10from django.contrib.gis.gdal.OGRGeometry import OGRGeomType 
    1011from django.contrib.gis.gdal.OGRError import OGRException, check_err 
    1112from django.contrib.gis.gdal.SpatialReference import SpatialReference 
     
    8485    def geom_type(self): 
    8586        "Returns the geometry type (OGRGeomType) of the Layer." 
    86         return lgdal.OGR_FD_GetGeomType(self._ldefn
     87        return OGRGeomType(lgdal.OGR_FD_GetGeomType(self._ldefn)
    8788 
    8889    @property 
    8990    def srs(self): 
    9091        "Returns the Spatial Reference used in this Layer." 
    91         return SpatialReference(lgdal.OSRClone(lgdal.OGR_L_GetSpatialRef(self._layer)), 'ogr') 
     92        ptr = lgdal.OGR_L_GetSpatialRef(self._layer) 
     93        if ptr: 
     94            srs = SpatialReference(lgdal.OSRClone(ptr), 'ogr') 
     95        else: 
     96            return None 
    9297 
    9398    @property 
  • django/branches/gis/django/contrib/gis/gdal/OGRGeometry.py

    r5527 r5587  
    132132 
    133133#### OGRGeometry Class #### 
     134class OGRGeometryIndexError(OGRException, KeyError): 
     135    """This exception is raised when an invalid index is encountered, and has 
     136    the 'silent_variable_feature' attribute set to true.  This ensures that 
     137    django's templates proceed to use the next lookup type gracefully when 
     138    an Exception is raised.  Fixes ticket #4740. 
     139    """ 
     140    silent_variable_failure = True 
     141 
    134142class OGRGeometry(object): 
    135143    "Generally encapsulates an OGR geometry." 
     
    413421                return (x.value, y.value, z.value) 
    414422        else: 
    415             raise IndexError, 'index out of range' 
     423            raise OGRGeometryIndexError, 'index out of range: %s' % str(index) 
    416424 
    417425    def __iter__(self): 
     
    446454        "Gets the ring at the specified index." 
    447455        if index < 0 or index >= self.geom_count: 
    448             raise IndexError, 'index out of range' 
     456            raise OGRGeometryIndexError, 'index out of range: %s' % str(index) 
    449457        else: 
    450458            return OGRGeometry(lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index)))) 
     
    482490        "Gets the Geometry at the specified index." 
    483491        if index < 0 or index >= self.geom_count: 
    484             raise IndexError, 'index out of range' 
     492            raise OGRGeometryIndexError, 'index out of range: %s' % str(index) 
    485493        else: 
    486494            return OGRGeometry(lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index)))) 
  • django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py

    r5510 r5587  
    8686# The GEOSException class 
    8787class GEOSException(Exception): pass 
     88class GEOSGeometryIndexError(GEOSException, KeyError): 
     89    """This exception is raised when an invalid index is encountered, and has 
     90    the 'silent_variable_feature' attribute set to true.  This ensures that 
     91    django's templates proceed to use the next lookup type gracefully when 
     92    an Exception is raised.  Fixes ticket #4740. 
     93    """ 
     94    silent_variable_failure = True 
    8895 
    8996# Getting the GEOS C library.  The C interface (CDLL) is used for 
     
    346353    @property 
    347354    def envelope(self): 
    348         "Return the geometries bounding box." 
     355        "Return the envelope for this geometry (a polygon)." 
    349356        return GEOSGeometry(lgeos.GEOSEnvelope(self._g)) 
    350357 
     
    433440        sz = self.size 
    434441        if (sz < 1) or (index < 0) or (index >= sz): 
    435             raise IndexError, 'index out of range' 
     442            raise GEOSGeometryIndexError, 'invalid GEOS Geometry index: %s' % str(index) 
    436443 
    437444    def _checkdim(self, dim): 
     
    595602        self._cache_cs() 
    596603        if index < 0 or index >= self._cs.size: 
    597             raise IndexError, 'index out of range' 
     604            raise GEOSGeometryIndexError, 'invalid GEOS Geometry index: %s' % str(index) 
    598605        else: 
    599606            return self._cs[index] 
     
    625632        return the exterior ring.  Indices > 0 will return the interior ring.""" 
    626633        if index < 0 or index > self.num_interior_rings: 
    627             raise IndexError, 'index out of range' 
     634            raise GEOSGeometryIndexError, 'invalid GEOS Geometry index: %s' % str(index) 
    628635        else: 
    629636            if index == 0: 
     
    688695        "Checks the given geometry index." 
    689696        if index < 0 or index >= self.num_geom: 
    690             raise IndexError, 'index out of range' 
     697            raise GEOSGeometryIndexError, 'invalid GEOS Geometry index: %s' % str(index) 
    691698 
    692699    def __iter__(self): 
  • django/branches/gis/django/contrib/gis/models.py

    r5397 r5587  
    4949 
    5050    def _cache_osr(self): 
    51         "Caches a GDAL OSR object for this Spatial Reference." 
     51        "Caches a GDAL OSR SpatialReference object for this SpatialRefSys model." 
    5252        if HAS_OSR: 
    5353            if not hasattr(self, '_srs'): 
     
    7272    @property 
    7373    def srs(self): 
     74        "Returns the SpatialReference equivalent of this model." 
    7475        self._cache_osr() 
    7576        return self._srs.clone() 
     
    8081        (semimajor axis, semiminor axis, and inverse flattening).""" 
    8182        if HAS_OSR: 
    82             # Setting values initially to False 
    8383            self._cache_osr() 
    84             major = self._srs.semi_major 
    85             minor = self._srs.semi_minor 
    86             invflat  = self._srs.inverse_flattening 
    87             return (major, minor, invflat) 
     84            return self._srs.ellipsoid 
    8885        else: 
    8986            m = spheroid_regex.match(self.srtext) 
  • django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py

    r5527 r5587  
    102102                        fld = feat[k] # Indexing with string value 
    103103 
    104                         # Asserting the string representation (which asserts the type) 
    105                         self.assertEqual('%s (%s)' % (k, v.__name__), str(fld)) 
     104                        # Asserting the string representation, and making sure we get 
     105                        #  the proper OGR Field instance. 
     106                        self.assertEqual('%s (%s)' % (k, fld.value), str(fld)) 
     107                        self.assertEqual(True, isinstance(fld, v)) 
    106108 
    107109                    # Testing __iter__ on the Feature