I have 2 raster files of different resolution (having different pixel size and origin, but both files are georeferenced). I want to align the 2 files so that each pixel of first raster coincides with the corresponding pixel of second raster. In QGIS, this can be done using the align tool, but I wish to write a script in python for this purpose.
-
1What do you have so far and where are you stuck? Aligning two rasters isn't as simple as it sounds.. it could be that the georeference has been set wrong (affine should fix) or there could be a difference in accuracy of the raster (rubber sheet or higher order polynomial). You could potentially use GDALWarp gdal.org/gdalwarp.html with a ground control points file from python with a shell or subprocess.Popen.– Michael StimsonJan 16, 2018 at 5:38
1 Answer
Select which image you want to use as reference and check the pixel size of that with gdalinfo. Then warp the second image to have the same pixel size with gdalwarp http://www.gdal.org/gdalwarp.html by using both -tr (target resolution) and -tap (target aligned pixels) options.
If the origin of the reference image does not match with the pixel size you must align also that with gdalwarp and -tap. Example: if pixel size is 2 meters and origin is at (500000,58002) then the pixels are aligned but if the origin is at (500000,58001) then they are not aligned.
With Python you can either call the gdalwarp executable or use warp as a library function directly in Python http://erouault.blogspot.fi/2015/10/gdal-and-ogr-utilities-as-library.html.
-
Thanks user30184 I have been able to match pixel size using gdal.Warp('orthoAligned.tif', ortho, targetAlignedPixels = True, xRes = 0.000001006513588, yRes = 0.000000905585904) but I also want to set the the output file resolution like (3053,2029). How can this be done? Jan 17, 2018 at 10:14
-
3I was able to set resolution using : gdal.Warp('orthoAligned.tif',ortho, width = xmax,height = ymax), The blog link was really helpful. Jan 17, 2018 at 10:29