{"record":{"id":"7169b0078dff37b1","repo":"django/django","slug":"cannot-create-ogr-geometry-from-input-s","errorCode":null,"errorMessage":"Cannot create OGR Geometry from input: %s","messagePattern":"Cannot create OGR Geometry from input: (.+?)","errorType":"exception","errorClass":"GDALException","httpStatus":null,"severity":"error","filePath":"django/contrib/gis/gdal/geometries.py","lineNumber":122,"sourceCode":"        elif isinstance(geom_input, memoryview):\n            # WKB was passed in\n            g = self._from_wkb(geom_input)\n        elif isinstance(geom_input, OGRGeomType):\n            # OGRGeomType was passed in, an empty geometry will be created.\n            g = capi.create_geom(geom_input.num)\n        elif isinstance(geom_input, self.ptr_type):\n            # OGR pointer (c_void_p) was the input.\n            g = geom_input\n        else:\n            raise GDALException(\n                \"Invalid input type for OGR Geometry construction: %s\"\n                % type(geom_input)\n            )\n\n        # Now checking the Geometry pointer before finishing initialization\n        # by setting the pointer for the object.\n        if not g:\n            raise GDALException(\n                \"Cannot create OGR Geometry from input: %s\" % geom_input\n            )\n        self.ptr = g\n\n        # Assigning the SpatialReference object to the geometry, if valid.\n        if srs:\n            self.srs = srs\n\n        # Setting the class depending upon the OGR Geometry Type\n        if (geo_class := GEO_CLASSES.get(self.geom_type.num)) is None:\n            raise TypeError(f\"Unsupported geometry type: {self.geom_type}\")\n        self.__class__ = geo_class\n\n    # Pickle routines\n    def __getstate__(self):\n        srs = self.srs\n        if srs:\n            srs = srs.wkt","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/gis/gdal/geometries.py#L104-L140","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\ngeom = OGRGeometry(user_wkt)  # may raise GDALException\n\n// after\nfrom django.contrib.gis.geometry import wkt_regex\nif not (isinstance(user_wkt, str) and wkt_regex.match(user_wkt)):\n    raise ValueError(f\"Not valid WKT: {user_wkt!r}\")\ngeom = OGRGeometry(user_wkt)","handlingStrategy":"validation","validationCode":"from django.contrib.gis.geometry import wkt_regex, hex_regex, json_regex\n\ndef is_valid_ogr_text_input(s: str) -> bool:\n    if not isinstance(s, str):\n        return False\n    return bool(wkt_regex.match(s) or json_regex.match(s) or hex_regex.match(s))\n\nif not is_valid_ogr_text_input(user_input):\n    raise ValueError(f'Refusing to construct OGRGeometry from invalid input: {user_input!r}')\ngeom = OGRGeometry(user_input)","typeGuard":"def is_valid_ogr_text_input(s) -> bool:\n    from django.contrib.gis.geometry import wkt_regex, hex_regex, json_regex\n    return isinstance(s, str) and bool(\n        wkt_regex.match(s) or json_regex.match(s) or hex_regex.match(s)\n    )","tryCatchPattern":"from django.contrib.gis.gdal import GDALException\ntry:\n    geom = OGRGeometry(user_input)\nexcept GDALException as e:\n    raise ValueError(f'Invalid geometry input from user: {user_input!r}') from e","preventionTips":["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."],"tags":["gis","ogr","geometry","input-validation"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}