[gdal-dev] Error trying to use OGR tutorial code

allie92187 allison.hoch at ll.mit.edu
Fri Mar 5 14:52:22 EST 2010


I am trying to get some code from http://www.gdal.org/ogr/ogr_apitut.html. I
modified the code slightly to use a shapefile I had containing two points
since the shapefile in the code is not provided. The code below is the code
taken from above website with changes marked by comments. The problem is
occurring in the line if( poGeometry != NULL 
            && wkbFlatten(poGeometry->getGeometryType()) ==
wkbMultiLineString ) .

When I was attempting to debug I added a print statement which caused the
program to terminate successfully, but when I remove this print statement 
(printf("%p\n", poGeometry);) the above line causes a seg fault.

Does anyone know why this is happening? It doesn't seem to be a problem with
earlier versions of gdal (1.4)


Thank you!

#include "ogrsf_frmts.h"
#include <iostream>
using namespace std;

int main()

{
    OGRRegisterAll();

    OGRDataSource       *poDS;

    //I changed the name of the shape file here:
    poDS = OGRSFDriverRegistrar::Open( "targets.shp", FALSE );
    if( poDS == NULL )
    {
        printf( "Open failed.\n" );
        exit( 1 );
    }
  
    OGRLayer  *poLayer;

    //I changed the name of the layer name here:
    poLayer = poDS->GetLayerByName( "targets" );

    OGRFeature *poFeature;
    poLayer->ResetReading();
    while( (poFeature = poLayer->GetNextFeature()) != NULL )
    {
        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
        int iField;
        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
        {
            OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );

            if( poFieldDefn->GetType() == OFTInteger )
                printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
            else if( poFieldDefn->GetType() == OFTReal )
                printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
            else if( poFieldDefn->GetType() == OFTString )
                printf( "%s,", poFeature->GetFieldAsString(iField) );
            else
                printf( "%s,", poFeature->GetFieldAsString(iField) );
        }
        OGRGeometry *poGeometry;
        poGeometry = poFeature->GetGeometryRef();

        //If I take  out this line that I added when decoding, there is a
seg fault - this really strange!
        printf("%p\n", poGeometry);

        //Here I'm checking for wkbMultiLineString instead of wkbPoint as
was in the original code
        if( poGeometry != NULL 
            && wkbFlatten(poGeometry->getGeometryType()) ==
wkbMultiLineString )  
        {
            OGRMultiLineString *mls = (OGRMultiLineString *)poGeometry;
            //This next line I added, but when it is uncommented there is a
segfault in the line below thta calls DestroyFeature. 
            //I'm not sure why this is happening.
            //printf("name: %s\n", poGeometry->getGeometryName());               


            //OGRPoint *poPoint = (OGRPoint *) poGeometry;                      
(Removed from original code)
            //printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );        
(Removed from original code)
        }
        else
        {
            printf( "no point geometry\n" );
        }       
        OGRFeature::DestroyFeature( poFeature );
    }
    OGRDataSource::DestroyDataSource( poDS ); }

-- 
View this message in context: http://n2.nabble.com/Error-trying-to-use-OGR-tutorial-code-tp4683021p4683021.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list