Django

Code

Changeset 6059

Show
Ignore:
Timestamp:
09/06/07 23:52:02 (1 year ago)
Author:
jbronn
Message:

gis: gdal: cleaned up the error module; got rid of relative imports in init; added the utility functions get_fields() and get_geoms() to Layer.

Files:

Legend:

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

    r5749 r6059  
    1 # OGR Error Codes 
    2 OGRERR_NONE = 0 
    3 OGRERR_NOT_ENOUGH_DATA = 1 
    4 OGRERR_NOT_ENOUGH_MEMORY = 2 
    5 OGRERR_UNSUPPORTED_GEOMETRY_TYPE = 3 
    6 OGRERR_UNSUPPORTED_OPERATION = 4 
    7 OGRERR_CORRUPT_DATA = 5 
    8 OGRERR_FAILURE = 6 
    9 OGRERR_UNSUPPORTED_SRS = 7 
     1""" 
     2  This module houses the OGR & SRS Exception objects, and the 
     3   check_err() routine which checks the status code returned by 
     4   OGR methods. 
     5""" 
    106 
    117# OGR & SRS Exceptions 
     
    139class SRSException(Exception): pass 
    1410 
    15 def check_err(code, msg=False): 
     11# OGR Error Codes 
     12OGRERR_DICT = { 1 : (OGRException, 'Not enough data.'), 
     13                2 : (OGRException, 'Not enough memory.'), 
     14                3 : (OGRException, 'Unsupported geometry type.'), 
     15                4 : (OGRException, 'Unsupported operation.'), 
     16                5 : (OGRException, 'Corrupt data.'), 
     17                6 : (OGRException, 'OGR failure.'), 
     18                7 : (SRSException, 'Unsupported SRS.'), 
     19                } 
     20OGRERR_NONE = 0 
     21 
     22def check_err(code): 
    1623    "Checks the given OGRERR, and raises an exception where appropriate." 
    17  
     24     
    1825    if code == OGRERR_NONE: 
    1926        return 
    20     elif code == OGRERR_NOT_ENOUGH_DATA: 
    21         raise OGRException, 'Not enough data!' 
    22     elif code == OGRERR_NOT_ENOUGH_MEMORY: 
    23         raise OGRException, 'Not enough memory!' 
    24     elif code == OGRERR_UNSUPPORTED_GEOMETRY_TYPE: 
    25         raise OGRException, 'Unsupported Geometry Type!' 
    26     elif code == OGRERR_UNSUPPORTED_OPERATION: 
    27         raise OGRException, 'Unsupported Operation!' 
    28     elif code == OGRERR_CORRUPT_DATA: 
    29         raise OGRException, 'Corrupt Data!' 
    30     elif code == OGRERR_FAILURE: 
    31         raise OGRException, 'OGR Failure!' 
    32     elif code == OGRERR_UNSUPPORTED_SRS: 
    33         raise SRSException, 'Unsupported SRS!' 
     27    elif code in OGRERR_DICT: 
     28        e, msg = OGRERR_DICT[code] 
     29        raise e, msg 
    3430    else: 
    35         raise OGRException, 'Unknown error code: "%s"' % str(code) 
     31        raise OGRException, 'Unknown error code: "%s"' % code 
  • django/branches/gis/django/contrib/gis/gdal/__init__.py

    r5749 r6059  
    1 from driver import Driver 
    2 from envelope import Envelope 
    3 from datasource import DataSource 
    4 from srs import SpatialReference, CoordTransform 
    5 from geometries import OGRGeometry 
    6 from geomtype import OGRGeomType 
    7 from error import check_err, OGRException, SRSException 
     1from django.contrib.gis.gdal.driver import Driver 
     2from django.contrib.gis.gdal.envelope import Envelope 
     3from django.contrib.gis.gdal.datasource import DataSource 
     4from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform 
     5from django.contrib.gis.gdal.geometries import OGRGeometry 
     6from django.contrib.gis.gdal.geomtype import OGRGeomType 
     7from django.contrib.gis.gdal.error import check_err, OGRException, SRSException 
    88 
  • django/branches/gis/django/contrib/gis/gdal/layer.py

    r5749 r6059  
    113113                 for i in xrange(self.num_fields) ] 
    114114     
     115    #### Layer Methods #### 
     116    def get_fields(self, field_name): 
     117        """Returns a list containing the given field name for every Feature 
     118        in the Layer.""" 
     119        if not field_name in self.fields: 
     120            raise OGRException, 'invalid field name: %s' % field_name 
     121        return [feat.get(field_name) for feat in self] 
     122 
     123    def get_geoms(self, geos=False): 
     124        """Returns a list containing the OGRGeometry for every Feature in 
     125        the Layer.""" 
     126        if geos: 
     127            from django.contrib.gis.geos import fromstr 
     128            return [fromstr(feat.geom.wkt) for feat in self] 
     129        else: 
     130            return [feat.geom for feat in self]