2

I am trying to use GDAL_calc but my two rasters are off by one pixel in the y dimension. One is 256005 the other is 256004 so GDAL_calc fails with a multiplication.

Is it possible in gdalwarp to keep the same pixel resolution but to remove the extra row in the one with 256005 or add a row of 0's (or nodata) to the one with 256004?

C:\temp>gdal_calc --co="COMPRESS=LZW" -A a2000mfw.tif -B wdpa_eq_1_2000.tif - tfile=mfw_pa_00.tif --calc="A*B"

> Error! Dimensions of file wdpa_eq_1_2000.tif (1295778, 256005) are different from other files (1295778, 256004). Cannot proceed

1 Answer 1

1

I use the next code in my plugin (but with gdal_translate) to do that (to remove rows or columns of raster). In this case is specific for my raster of 791 rows and 1680 columns (adapt for your particular situation) to convert it in a raster of 790 rows and 1679 columns (to cut only one row and one column):

import os

rlayer = iface.activeLayer()

#raster layer
provider = rlayer.dataProvider()

input_raster_path = provider.dataSourceUri()

print input_raster_path

xsize = rlayer.rasterUnitsPerPixelX()
ysize = rlayer.rasterUnitsPerPixelY()

extent = rlayer.extent()

xmin_raster = extent.xMinimum()
ymax_raster = extent.yMaximum()

print xmin_raster, ymax_raster

n_rows = 790
n_columns = 1679

row_init = 1
column_init = 1 

row_end = row_init + n_rows - 1
column_end = column_init + n_columns - 1

xmin = xmin_raster + (column_init - 1 ) * xsize
xmax = xmin_raster + (column_end) * xsize

ymin = ymax_raster - (row_end) * ysize
ymax = ymax_raster - (row_init - 1) * ysize

extent = " -projwin " + str(xmin) + " " + str(ymax) + " " + str(xmax) + " " + str(ymin) + " "

output_raster_path = "/home/zeito/pyqgis_data/my_clip_layer.tif"

cmd = "gdal_translate " + extent + input_raster_path + " " + output_raster_path        

print cmd

os.system(cmd)

After running the code at the Python Console of QGIS, the cut is perfect; as it can be observed at the next image:

enter image description here

1
  • is it possible to do this without iface? working on a limited server with limited permissions.
    – Pictory
    Apr 24, 2021 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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