OGR can't read all attributes of the features

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

OGR can't read all attributes of the features

zhzhsh
I want to use OGR to read S57 file. now I can only read the attributes all features have,can't read the special attributes.For example, I read the DEPARE, I can only read the attributes such as:RCID, PRIM, GRUP, OBJL, RVER, AGEN, FIDN, FIDS, LNAM, LANM_REFS, FFPT_RIND, now I can't get the attributes such as: DRVAL1; DRVAL2; QUASOU; SOUACC; VERDAT. How I can get these?(my code followed:)

int main()
{
    OGRRegisterAll();
       
    OGRDataSourceH       poDS;
    OGRSFDriverH ogrDriver;

    poDS = (OGRDataSourceH) OGRSFDriverRegistrar::Open( "Gb5x01sw.000" , FALSE , (OGRSFDriver**)&ogrDriver);
    if( poDS == NULL )
    {
        printf( "Open failed.\n" );
        exit( 1 );
    }
    pStr = ((OGRSFDriver*)ogrDriver)->GetName();

    OGRLayer  *poLayer;

    int poLayers = ((OGRDataSource*)poDS)->GetLayerCount()
    for (int i=0; i < ((OGRDataSource*)poDS)->GetLayerCount(); i++)
    {
        poLayer = ((OGRDataSource*)poDS)->GetLayer(i);
        OGRFeature *poFeature;
        int nFeature = poLayer->GetFeatureCount();
        poLayer->ResetReading();
               
        while( (poFeature = poLayer->GetNextFeature()) != NULL )
        {
            OGRFeatureDefn *poFDefn = poFeature->GetDefnRef();
            int iField;

            for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
            {
                OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
               
                if( poFieldDefn->GetType() == OFTInteger )
                    printf( "(%s->%d)\n,", pStr,poFeature->GetFieldAsInteger( iField ) );
                else if( poFieldDefn->GetType() == OFTReal )
                    printf( "(%s->%.3f)\n,", pStr,poFeature->GetFieldAsDouble(iField) );
                else if( poFieldDefn->GetType() == OFTString )
                    printf( "(%s->%s)\n,", pStr,poFeature->GetFieldAsString(iField) );
                else
                    printf( "(%s->%s)\n,", pStr,poFeature->GetFieldAsString(iField) );
            }
               
            OGRGeometry *poGeometry;
            poGeometry = poFeature->GetGeometryRef();
            if (poGeometry != NULL)
            {
                pStr = poGeometry->getGeometryName();
                if( poGeometry != NULL
                 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
                {
                }
                               
                if( poGeometry != NULL
                  && wkbFlatten(poGeometry->getGeometryType()) == wkbLineString )
                {
                }
                if( poGeometry != NULL
                  && wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon)
                {

                }

                else
                {
                    printf( "no point geometry\n" );
                }
                OGRFeature::DestroyFeature( poFeature );
            }
    }
    OGRDataSource::DestroyDataSource( ((OGRDataSource*)poDS) );
    return 0;
}

By the way, I have another question:

when I used the code above I can only read about 6 layers, but with QGIS I can  read 65 layers, I checked the code of QGIS, QGIS also use OGR to parse S57. such as:
  for ( unsigned int i = 0; i < layerCount() ; i++ )
  {
    QString theLayerName = QString::fromLocal8Bit(OGR_FD_GetName(OGR_L_GetLayerDefn(OGR_DS_GetLayer(ogrDataSource, i))));
    OGRwkbGeometryType layerGeomType = OGR_FD_GetGeomType(OGR_L_GetLayerDefn(OGR_DS_GetLayer(ogrDataSource, i)));

    int theLayerFeatureCount = OGR_L_GetFeatureCount(OGR_DS_GetLayer(ogrDataSource, i), 1);

    QString geom;
    switch ( layerGeomType )
    {
      case wkbUnknown:            geom = "Unknown"; break;
      case wkbPoint:              geom = "Point"; break;
      case wkbLineString:         geom = "LineString"; break;
      case wkbPolygon:            geom = "Polygon"; break;
      case wkbMultiPoint:         geom = "MultiPoint"; break;
      case wkbMultiLineString:    geom = "MultiLineString"; break;
      case wkbGeometryCollection: geom = "GeometryCollection"; break;
      case wkbNone:               geom = "None"; break;
      case wkbPoint25D:           geom = "Point25D"; break;
      case wkbLineString25D:      geom = "LineString25D"; break;
      case wkbPolygon25D:         geom = "Polygon25D"; break;
      case wkbMultiPoint25D:      geom = "MultiPoint25D"; break;
      case wkbMultiLineString25D: geom = "MultiLineString25D"; break;
      case wkbMultiPolygon25D:    geom = "MultiPolygon25D"; break;
      default: geom="Unknown WKB: " + QString::number( layerGeomType );
    }
    theList.append( QString::number( i ) + ":" + theLayerName + ":" + QString::number( theLayerFeatureCount ) + ":" + geom );
  }
  return theList;
}
the function layercount() can read 65 layers ,I can't understand.

Hope someone help me!
Thank you very much!