I have two geotiff images that I would like to resample the same resolution.
If I use ArcGIS Desktop this is easy to do but how do I do this using QGIS?
qgisrasterresampling
I have two geotiff images that I would like to resample the same resolution.
If I use ArcGIS Desktop this is easy to do but how do I do this using QGIS?
The task could feel trivial by reading the gdalwarp documentation http://www.gdal.org/gdalwarp.html and GDAL AAIGrid -- Arc/Info ASCII Grid driver documentation http://www.gdal.org/frmt_various.html. The target pixel size is three times bigger than the native resolution 0.008333333333 degrees/pixel (not 1000 m/pixel, see the comments).
gdalwarp -of AAIGrid -tr 0.024999 0.024999 input.asc output.asc
However, it is a bit more difficult than that.
Therefore the conversion must be done in two steps and with a better resampling method.
First step is to create an interim output as GDAL Virtual raster (.VRT) with "average" resampling
gdalwarp -of VRT -r average -tr 0.024999 0.024999 input.asc interim.vrt
Second step is to convert the interim DEM into new ASCII Grid file with gdal_translate
gdal_translate -of AAIGrid interim.vrt output.asc
Note:
The formats which are supported for output can be listed with gdalwarp --formats command. Formats which support direct output are marked with "+" character. However, all of them will not really work (NTv2 Datum Grid Shift file for example).
On Windows
gdalwarp --formats|find "+"
FITS -raster- (rw+): Flexible Image Transport System
HDF4Image -raster- (rw+): HDF4 Dataset
netCDF -raster- (rw+s): Network Common Data Format
VRT -raster- (rw+v): Virtual Raster
GTiff -raster- (rw+vs): GeoTIFF
NITF -raster- (rw+vs): National Imagery Transmission Format
HFA -raster- (rw+v): Erdas Imagine Images (.img)
ELAS -raster- (rw+v): ELAS
MEM -raster- (rw+): In Memory Raster
BMP -raster- (rw+v): MS Windows Device Independent Bitmap
PCIDSK -raster,vector- (rw+v): PCIDSK Database File
ILWIS -raster- (rw+v): ILWIS Raster Map
SGI -raster- (rw+): SGI Image File Format 1.0
Leveller -raster- (rw+): Leveller heightfield
Terragen -raster- (rw+): Terragen heightfield
ISIS2 -raster- (rw+v): USGS Astrogeology ISIS cube (Version 2)
ERS -raster- (rw+v): ERMapper .ers Labelled
RMF -raster- (rw+v): Raster Matrix Format
RST -raster- (rw+v): Idrisi Raster A.1
INGR -raster- (rw+v): Intergraph Raster
GSBG -raster- (rw+v): Golden Software Binary Grid (.grd)
GS7BG -raster- (rw+v): Golden Software 7 Binary Grid (.grd)
PNM -raster- (rw+v): Portable Pixmap Format (netpbm)
ENVI -raster- (rw+v): ENVI .hdr Labelled
EHdr -raster- (rw+v): ESRI .hdr Labelled
PAux -raster- (rw+): PCI .aux Labelled
MFF -raster- (rw+): Vexcel MFF Raster
MFF2 -raster- (rw+): Vexcel MFF2 (HKV) Raster
BT -raster- (rw+v): VTP .bt (Binary Terrain) 1.3 Format
LAN -raster- (rw+v): Erdas .LAN/.GIS
IDA -raster- (rw+v): Image Data and Analysis
GTX -raster- (rw+v): NOAA Vertical Datum .GTX
NTv2 -raster- (rw+vs): NTv2 Datum Grid Shift
CTable2 -raster- (rw+v): CTable2 Datum Grid Shift
KRO -raster- (rw+v): KOLOR Raw
ADRG -raster- (rw+vs): ARC Digitized Raster Graphics
SAGA -raster- (rw+v): SAGA GIS Binary Grid (.sdat)
PDF -raster,vector- (rw+vs): Geospatial PDF
I found that the easiest way is to create a new raster file with dimensions equal to the reference file, SetGeoTransofrm and SetProjection of the new raster file to match the reference file then re-project the file and output, sample code shown below:
from osgeo import gdal, gdalconst
inputfile = #Path to input file
input = gdal.Open(inputfile, gdalconst.GA_ReadOnly)
inputProj = input.GetProjection()
inputTrans = input.GetGeoTransform()
referencefile = #Path to reference file
reference = gdal.Open(referencefile, gdalconst.GAReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
bandreference = reference.GetRasterBand(1)
x = reference.RasterXSize
y = reference.RasterYSize
outputfile = #Path to output file
driver= gdal.GetDriverByName('GTiff')
output = driver.Create(outputfile,x,y,1,bandreference.DataType)
output.SetGeoTransform(referenceTrans)
output.SetProjection(referenceProj)
gdal.ReprojectImage(input,output,inputProj,referenceProj,gdalconst.GRA_Bilinear)
del output
Best Answer
This is easy in QGIS too, though a little less obvious. There are a couple of ways you can do it:
Using GDAL_Warp - this tool lets you set the output resolution either by specifying the width and height of the output raster or by specifying the -tr switch (see the documentation). You can get to the GDAL_warp tool by going Raster->Projections->Warp (I did say it wasn't obvious from a resampling point of view!).
(v3.x) The -tr switch is enabled by using the Output file resolution in target georeferenced units box. For example, to downsample a 1m DEM to a 2m DEM, you can enter 2 in that field. However, there is no option to pass two different arguments for non-square pixels. Say your target pixel size is
0.3125,0.25
, meaning thexres
is0.3125
and theyres
is0.25
. If you now pass the value0.3125
in that box, it will set-tr 0.3125 0.3125
in the command. To counter this limitation, simply copy the code, paste to the command line, edit the -tr flag and run. For example:gdalwarp -t_srs EPSG:4326 -tr 0.3125 0.25 -r near -te 71.40625 24.875 84.21875 34.375 -te_srs EPSG:4326 -of GTiff foo.tiff bar.tiff
(depending on your instalation and environment variables, you may also need to explicitly state the path to gdalwarp).