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

Shawn Gong SGONG at mdacorporation.com
Mon Nov 27 08:34:07 PST 2017


Thank you Sean.



What I want to do is to compile C/C++ code into "_my_c_module.pyd" and then to be called by Python. The Python code passes an argument to C/C++.

I did previously by passing a file name (str) or a numpy array as argument to C/C++, and it is all good.

This time I want to pass a gdal dataset as an argument:



in Python:

    from osgeo import gdal

    import _my_c_module

    ds=gdal.Open('my_filename")

    result = _my_c_module.function1(ds)



is this possible? If yes, how?



Shawn

________________________________
From: Sean Gillies [sean at mapbox.com]
Sent: November-27-17 11:09 AM
To: Shawn Gong
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] pass gdal dataset as an argument from Python to C

Shawn,

PyObject and GDALDatasetH are incompatible types and you can't pass the former to a function that expects the latter. I'm surprised your program can compile with these incompatible types.

Do you mean for your module to extend GDAL's Python bindings? If you look inside the C code of GDAL's Python bindings you may be able to see how to get the GDALDatasetH associated with a Python dataset object.

Hope this helps,


On Thu, Nov 23, 2017 at 1:25 PM, Shawn Gong <SGONG at mdacorporation.com<mailto:SGONG at mdacorporation.com>> wrote:

Hi list,



Has anyone done this before?

I try to pass a gdal dataset as an argument from Python to C.



in my C code, I have

PyObject* py_my_func(PyObject *self, PyObject *args)
{

    PyObject *poDataset;



   if(!PyArg_ParseTuple(args, "Odd", &poDataset, &a1, &a2))
  {
    return NULL;
  }



  my_func(poDataset, a1, a2);
}



void my_func(GDALDatasetH hDataset, double a1, double a2)

{

}



Python crashed and hDataset was NULL

Is it possible to do or I did something wrong?



BTW, I have passed either a filename or a gdal array as an argument from Python to C, with success.


Thanks,
Shawn

_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org<mailto:gdal-dev at lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/gdal-dev



--
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20171127/e0d94865/attachment.html>


More information about the gdal-dev mailing list