[gdal-dev] Clip Raster with Polygon C#

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] Clip Raster with Polygon C#

tval
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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clip Raster with Polygon C#

Paul Meems
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 <[hidden email]>:
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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clip Raster with Polygon C#

tval
That was it. Thanks! I had not tried putting the shapefile as an option.



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev