# How to use "gdal" CLI algorithms from C++

Added in version 3.12.

"gdal" CLI algorithms are available as `GDALAlgorithm` instances, and the include file to use is `gdalalgorithm.h`.

The first step is to get the instance of the `GDALGlobalAlgorithmRegistry` with `GDALGlobalAlgorithmRegistry::GetSingleton()` and then call `GDALAlgorithmRegistry::Instantiate()` with the path of the algorithm.

For example:

    auto &singleton = GDALGlobalAlgorithmRegistry::GetSingleton();
    GDALAlgorithm *algPtr = singleton.Instantiate("raster", "reproject");
    if (!algPtr)
    {
        std::cout << "cannot instantiate algorithm" << std::endl;
        return 1;
    }
    GDALAlgorithm &alg = *algPtr;

Arguments can be queried and set through `GDALAlgorithm::operator[]()`.

For example:

    alg["input"] = "byte.tif";
    alg["output"] = "out.tif";
    alg["dst-crs"] = "EPSG:26711";
    alg["resampling"] = "cubic";
    alg["resolution"] = std::vector<double>{60, 60};
    alg["overwrite"] = true;

Execution of the algorithm is performed by `GDALAlgorithm::Run()`, with an optional progress function. Proper closing of the output dataset must be performed with `GDALAlgorithm::Finalize()`

For example:

    if (alg.Run() && alg.Finalize())
    {
        std::cout << "success" << std::endl;
        return 0;
    }
    else
    {
        std::cout << "failure" << std::endl;
        return 1;
    }

When outputting to a MEM -- In Memory datasets dataset, you typically want to get the output dataset with:

    GDALDataset *poDS = alg["output"].Get<GDALArgDatasetValue>().GetDatasetRef();

Putting all things together:

    #include "gdal.h"
    #include "gdalalgorithm.h"
    #include "gdal_dataset.h"

    #include <iostream>

    int main()
    {
        GDALAllRegister();
        auto& singleton = GDALGlobalAlgorithmRegistry::GetSingleton();
        GDALAlgorithm *algPtr = singleton.Instantiate("raster", "reproject");
        if( !algPtr )
        {
            std::cout << "cannot instantiate algorithm" << std::endl;
            return 1;
        }
        GDALAlgorithm& alg = *algPtr;
        alg["input"] = "byte.tif";
        alg["output-format"] = "MEM";
        alg["dst-crs"] = "EPSG:26711";
        alg["resampling"] = "cubic";
        alg["resolution"] = std::vector<double>{60, 60};
        if( alg.Run() )
        {
            GDALDataset *poDS = alg["output"].Get<GDALArgDatasetValue>().GetDatasetRef();
            std::cout << "width=" << poDS->GetRasterXSize() << std::endl;
            std::cout << "height=" << poDS->GetRasterYSize() << std::endl;
            alg.Finalize();
            return 0;
        }
        else
        {
            std::cout << "failure" << std::endl;
            return 1;
        }
    }