[gdal-dev] Can't convert geometry from Dgn file to Shape file ?

taibc taibc_coltech at yahoo.com
Fri May 31 20:18:09 PDT 2013


Hi everyone,

I am using below codes to try convert geometry from a Dgn file to Shape
file. But, I can't exit the FeatureReading loop:

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

Please find my attached dgn file.  Dc19.dgn
<http://osgeo-org.1560.x6.nabble.com/file/n5057267/Dc19.dgn>  

Are there anyone know why and how to fix it ?

Thanks and regards,
Tai



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Can-t-convert-geometry-from-Dgn-file-to-Shape-file-tp5057267.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list