OGRShapeDataSource::OpenFile

bool OGRShapeDataSource::OpenFile( const char *pszNewName, bool bUpdate )

{
    const char *pszExtension = CPLGetExtension( pszNewName );

    if( !EQUAL(pszExtension,”shp”) && !EQUAL(pszExtension,”shx”)
        && !EQUAL(pszExtension,”dbf”) )
        return false;

/* ——————————————————————– */
/*      SHPOpen() should include better (CPL based) error reporting,    */
/*      and we should be trying to distinguish at this point whether    */
/*      failure is a result of trying to open a non-shapefile, or       */
/*      whether it was a shapefile and we want to report the error      */
/*      up.                                                             */
/*                                                                      */
/*      Care is taken to suppress the error and only reissue it if      */
/*      we think it is appropriate.                                     */
/* ——————————————————————– */
    CPLPushErrorHandler( CPLQuietErrorHandler );
    SHPHandle hSHP = bUpdate ?
        DS_SHPOpen( pszNewName, “r+” ) :
        DS_SHPOpen( pszNewName, “r” );
    CPLPopErrorHandler();

    if( hSHP == nullptr
        && (!EQUAL(CPLGetExtension(pszNewName),”dbf”)
            || strstr(CPLGetLastErrorMsg(),”.shp”) == nullptr) )
    {
        CPLString osMsg = CPLGetLastErrorMsg();

        CPLError( CE_Failure, CPLE_OpenFailed, “%s”, osMsg.c_str() );

        return false;
    }
    CPLErrorReset();

/* ——————————————————————– */
/*      Open the .dbf file, if it exists.  To open a dbf file, the      */
/*      filename has to either refer to a successfully opened shp       */
/*      file or has to refer to the actual .dbf file.                   */
/* ——————————————————————– */
    DBFHandle hDBF = nullptr;
    if( hSHP != nullptr || EQUAL(CPLGetExtension(pszNewName), “dbf”) )
    {
        if( bUpdate )
        {
            hDBF = DS_DBFOpen( pszNewName, “r+” );
            if( hSHP != nullptr && hDBF == nullptr )
            {
                for( int i = 0; i < 2; i++ )
                {
                    VSIStatBufL sStat;
                    const char* pszDBFName =
                        CPLResetExtension(pszNewName,
                                          (i == 0 ) ? “dbf” : “DBF”);
                    VSILFILE* fp = nullptr;
                    if( VSIStatExL( pszDBFName, &sStat,
                                    VSI_STAT_EXISTS_FLAG) == 0 )
                    {
                        fp = VSIFOpenL(pszDBFName, “r+”);
                        if( fp == nullptr )
                        {
                            CPLError(
                                CE_Failure, CPLE_OpenFailed,
                                “%s exists, “
                                “but cannot be opened in update mode”,
                                pszDBFName );
                            SHPClose(hSHP);
                            return false;
                        }
                        VSIFCloseL(fp);
                        break;
                    }
                }
            }
        }
        else
        {
            hDBF = DS_DBFOpen( pszNewName, “r” );
        }
    }
    else
    {
        hDBF = nullptr;
    }

    if( hDBF == nullptr && hSHP == nullptr )
        return false;

/* ——————————————————————– */
/*      Create the layer object.                                        */
/* ——————————————————————– */
    OGRShapeLayer *poLayer =
        new OGRShapeLayer( this, pszNewName, hSHP, hDBF, nullptr, false, bUpdate,
                           wkbNone );
    poLayer->SetModificationDate(
        CSLFetchNameValue( papszOpenOptions, “DBF_DATE_LAST_UPDATE” ) );
    poLayer->SetAutoRepack(
        CPLFetchBool( papszOpenOptions, “AUTO_REPACK”, true ) );
    poLayer->SetWriteDBFEOFChar(
        CPLFetchBool( papszOpenOptions, “DBF_EOF_CHAR”, true ) );

/* ——————————————————————– */
/*      Add layer to data source layer list.                            */
/* ——————————————————————– */
    AddLayer(poLayer);

    return true;
}

转载自:https://blog.csdn.net/durongze/article/details/81369445

You may also like...

退出移动版