[gdal-dev] Problem with writing Python interface using swig
YANG CAN
ycycy1990 at hotmail.com
Fri May 24 03:45:46 PDT 2019
Dear all,
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.
I reproduce the error below.
The c++ code as a file `mygdal.hpp`
```
#include "gdal/ogrsf_frmts.h" // C++ API for GDAL
#include <iostream>
#include <stdio.h>
void test()
{
OGRLineString* cutoffline = new OGRLineString();
cutoffline->addPoint(2.0,1.0);
cutoffline->addPoint(2.0,0.0);
cutoffline->addPoint(3.0,0.5);
std::cout<<"Number of points "<< cutoffline->getNumPoints()<<"\n";
std::cout<<"Length "<< cutoffline->get_Length()<<"\n";
delete cutoffline;
};
```
The swig interface code as a file `mygdal.i`
```
%module mygdal
%{
/* Put header files here or function declarations like below */
#include "mygdal.hpp"
%}
%include "mygdal.hpp"
```
The command to build the program
```
swig -python -c++ mygdal.i
g++ -O2 -fpic -std=c++11 -c mygdal.hpp mygdal_wrap.cxx -I/usr/include/python2.7 -lgdal
g++ -shared mygdal_wrap.o -o _mygdal.so -lgdal
```
However, the python code shows
```
$ python -c "import mygdal;mygdal.test()"
Number of points 40483584
Length 0
```
After I run the python code with valgrind, it shows
```
==23332== Invalid write of size 8
==23332== at 0x76016AC: setX (ogr_geometry.h:304)
==23332== by 0x76016AC: OGRSimpleCurve::getPoint(int, OGRPoint*) const (ogrlinestring.cpp:298)
==23332== by 0x698CA59: test() (in /home/cyang/workspace/fmm/python/_mygdal.so)
```
I guess the problem is this line:
https://github.com/OSGeo/gdal/blob/01de5188008b589b33bf1bb3c1a24fe77137882c/gdal/ogr/ogrlinestring.cpp#L320
I am new to swig so I am not sure if I make some stupid mistake, so can anyone help to fix this problem?
Best regards,
Can Yang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20190524/6a3e2a94/attachment.html>
More information about the gdal-dev
mailing list