ogrcsvlayer.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 455 行 · 第 1/2 页

CPP
455
字号
    if( papszTokens == NULL )        return NULL;/* -------------------------------------------------------------------- *//*      Create the OGR feature.                                         *//* -------------------------------------------------------------------- */    OGRFeature *poFeature;    poFeature = new OGRFeature( poFeatureDefn );/* -------------------------------------------------------------------- *//*      Set attributes for any indicated attribute records.             *//* -------------------------------------------------------------------- */    int         iAttr;    int         nAttrCount = MIN(CSLCount(papszTokens),                                 poFeatureDefn->GetFieldCount() );        for( iAttr = 0; iAttr < nAttrCount; iAttr++)    {        poFeature->SetField( iAttr, papszTokens[iAttr] );    }    CSLDestroy( papszTokens );/* -------------------------------------------------------------------- *//*      Translate the record id.                                        *//* -------------------------------------------------------------------- */    poFeature->SetFID( nNextFID++ );    m_nFeaturesRead++;    return poFeature;}/************************************************************************//*                           GetNextFeature()                           *//************************************************************************/OGRFeature *OGRCSVLayer::GetNextFeature(){    OGRFeature  *poFeature = NULL;    if( bNeedRewind )        ResetReading();    /* -------------------------------------------------------------------- *//*      Read features till we find one that satisfies our current       *//*      spatial criteria.                                               *//* -------------------------------------------------------------------- */    while( TRUE )    {        poFeature = GetNextUnfilteredFeature();        if( poFeature == NULL )            break;        if( m_poAttrQuery == NULL || m_poAttrQuery->Evaluate( poFeature ) )            break;        delete poFeature;    }    return poFeature;}/************************************************************************//*                           TestCapability()                           *//************************************************************************/int OGRCSVLayer::TestCapability( const char * pszCap ){    if( EQUAL(pszCap,OLCSequentialWrite) )        return bInWriteMode;    else if( EQUAL(pszCap,OLCCreateField) )        return bNew && !bHasFieldNames;    else        return FALSE;}/************************************************************************//*                            CreateField()                             *//************************************************************************/OGRErr OGRCSVLayer::CreateField( OGRFieldDefn *poNewField, int bApproxOK ){/* -------------------------------------------------------------------- *//*      If we have already written our field names, then we are not     *//*      allowed to add new fields.                                      *//* -------------------------------------------------------------------- */    if( bHasFieldNames || !bNew )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Unable to create new fields after first feature written.");        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Does this duplicate an existing field?                          *//* -------------------------------------------------------------------- */    if( poFeatureDefn->GetFieldIndex( poNewField->GetNameRef() ) != -1 )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "Attempt to create field %s, but a field with this name already exists.",                  poNewField->GetNameRef() );        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Is this a legal field type for CSV?  For now we only allow      *//*      simple integer, real and string fields.                         *//* -------------------------------------------------------------------- */    switch( poNewField->GetType() )    {      case OFTInteger:      case OFTReal:      case OFTString:        // these types are OK.        break;      default:        if( bApproxOK )        {            CPLError( CE_Warning, CPLE_AppDefined,                       "Attempt to create field of type %s, but this is not supported\n"                      "for .csv files.  Just treating as a plain string.",                      poNewField->GetFieldTypeName( poNewField->GetType() ) );        }        else        {            CPLError( CE_Failure, CPLE_AppDefined,                       "Attempt to create field of type %s, but this is not supported\n"                      "for .csv files.",                      poNewField->GetFieldTypeName( poNewField->GetType() ) );            return OGRERR_FAILURE;        }    }    /* -------------------------------------------------------------------- *//*      Seems ok, add to field list.                                    *//* -------------------------------------------------------------------- */    poFeatureDefn->AddFieldDefn( poNewField );    return OGRERR_NONE;}/************************************************************************//*                           CreateFeature()                            *//************************************************************************/OGRErr OGRCSVLayer::CreateFeature( OGRFeature *poNewFeature ){    int iField;    bNeedRewind = TRUE;/* -------------------------------------------------------------------- *//*      Write field names if we haven't written them yet.               *//* -------------------------------------------------------------------- */    if( !bHasFieldNames )    {        for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )        {            char *pszEscaped;            if( iField > 0 )                fprintf( fpCSV, "%s", "," );            pszEscaped =                 CPLEscapeString( poFeatureDefn->GetFieldDefn(iField)->GetNameRef(),                                  -1, CPLES_CSV );            VSIFPrintf( fpCSV, "%s", pszEscaped );            CPLFree( pszEscaped );        }        if( bUseCRLF )            VSIFPutc( 13, fpCSV );        VSIFPutc( '\n', fpCSV );        bHasFieldNames = TRUE;    }/* -------------------------------------------------------------------- *//*      Make sure we are at the end of the file.                        *//* -------------------------------------------------------------------- */    VSIFSeek( fpCSV, 0, SEEK_END );/* -------------------------------------------------------------------- *//*      Write out all the field values.                                 *//* -------------------------------------------------------------------- */        for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )    {        char *pszEscaped;                if( iField > 0 )            fprintf( fpCSV, "%s", "," );                pszEscaped =             CPLEscapeString( poNewFeature->GetFieldAsString(iField),                             -1, CPLES_CSV );                VSIFWrite( pszEscaped, 1, strlen(pszEscaped), fpCSV );        CPLFree( pszEscaped );    }        if( bUseCRLF )        VSIFPutc( 13, fpCSV );    VSIFPutc( '\n', fpCSV );    return OGRERR_NONE;}/************************************************************************//*                              SetCRLF()                               *//************************************************************************/void OGRCSVLayer::SetCRLF( int bNewValue ){    bUseCRLF = bNewValue;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?