0

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()

2
  • 2
    Welcome to GIS SE. Although we appreciate all cultures and languages, this is an English language site. Please edit your question to include the English translation.
    – Aaron
    Commented Oct 12, 2021 at 22:27
  • You're creating a spatial reference object but not assigning it, have a look at gis.stackexchange.com/questions/60371/… Commented Oct 13, 2021 at 1:04

0

Your Answer

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

Browse other questions tagged or ask your own question.