<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;">Dear all,</span><br>
</div>
<div>
<div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I have a C++ project where I used GDAL OGR geometry as the basic data type. When I try to write a python interface for my project using Swig but I encountered a problem that the length and number of points returned from OGRLinestring become some strange values. </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I reproduce the error below. </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
The c++ code as a file `mygdal.hpp`</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>#include "gdal/ogrsf_frmts.h" // C++ API for GDAL<br>
</span>
<div>#include <iostream><br>
</div>
<div>#include <stdio.h><br>
</div>
<div><br>
</div>
<div>void test()<br>
</div>
<div>{<br>
</div>
<div>    OGRLineString* cutoffline = new OGRLineString();<br>
</div>
<div>    cutoffline->addPoint(2.0,1.0);<br>
</div>
<div>    cutoffline->addPoint(2.0,0.0);<br>
</div>
<div>    cutoffline->addPoint(3.0,0.5);<br>
</div>
<div>    std::cout<<"Number of points "<< cutoffline->getNumPoints()<<"\n";<br>
</div>
<div>    std::cout<<"Length "<< cutoffline->get_Length()<<"\n";<br>
</div>
<div>    delete cutoffline;<br>
</div>
<span>};</span><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
The swig interface code as a file `mygdal.i`</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>%module mygdal<br>
</span>
<div><br>
</div>
<div>%{<br>
</div>
<div>/* Put header files here or function declarations like below */<br>
</div>
<div>#include "mygdal.hpp"<br>
</div>
<div>%}<br>
</div>
<div><br>
</div>
<span>%include "mygdal.hpp"</span><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
The command to build the program</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>swig -python -c++ mygdal.i<br>
</span>
<div>g++ -O2 -fpic -std=c++11 -c mygdal.hpp mygdal_wrap.cxx -I/usr/include/python2.7 -lgdal<br>
</div>
<span>g++ -shared mygdal_wrap.o -o _mygdal.so -lgdal</span><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
However, the python code shows</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>$ python -c "import mygdal;mygdal.test()"<br>
</span>
<div>Number of points 40483584<br>
</div>
<div>Length 0</div>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
After I run the python code with valgrind, it shows </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>==23332== Invalid write of size 8<br>
</span>
<div>==23332==    at 0x76016AC: setX (ogr_geometry.h:304)<br>
</div>
<div>==23332==    by 0x76016AC: OGRSimpleCurve::getPoint(int, OGRPoint*) const (ogrlinestring.cpp:298)<br>
</div>
<span>==23332==    by 0x698CA59: test() (in /home/cyang/workspace/fmm/python/_mygdal.so)</span><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
```</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I guess the problem is this line:</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<a href="https://github.com/OSGeo/gdal/blob/01de5188008b589b33bf1bb3c1a24fe77137882c/gdal/ogr/ogrlinestring.cpp#L320" id="LPNoLP361842">https://github.com/OSGeo/gdal/blob/01de5188008b589b33bf1bb3c1a24fe77137882c/gdal/ogr/ogrlinestring.cpp#L320</a><br>
</div>
<br>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I am new to swig so I am not sure if I make some stupid mistake, so can anyone help to fix this problem? </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Best regards,</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Can Yang</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
</div>
</div>
</body>
</html>