2

QGIS prompt window

                file_writer = QgsRasterFileWriter(self.mypath + f'/{self.Name}_2x2DEM.tif')
                pipe = QgsRasterPipe()
                provider = DEMlayer.dataProvider()
                pipe.set(provider.clone())
                file_writer.writeRaster(pipe, 241, 213, DEMlayer.extent(), DEMlayer.crs(),)

For this code here, I hard coded it with 241 in Column, 213 in Rows in order to get 2x2 resolution.

Main problem: When I try to reuse the code with another GeoTiff image with different size, the resolution is not correct.

Is there anyway to set the horizontal to 2 and vertical to 2 instead of using rows/column?

3

Raster writer https://qgis.org/pyqgis/master/core/QgsRasterFileWriterTask.html requires the number of columns and rows as input. For achieving the desired resolution you must calculate the values first.

  • Take the layer extent
  • Take MinX, MaxX, MinY, MaxX from the extent
  • Divide the net width and height by resolution
  • Thats it
2
  • Is there anything with the net width and height with any function? Nov 18 '20 at 8:51
  • 1
    Sure, max - min.
    – user30184
    Nov 18 '20 at 11:22

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.