0

I have to mosaic several pairs of images (A and B). "A", "B" images has the same format (tif), projection, cell size, extent. Cutline in shape format is available if its needed. Nodata values are not present.

Im looking for a scriptable (preferable gdal) solution to do this, but nothing useful found yet.

illustration

6
  • 1
    is gdal_merege what you need? gdal.org/programs/gdal_merge.html
    – Zipper1365
    Commented Jan 16, 2020 at 12:59
  • I guess not. It cannot handle cutlines.
    – STO
    Commented Jan 16, 2020 at 13:12
  • 1
    You may very well be right, I don't do much with GDAL anymore, but you may be able to specify a no data value (the white space in your example) to be ignored in the mosaicing. The cutline could be irrelevant, no?
    – Zipper1365
    Commented Jan 16, 2020 at 13:18
  • 1
    Try gdalwarp gdal.org/programs/gdalwarp.html Description The gdalwarp utility is an image mosaicing, reprojection and warping utility.
    – user30184
    Commented Jan 16, 2020 at 14:05
  • @Zipper1365 Tried and at the first glance it is okay. But if there is a white pixel on left side of image "A", than it will appear Black on "C". And that's not okay. The images are orthophotos so the burnt out areas have to remain white.
    – STO
    Commented Jan 16, 2020 at 14:48

1 Answer 1

1

If you open the tiffs as numpys you can apply any condition you want to them with numpy where.

ds = gdal.Open(A)
band = ds.GetRasterBand(1)
arrA = band.ReadAsArray()
ds = gdal.Open(B)
band = ds.GetRasterBand(1)
arrB = band.ReadAsArray()
arrC = np.where((arrA != <yourwhitevalue>), arrA, arrB)
[cols, rows] = array.shape
driver = gdal.GetDriverByName("GTiff")
outdata = driver.Create('{}.tif'.format('C'), rows, cols, 1,gdal.GDT_UInt32)
outdata.SetGeoTransform(ds.GetGeoTransform())  ##sets same geotransform as input
outdata.SetProjection(ds.GetProjection())  ##sets same projection as input
outdata.GetRasterBand(1).WriteArray(array)
outdata.GetRasterBand(1).SetNoDataValue(0)  ##if you want these values transparent
outdata.FlushCache()  ##saves to disk!!
outdata = None
band = None
ds = None

Your Answer

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

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