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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Shawn GONG

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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: pass gdal dataset as an argument from Python to C

Sean Gillies-3
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 <[hidden email]> 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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev



--
Sean Gillies

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: pass gdal dataset as an argument from Python to C

Shawn GONG

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 [[hidden email]]
Sent: November-27-17 11:09 AM
To: Shawn Gong
Cc: [hidden email]
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 <[hidden email]> 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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev



--
Sean Gillies

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: pass gdal dataset as an argument from Python to C

Sean Gillies-3
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_wrap.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.

I presume you've already tried writing "_my_module" in Python and found it to be too slow? 

Good luck!

On Mon, Nov 27, 2017 at 9:34 AM, Shawn Gong <[hidden email]> wrote:

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 [[hidden email]]
Sent: November-27-17 11:09 AM
To: Shawn Gong
Cc: [hidden email]
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 <[hidden email]> 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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev



--
Sean Gillies



--
Sean Gillies

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: pass gdal dataset as an argument from Python to C

Even Rouault-2

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


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: pass gdal dataset as an argument from Python to C

Shawn GONG

Thank you both Sean and Even.

 
SWIG is too complicated for me, and I will find a work around.

Shawn


From: Even Rouault [[hidden email]]
Sent: November-27-17 2:10 PM
To: [hidden email]
Cc: Sean Gillies; Shawn Gong
Subject: Re: [gdal-dev] pass gdal dataset as an argument from Python to C

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


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: pass gdal dataset as an argument from Python to C

Ari Jolma-2
In reply to this post by Sean Gillies-3

Sean Gillies kirjoitti 27.11.2017 klo 20:46:

I believe it's possible, yes, but it will be challenging. I'm not aware of anybody else doing this.

I'm doing it but in the Perl side. It's not impossible but Swig does wrap the C pointers quite deep into the Perl object (it must be similar in the Python Swig), so it needs a bit of research. However, I think it is a quite stable way Swig does it and it's a matter of making a typemap or a small function.

I think it is quite an important way to build extensions or link systems. In my case I needed a system computing a raster dataset (band) from other bands using a Bayesian network. I had built a Perl interface to a proprietary Bayesian network engine, and thus I needed only the the linkage code, which I built and is this:

https://github.com/ajolma/Geo-GDAL-Bayes-Hugin

See the typemap file for an example of a typemap (GDAL Band Perl object to a C GDALBandH pointer).

Ari


In https://github.com/OSGeo/gdal/blob/trunk/gdal/swig/python/extensions/gdal_wrap.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.

I presume you've already tried writing "_my_module" in Python and found it to be too slow? 

Good luck!

On Mon, Nov 27, 2017 at 9:34 AM, Shawn Gong <[hidden email]> wrote:
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 [[hidden email]]
Sent: November-27-17 11:09 AM
To: Shawn Gong
Cc: [hidden email]
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 <[hidden email]> 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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev



--
Sean Gillies



--
Sean Gillies


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev