-1

I am using QGIS Madeira 3.4.3.

I am downsampling 100m TIFFs to 1000m. I am using Export - Save As GeoTIFF specifying cellsize 1000m, and the appropriate No Data value. This works just fine with the first four files. The output is a GeoTIFF with pixel size 1000, -1000.

However, I have another folder of 100m TIFFs and exporting them produces GeoTIFFs with pixel size 999.404,-999.667

All of the TIFFs are in the same Albers Equal Area projection on the NAD 83 datum. There is some variation in their origin points and extents.

Saving as a GeoPackage rather than a TIFF produces exactly the same discrepancy.

A discrepancy of half a metre in a thousand does not sound large, but with 800 cells spanning the coast it adds up to 400m difference. Besides, the point of the exercise is to get a broad selection of raters to align precisely in advance of analysis.

Has anyone seen this sort of discrepancy before?

Solutions?

Work arounds?


I looked at the Python code and the batch file. Then decided to try out GDAL_Translate in a quick and dirty batch file constructed using TextPad block editing.

:: Downsample 100m TIFFs to 1000m GeoTIFFs C:\OSGeo4W64\bin\GDAL_Translate -of GTIFF -tr 1000 1000 -r average bathy.tif bathy_1000m.tif C:\OSGeo4W64\bin\GDAL_Translate -of GTIFF -tr 1000 1000 -r average rugosity.tif rugosity_1000m.tif . . .

GDAL_Translate worked great even without a delay between batch commands. Luckily translate was fast and the TIFFs not overly large.

No issues with discrepancies in cell sizes.

I wonder if that had something to do with running outside of QGIS project?

A question for another day.

Stepping past the issue of cell size, what I really needed to do is resample some 100m rasters to 1000m and align them with a set of 1000m rasters. It would be better to carry that out in one command rather than downsampling and then realigning with possibly another round of resampling -- not the best approach.

I originally carried this out in QGIS with the Align Rasters tool. But, I had trouble with edge effects. Align rasters is supposed to ignore NoData cells, but that only seemed to apply to the reference 1000m cells while NoData values from the 100m cells were treated as legitimate values and averaged in during resampling.

What I am looking for is a GDAL command that will allow me to downsample and realign in one go all while observing NoData values and preserving the specification of NoData value in the output.

I have looked at GDALWarp but am not looking to warp the rasters. They are all already in the same Albers Equal Area projection on the NAD83 datum. I would prefer not to have to try to choose parameters to strangle GDALWarps tendency to warp.

Is there another GDAL command I should be looking at?

  • Would you consider GDAL_Translate -of GTIFF -tr 1000 1000 in a batch? Are you using windows? Is python an option? – Michael Stimson Mar 12 at 5:06
  • @MichaelStimson Michael, GDAL_Translate and Python are options. I have some training and experience with Python and ArcPy. However, this project is my first serious foray into QGIS and, end of fiscal draws nigh. There is not a lot time to learn the ins and outs of PyQGIS on this project. What would the code look like and where would I run it from? – Doug H Mar 12 at 20:27
1

Assuming you're on Windows there are two ways to batch script without ArcGIS, QGIS etc.. both rely on GDAL_Translate being able to be called from CMD. If you have installed GDAL as well start there otherwise open a command prompt in the QGIS bin folder (where GDAL_Translate is). In both examples you will need to change the path to find your input files and the path to export the output files to.. hopefully nothing else.

DOS Batch method: Use notepad or your favorite text editor to create a batch file then execute:

:: Supress verbose messages
@ECHO OFF
:: !Important: no spaces between the variable name, the equals and the value
:: Quote paths that have spaces, but NOT HERE, quote in the for loop
SET  IN_Folder=C:\some\path
SET Out_Folder=C:\your\output path
SET ConvertExt=tif

:: the case of the variable %%A is important
:: it's %%A in a batch file but %A if typed directly
FOR %%A IN (%IN_Folder%\*.%ConvertExt%) DO (
        echo Converting %%~nxA
        GDAL_Translate -of GTIFF -tr 1000 1000 %%A %Out_Folder%\%%~nA.tif
)

@ECHO ON

Change the paths to match your situation. This is a DOS for loop (for each file matching in the %IN_Folder%\*.%ConvertExt%, read more here with their full paths, the %%~nA gets the file name only, without extension (read more here) which is then output to your Out_Folder.

The python way, again in CMD:

import os, subprocess
InFolder = r'C:\some\path'
OutFolder= r'C:\your\outputpath' # don't use spaces in either

# Loop for every file in the folder
for ThisFile in os.listdir(InFolder):
    # break up the file name into name and extension
    fileName, fileExt = os.path.splitext(ThisFile)
    # python is case sensitive, always compare strings in the same case
    if fileExt.upper() == '.TIF': 
        # build the command by substituing variables where {} exists
        subCommand = 'gdal_translate -of gtiff -tr 1000 1000 {}\\{} {}\\{}.tif'.format(InFolder,ThisFile,OutFolder,fileName)
        # subprocess.Popen takes a list, I'm being lazy and splitting the string into a list using spaces
        # this is why *any* spaces that are unexpected are a bad idea
        pcs = subprocess.Popen(subCommand.split(' '))
        pcs.wait() # wait until this one is done before moving on

Change the paths to match your situation. Use Notepad, Notepad++, IDLE, pyWin or your favorite text editor to create a .py file and run in a CMD window.

Works the same as DOS batch, loop through the files in the input folder and calls GDAL_Translate for each one.

  • 1
    be sure also to use -r average to aggregate from 100 m resolution to 1000 m resolution. The default method is nearest, which is the wrong method for this situation, as each 1000 m cell will only be represented by the centre value, rather than the average of the values within it. – Mike T Mar 13 at 3:01
  • Good point @MikeT, there are several resampling methods available and the default option, as you say, is only good for classified (integer) rasters but it is the fastest. An excellent discourse on the comparative options is gis.stackexchange.com/questions/10931/… - the graphical representations are particularly helpful in understanding the results. – Michael Stimson Mar 13 at 3:38
  • Actually, for classified data -r mode is the best to gather the most common category. Again, nearest is the wrong method, since the question is not about interpolation. Nearest should only be considered if the resolutions are similar, or resampling to a finer resolution. – Mike T Mar 13 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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