1

As previously seen here I created a function to save a raster with gdalon python:

def array2raster2(fname,  rasterOrigin, pixelWidth, pixelHeight,array,  proj, shape):

    originX = rasterOrigin[0]
    originY = rasterOrigin[1]
    drv = gdal.GetDriverByName("GTiff")
    dst_ds = drv.Create(fname, shape[1], shape[0], 4)#, gdal.GDT_UInt16)
    dst_ds.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
    dst_ds.SetProjection(proj)
    dst_ds.GetRasterBand(1).WriteArray(array[:, :, 0])  # write r-band to the    raster
    dst_ds.GetRasterBand(2).WriteArray(array[:, :, 1])  # write g-band to the raster
    dst_ds.GetRasterBand(3).WriteArray(array[:, :, 2])
    dst_ds.GetRasterBand(4).WriteArray(array[:, :, 3])

    dst_ds.GetRasterBand(1).SetRasterColorInterpretation(gdal.GCI_RedBand)
    dst_ds.GetRasterBand(2).SetRasterColorInterpretation(gdal.GCI_GreenBand)
    dst_ds.GetRasterBand(3).SetRasterColorInterpretation(gdal.GCI_BlueBand)

    dst_ds.FlushCache()
    dst_ds=None

And added the last 3 lines for SetRasterColorInterpretation()and worked great when using 3 channels.

But now I am using now 4 channels, 4Th channel for NIR band, as on SPOT satelite images or Pleiades, but when opening again on ENVI or QGIS the 4th band is shown as the first, what do I need to do to fix that? I checked on gdal.GCI_values and only shows for 3 bands, I understood that gdal.GCI_AlphaBandis for transparency band so I don't need that...yet.

And second issue, I have the borders of the image as 0, how can I set that value to Nodata so I don't have borders visualized on ENVI or any other GIS?

1

Add the ALPHA=NO creation option to avoid the 4th band (NIR) being interpreted as alpha channel and finally set NIR band color interpretation as undefined:

co = ["ALPHA=NO"] # creation options
dst_ds = drv.Create(fname, shape[1], shape[0], 4, gdal.GDT_UInt16, options = co)

# your code here

dst_ds.GetRasterBand(4).SetRasterColorInterpretation(gdal.GCI_Undefined)

Your Answer

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

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