[gdal-dev] FW: point within polygon c++ API problem

Ran, Limei lran at email.unc.edu
Wed Jan 2 19:07:52 PST 2013


Hello Frank,

I think the fragmentation is caused by how the program is compiled.  I did a test using the attached test code with point within polygon function.  I appreciate if you can suggest how to compile it correctly with static option. 

When I compiled it in the following way, it worked:
> g++ -I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/gdal-1.9.1/local/include -I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/include test_geos.cpp -o test_geos -L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/gdal-1.9.1/local/lib -lgdal -L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/lib -lgeos_c

> ./test_geos
Testing point in polygon
        Set point
        Completed set point
        Set polygon
        Completed set polygon
Check point within polygon
        Point is not in the polygon

When I compiled it using the static way (which is the same as our API programs), it has segmentation fault:
> g++ -I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/gdal-1.9.1/local/include -I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/include -I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/proj-4.8.0/local/include test_geos.cpp -o test_geos -static -pthread -L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/gdal-1.9.1/local/lib -lgdal -L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/lib -lgeos_c -lgeos -L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/proj-4.8.0/local/lib -lproj -ldl

> ./test_geos
Testing point in polygon
        Set point
        Completed set point
        Set polygon
        Completed set polygon
Check point within polygon
Segmentation fault

We have been compiling our GDAL C++ APIs with static -pthread.  So, the programs can be used without recompiling on different systems.  Could I be doing something wrong here?

Thank you and happy new year,

Limei
==================================
Center for Environmental Modeling for Policy Development
Institute for the Environment
University of North Carolina at Chapel Hill
==================================

________________________________________
From: Ran, Limei
Sent: Thursday, December 20, 2012 10:20 AM
To: Frank Warmerdam
Subject: RE: [gdal-dev] FW: point within polygon c++ API problem

Hello Frank,

I did a small testing codes (see the following lines) on GEOS in my c++ API by following the sample test codes in http://trac.osgeo.org/gdal/browser/trunk/autotest/pymod/ogrtest.py.

......
//test GEOS
                OGRPoint *pnt1;
                pnt1->setX (10);
                pnt1->setY (20);

                OGRPoint *pnt2;
                pnt1->setX (30);
                pnt1->setY (20);

                OGRGeometry *result;
                result = pnt1->Union( pnt2 );

                if ( result == NULL )
                {
                   printf ("\tGEOS is not avaiable...\n");
                   exit ( 1 );
                }
                pnt1->empty();
                pnt2->empty();
.......

The program has segmentation fault when reaching Union.  So, I guess somehow GEOS is not installed or configured right.  Here is config info for my GDAL configuration:
....
config.log:ac_cv_lib_geos_c_GEOSversion=yes
config.log:ac_cv_prog_GEOS_CONFIG=/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/bin/geos-config
config.log:GEOS_CFLAGS='-I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/include'
config.log:GEOS_CONFIG='/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/bin/geos-config'
config.log:GEOS_LIBS='-L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/lib -lgeos_c'
config.log:GEOS_VERSION='3.3.6'
config.log:HAVE_GEOS='yes'
config.status:S["GEOS_CONFIG"]="/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/bin/geos-config"
config.status:S["GEOS_VERSION"]="3.3.6"
config.status:S["HAVE_GEOS"]="yes"
config.status:S["GEOS_CFLAGS"]="-I/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/include"
config.status:S["GEOS_LIBS"]="-L/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/geos-3.3.6/local/lib -lgeos_c"
.......

When I compiled  my C++ API,  if I just have: #include <geos.h>  and $(LIBPATH_GEOS) -lgeos, I get errors:
...
/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/gdal-1.9.1/ogr/ogrgeometryfactory.cpp:1481: undefined reference to `GEOSGeomTypeId'
/nas01/depts/ie/cempd/SA/sa_06_2009/src/libs/gdal-1.9.1/ogr/ogrgeometryfactory.cpp:1481: undefined reference to `GEOSisEmpty'
...

I can only compile my c++ codes with: #include <geos_c.h>  and  $(LIBPATH_GEOS) -lgeos_c -lgeos

Thank you very much,

Limei

-----Original Message-----
From: fwarmerdam at gmail.com [mailto:fwarmerdam at gmail.com] On Behalf Of Frank Warmerdam
Sent: Wednesday, December 19, 2012 8:51 PM
To: Ran, Limei
Subject: Re: [gdal-dev] FW: point within polygon c++ API problem

Limei,

For what it's worth, I looked over the code and your approach seemed fine.  I'm not sure why it crashed.  I didn't try to build a sample to demonstrate the problem on my tree.  If you could prepare a fairly self contained simple demonstration it might be possible to get more help.

Best regards,
Frank

On Wed, Dec 19, 2012 at 12:18 PM, Ran, Limei <lran at email.unc.edu> wrote:
> Hello Even,
>
>
>
> Since you helped me on GEOS compiling with GDAL, I think you may have
> ideas on what went wrong with my codes in the following.  Could you
> tell me what you think on my segmentation fault error?
>
>
>
> Thank you,
>
>
>
> Limei
>
>
>
>
>
> From: Ran, Limei
> Sent: Tuesday, December 18, 2012 3:21 PM
> To: gdal-dev at lists.osgeo.org
> Subject: point within polygon c++ API problem
>
>
>
> Hello,
>
>
>
> I am working on a C++ codes which reads polygon from polygon shapefile
> to see whether a point is within a polygon.  But, I got segmentation
> fault for pt.within (poGeometry).  Could you tell me where I may have errors.
>
>
>
> 1.      GDAL 1.9.1 is compiled with currect GEOS 3.3.6
>
> 2.      My c++ program compiled with GDAL.
>
> 3.      Here are my codes:
>
>
>
> ……
>
>       float x = grid.x[col];
>
>        float y = grid.y[row];
>
>
>
>        OGRPoint pt;
>
>         pt.setX( x );
>
>         pt.setY( y );
>
>
>
>         printf ("\tSet point\n");
>
>
>
>         OGRFeature *poFeature;
>
>         poLayer->ResetReading();
>
>
>
>         while( (poFeature = poLayer->GetNextFeature()) != NULL )
>
>         {
>
>            OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
>
>            int iField;
>
>
>
>            OGRGeometry *poGeometry;
>
>
>
>            poGeometry = poFeature->GetGeometryRef();
>
>            if( poGeometry != NULL && ( poGeometry->getGeometryType() )
> == wkbPolygon )
>
>            {
>
>               printf ("\tGot polygon\n");
>
>
>
>               if ( pt.Within ( poGeometry ) )
>
>               {  //find the polygon where point is within and get item
> values
>
>
>
>                  printf ("\tpoint in polygon\n");
>
> ………
>
>
>
> I got segmentation fault when processing gets:  if ( pt.Within (
> poGeometry
> ) )
>
>
>
>
>
> Thank you,
>
>
>
> Limei
>
>
>
>
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev



--
---------------------------------------+--------------------------------
---------------------------------------+------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Software Developer
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test_geos.cpp
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130103/787cd9dc/attachment.ksh>


More information about the gdal-dev mailing list