I am trying to align rasters in pyqgis (snap raster ArcMap equivalent).

There is a class reference in documentation, which is doing exactly what I need. QgsAlignRaster class.

Anyway, my problem is I can't get the extent right. If I put 2 rasters with different extent, like 2 overlapping rasters, the one which I want aligned will get clipped by the other, so I am missing almost half of the original raster.

Is there a way to set that I do not want to clip it?

Documentation: https://qgis.org/pyqgis/master/analysis/other/QgsAlignRaster.html

My code is:

def alignRaster(raster, raster_to_align, output, proj, cell_size):

 alignClass = QgsAlignRaster()
 item1 = QgsAlignRaster.Item(raster, "")
 item2 = QgsAlignRaster.Item(raster_to_align, output)

 alignClass.setRasters([item1, item2])
 alignClass.setParametersFromRaster(raster, proj)

What i tried is setting the extent of raster with:

rLyr = QgsRasterLayer(raster)
clipExtent = rLyr.extent()

I also tried setting clip extent to null, which is the default option I believe, since if I try to read the the clip extent, its says this:

>> <QgsRectangle: 0 0, 0 0>

I am out of ideas, is there a solution to my problem?

In the picture below, you can see how it gets clipped (gray raster is the new one)...

enter image description here

| improve this question | | | | |
  • i have some idea about your problem,can you show me code with input parameters ? – jessie jes Sep 28 '18 at 22:44
  • Well, inputs are raster which I want aligned, raster which is ok, proj as WKT string of projection (EPSG:3979) and cell size, which is in this case 30x30m. and of course output to tiff file. I also tried to solve it in QGIS desktop, but it does the same thing no matter the input parameters. – Jan Doležal Oct 1 '18 at 9:04

Your Answer

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

Browse other questions tagged or ask your own question.