I am using a template raster to create another raster with the same resolution/projection/extent from an array. The original raster looks good, but the new raster results in stripes.
ds = gdal.Open(template_raster)
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
[rows,cols] = arr.shape
driver = gdal.GetDriverByName('GTiff')
out_ds=driver.Create(output_raster,cols,rows,1,gdal.GDT_Float32)
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(input_array)
out_ds.SetGeoTransform(ds.GetGeoTransform())
out_ds.SetProjection(ds.GetProjection())
srs = osr.SpatialReference()
wkt = ds.GetProjection()
srs.ImportFromWkt(wkt)
out_ds.SetProjection(srs.ExportToWkt())
out_band.FlushCache()
del input_array
del out_band
out_ds = None
del ds
asked Jul 24, 2019 at 3:41
1 Answer 1
It seems possible that you have an old variable for the array. I cleaned up your code a little by referring directly to arr
.
ds = gdal.Open(template_raster)
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
[rows,cols] = arr.shape
driver = gdal.GetDriverByName('GTiff')
out_ds=driver.Create(output_raster,cols,rows,1,gdal.GDT_Float32)
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(arr)
out_ds.SetGeoTransform(ds.GetGeoTransform())
out_ds.SetProjection(ds.GetProjection())
out_band.FlushCache()
del input_array
del out_band
out_ds = None
del ds
answered Jul 26, 2019 at 21:53
lang-py
input_array
defined in your code. Did you mean to writearr
or are you referencing another array that did not show in your code? Furthermore, why are you setting the output raster projection twice?out_ds.SetProjection(ds.GetProjection())
gets the job done so you would not need to create a spatial reference, import the wkt and then set the projection again.