Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compression of output LULC #35

Open
vwvbrand opened this issue Mar 11, 2025 · 1 comment
Open

Compression of output LULC #35

vwvbrand opened this issue Mar 11, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@vwvbrand
Copy link
Contributor

vwvbrand commented Mar 11, 2025

The output enriched LULC in enrich-lulc component should be compressed and translated to Cloud Optimised Geotiff (COG) for web services of data cubes.
So, two export options should be available:

  • compressed (LZW), COG (default)
  • non-compressed, GTiff
@vwvbrand vwvbrand added the enhancement New feature or request label Mar 11, 2025
@vwvbrand
Copy link
Contributor Author

The in-built solution for the function write_raster in enrichment\lulc_enrichment_wrapper.py is the following:

def write_raster(self, output_data:any, output_ds:any, output_raster:str, nodata_value:int, compress:bool):
        """
        Write a new raster dataset from the given data array.

        Args:
            output_data (np.array): data array to write to the raster
            output_ds (gdal.Dataset): dataset of the input raster
            output_raster (str): path to the output raster dataset
            nodata_value (int): no data value for the output raster
            compress (bool): if True, compress by LZW type and save as a Cloud Optimised Geotiff.
        """

        # export as a temporary raster if compress is defined
        temp_raster = output_raster if not compress else output_raster + "_tmp.tif"

        # get the driver to write a new GeoTIFF
        driver = gdal.GetDriverByName('GTiff')
        out_ds = driver.Create(temp_raster, output_ds.RasterXSize, output_ds.RasterYSize, 1, gdal.GDT_Byte)

        # set geo-transform and projection from the input raster
        out_ds.SetGeoTransform(output_ds.GetGeoTransform())
        out_ds.SetProjection(output_ds.GetProjection())

        # write the data to the output raster
        out_band = out_ds.GetRasterBand(1)
        out_band.WriteArray(output_data)

        # set nodata value 
        out_band.SetNoDataValue(nodata_value)

        # flush and close
        out_band.FlushCache()
        output_ds = None
        out_ds = None 

        if compress:
            print("Saving enriched LULC as a compressed Cloud Optimised Geotiff...")

            # open temp_raster as a GDAL dataset before passing it
            temp_ds = gdal.Open(temp_raster, gdal.GA_ReadOnly) 

            cog_driver = gdal.GetDriverByName("COG")
            cog_driver.CreateCopy(
                output_raster, temp_ds,
                options=['COMPRESS=LZW', 'BIGTIFF=IF_SAFER', 'OVERVIEWS=AUTO']
            )
            
            temp_ds = None
            os.remove(temp_raster)

        print(f"Output raster saved to {output_raster}")

To call the function:

self.write_raster(output_data, output_ds, lulc_upd, nodata_value, compress=True)

It is proposed to add compress parameter to @app.command("enrich-lulc") in main.py.

Riyad-boop added a commit that referenced this issue Mar 13, 2025
completed issues:

Co-Authored-By: Vitalii Kriukov <59083235+vwvbrand@users.noreply.github.com>
#35
vwvbrand pushed a commit that referenced this issue Mar 24, 2025
completed issues:

Co-Authored-By: Vitalii Kriukov <59083235+vwvbrand@users.noreply.github.com>
#35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant