2

I want to change projections for images using gdal bindings for c#. Gdal_translate seems like just renaming of projection (using a_srs parameter), but not reprojecting, so I used gdalwarp with s_srs parameter for that task. It's pretty clear what to pass to the Gdal.wrapper_GDALTranslate function: path to the destination file, dataset of the input file and some minor stuff. It also has more or less clear documentation. But for gdalwarp there are 3 kinds of different methods:

Gdal.wrapper_GDALWarpDestName(string dest, int object_list_count, 
SWIGTYPE_p_p_GDALDatasetShadow poObjects,
GDALWarpAppOptions warpAppOptions, Gdal.GDALProgressFuncDelegate callback,
string callback_data); //returns Dataset

Gdal.wrapper_GDALWarpDestDS(Dataset dstDS, int object_list_count,
SWIGTYPE_p_p_GDALDatasetShadow poObjects, GDALWarpAppOptions warpAppOptions,
Gdal.GDALProgressFuncDelegate callback, string callback_data); //returns int

Gdal.AutoCreateWarpedVRT(Dataset src_ds, string src_wkt, string dst_wkt, 
ResampleAlg eResampleAlg, double maxerror); //returns Dataset

It's absolutely not clear what these functions do and how they do it. Docs on gdalwarp page don't have any of these. I also don't understand the parameters: there is no input file or dataset parameter, only destination ones; the SWIGTYPE_p_p_GDALDatasetShadow class, which also doesn't have docs (there are not a single word about "shadow" either).

Summarizing, I'd like to hear some explanations about how this funcs work and how to make them work as binary gdalwarp.exe does.

1

1 Answer 1

6

Using How to use wrapper_GDALWarpDestName in C#, I managed to write working method, that works like console GdalWarp does.

public static bool GdalWarp(string inputFile, string outputFile, string[] options, OSGeo.GDAL.Gdal.GDALProgressFuncDelegate callback)
{
    using (Dataset inputDataset = Gdal.Open(inputFile, Access.GA_ReadOnly))
    {
        IntPtr[] ptr = {Dataset.getCPtr(inputDataset).Handle};
        GCHandle gcHandle = GCHandle.Alloc(ptr, GCHandleType.Pinned);
        Dataset result = null;
        try
        {
            SWIGTYPE_p_p_GDALDatasetShadow dss = new SWIGTYPE_p_p_GDALDatasetShadow(gcHandle.AddrOfPinnedObject(), false, null);
            result = Gdal.wrapper_GDALWarpDestName(outputFile, 1, dss, new GDALWarpAppOptions(options), callback, null);
        }
        catch (Exception) { return false; }
        finally
        {
            gcHandle.Free();
            result.Dispose();
        }
    }
    return true;
}
2
  • 2
    I'm curious what your GDALProgressFuncDelegate looks like
    – gisdude
    Commented Oct 31, 2019 at 15:59
  • result.Dispose(); should be result?.Dispose();
    – szamil
    Commented Mar 3, 2022 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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