[gdal-dev] Clip Raster with Polygon C#

Paul Meems bontepaarden at gmail.com
Tue Apr 3 00:19:56 PDT 2018


Here's a snippet of the code I use:

using (var ds = GDAL.Open(input, Access.GA_ReadOnly))
{
    if (ds == null)
    {
        throw new Exception("Can't open GDAL dataset: " + input);
    }

            var options = new[]
            {
                "-of", "vrt",
                "-overwrite",
                "-multi",
                "-crop_to_cutline",
                "-cutline", borderFilename
            };

    var gdalOptions = new GDALWarpAppOptions(options);
    var ptr = new[] { Dataset.getCPtr(ds).Handle };
    var gcHandle = GCHandle.Alloc(ptr, GCHandleType.Pinned);
    try
    {
        var dss = new
SWIGTYPE_p_p_GDALDatasetShadow(gcHandle.AddrOfPinnedObject(), false, null);
        using (var newDs = GDAL.wrapper_GDALWarpDestName(output, 1, dss,
gdalOptions, null, null))
        {
            if (newDs == null)
            {
                throw new Exception("GdalWarp failed: " + GDAL
.GetLastErrorMsg());
            }
        }
    }
    finally
    {
        if (gcHandle.IsAllocated)
            gcHandle.Free();
    }

    return true;
}

Does this help you?

Regards,

Paul



2018-04-02 22:18 GMT+02:00 tval <tyeik at gktechinc.com>:

> Is it possible to use the Gdal Warp wrapper or another method for clipping
> a
> raster to a polygon? I've tried using Gdal.wrapper_GDALWarpDestName and
> using input datasets such as a .tif and a .shp, but was unsuccessful.
> Perhaps I had the wrong options or inputs. Any help would be appreciated.
> Thanks.
>
>
>
> --
> Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20180403/b68e69ce/attachment.html>


More information about the gdal-dev mailing list