[Gdal-dev] C# Bindings
Tamas Szekeres
szekerest at gmail.com
Tue Jan 10 14:24:34 EST 2006
Simon,
For Visual Studio 2003 SWIG binding is compilable using some minor
modifications in the make file see
http://bugzilla.remotesensing.org/show_bug.cgi?id=949
For more details. However some additional typemaps should be constructed in
order to make this feature really usable.
I haven`t enough time to set up a VS 2005 development environment, so I
did't try to compile with it till now.
Tamas Szekeres
-----Original Message-----
From: gdal-dev-bounces at lists.maptools.org
[mailto:gdal-dev-bounces at lists.maptools.org] On Behalf Of Simon Perkins
Sent: Monday, January 09, 2006 3:33 AM
To: gdal-dev at lists.maptools.org
Subject: [Gdal-dev] C# Bindings
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
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.16/225 - Release Date: 2006.01.09.
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.17/226 - Release Date: 2006.01.10.
More information about the Gdal-dev
mailing list