Quantcast

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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Paul Meems
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.

What do I need to do to get this working in my C# application?

Thanks,

Paul


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

Re: How to use wrapper_GDALRasterizeDestName with C# bindings

Even Rouault-2

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


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

Re: How to use wrapper_GDALRasterizeDestName with C# bindings

Paul Meems
Thanks Even,

I now have 
using (var ds = Gdal.OpenEx(input, 1, null, null, null)) 
I couldn't find GDAL_OF_VECTOR in the C# bindings. I'm assuming it must be 1 since GDAL_OF_RASTER is 0.

It is now working.




Paul

Paul Meems 
Release manager, configuration manager
and forum moderator of MapWindow GIS.
www.mapwindow.org

Owner of MapWindow.nl - Support for
Dutch speaking users.
www.mapwindow.nl


The MapWindow GIS project has moved to GitHub!


Download the latest MapWinGIS mapping engine.

Download the latest MapWindow 5 open source desktop application.


2017-03-14 15:23 GMT+01:00 Even Rouault <[hidden email]>:

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



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

Re: How to use wrapper_GDALRasterizeDestName with C# bindings

Even Rouault-2

On mardi 14 mars 2017 15:57:31 CET Paul Meems wrote:

> Thanks Even,

>

> I now have

> using (var ds = Gdal.OpenEx(input, 1, null, null, null))

> I couldn't find GDAL_OF_VECTOR in the C# bindings.

 

Constants are defined in swig/include/gdalconst.i as:

 

// Open flags

%constant OF_ALL = GDAL_OF_ALL;

%constant OF_RASTER = GDAL_OF_RASTER;

%constant OF_VECTOR = GDAL_OF_VECTOR;

%constant OF_GNM = GDAL_OF_GNM;

%constant OF_READONLY = GDAL_OF_READONLY;

%constant OF_UPDATE = GDAL_OF_UPDATE;

%constant OF_SHARED = GDAL_OF_SHARED;

%constant OF_VERBOSE_ERROR = GDAL_OF_VERBOSE_ERROR;

 

 

> I'm assuming it must be

> 1 since GDAL_OF_RASTER is 0.

 

 

/** Allow raster and vector drivers to be used.

* Used by GDALOpenEx().

* @since GDAL 2.0

*/

#define GDAL_OF_ALL 0x00

 

/** Allow raster drivers to be used.

* Used by GDALOpenEx().

* @since GDAL 2.0

*/

#define GDAL_OF_RASTER 0x02

 

/** Allow vector drivers to be used.

* Used by GDALOpenEx().

* @since GDAL 2.0

*/

#define GDAL_OF_VECTOR 0x04

 

 

1 is working since :

 

 

/** Open in update mode.

* Used by GDALOpenEx().

* @since GDAL 2.0

*/

#define GDAL_OF_UPDATE 0x01

 

 

And as no explicit GDAL_OF_RASTER or GDAL_OF_VECTOR is specified, GDAL_OF_ALL is assumed. But you probably don't need/want to open in update mode.

 

>

> It is now working.

>

>

>

>

> Paul

>

> *Paul Meems *

> Release manager, configuration manager

> and forum moderator of MapWindow GIS.

> www.mapwindow.org

>

> Owner of MapWindow.nl - Support for

> Dutch speaking users.

> www.mapwindow.nl

>

>

> *The MapWindow GIS project has moved to GitHub

> <https://github.com/MapWindow>!*

>

>

> Download the latest MapWinGIS mapping engine.

> <https://github.com/MapWindow/MapWinGIS/releases>

>

> Download the latest MapWindow 5 open source desktop application.

> <https://github.com/MapWindow/MapWindow5/releases>

>

> 2017-03-14 15:23 GMT+01:00 Even Rouault <[hidden email]>:

> > 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

 

 

--

Spatialys - Geospatial professional services

http://www.spatialys.com


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Loading...