1

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

Output Raster With Stripes

1
  • 1
    I do not see input_array defined in your code. Did you mean to write arr 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. Jul 24, 2019 at 14:20

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.