<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Sean Gillies kirjoitti 27.11.2017 klo 20:46:<br>
</p>
<blockquote type="cite"
cite="mid:CADPhZXxFsUzrA76XZuaONcY9L+AAJNq7CRintbaaYZ=beptZFA@mail.gmail.com">
<div dir="ltr">I believe it's possible, yes, but it will be
challenging. I'm not aware of anybody else doing this.</div>
</blockquote>
<br>
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.<br>
<br>
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:<br>
<br>
<a class="moz-txt-link-freetext" href="https://github.com/ajolma/Geo-GDAL-Bayes-Hugin">https://github.com/ajolma/Geo-GDAL-Bayes-Hugin</a><br>
<br>
See the typemap file for an example of a typemap (GDAL Band Perl
object to a C GDALBandH pointer).<br>
<br>
Ari<br>
<br>
<blockquote type="cite"
cite="mid:CADPhZXxFsUzrA76XZuaONcY9L+AAJNq7CRintbaaYZ=beptZFA@mail.gmail.com">
<div dir="ltr">
<div><br>
</div>
<div>In <a
href="https://github.com/OSGeo/gdal/blob/trunk/gdal/swig/python/extensions/gdal_wrap.cpp"
moz-do-not-send="true">https://github.com/OSGeo/gdal/blob/trunk/gdal/swig/python/extensions/gdal_wrap.cpp</a>
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.</div>
<div><br>
</div>
<div>I presume you've already tried writing "<span
style="color:rgb(0,0,0);font-family:Tahoma;font-size:13.3333px">_my_module"
in Python and found it to be too slow? </span></div>
<div><span
style="color:rgb(0,0,0);font-family:Tahoma;font-size:13.3333px"><br>
</span></div>
<div>Good luck!</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Mon, Nov 27, 2017 at 9:34 AM, Shawn
Gong <span dir="ltr"><<a
href="mailto:SGONG@mdacorporation.com" target="_blank"
moz-do-not-send="true">SGONG@mdacorporation.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<div
style="direction:ltr;font-family:Tahoma;color:#000000;font-size:10pt">
<div class="m_1828817089082271248PlainText"><font
face="Tahoma">Thank you Sean.</font></div>
<p> </p>
<p><font size="2"><font face="Tahoma">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++.</font></font></p>
<p><font size="2"><font face="Tahoma">I did previously
by passing a file name (str) or a numpy array as
argument to C/C++, and it is all good.
</font></font></p>
<p>This time I want to pass a gdal dataset as an
argument:</p>
<p> </p>
<p>in Python:</p>
<p> from osgeo import gdal</p>
<p> import _my_c_module</p>
<p> ds=gdal.Open('my_filename"<wbr>)</p>
<p> result = _my_c_module.function1(ds)</p>
<p><font size="2"><font face="Tahoma"> </font></font></p>
<p class="m_1828817089082271248PlainText">is this
possible? If yes, how?</p>
<p class="m_1828817089082271248PlainText"> </p>
<p class="m_1828817089082271248PlainText"><br>
Shawn<br>
</p>
<div style="FONT-SIZE:16px;FONT-FAMILY:Times New
Roman;COLOR:#000000">
<hr>
<div id="m_1828817089082271248divRpF956507"
style="DIRECTION:ltr"><font size="2" face="Tahoma"
color="#000000"><b>From:</b> Sean Gillies [<a
href="mailto:sean@mapbox.com" target="_blank"
moz-do-not-send="true">sean@mapbox.com</a>]<br>
<b>Sent:</b> November-27-17 11:09 AM<br>
<b>To:</b> Shawn Gong<br>
<b>Cc:</b> <a
href="mailto:gdal-dev@lists.osgeo.org"
target="_blank" moz-do-not-send="true">gdal-dev@lists.osgeo.org</a><br>
<b>Subject:</b> Re: [gdal-dev] pass gdal dataset
as an argument from Python to C<br>
</font><br>
</div>
<div>
<div class="h5">
<div>
<div dir="ltr">Shawn,
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>Hope this helps,</div>
<div><br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Thu, Nov 23, 2017
at 1:25 PM, Shawn Gong <span dir="ltr">
<<a
href="mailto:SGONG@mdacorporation.com"
target="_blank" moz-do-not-send="true">SGONG@mdacorporation.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote"
style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px
0.8ex;BORDER-LEFT:#ccc 1px solid">
<div>
<div
style="FONT-SIZE:10pt;FONT-FAMILY:Tahoma;COLOR:#000000;DIRECTION:ltr">
<div>
<p>Hi list,</p>
<p> </p>
<p>Has anyone done this before?</p>
<p>I try to pass a gdal dataset as
an argument from Python to C.</p>
<p> </p>
<p>in my C code, I have</p>
<p>PyObject* py_my_func(PyObject
*self, PyObject *args)<br>
{</p>
<p> PyObject *poDataset;</p>
<p> </p>
<p> if(!PyArg_ParseTuple(args,
"Odd", &poDataset, &a1,
&a2))<br>
{<br>
return NULL;<br>
}</p>
<p> </p>
<p> my_func(poDataset, a1, a2);<br>
}</p>
<p> </p>
<p>void my_func(GDALDatasetH
hDataset, double a1, double a2)</p>
<p>{</p>
<p>}</p>
<p> </p>
<p>Python crashed and hDataset was
NULL</p>
<p>Is it possible to do or I did
something wrong?</p>
<p> </p>
<p>BTW, I have passed either a
filename or a gdal array as an
argument from Python to C, with
success.</p>
<div
style="FONT-SIZE:13px;FONT-FAMILY:Tahoma">
<div
class="m_1828817089082271248m_3308011925270217570BodyFragment"><font
size="2"><font face="Tahoma">
</font> </font></div>
<div
class="m_1828817089082271248m_3308011925270217570PlainText"><font
face="Tahoma">Thanks,<br>
Shawn<br>
</font></div>
</div>
</div>
</div>
</div>
<br>
______________________________<wbr>_________________<br>
gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org"
target="_blank" moz-do-not-send="true">gdal-dev@lists.osgeo.org</a><br>
<a
href="https://lists.osgeo.org/mailman/listinfo/gdal-dev"
rel="noreferrer" target="_blank"
moz-do-not-send="true">https://lists.osgeo.org/mailma<wbr>n/listinfo/gdal-dev</a><br>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div
class="m_1828817089082271248gmail_signature">
<div dir="ltr">Sean Gillies</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr">Sean Gillies</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a></pre>
</blockquote>
<br>
</body>
</html>