django/django · error · GDALException

Invalid data source input type: "{}".

Error message

Invalid data source input type: "{}".

What it means

Thrown by GDALRaster.__init__ when the ds_input argument is none of the accepted types (str, bytes, dict, c_void_p, or a pathlib.Path/JSON string that _preprocess_input converts). The constructor dispatches on input type to decide whether to open a file, build a VSI memory raster, create a new in-memory raster, or adopt an existing GDAL pointer; an unrecognized type leaves no valid code path. The message formats the offending Python type via type(ds_input).

Source

Thrown at django/contrib/gis/gdal/raster/source.py:215

            # Set SRID
            self.srs = ds_input.get("srid")

            # Set additional properties if provided
            if "origin" in ds_input:
                self.origin.x, self.origin.y = ds_input["origin"]

            if "scale" in ds_input:
                self.scale.x, self.scale.y = ds_input["scale"]

            if "skew" in ds_input:
                self.skew.x, self.skew.y = ds_input["skew"]
        elif isinstance(ds_input, c_void_p):
            # Instantiate the object using an existing pointer to a gdal
            # raster.
            self._ptr = ds_input
        else:
            raise GDALException(
                'Invalid data source input type: "{}".'.format(type(ds_input))
            )

    def __del__(self):
        if self.is_vsi_based:
            # Remove the temporary file from the VSI in-memory filesystem.
            capi.unlink_vsi_file(force_bytes(self.name))
        super().__del__()

    def __str__(self):
        return self.name

    def __repr__(self):
        """
        Short-hand representation because WKB may be very large.
        """
        return "<Raster object at %s>" % hex(addressof(self._ptr))

View on GitHub (pinned to ae25a40be0)

Solutions

  1. Pass a file path string or pathlib.Path to an existing raster file
  2. Pass a bytes buffer to open a raster from in-memory binary data
  3. Pass a dict (with 'width','height','srid' keys) to create a new raster, or a JSON string matching that shape
  4. Pass a c_void_p if you hold an existing GDAL dataset pointer

Example fix

// before
GDALRaster(open('raster.tif'))
GDALRaster(3857)
// after
GDALRaster('raster.tif')
GDALRaster({'width': 10, 'height': 10, 'srid': 3857, 'bands': [{'data': range(100)}]})
Defensive patterns

Strategy: type-guard

Validate before calling

from pathlib import Path
from ctypes import c_void_p

def validate_raster_input(ds_input):
    if isinstance(ds_input, Path):
        return str(ds_input)
    if isinstance(ds_input, (str, bytes, dict, c_void_p)):
        return ds_input
    raise TypeError(f'Unsupported GDALRaster input type: {type(ds_input).__name__}')

Type guard

from pathlib import Path
from ctypes import c_void_p

def is_raster_input(v) -> bool:
    return isinstance(v, (str, bytes, dict, c_void_p, Path))

Try / catch

from django.contrib.gis.gdal.error import GDALException
try:
    raster = GDALRaster(maybe_bad)
except GDALException as e:
    if 'Invalid data source input type' in str(e):
        # normalize input then retry
        ...
    raise

Prevention

When it happens

Trigger: Calling GDALRaster() with an int, float, None, a list, a memoryview, a file object, an already-built GDALRaster instance, or any custom object. Also triggered when a Path/JSON preprocessing path silently returns a non-str/dict value (e.g. a third-party Path subclass that fails isinstance checks).

Common situations: Passing a raw file handle (open('tif')) instead of the path string; passing an integer SRID or band index expecting raster creation; passing None after a previous step returned no value; migrating code that fed GDALRaster an existing GDALRaster object rather than its ._ptr.

Related errors


AI-assisted analysis of django/django@ae25a40be0 (2026-08-06). Data as JSON: /api/errors/677830b8b7d6aabb. Report an issue: GitHub.