I am processing a raster image inside my Django app. From a GeoTIFF file I am converting it to a COG. I am using gdal.Translate for the operation. I want to save the processed image without saving it locally. I want to use the GDAL dataset to read its bytes and be saved in a Django model's file field.
But suppose in this example, I am opening a dataset using gdal.Open. How can I save that to FileField?
from django.core.files.base import ContentFile
from django.db.models.fields.files import FileField
import osgeo
from osgeo import gdal
dataset = gdal.Open('/path/to/dataset.tif')
data_bytes = dataset.ReadAsArray().tobytes()
file_field = FileField()
file_field.save('dataset.tif', content_file, save=False)
model_instance = MyModel(file_field=file_field)
model_instance.save()
I am using GDAL 3.2.2.1 due to some constraints. Does function ReadAsArray not available in the version?
ModuleNotFoundError: No module named 'osgeo._gdal_array'
How can I do this correctly?