6

I need to change the pixel size of a GTiff in Gdal without changing the extent.

When I use gdal_translate -tr xres yres src_dataset dst_dataset

It changes the extent. I do not need a rescaling or interpolation. I just need to change the pixel size value from .041 to 4.6 for both x and y and not change anything else including the extent. Think of it as changing a value in the header.

OR

the inverse.

Just overwrite the extent but not change the pixel size would work. Open to Arc solutions as well.

11
  • Are you sure you've got the right syntax? The command line tool, for instance, is called by gdal_translate not gdal translate. Also, as you may already be aware, this would involve a pixel interpolation, so you should explicitly choose one using the -r flag and an appropriate option. Commented Jul 6, 2016 at 1:18
  • @PauloRaposo yes I am sure. I explicitly do not want a pixel interpolation. I just need to change the value. I just forgot the underscore. no interpolation required, just need to change the Tiff pixel size value. Commented Jul 6, 2016 at 1:23
  • If I'm understanding you correctly, you want the whole raster dimensions (size) to stay constant, but you want larger pixels (i.e., coarser resolution). If that's true, avoiding interpolation is impossible; rasters are statistical sample fields, and changing the spacing of the samples (the pixel size) can't be meaningfully done without some kind of interpolation. Commented Jul 6, 2016 at 1:27
  • @PauloRaposo you are not understanding (apologies). I know all that (look at my rep). Think of it as changing a value in a header. What you are saying is all about interpolation. I just have an erroneous value and I need to change it. I just need to overwrite the information in the header. Nothing in the actual raster changes. Commented Jul 6, 2016 at 1:32
  • If you want to correct your cell size export to worlded TIFF or BIL (eHDR) format using GDAL_Translate then edit (with notepad or similar text editor) the TFW/HDR file to adjust the cell dimensions. After adjustment then use GDAL_Translate again to convert to GeoTIFF. I prefer BIL (eHDR) for this as the situation can arise where you have a worlded GeoTIFF which can cause arguments. Another great tool for this is GeoTiff Examine freegeographytools.com/2007/… which will allow you to edit the GeoTags directly. Commented Jul 6, 2016 at 1:39

1 Answer 1

9

Here's a quick script I put together to correct the cell size of a folder of GeoTiff rasters:

import os, sys

from osgeo import gdal
from osgeo import gdalconst

WorkingFolder = sys.argv[1] # first command line argument
# change to a hard path like r'c:\your\path' or 'c:\\your\\path'
# without the r if it suits your purposes..

for f in os.listdir(WorkingFolder):   # loop through for each file in the folder
    fName, fExt = os.path.splitext(f) # break up file name and extension
    if fExt.upper() == '.TIF':
        # this is a tif file, open it and get the existing geotransform
        ds = gdal.OpenShared(os.path.join(WorkingFolder,f),gdalconst.GA_Update) # open the raster using FULL path
        gt = ds.GetGeoTransform()
        # gt is a tuple :
        # adfGeoTransform[0] /* top left x */
        # adfGeoTransform[1] /* w-e pixel resolution */
        # adfGeoTransform[2] /* 0 */
        # adfGeoTransform[3] /* top left y */
        # adfGeoTransform[4] /* 0 */
        # adfGeoTransform[5] /* n-s pixel resolution (negative value) */
        # from http://www.gdal.org/gdal_tutorial.html

        # you only want to change 1 and 5 but tuples are read-only so
        # I convert to a list:
        gtLst = list(gt)

        # change the pixel size here
        # 'y' pixel size is usually negative
        gtLst[1] = 4.6
        gtLst[5] = -4.6

        # apply the changes
        ds.SetGeoTransform(gtLst)
        ds = None # close the dataset and commit the changes

It's mostly comments but shows the key points. The updated cell size is hard coded, perhaps it should be a variable, but will work in this case.

Be careful of GeoTIFF with world files - this will only update the TIFF and not the TFW file (unless the TIF is not a GeoTIFF) which will lead to contradictions in georeference, depending on what other packages do with this situation you could still end up with the old cell size. It's best to remove any TFW files that exist (move to another folder just in case the georeference isn't embedded).

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.