1

I am using below codes to try convert geometry from a Dgn file to shapefile. But, I can't exit the Feature reading loop.

Does anyone know why and how to fix it?

Please download the .dgn file from the link: DgnFile

bool Convert()
{
    OGRRegisterAll();
    OGRDataSource       *poDS,  *poSHPDS;
    const char *pszDriverName = "ESRI Shapefile";
    OGRSFDriver *poDriver;
    poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
                  pszDriverName );

    // Open file DGN
    poDS = OGRSFDriverRegistrar::Open( sourceFile, FALSE );

    if( poDS == NULL )

    {
         cout << "Error opeing DGN file." << endl;
         return false;
    } 
    // Create datasource for file shp
  poSHPDS = poDriver->CreateDataSource(destinationPath,NULL);

  if( poSHPDS == NULL )
  {
        cout << "Error creating shape file." << endl;
        return false;
  }     
  OGRLayer  *poLayer, *poSHPLayer; 

  // Get Layer from DGN file

  poLayer = poDS->GetLayer(0);

  // Create new Layer of SHP file

  poSHPLayer = poSHPDS->CreateLayer (shpFileName, NULL, wkbPolygon, NULL);

  if(poSHPLayer == NULL)

  {

          cout << "Error creating Layer of Shape File." << endl;

          return false;
  }

  OGRFeature *poFeature;

  poLayer->ResetReading(); 

  **while( (poFeature = poLayer->GetNextFeature()) != NULL ) // Can't exit this loop** 

  {

        OGRGeometry *poGeometry;
        poGeometry = poFeature->GetGeometryRef();

        // if feature is a Polygon
        if( poGeometry != NULL && (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon ) )
        {
                OGRFeature *poSHPFeature;
                //Create new Feature shape file
                poSHPFeature = OGRFeature::CreateFeature (poSHPLayer->GetLayerDefn());
                poSHPFeature->SetGeometry (poGeometry);
                OGRPolygon *poPolygon = (OGRPolygon*) poSHPFeature->GetGeometryRef();

                poLayer->SetSpatialFilter(poGeometry);
                poLayer->ResetReading ();

         }
         OGRFeature::DestroyFeature( poFeature );

    }
    OGRDataSource::DestroyDataSource( poDS );
    OGRDataSource::DestroyDataSource( poSHPDS );
    return true;
} 
  • Perhaps you don't want to reset the read inside the loop? (i.e. try removing poLayer->ResetReading ();) – BradHards Jun 1 '13 at 6:57
  • @BradHards : Do you know how to split polygons if there is a polygon contain another small polygon. Please see my image: mediafire.com/download/62b9u4752jj0v47/image.png . After converting from dgn to shp, I can't choose the small polygon. Thank you very much – taibc Jun 1 '13 at 8:21
  • That looks like a completely different question. – BradHards Jun 1 '13 at 22:42
  • @BradHards : I posted another question at here: gis.stackexchange.com/questions/62428/… – taibc Jun 2 '13 at 2:40
1

The ResetReading() call inside the loop is "taking you back to the start" on each iteration. You'll need to remove the

poLayer->ResetReading ();

line to continue iterating over the layer features.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.