Simplified index bounds checking in GDAL objects.

This commit is contained in:
Nick Pope 2017-09-01 15:48:43 +01:00 committed by Tim Graham
parent f9c2fd30be
commit adc07e8f90
3 changed files with 13 additions and 12 deletions

View File

@ -96,9 +96,10 @@ class DataSource(GDALBase):
if not layer:
raise OGRIndexError('invalid OGR Layer name given: "%s"' % index)
elif isinstance(index, int):
if index < 0 or index >= self.layer_count:
if 0 <= index < self.layer_count:
layer = capi.get_layer(self._ptr, index)
else:
raise OGRIndexError('index out of range')
layer = capi.get_layer(self._ptr, index)
else:
raise TypeError('Invalid index type: %s' % type(index))
return Layer(layer, self)

View File

@ -35,10 +35,10 @@ class Feature(GDALBase):
"""
if isinstance(index, str):
i = self.index(index)
else:
if index < 0 or index > self.num_fields:
raise OGRIndexError('index out of range')
elif 0 <= index < self.num_fields:
i = index
else:
raise OGRIndexError('index out of range')
return Field(self, i)
def __iter__(self):

View File

@ -555,7 +555,7 @@ class LineString(OGRGeometry):
def __getitem__(self, index):
"Return the Point at the given index."
if index >= 0 and index < self.point_count:
if 0 <= index < self.point_count:
x, y, z = c_double(), c_double(), c_double()
capi.get_point(self.ptr, index, byref(x), byref(y), byref(z))
dim = self.coord_dim
@ -625,10 +625,10 @@ class Polygon(OGRGeometry):
def __getitem__(self, index):
"Get the ring at the specified index."
if index < 0 or index >= self.geom_count:
raise OGRIndexError('index out of range: %s' % index)
else:
if 0 <= index < self.geom_count:
return OGRGeometry(capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
else:
raise OGRIndexError('index out of range: %s' % index)
# Polygon Properties
@property
@ -664,10 +664,10 @@ class GeometryCollection(OGRGeometry):
def __getitem__(self, index):
"Get the Geometry at the specified index."
if index < 0 or index >= self.geom_count:
raise OGRIndexError('index out of range: %s' % index)
else:
if 0 <= index < self.geom_count:
return OGRGeometry(capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
else:
raise OGRIndexError('index out of range: %s' % index)
def __iter__(self):
"Iterate over each Geometry."