What I want to do is take an image of N bands, merge it with another one of 1 band but preserve the geographic information of the initial image in the merged image.
I can already do the merge but the geo reference information comes out incomplete.
Here is my code:
def array2raster(new_file_name,array,data):
bandas = array.shape[2]
cols = data.RasterXSize
rows = data.RasterYSize
origen = data.GetGeoTransform()
origenX = origen[0]
origenY = origen[3]
pixelWidth=origen[1]
pixelHeight=origen[5]
driver = gdal.GetDriverByName("GTiff")
sr = osr.SpatialReference()
data.SetProjection(sr.ExportToWkt())
outRaster = driver.Create(new_file_name, cols, rows, bandas, gdal.GDT_Byte)
outRaster.SetGeoTransform((origenX, pixelWidth, origen[2], origenY,origen[4], pixelHeight))
for i in range(bandas):
band = i+1
banda = array[:,:,i]
outRaster.GetRasterBand(band).WriteArray(banda)
outRaster.GetRasterBand(band).SetNoDataValue(np.nan)
i = i+1
#s_ref =gdal.Copy
s_ref = osr.SpatialReference()
s_ref.ImportFromEPSG(4326)
outRaster.SetProjection(s_ref.ExportToWkt())
print("se guarda la info con éxito")
outRaster.FlushCache()