1

I am trying to change the pixel width and pixel height in the GeoTransform inside a georeferenced .tif file. I wrote a bit of code to read the file then change the GeoTransform parameters and write another file. I cannot get the process to just change the file in question (and not make a new file). Is there a way to just modify the file? I have hundreds to modify so I do not want to do one at a time. The following code works fine but it is a bit overkill.

import sys import os import glob import numpy as np try: from osgeo import ogr, osr, gdal except: sys.exit('ERROR: cannot find GDAL/OGR modules') # Enable GDAL/OGR exceptions gdal.UseExceptions() OUTPUTPATH = r"G:\Myfiles\Imagery\Small\resize" INPUTPATH = r"G:\Myfiles\Imagery\Small" def readFile(filename): filehandle = gdal.Open(filename) redband = (filehandle.GetRasterBand(1)).ReadAsArray() greenband = (filehandle.GetRasterBand(2)).ReadAsArray() blueband = (filehandle.GetRasterBand(3)).ReadAsArray() geotransform = filehandle.GetGeoTransform() print(geotransform) print(f"Total raster bands {filehandle.RasterCount}") #(12709752.0, 0.25, 0.0, 562848.0, 0.0, -0.25) offX, xsize, line1, offY, line2, ysize = geotransform geotransform = (offX, 1.0, line1, offY, line2, -1.0) geoproj = filehandle.GetProjection() xsize = filehandle.RasterXSize ysize = filehandle.RasterYSize print(geotransform) print(geoproj) filehandle = None return xsize,ysize,geotransform,geoproj,redband, greenband, blueband def writeFile(filename,geotransform,geoprojection, redband, greenband, blueband): (ny,nx) = redband.shape format = "GTiff" driver = gdal.GetDriverByName(format) driver = gdal.GetDriverByName("GTiff") driver.Register() #dst_ds = driver.Create(filename, x, y, 1, data.DataType) #dst_ds = driver.Create(filename,nx, ny,3,options=["INTERLEAVE=PIXEL"]) dst_ds = driver.Create(filename, nx, ny, bands = 3, eType = gdal.GDT_Byte) #was gdal.GDT_int16 dst_ds.SetGeoTransform(geotransform) # specify coords # WGS84 lat/long dst_ds.SetProjection(geoprojection) # export coords to file reds = dst_ds.GetRasterBand(1) greens = dst_ds.GetRasterBand(2) blues = dst_ds.GetRasterBand(3) reds.WriteArray(redband) # write r-band to the raster reds.SetNoDataValue(np.nan) reds.FlushCache() greens.WriteArray(greenband) # write g-band to the raster greens.SetNoDataValue(np.nan) greens.FlushCache() blues.WriteArray(blueband) # write b-band to the raster blues.SetNoDataValue(np.nan) blues.FlushCache() #Close main raster dataset dst_ds.FlushCache() # write to disk dst_ds = None reds = None blues = None greens = None redband = None blueband = None greenband = None path1 = INPUTPATH + r"\*.tif" filelist = glob.glob(path1) for oneFile in filelist: print(f"File = {oneFile} " ) InputBaseName = os.path.split(oneFile)[1] [xsize, ysize, geotransform, geoproj, R,G,B] = readFile(os.path.join(INPUTPATH, oneFile)) writefilename = os.path.join(OUTPUTPATH,InputBaseName) writeFile(writefilename, geotransform, geoproj, R,G,B) print("Done") 

If I didn't have to write a new file, this would be a bit quicker. This video helped a bunch - https://www.youtube.com/watch?v=p_BsFdV_LUk

1
  • Kadir, you are right. I ended up not doing anything with it except launching it from inside QGIS. I'll take that part out. Commented Jan 14, 2022 at 12:01

1 Answer 1

1

You cannot change the raster size (pixel width / height) of an existing GeoTIFF file. You must write a new file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.