{"record":{"id":"68ad4b3f8fd39826","repo":"django/django","slug":"failed-creating-vsi-raster-from-the-input-buffer","errorCode":null,"errorMessage":"Failed creating VSI raster from the input buffer.","messagePattern":"Failed creating VSI raster from the input buffer\\.","errorType":"exception","errorClass":"GDALException","httpStatus":null,"severity":"error","filePath":"django/contrib/gis/gdal/raster/source.py","lineNumber":130,"sourceCode":"            # that the vsimem file remains available until the GDALRaster is\n            # deleted.\n            self._ds_input = c_buffer(ds_input)\n            # Create random name to reference in vsimem filesystem.\n            vsi_path = os.path.join(VSI_MEM_FILESYSTEM_BASE_PATH, str(uuid.uuid4()))\n            # Create vsimem file from buffer.\n            capi.create_vsi_file_from_mem_buffer(\n                force_bytes(vsi_path),\n                byref(self._ds_input),\n                size,\n                VSI_TAKE_BUFFER_OWNERSHIP,\n            )\n            # Open the new vsimem file as a GDALRaster.\n            try:\n                self._ptr = capi.open_ds(force_bytes(vsi_path), self._write)\n            except GDALException:\n                # Remove the broken file from the VSI filesystem.\n                capi.unlink_vsi_file(force_bytes(vsi_path))\n                raise GDALException(\"Failed creating VSI raster from the input buffer.\")\n        elif isinstance(ds_input, dict):\n            # A new raster needs to be created in write mode\n            self._write = 1\n\n            # Create driver (in memory by default)\n            driver = Driver(ds_input.get(\"driver\", \"MEM\"))\n\n            # For out of memory drivers, check filename argument\n            if driver.name != \"MEM\" and \"name\" not in ds_input:\n                raise GDALException(\n                    'Specify name for creation of raster with driver \"{}\".'.format(\n                        driver.name\n                    )\n                )\n\n            # Check if width and height where specified\n            if \"width\" not in ds_input or \"height\" not in ds_input:\n                raise GDALException(","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/gis/gdal/raster/source.py#L112-L148","documentation":"Raised as GDALException from GDALRaster.__init__ when ds_input is bytes, a /vsimem file was created from the buffer (capi.create_vsi_file_from_mem_buffer), but the subsequent capi.open_ds on that vsimem path raised a GDALException. The cleanup (capi.unlink_vsi_file) runs before re-raising. The bytes were reachable as a file but GDAL could not interpret them as a raster.","triggerScenarios":"GDALRaster(b'<not a raster>') with arbitrary bytes; bytes that are a valid file but of a format the GDAL build cannot read; truncated raster bytes (interrupted download); wrong byte order or endianness in hand-crafted WKB-like input.","commonSituations":"Reading raster blobs from a DB column or cache and passing them straight to GDALRaster; receiving uploaded bytes whose format is not supported;混淆 of vector bytes (GeoJSON/shapefile) passed where raster is expected.","solutions":["Confirm the bytes are a real raster format GDAL supports (write them to a temp file and run gdalinfo).","Use a universally-supported format when generating the bytes (GeoTIFF in-memory).","Wrap construction in try/except GDALException to reject unparseable blobs."],"exampleFix":"# before\nraster = GDALRaster(blob)\n\n# after\nimport tempfile, subprocess\nwith tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as f:\n    f.write(blob); tmp = f.name\nsubprocess.run(['gdalinfo', tmp], check=True)  # validate before constructing\nraster = GDALRaster(blob)","handlingStrategy":"try-catch","validationCode":"def raster_bytes_ok(blob):\n    # cheap sniff: write to temp file and let gdalinfo judge\n    import tempfile, subprocess, os\n    with tempfile.NamedTemporaryFile(suffix='.bin', delete=False) as f:\n        f.write(blob); p = f.name\n    ok = subprocess.run(['gdalinfo', p], capture_output=True).returncode == 0\n    os.unlink(p)\n    return ok","typeGuard":null,"tryCatchPattern":"from django.contrib.gis.gdal.error import GDALException\ntry:\n    raster = GDALRaster(blob)\nexcept GDALException as e:\n    if 'VSI raster' in str(e):\n        blob = None  # bytes were not a recognizable raster\n    raise","preventionTips":["Produce in-memory raster bytes in a broadly supported format (GeoTIFF).","Validate blobs from DB/cache with gdalinfo before constructing GDALRaster.","Do not pass vector bytes (GeoJSON/shapefile) to GDALRaster; use OGR instead."],"tags":["gdal","raster","vsimem","bytes","open"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}