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

SIVA RAMA KRISHNA s.r.kriishna at gmail.com
Tue Dec 4 03:53:01 PST 2012


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);
     }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20121204/077ffc91/attachment.html>


More information about the gdal-dev mailing list