Writing geotiff file into folder

razan
razan Partner, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 6 Partner
edited July 16 in Using Dataiku

Hello community,

I want to write .TIF file into a folder, after I run the code recipe the run is successful but the file is empty. Here is an example of my code and a photo of the output:

input_folder = dataiku.Folder('input_folder_name')
output_folder = dataiku.Folder('output_folder_name')

band = input_folder['path'] + inputfolder.list_paths_in_partition()[0]

in_ds = gdal.Open(band)
in_band = in_ds.GetRasterBand(1)

gtiff_driver = gdal.GetDriverByName('GTiff')
out_ds = gtiff_driver.Create('nat_color.tif',
                             in_band.XSize, in_band.YSize, 1,
                             in_band.DataType)

out_ds.SetProjection(in_ds.GetProjection())
out_ds.SetGeoTransform(in_ds.GetGeoTransform())

out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(in_band.ReadAsArray())
out_ds.FlushCache()
for i in range(1, 4):
    out_ds.GetRasterBand(i).ComputeStatistics(False)
del out_ds

output_folder.upload_stream('Nat_color.TIF', 'nat_color.tif')

Screenshot from 2022-11-15 10-45-02.png


Operating system used: Ubuntu 22

Tagged:

Best Answer

  • razan
    razan Partner, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 6 Partner
    edited July 17 Answer ✓

    I have solved it using os library, where you open the output folder using os.chdir() and after you flush the file cache you delete the output dataset (out_ds) or set it to None.

    import os
    
    input_folder = dataiku.Folder('input_folder_name')
    output_folder = dataiku.Folder('output_folder_name')
    
    os.chdir(output_folder.get_info()['path'])
    
    band = input_folder['path'] + inputfolder.list_paths_in_partition()[0]
    
    in_ds = gdal.Open(band)
    in_band = in_ds.GetRasterBand(1)
    
    gtiff_driver = gdal.GetDriverByName('GTiff')
    out_ds = gtiff_driver.Create('nat_color.tif',
                                 in_band.XSize, in_band.YSize, 1,
                                 in_band.DataType)
    
    out_ds.SetProjection(in_ds.GetProjection())
    out_ds.SetGeoTransform(in_ds.GetGeoTransform())
    
    out_band = out_ds.GetRasterBand(1)
    out_band.WriteArray(in_band.ReadAsArray())
    out_ds.FlushCache()
    for i in range(1, 4):
        out_ds.GetRasterBand(i).ComputeStatistics(False)
    
    out_ds = None
    in_band = None
    in_ds = None

Setup Info
    Tags
      Help me…