1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

gis: gdal: The feature and layer modules now uses OGRIndexError.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6421 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2007-09-25 03:55:29 +00:00
parent 65b4cb2cca
commit ea0ae54756
2 changed files with 11 additions and 7 deletions

View File

@ -4,7 +4,7 @@ from ctypes import c_char_p, c_int, string_at
# The GDAL C library, OGR exception, and the Field object # The GDAL C library, OGR exception, and the Field object
from django.contrib.gis.gdal.libgdal import lgdal 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.field import Field
from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType
@ -35,11 +35,11 @@ class Feature(object):
i = self.index(index) i = self.index(index)
else: else:
if index < 0 or index > self.num_fields: if index < 0 or index > self.num_fields:
raise IndexError, 'index out of range' raise OGRIndexError, 'index out of range'
i = index i = index
return Field(lgdal.OGR_F_GetFieldDefnRef(self._feat, c_int(i)), return Field(lgdal.OGR_F_GetFieldDefnRef(self._feat, c_int(i)),
string_at(lgdal.OGR_F_GetFieldAsString(self._feat, c_int(i)))) string_at(lgdal.OGR_F_GetFieldAsString(self._feat, c_int(i))))
def __iter__(self): def __iter__(self):
"Iterates over each field in the Feature." "Iterates over each field in the Feature."
for i in xrange(self.num_fields): for i in xrange(self.num_fields):
@ -94,14 +94,18 @@ class Feature(object):
#### Feature Methods #### #### Feature Methods ####
def get(self, field): 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) field_name = getattr(field, 'name', field)
return self.__getitem__(field_name).value return self.__getitem__(field_name).value
def index(self, field_name): def index(self, field_name):
"Returns the index of the given field name." "Returns the index of the given field name."
i = lgdal.OGR_F_GetFieldIndex(self._feat, c_char_p(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 return i
def clone(self): def clone(self):

View File

@ -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.envelope import Envelope, OGREnvelope
from django.contrib.gis.gdal.feature import Feature from django.contrib.gis.gdal.feature import Feature
from django.contrib.gis.gdal.geometries import OGRGeomType 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 from django.contrib.gis.gdal.srs import SpatialReference
# For more information, see the OGR C API source code: # For more information, see the OGR C API source code:
@ -48,7 +48,7 @@ class Layer(object):
if index < 0: if index < 0:
index = end - index index = end - index
if index < 0 or index >= self.num_feat: if index < 0 or index >= self.num_feat:
raise IndexError, 'index out of range' raise OGRIndexError, 'index out of range'
return make_feature(index) return make_feature(index)
else: else:
# A slice was given # A slice was given