[Gdal-dev] C# Bindings
Simon Perkins
sy at perkins.net
Sun Jan 8 21:33:03 EST 2006
Hi,
Michael Paul and Morten Nielson posted some hand-rolled C# bindings to
this list a few weeks back. I've reworked and extended that effort to
produce a partial set of C# bindings for GDAL that are very similar to
the C++ classes that underlie GDAL, i.e. we have C# classes for
Gdal.Dataset, Gdal.RasterBand, and so forth. Underneath it all, it calls
the GDAL C API, using the C# Pinvoke mechanism (which in turn calls the
GDAL C++ classes!). The bindings are written for C# 2.0 and use modern
features like generics, so you'll need Visual Studio 2005 to get these
to work.
Attached is a VS 2005 solution with two projects: the GDAL bindings
library, and a simple windows test application that allows you to load
in and view an image via GDAL. To see it in action, unpack the solution,
make sure you have the GDAL library in a directory pointed to by your
PATH variable, build and run the test application. I used the FWTools
distribution in which the GDAL library is called gdal_fw.dll, so if you
want to use a GDAL that's named differently, e.g. gdal13.dll, then
change the library name defined as the gdalDll constant at the top of
InterOp.cs.
To use the GDAL bindings in your own code, you can do things like:
string myFile = "testfile.tif";
using (Gdal.Dataset dataset = new Gdal.Dataset(myFile)) {
// Get the width, height and number of bands
int width = dataset.XSize;
int height = dataset.YSize;
int nBands = dataset.RasterCount;
// Get the projection string
string projection = dataset.GetProjection();
// Get the GDAL driver for this format
Gdal.Driver driver = dataset.GetDriver();
// Get the format name
string format = driver.LongName;
// Read the first band of the image into a byte array
Gdal.RasterBand band = dataset.GetRasterBand(1);
byte[] buffer = new byte[width * height];
band.RasterIO(Gdal.RWFlag.Read, 0, 0, width, height, buffer, width,
height);
} // Dataset is automatically closed at this point
Note that just like the GDALDataset C++ class, a Gdal.Dataset must be
associated with a file upon construction and will automatically
open/create that file. You can close the file either by explicitly
calling Close(), or by making use of the using() { .. } construct as
above. This makes sure that Dispose() is invoked on the Dataset as soon
as the Dataset goes out of scope, which in turn just invokes Close().
The raw Pinvoke bindings are all contained in the file InterOp.cs, so in
theory, this could be generated entirely by SWIG. Unfortunately I know
nothing about SWIG and so far I haven't been able to get SWIG to
successfully compile bindings with Visual Studio 2005. It's actually
pretty straightforward to add new Pinvoke signatures to InterOp.cs by
hand, though getting SWIG to take care of that would be nice. Any input
on that appreciated! I'm assuming that SWIG isn't going to be able to
generate the C# class-oriented interface, but I don't know enough about
it to be sure.
This partial implementation is good enough for my needs now. If anyone
else wants to use it, please feel free. If you want to extend it and
send updates to me, great! The code doesn't contain any explcit licence
headers, but Michael was suggesting an MIT licence, same as GDAL. Fine
with me.
Cheers,
Sy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GDAL_csharp.zip
Type: application/x-zip-compressed
Size: 26842 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/gdal-dev/attachments/20060108/53c498d6/GDAL_csharp.bin
More information about the Gdal-dev
mailing list