[gdal-dev] creating a shape from another with attributes and values

Brent Fraser bfraser at geoanalytic.com
Tue Dec 4 07:42:12 PST 2012


You need to do a CreateField() on your destination, something like:

     poFDefn = poLayer->GetLayerDefn();
     for( int iField = 0; iField < poFDefn->GetFieldCount(); iField++ ){
         OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );

         if( poDstLayer->CreateField( poFieldDefn ) != OGRERR_NONE ){
             printf( "Creation of field failed.\n" );
         }
}

Best Regards,
Brent Fraser

On 12/4/2012 4:53 AM, SIVA RAMA KRISHNA wrote:
> To All,
>
> I am using the following code  for creating a shape file from another 
> with the attributes value;
> i am unable to fetch attribute values from source and change the 
> features from newly created
> shape files
>
>
>
> Any Sort of help is greatly appriciated
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> #include "main.h"
> #include<QList>
> #include<QStringList>
> #include<QDebug>
> //OGRFeature* TranslateFeature(OGRFeature* poSrcFeature,OGRFeatureDefn 
> *poFeatureDefn);
> OGRLayer            *poSrcLayer;
> OGRLayer            *poDstLayer;
> OGRDataSource       *poDS;
> OGRDataSource       *poODS=NULL;
> OGRFeature           *poDstFeature;
> OGRFeature           *poSrcFeature;
> OGRFeatureDefn       *poFeatureDefn;
>
> int main()
> {
>
>      int iField;
>       const char *pszDriverName = "ESRI Shapefile";
>        OGRSFDriver *poDriver;
>
>        OGRRegisterAll();
>        QList<double>fieldid;
>        QList<QString>fieldname;
>        const char* str="indiaaa" ;
>
>        poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
>                    pszDriverName );
>        if( poDriver == NULL )
>        {
>            printf( "%s driver not available.\n", pszDriverName );
>            exit( 1 );
>        }
>
>        poDS = poDriver->Open( "/home/support/Images/IND_adm 
> (3)/IND_adm1.shp" );
>        poSrcLayer=poDS->GetLayerByName("IND_adm1");
>        poDriver->DeleteDataSource("firsts1.shp");
>        poODS = poDriver->CreateDataSource( "firsts1.shp", NULL );
>        poDstLayer = poODS->CreateLayer( "firsts1",NULL,wkbPolygon);
>        poSrcLayer->ResetReading();
>        int i;
>        OGRGeometry *poGeometry;
>       // OGRGeometry *bufferGeometry;
>        poDstFeature = 
> OGRFeature::CreateFeature(poDstLayer->GetLayerDefn());
>        poDstLayer->CreateFeature(poDstFeature);
>        poSrcFeature=poSrcLayer->GetNextFeature();
>         do{
>             i=1;
>
>            OGRFeatureDefn *poFDefn = poSrcLayer->GetLayerDefn();
>                   int iField;
>
>                   for( iField = 0; iField < poFDefn->GetFieldCount(); 
> iField++ )
>                   {
> qDebug()<<"fieldcount"<<poFDefn->GetFieldCount();
>                       printf("\n");
>                       OGRFieldDefn *poFieldDefn = 
> poFDefn->GetFieldDefn( iField );
>
>                     if( poFieldDefn->GetType() == OFTInteger )
>                       {
>                           printf( "%d,", 
> poSrcFeature->GetFieldAsInteger( iField ) );
>
>
>                        }
>                       else if( poFieldDefn->GetType() == OFTReal )
>                       {
>                           printf( "%.3f,", 
> poSrcFeature->GetFieldAsDouble(iField) );
> fieldid.append(poSrcFeature->GetFieldAsDouble( iField ));
>
>                        }
>                       else if( poFieldDefn->GetType() == OFTString )
>                       {
>                           printf( "%s, Name", 
> poSrcFeature->GetFieldAsString(iField) );
> fieldname.append(poSrcFeature->GetFieldAsString(iField));
>
>                        }
>                       else
>                     {
>                           printf( "%s,name1", 
> poSrcFeature->GetFieldAsString(iField) );
> fieldname.append(poSrcFeature->GetFieldAsString(iField));
>
>                       }
>                   }
>                   poGeometry=poSrcFeature->GetGeometryRef();
> poDstFeature->SetGeometryDirectly(poGeometry);
>                   poDstLayer->CreateFeature(poDstFeature);
>                   poDstFeature->SetFID(100);
> //                  poDstFeature->SetField(i,str); // this is not 
> modifying
> //                  i++;
> //                  //poDstFeature->SetFrom(poSrcFeature,true);
>
>
>      }while( (poSrcFeature = poSrcLayer->GetNextFeature()) != NULL );
>
>
>
>            poDstLayer->ResetReading();
>
>            qDebug()<<"size"<<fieldname.size();
>
>              OGRDataSource::DestroyDataSource(poODS);
>      }
>
>
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20121204/6ceedd84/attachment-0001.html>


More information about the gdal-dev mailing list