I think I understood the steps to write an array to a raster file using python. however it doesn't work in my case and I don't know why. I have an array that I want to write to a Geotiff with Swiss Oblique Mercator Projection. I have a DEM as Geotiff in the exact same location with same projections and Geotransform, so I thought I just read all parameters from that DEM and use them to write my new raster. Here's the code that I'm, using:
driver.Register()
fn = '/media/LACIE_SHARE/davos2015.tif'
inDs = gdal.Open(fn)
if inDs is None:
print 'Could not open ' + fn
sys.exit(1)
rows=inDs.RasterYSize
cols=inDs.RasterXSize
driver=inDs.GetDriver()
outDs=driver.Create('/home/grassdata/test.tif',cols,rows,1,gdal.GDT_Float32)
if outDs is None:
print "crap"
sys.exit(1)
outBand=outDs.GetRasterBand(1)
outData=coh0 #coh0 is the array I want to convert to raster. it's an array with float32 entries.
outBand.WriteArray(outData) # this gives 0
outBand.FlushCache()
outBand.SetNoDataValue(-99) #this gives 0
outDs.SetGeoTransform(inDs.GetGeoTransform()) #this gives 0
outDs.SetProjection(inDs.GetProjection()) #this gives 0
del outData
the lines where I commented accordingly raise 0 as result and I don't understand why... Thanks for the help!
test.tif
created at all? 2) Trygdal.GetDriverByName('GTiff')
instead of getting the driver from the input dataset. 3) You can trygdal.UseExceptions()
at the beginning of your script to catch exceptions on the fly. 4) HAve you had a look at GDALs Python Gotchas – Kersten Sep 2 at 14:46FlushCache()
is usually called on the dataset and not the band. Are you able to provide a small subset of the DEM as well as the corresponsing coherence(?)coh0
array? – Kersten 2 days agocoh0=np.fromfile('/yourpath/coh0.npy',dtype='<f32') coh0=coh0[20:].reshape(3200,3700)
thanks for trying to help – uetli 2 days ago