diff --git a/django/contrib/gis/gdal/datasource.py b/django/contrib/gis/gdal/datasource.py index 179842e518..a399593b88 100644 --- a/django/contrib/gis/gdal/datasource.py +++ b/django/contrib/gis/gdal/datasource.py @@ -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) diff --git a/django/contrib/gis/gdal/feature.py b/django/contrib/gis/gdal/feature.py index 6e857ca074..641e72dde7 100644 --- a/django/contrib/gis/gdal/feature.py +++ b/django/contrib/gis/gdal/feature.py @@ -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): diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py index ca7732be75..5f68ffa268 100644 --- a/django/contrib/gis/gdal/geometries.py +++ b/django/contrib/gis/gdal/geometries.py @@ -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."