django/django · error · GDALException
Failed creating VSI raster from the input buffer.
Error message
Failed creating VSI raster from the input buffer.
What it means
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.
Source
Thrown at django/contrib/gis/gdal/raster/source.py:130
# that the vsimem file remains available until the GDALRaster is
# deleted.
self._ds_input = c_buffer(ds_input)
# Create random name to reference in vsimem filesystem.
vsi_path = os.path.join(VSI_MEM_FILESYSTEM_BASE_PATH, str(uuid.uuid4()))
# Create vsimem file from buffer.
capi.create_vsi_file_from_mem_buffer(
force_bytes(vsi_path),
byref(self._ds_input),
size,
VSI_TAKE_BUFFER_OWNERSHIP,
)
# Open the new vsimem file as a GDALRaster.
try:
self._ptr = capi.open_ds(force_bytes(vsi_path), self._write)
except GDALException:
# Remove the broken file from the VSI filesystem.
capi.unlink_vsi_file(force_bytes(vsi_path))
raise GDALException("Failed creating VSI raster from the input buffer.")
elif isinstance(ds_input, dict):
# A new raster needs to be created in write mode
self._write = 1
# Create driver (in memory by default)
driver = Driver(ds_input.get("driver", "MEM"))
# For out of memory drivers, check filename argument
if driver.name != "MEM" and "name" not in ds_input:
raise GDALException(
'Specify name for creation of raster with driver "{}".'.format(
driver.name
)
)
# Check if width and height where specified
if "width" not in ds_input or "height" not in ds_input:
raise GDALException(View on GitHub (pinned to ae25a40be0)
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.
Example fix
# before
raster = GDALRaster(blob)
# after
import tempfile, subprocess
with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as f:
f.write(blob); tmp = f.name
subprocess.run(['gdalinfo', tmp], check=True) # validate before constructing
raster = GDALRaster(blob) Defensive patterns
Strategy: try-catch
Validate before calling
def raster_bytes_ok(blob):
# cheap sniff: write to temp file and let gdalinfo judge
import tempfile, subprocess, os
with tempfile.NamedTemporaryFile(suffix='.bin', delete=False) as f:
f.write(blob); p = f.name
ok = subprocess.run(['gdalinfo', p], capture_output=True).returncode == 0
os.unlink(p)
return ok Try / catch
from django.contrib.gis.gdal.error import GDALException
try:
raster = GDALRaster(blob)
except GDALException as e:
if 'VSI raster' in str(e):
blob = None # bytes were not a recognizable raster
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not open the datasource at "{}" ({}).
- Nodata value must be numeric or None.
- Offset too big for this raster.
- Size is larger than raster.
- Unable to get band index %d
AI-assisted analysis of django/django@ae25a40be0 (2026-08-06).
Data as JSON: /api/errors/68ad4b3f8fd39826.
Report an issue: GitHub.