Discover all of the brand-new features and improvements to existing capabilities in the Dataiku 11.3 updateLET'S GO

Writing geotiff file into folder

Solved!
razan
Level 2
Writing geotiff file into folder

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

0 Kudos
1 Solution
razan
Level 2
Author

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

 

View solution in original post

0 Kudos
1 Reply
razan
Level 2
Author

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

 

0 Kudos