[gdal-dev] pass gdal dataset as an argument from Python to C

Even Rouault even.rouault at spatialys.com
Mon Nov 27 11:10:57 PST 2017


On lundi 27 novembre 2017 11:46:50 CET Sean Gillies wrote:
> I believe it's possible, yes, but it will be challenging. I'm not aware of
> anybody else doing this.
> 
> In
> https://github.com/OSGeo/gdal/blob/trunk/gdal/swig/python/extensions/gdal_wr
> ap.cpp you can see examples of C++ functions that access the GDAL objects
> referenced by Python objects. This isn't a stable public API by any means,
> and therefore I hesitate to recommend it. Maybe Even would be able to
> comment more on how much support there is for the SWIG-generated C++ Python
> classes.
> 

Yes should be doable by "importing" some SWIG_ methods from gdal_wrap.cpp. This might be
fragile in case the SWIG_ wrapping structures evolve.

Interesting bits:

SWIGINTERN PyObject *_wrap_Dataset_RasterXSize_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0; int bLocalUseExceptionsCode = bUseExceptions;
  GDALDatasetShadow *arg1 = (GDALDatasetShadow *) 0 ;
  void *argp1 = 0 ;
  int res1 = 0 ;
  PyObject * obj0 = 0 ;
  int result;
  
  if (!PyArg_ParseTuple(args,(char *)"O:Dataset_RasterXSize_get",&obj0)) SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_GDALDatasetShadow, 0 |  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Dataset_RasterXSize_get" "', argument " "1"" of type '" "GDALDatasetShadow *""'"); 
  }
  arg1 = reinterpret_cast< GDALDatasetShadow * >(argp1);
  ....
}

(GDALDatasetShadow* is an alias of GDALDataset*)

static PyMethodDef SwigMethods[] = {
	 { (char *)"Dataset_RasterXSize_get", _wrap_Dataset_RasterXSize_get, METH_VARARGS, (char *)"Dataset_RasterXSize_get(Dataset self) -> int"},
}


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


More information about the gdal-dev mailing list