I'm unable to add a projection to a newly-created raster
with GDAL-python
, using the code below:
arr = np.random.rand(600, 400)
filename = 'path_to_file_'
driver = gdal.GetDriverByName('GTiff')
x_size = arr.shape[1]
y_size = arr.shape[0]
dataset = driver.Create(filename, x_size, y_size,
eType=gdal.GDT_Float32)
dataset.SetProjection(projection)
dataset.GetRasterBand(1).WriteArray(arr)
It's expected in the code above that the projection isn't attached to the dataset, because the dataset has already been written when the projection is added to it.
Is there another way to create an image in a way that the projection
is attached to the saved image?
Note that the projection
(WGS84
) that I'm trying to attach to the image to create was extracted from an image used as input to create the newly-created image (i.e. I'm just trying to keep the projection of the source image in the image to save). Here is the string
returned by dataset.GetProjection()
(i.e. the content of the variable projection
):
'GEOGCS["WGS 84",DATUM["World Geodetic System 1984",SPHEROID["WGS 84",6378137.0,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0.0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433],AXIS["Geodetic longitude",EAST],AXIS["Geodetic latitude",NORTH],AUTHORITY["EPSG","4326"]]'