[gdal-dev] How to use wrapper_GDALRasterizeDestName with C# bindings

Even Rouault even.rouault at spatialys.com
Tue Mar 14 07:23:40 PDT 2017


On mardi 14 mars 2017 15:12:20 CET Paul Meems wrote:
> I'm trying to use this gdal_rasterize command in my C# application:
> gdal_rasterize -a DN -a_srs "EPSG:28992" -tr 5 5 -a_nodata 0 -l test
> test.shp test.shp
> 
> My goal is to convert a shapefile to a tiff-file. The above command does
> what I want.
> I need to call it from within my C# application.
> I'm already successfully using wrapper_GDALTranslate
> and wrapper_GDALWarpDestName but I'm struggling
> with wrapper_GDALRasterizeDestName.
> 
> I have this code:
>             using (var ds = Ogr.Open(shapefile, 0)) // 0 means read-only. 1
> means writeable.
>             {
>                 if (ds == null)
>                 {
>                     throw new Exception("Can't open OGR dataset: " +
> shapefile);
>                 }
>                 var gdalOptions = new GDALRasterizeOptions(options);
>                 using (var newDs =
> Gdal.wrapper_GDALRasterizeDestName(output, ds, gdalOptions, null, null))
>                 {
>                     if (newDs == null)
>                     {
>                         throw new Exception("GdalRasterize failed: " +
> Gdal.GetLastErrorMsg());
>                     }
>                 }
>                 return true;
>             }
> 
> The problem is with ds, the second parameter of
> wrapper_GDALRasterizeDestName. Because it is a shapefile I open it with
> Ogr, but a Gdal dataset is expected.
> When I open my shapefile with Gdal.Open() I get an error saying my
> shapefile is not recognized as a supported file format.

Use Gdal.OpenEx(). This will allow vector drivers to be used and return a GDAL dataset.

> 
> What do I need to do to get this working in my C# application?
> 
> Thanks,
> 
> Paul


-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170314/8bc63b74/attachment.html>


More information about the gdal-dev mailing list