[gdal-dev] Clarification of OGR write tutorial code

Even Rouault even.rouault at mines-paris.org
Tue Aug 24 14:00:08 EDT 2010


Tim,

yes you could likely move the OGRFeature::CreateFeature / 
OGRFeature::DestroyFeature out of the loop. If you do this, depending on the 
underlying OGR driver, you'll probably need doing a poFeature-
>SetFID(OGRNullFID) before poLayer->CreateFeature(), since the previous call 
to CreateFeature() would have set the FID to a non null-value and 
CreateFeature() might not like seeing a non-null FID the next time.

Le mardi 24 août 2010 19:14:32, Tim Keitt a écrit :
> I made my first foray into OGR today and want to be sure I understand
> the following code from the tutorial.
> 
> In particular, I am wondering why the feature is created and destroyed
> in each loop. Could one instead create a single feature object and
> simply update its x and y values repeatedly? Clearly the feature
> created in the layer does not reference poFeature, because it is
> destroyed, right?  Can the OGRFeature::CreateFeature and
> OGRFeature::DestroyFeature calls be moved outside the loop?
> 
> Thanks.
> 
> THK
> 
>   while( !feof(stdin)
>            && fscanf( stdin, "%lf,%lf,%32s", &x, &y, szName ) == 3 )
>     {
>         OGRFeature *poFeature;
> 
>         poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
>         poFeature->SetField( "Name", szName );
> 
>         OGRPoint pt;
> 
>         pt.setX( x );
>         pt.setY( y );
> 
>         poFeature->SetGeometry( &pt );
> 
>         if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
>         {
>            printf( "Failed to create feature in shapefile.\n" );
>            exit( 1 );
>         }
> 
>          OGRFeature::DestroyFeature( poFeature );
>     }


More information about the gdal-dev mailing list