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

asked Jul 24, 2019 at 3:41
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. Commented 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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jul 26, 2019 at 21:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.