django/django · error · GDALException
Cannot create OGR Geometry from input: %s
Error message
Cannot create OGR Geometry from input: %s
What it means
Raised by OGRGeometry.__init__ after the underlying GDAL/OGR C library returned a null geometry pointer despite the input passing Python-level type checks. The input was recognized as WKT, WKB, GeoJSON, GML, or a type name, but OGR could not parse it into a real geometry, so Django treats the construction as failed. It is a GDALException signaling malformed or semantically invalid geometry input.
Source
Thrown at django/contrib/gis/gdal/geometries.py:122
elif isinstance(geom_input, memoryview):
# WKB was passed in
g = self._from_wkb(geom_input)
elif isinstance(geom_input, OGRGeomType):
# OGRGeomType was passed in, an empty geometry will be created.
g = capi.create_geom(geom_input.num)
elif isinstance(geom_input, self.ptr_type):
# OGR pointer (c_void_p) was the input.
g = geom_input
else:
raise GDALException(
"Invalid input type for OGR Geometry construction: %s"
% type(geom_input)
)
# Now checking the Geometry pointer before finishing initialization
# by setting the pointer for the object.
if not g:
raise GDALException(
"Cannot create OGR Geometry from input: %s" % geom_input
)
self.ptr = g
# Assigning the SpatialReference object to the geometry, if valid.
if srs:
self.srs = srs
# Setting the class depending upon the OGR Geometry Type
if (geo_class := GEO_CLASSES.get(self.geom_type.num)) is None:
raise TypeError(f"Unsupported geometry type: {self.geom_type}")
self.__class__ = geo_class
# Pickle routines
def __getstate__(self):
srs = self.srs
if srs:
srs = srs.wktView on GitHub (pinned to ae25a40be0)
Solutions
- Validate the input string against the expected format (wkt_regex / hex_regex / json_regex) before constructing OGRGeometry.
- Catch GDALException around the constructor and report the offending input back to the user instead of crashing.
- Re-serialize the geometry from a trusted source (e.g. re-export from PostGIS as EWKB/EWKT) to eliminate truncation or encoding corruption.
- For WKB, verify len(geom_input) matches the declared WKB size header before calling OGRGeometry.
Example fix
// before
geom = OGRGeometry(user_wkt) # may raise GDALException
// after
from django.contrib.gis.geometry import wkt_regex
if not (isinstance(user_wkt, str) and wkt_regex.match(user_wkt)):
raise ValueError(f"Not valid WKT: {user_wkt!r}")
geom = OGRGeometry(user_wkt) Defensive patterns
Strategy: validation
Validate before calling
from django.contrib.gis.geometry import wkt_regex, hex_regex, json_regex
def is_valid_ogr_text_input(s: str) -> bool:
if not isinstance(s, str):
return False
return bool(wkt_regex.match(s) or json_regex.match(s) or hex_regex.match(s))
if not is_valid_ogr_text_input(user_input):
raise ValueError(f'Refusing to construct OGRGeometry from invalid input: {user_input!r}')
geom = OGRGeometry(user_input) Type guard
def is_valid_ogr_text_input(s) -> bool:
from django.contrib.gis.geometry import wkt_regex, hex_regex, json_regex
return isinstance(s, str) and bool(
wkt_regex.match(s) or json_regex.match(s) or hex_regex.match(s)
) Try / catch
from django.contrib.gis.gdal import GDALException
try:
geom = OGRGeometry(user_input)
except GDALException as e:
raise ValueError(f'Invalid geometry input from user: {user_input!r}') from e Prevention
- Validate WKT/WKB/GeoJSON strings against Django's regexes before constructing OGRGeometry.
- Never feed untrusted user strings directly into the OGRGeometry constructor.
- Log the offending input verbatim when GDALException fires so corruption is reproducible.
When it happens
Trigger: Constructing OGRGeometry with malformed WKT ('PONT(1 2)'), truncated/corrupt WKB bytes, GeoJSON missing coordinates, an unparseable GML string, or a short-hand string that passes the regex/type-name branch but yields a null OGR pointer (geometries.py:121).
Common situations: Loading user-supplied or third-party shapefile/GeoJSON data with typos; feeding EWKT produced by PostGIS that OGR cannot parse; passing a WKB buffer truncated by a bad db column read; copy-pasting WKT with swapped/mismatched parentheses.
Related errors
- Invalid input type for OGR Geometry construction: %s
- Unsupported geometry type: {self.geom_type}
- Input to 'set_3d' must be a boolean, got '{value!r}'.
- Input to 'set_measured' must be a boolean, got '{value!r}'.
- Invalid OGR String Type "%s"
AI-assisted analysis of django/django@ae25a40be0 (2026-08-06).
Data as JSON: /api/errors/7169b0078dff37b1.
Report an issue: GitHub.