{"record":{"id":"6ac5f17537830818","repo":"django/django","slug":"transform-only-accepts-spatialreference-string-a","errorCode":null,"errorMessage":"Transform only accepts SpatialReference, string, and integer objects.","messagePattern":"Transform only accepts SpatialReference, string, and integer objects\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"django/contrib/gis/gdal/raster/source.py","lineNumber":532,"sourceCode":"            write=self._write,\n        )\n\n    def transform(\n        self, srs, driver=None, name=None, resampling=\"NearestNeighbour\", max_error=0.0\n    ):\n        \"\"\"\n        Return a copy of this raster reprojected into the given spatial\n        reference system.\n        \"\"\"\n        # Convert the resampling algorithm name into an algorithm id\n        algorithm = GDAL_RESAMPLE_ALGORITHMS[resampling]\n\n        if isinstance(srs, SpatialReference):\n            target_srs = srs\n        elif isinstance(srs, (int, str)):\n            target_srs = SpatialReference(srs)\n        else:\n            raise TypeError(\n                \"Transform only accepts SpatialReference, string, and integer \"\n                \"objects.\"\n            )\n\n        if target_srs.srid == self.srid and (not driver or driver == self.driver.name):\n            return self.clone(name)\n        # Create warped virtual dataset in the target reference system\n        target = capi.auto_create_warped_vrt(\n            self._ptr,\n            self.srs.wkt.encode(),\n            target_srs.wkt.encode(),\n            algorithm,\n            max_error,\n            c_void_p(),\n        )\n        target = GDALRaster(target)\n\n        # Construct the target warp dictionary from the virtual raster","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/gis/gdal/raster/source.py#L514-L550","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an EPSG integer: raster.transform(3857)","Pass a WKT/PROJ string or a SpatialReference instance","If you need to also set size/origin/driver, call raster.warp({...}) instead"],"exampleFix":"// before\nreprojected = raster.transform({'srid': 3857})\n// after\nreprojected = raster.transform(3857)\n# or for full control\nreprojected = raster.warp({'srid': 3857, 'width': 100, 'height': 100})","handlingStrategy":"type-guard","validationCode":"from django.contrib.gis.gdal import SpatialReference\n\ndef coerce_target_srs(srs):\n    if isinstance(srs, (SpatialReference, int, str)):\n        return srs\n    raise TypeError('transform target must be SpatialReference/int/str')","typeGuard":"from django.contrib.gis.gdal import SpatialReference\n\ndef is_transformable(v) -> bool:\n    return isinstance(v, (SpatialReference, int, str))","tryCatchPattern":"try:\n    out = raster.transform(target)\nexcept TypeError as e:\n    if 'Transform only accepts' in str(e):\n        out = raster.transform(SpatialReference(target))\n    raise","preventionTips":["Pass EPSG ints for the common case","Use warp(dict) when you need size/origin/driver control","Never pass None or dict to transform"],"tags":["django-gis","gdal","raster","transform","type-validation"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}