django/django · error · IndexError
Index out of range when accessing points of a line string: %
Error message
Index out of range when accessing points of a line string: %s.
What it means
Raised as IndexError by LineString.__getitem__ when the requested index is outside [0, point_count) (geometries.py:657). It guards the underlying OGR get_point C call from an out-of-bounds read.
Source
Thrown at django/contrib/gis/gdal/geometries.py:657
class LineString(OGRGeometry):
def __getitem__(self, index):
"Return the Point at the given index."
if 0 <= index < self.point_count:
x, y, z, m = c_double(), c_double(), c_double(), c_double()
capi.get_point(self.ptr, index, byref(x), byref(y), byref(z), byref(m))
if self.is_3d and self.is_measured:
return x.value, y.value, z.value, m.value
if self.is_3d:
return x.value, y.value, z.value
if self.is_measured:
return x.value, y.value, m.value
dim = self.coord_dim
if dim == 1:
return (x.value,)
elif dim == 2:
return (x.value, y.value)
else:
raise IndexError(
"Index out of range when accessing points of a line string: %s." % index
)
def __len__(self):
"Return the number of points in the LineString."
return self.point_count
@property
def tuple(self):
"Return the tuple representation of this LineString."
return tuple(self[i] for i in range(len(self)))
coords = tuple
def _listarr(self, func):
"""
Internal routine that returns a sequence (list) corresponding with
the given function.View on GitHub (pinned to ae25a40be0)
Solutions
- Bounds-check before indexing: if 0 <= i < len(line): ...
- Use negative-safe iteration: for pt in line.tuple: ...
- Cache len(line) / line.point_count and clamp the index.
Example fix
// before last = line[len(line)] # off-by-one -> IndexError // after last = line[len(line) - 1]
Defensive patterns
Strategy: validation
Validate before calling
def safe_line_point(line, i):
if not (0 <= i < len(line)):
raise IndexError(f'vertex {i} out of range [0,{len(line)})')
return line[i] Type guard
def is_valid_line_index(line, i) -> bool:
return isinstance(i, int) and 0 <= i < len(line) Try / catch
try:
pt = line[i]
except IndexError:
pt = None # or clamp i to range Prevention
- Always bounds-check with len(line) before indexing a LineString.
- Prefer iteration (for pt in line.tuple) over fixed-index access.
- Compute the last index as len(line) - 1, not len(line).
When it happens
Trigger: Calling line[i] where i < 0 or i >= line.point_count; iterating with a hard-coded index larger than the actual vertex count; off-by-one when computing the last vertex.
Common situations: Parsing geometries of varying vertex counts and assuming a fixed length; index derived from user input without bounds checking; reverse iteration using positive indices.
Related errors
- Index out of range when accessing rings of a polygon: %s.
- Index out of range when accessing geometry in a collection:
- Negative indices are not allowed on OGR Layers.
- Index out of range when accessing layers in a datasource: %s
- Invalid index type: %s
AI-assisted analysis of django/django@ae25a40be0 (2026-08-06).
Data as JSON: /api/errors/ab2b70709939c2aa.
Report an issue: GitHub.