diff --git a/django/contrib/gis/gdal/feature.py b/django/contrib/gis/gdal/feature.py index e487565519..a3af85e70b 100644 --- a/django/contrib/gis/gdal/feature.py +++ b/django/contrib/gis/gdal/feature.py @@ -4,7 +4,7 @@ from ctypes import c_char_p, c_int, string_at # The GDAL C library, OGR exception, and the Field object from django.contrib.gis.gdal.libgdal import lgdal -from django.contrib.gis.gdal.error import OGRException +from django.contrib.gis.gdal.error import OGRException, OGRIndexError from django.contrib.gis.gdal.field import Field from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType @@ -35,11 +35,11 @@ class Feature(object): i = self.index(index) else: if index < 0 or index > self.num_fields: - raise IndexError, 'index out of range' + raise OGRIndexError, 'index out of range' i = index return Field(lgdal.OGR_F_GetFieldDefnRef(self._feat, c_int(i)), string_at(lgdal.OGR_F_GetFieldAsString(self._feat, c_int(i)))) - + def __iter__(self): "Iterates over each field in the Feature." for i in xrange(self.num_fields): @@ -94,14 +94,18 @@ class Feature(object): #### Feature Methods #### def get(self, field): - "Returns the value of the field, instead of an instance of the Field object." + """ + Returns the value of the field, instead of an instance of the Field + object. May take a string of the field name or a Field object as + parameters. + """ field_name = getattr(field, 'name', field) return self.__getitem__(field_name).value def index(self, field_name): "Returns the index of the given field name." i = lgdal.OGR_F_GetFieldIndex(self._feat, c_char_p(field_name)) - if i < 0: raise IndexError, 'invalid OFT field name given: "%s"' % field_name + if i < 0: raise OGRIndexError, 'invalid OFT field name given: "%s"' % field_name return i def clone(self): diff --git a/django/contrib/gis/gdal/layer.py b/django/contrib/gis/gdal/layer.py index f11adc5a8f..b3cf10dee5 100644 --- a/django/contrib/gis/gdal/layer.py +++ b/django/contrib/gis/gdal/layer.py @@ -8,7 +8,7 @@ from django.contrib.gis.gdal.libgdal import lgdal from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope from django.contrib.gis.gdal.feature import Feature from django.contrib.gis.gdal.geometries import OGRGeomType -from django.contrib.gis.gdal.error import OGRException, check_err +from django.contrib.gis.gdal.error import OGRException, OGRIndexError, check_err from django.contrib.gis.gdal.srs import SpatialReference # For more information, see the OGR C API source code: @@ -48,7 +48,7 @@ class Layer(object): if index < 0: index = end - index if index < 0 or index >= self.num_feat: - raise IndexError, 'index out of range' + raise OGRIndexError, 'index out of range' return make_feature(index) else: # A slice was given