[gdal-dev] Add a new filed to an existing shapefile

Limei Ran lran at unc.edu
Fri May 16 09:19:08 EDT 2008


Hi:

I am trying to add a new double field to an existing shapefile using the 
following codes.  But,  poLayer->CreateField failed.  Could you tell me 
what is wrong with the codes?

Thanks,

Limei

================================================
 //open output polygon shapefile
   poDS = OGRSFDriverRegistrar::Open( out_polyFile.c_str(), TRUE );
   if( poDS == NULL )
   {
       printf( "\tOpen shapefile file failed: %s.\n", 
out_polyFile.c_str() );
       exit( 1 );
   }
   poLayer = poDS->GetLayer( 0 );
   if( poLayer == NULL )
   {
      printf( "\tFAILURE: Couldn't fetch layer %s!\n", 
out_polyFile.c_str() );
      exit( 1 );
   }

   //create weight item to the attribute
   OGRFieldDefn oField( itemName.c_str(), OFTReal);
   printf ("\tBuiled field item: %s\n", itemName.c_str() );

   if( poLayer->CreateField( &oField, TRUE ) != OGRERR_NONE )
   {
      printf( "\tFailed to create field: %s\n",itemName.c_str() );
      exit( 1 );
   }

===============================


More information about the gdal-dev mailing list