[gdal-dev] Clarification of OGR write tutorial code

Frank Warmerdam warmerdam at pobox.com
Tue Aug 24 13:33:40 EDT 2010


Tim Keitt wrote:
> 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?

Tim,

Yes, the same feature could be reused and the create/destroy moved
outside the call.  That is the call to OGRFeature::CreateFeature().
The call to OGRLayer::CreateFeature() must of course be within the loop.

Best regards,

>   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 );
>     }
> 
> 


-- 
---------------------------------------+--------------------------------------
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 Programmer for Rent



More information about the gdal-dev mailing list