django/django · error · TypeError

Transform only accepts SpatialReference, string, and integer

Error message

Transform only accepts SpatialReference, string, and integer objects.

What it means

Raised by GDALRaster.transform when the srs argument is not a SpatialReference, int, or str. transform needs to construct a target SpatialReference to reproject the raster, and it only accepts those three input shapes. This is a TypeError, distinct from the ValueError raised by the srs setter for the same kind of bad input.

Source

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

            write=self._write,
        )

    def transform(
        self, srs, driver=None, name=None, resampling="NearestNeighbour", max_error=0.0
    ):
        """
        Return a copy of this raster reprojected into the given spatial
        reference system.
        """
        # Convert the resampling algorithm name into an algorithm id
        algorithm = GDAL_RESAMPLE_ALGORITHMS[resampling]

        if isinstance(srs, SpatialReference):
            target_srs = srs
        elif isinstance(srs, (int, str)):
            target_srs = SpatialReference(srs)
        else:
            raise TypeError(
                "Transform only accepts SpatialReference, string, and integer "
                "objects."
            )

        if target_srs.srid == self.srid and (not driver or driver == self.driver.name):
            return self.clone(name)
        # Create warped virtual dataset in the target reference system
        target = capi.auto_create_warped_vrt(
            self._ptr,
            self.srs.wkt.encode(),
            target_srs.wkt.encode(),
            algorithm,
            max_error,
            c_void_p(),
        )
        target = GDALRaster(target)

        # Construct the target warp dictionary from the virtual raster

View on GitHub (pinned to ae25a40be0)

Solutions

  1. Pass an EPSG integer: raster.transform(3857)
  2. Pass a WKT/PROJ string or a SpatialReference instance
  3. If you need to also set size/origin/driver, call raster.warp({...}) instead

Example fix

// before
reprojected = raster.transform({'srid': 3857})
// after
reprojected = raster.transform(3857)
# or for full control
reprojected = raster.warp({'srid': 3857, 'width': 100, 'height': 100})
Defensive patterns

Strategy: type-guard

Validate before calling

from django.contrib.gis.gdal import SpatialReference

def coerce_target_srs(srs):
    if isinstance(srs, (SpatialReference, int, str)):
        return srs
    raise TypeError('transform target must be SpatialReference/int/str')

Type guard

from django.contrib.gis.gdal import SpatialReference

def is_transformable(v) -> bool:
    return isinstance(v, (SpatialReference, int, str))

Try / catch

try:
    out = raster.transform(target)
except TypeError as e:
    if 'Transform only accepts' in str(e):
        out = raster.transform(SpatialReference(target))
    raise

Prevention

When it happens

Trigger: Calling raster.transform(None), raster.transform(a_dict), raster.transform(a_CoordTransform), or raster.transform(4.5). Also triggered when passing a raster or geometry object where an SRS was expected.

Common situations: Confusing transform (takes a target SRS) with warp (takes a dict); passing None when no transform was intended; passing a float SRID or a WKT fragment of the wrong type.

Related errors


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