[gdal-dev] Problem with writing Python interface using swig

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[gdal-dev] Problem with writing Python interface using swig

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();
    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:

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

gdal-dev mailing list
[hidden email]