[gdal-dev] Can c++ types be used directly from the SWIG interfaces?

Even Rouault even.rouault at spatialys.com
Tue Sep 1 11:02:08 PDT 2026


Michael,

This is likely a design decision from 20 years ago when the "new" 
bindings have been added. I wasn't involved, but I can imagine at least 
2 reasons to prefer binding C functions rather than C++ classes:

- make sure that the bindings generated against GDAL X still run against 
libgdal Y where Y >= X without being rebuilt, since the C ABI is 
backwards compatible. But that doesn't seem to be a very 
realistic/common scenario

- avoid exposing to SWIG things that haven't been considered stable 
enough to be exposed to C

Maybe other GDAL users, such as people doing Rust or R bindings, could 
give us some hints if they'd be interested in binding the 
GDALGeoTransform class to their language. If so, making C bindings would 
serve everybody.

Even

Le 31/08/2026 à 21:50, Michael via gdal-dev a écrit :
> I want to expose the GDALGeoTransform class. It doesn't have a C 
> struct representation (like OGREnvelope) or an opaque C handle type. I 
> could add C interfaces to GDALGeoTransfor, but that seems absurd since 
> GDALGeoTransfor is mostly a C++ wrapper of the C routines.
>
> However, using the GDALGeoTransfor class directly from the SWIG 
> interfaces works surprisingly well (See below). What doesn't work is 
> the 'Apply' overloads which accept a `GDALRasterWindow` type, because 
> SWIG can't include gdal_rasterband.h while CPL_SUPRESS_CPLUSPLUS is 
> defined.
>
> So my questions are: can I use the GDALGeoTransfor c++ class directly 
> from SWIG and, if not, why?
> -------------
>
> %{
> #include "gdal_geotransform.h"
> %}
>
> class GDALGeoTransform {
> private:
>   const double *data() const;
>   ~GDALGeoTransform();
> public:
> %mutable;
>   GDALGeoTransform();
>   GDALGeoTransform(const double coeffs[6]);
>   GDALGeoTransform(double xorigIn, double xscaleIn, double xrotIn, 
> double yorigIn, double yrotIn, double yscaleIn);
>   void Rescale(double dfXRatio, double dfYRatio);
> %immutable;
>   void Apply(double dfPixel, double dfLine, double *pdfGeoX, double 
> *pdfGeoY) const;
>   bool GetInverse(GDALGeoTransform &inverse) const;
>   bool IsAxisAligned() const;
> };
>
> -- 
> Michael Bucari
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
http://www.spatialys.com
My software is free, but my time generally not.
LLMs contribute to global warming and brain rot.
Let's guillotine them! "Ah ! ça ira, ça ira, ça ira !"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20260901/766d042f/attachment.htm>


More information about the gdal-dev mailing list