ogrfmedatasource.cpp

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

CPP
1,716
字号
        return NULL;    }    /* -------------------------------------------------------------------- *//*      Apply extra values to user directives.                          *//* -------------------------------------------------------------------- */    int            i;    for( i = 2; papszLines[i] != NULL; i++ )        poUserDirectives->append( papszLines[i] );/* -------------------------------------------------------------------- *//*      Prepare reader:dataset response string.                         *//* -------------------------------------------------------------------- */    char     *pszReturn;    pszReturn = CPLStrdup(CPLSPrintf("%s:%s", papszLines[0], papszLines[1] ));    CSLDestroy( papszLines );    return pszReturn;}/************************************************************************//*                         SaveDefinitionFile()                         *//************************************************************************/static void SaveDefinitionFile( const char *pszFilename,                                 const char *pszReader,                                 const char *pszDatasource,                                IFMEStringArray &oUserDirectives ){    FILE     *fp;    int      i;    fp = VSIFOpen( CPLResetExtension( pszFilename, "fdd" ), "wt" );    if( fp == NULL )        return;    fprintf( fp, "%s\n", pszReader );    fprintf( fp, "%s\n", pszDatasource );    for( i = 0; i < (int) oUserDirectives.entries(); i++ )    {        fprintf( fp, "%s\n", oUserDirectives(i) );    }    VSIFClose( fp );}/************************************************************************//*                             ExtractSRS()                             *//************************************************************************/OGRSpatialReference *OGRFMEDataSource::ExtractSRS(){/* -------------------------------------------------------------------- *//*      Try to find the COORDSYS in the user directives.                *//* -------------------------------------------------------------------- */    const char *pszCoordSys = NULL;    for( int i = 0; i < (int) poUserDirectives->entries(); i += 2 )    {        if( EQUAL((*poUserDirectives)(i),"COORDSYS") )            pszCoordSys = (*poUserDirectives)(i+1);    }    if( pszCoordSys == NULL || strlen(pszCoordSys) == 0 )        return NULL;/* -------------------------------------------------------------------- *//*      Translate FME name to an OGRSpatialReference.                   *//* -------------------------------------------------------------------- */    return FME2OGRSpatialRef( pszCoordSys );}/************************************************************************//*                                Open()                                *//************************************************************************/int OGRFMEDataSource::Open( const char * pszCompositeName ){    FME_MsgNum          err;    CPLAssert( poSession == NULL );  // only open once/* -------------------------------------------------------------------- *//*      Do some initial validation.  Does this even look like it        *//*      could plausibly be an FME suitable name?  We accept PROMPT:,    *//*      <reader>: or anything ending in .fdd as a reasonable candidate. *//* -------------------------------------------------------------------- */    int  i;    for( i = 0; pszCompositeName[i] != ':' && pszCompositeName[i] != '\0'; i++)    {        if( pszCompositeName[i] == '/' || pszCompositeName[i] == '\\'            || pszCompositeName[i] == '.' )            break;    }             if( (i < 2 || pszCompositeName[i] != ':'          || EQUALN(pszCompositeName,"OCI:",4)         || EQUALN(pszCompositeName,"gltp:",5)         || EQUALN(pszCompositeName,"http",4)         || EQUALN(pszCompositeName,"DODS:",5)         || EQUALN(pszCompositeName,"ODBC:",5)         || EQUALN(pszCompositeName,"MYSQL:",5))        && !EQUAL(CPLGetExtension( pszCompositeName ), "fdd")        && !EQUALN(pszCompositeName,"PROMPT",6) )    {        CPLDebug( kPROVIDERNAME,                   "OGRFMEDataSource::Open(%s) don't try to open via FME.",                   pszCompositeName );        return FALSE;    }    CPLDebug( kPROVIDERNAME, "OGRFMEDataSource::Open(%s):%p/%d",               pszCompositeName, this, CPLGetPID() );/* -------------------------------------------------------------------- *//*      Create an FME Session.                                          *//* -------------------------------------------------------------------- */    poSession = AcquireSession();    if( poSession == NULL )        return FALSE;    nSharedSessionRefCount++;    CPLDebug( kPROVIDERNAME, "%p:acquired session", this );    poUserDirectives = poSession->createStringArray();/* -------------------------------------------------------------------- *//*      Redirect FME log messages through CPLDebug().                   *//* -------------------------------------------------------------------- */    IFMELogFile   *poLogFile = poSession->logFile();    poLogFile->setFileName( NULL, FME_FALSE );    poLogFile->setCallBack( FME_Logger );    CPLDebug( kPROVIDERNAME, "%p:reset logfile", this );/* -------------------------------------------------------------------- *//*      Prompt for a source, if none is provided.                       *//* -------------------------------------------------------------------- */    if( EQUAL(pszCompositeName,"") || EQUALN(pszCompositeName,"PROMPT",6) )    {        pszName = PromptForSource();        if( pszName == NULL )        {            ReleaseSession();            return FALSE;        }    }    else if( CPLGetExtension( pszCompositeName ) != NULL             && EQUAL(CPLGetExtension( pszCompositeName ),"fdd") )    {        pszName = ReadFileSource(pszCompositeName);        if( pszName == NULL )        {            ReleaseSession();            return FALSE;        }    }    else    {        pszName = CPLStrdup( pszCompositeName );    }/* -------------------------------------------------------------------- *//*      Extract the reader name and password compontents.  The          *//*      reader name will be followed by a single colon and then the     *//*      FME DATASET name.                                               *//* -------------------------------------------------------------------- */    for( i = 0; pszName[i] != '\0' && pszName[i] != ':'; i++ ) {}    if( pszName[i] == '\0' || i < 2 )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Failed to parse reader and data source from:\n%s",                   pszName );        ReleaseSession();        return FALSE;    }    pszReaderName = CPLStrdup( pszName );    pszReaderName[i] = '\0';    pszDataset = CPLStrdup(pszName + i + 1);    CPLDebug( kPROVIDERNAME, "%s:parsed out dataset", pszDataset );/* -------------------------------------------------------------------- *//*      If we prompted for a defintion that includes a file to save     *//*      it to, do the save now.                                         *//* -------------------------------------------------------------------- */    if( EQUALN(pszCompositeName,"PROMPT:",7)         && strlen(pszCompositeName) > 7 )    {        SaveDefinitionFile( pszCompositeName+7,                             pszReaderName, pszDataset,                             *poUserDirectives );    }/* -------------------------------------------------------------------- *//*      Is there a Coordsys statement in the user directives?           *//* -------------------------------------------------------------------- */    OGRSpatialReference *poSRS = ExtractSRS();    CPLDebug( kPROVIDERNAME, "got the SRS parsed");    bCoordSysOverride = poSRS != NULL;/* -------------------------------------------------------------------- *//*      Allocate an FME string, and feature for use here and            *//*      elsewhere.                                                      *//* -------------------------------------------------------------------- */    poFMEFeature = poSession->createFeature();    poFMEString = poSession->createString();/* -------------------------------------------------------------------- *//*      Are we going to use the direct access DB mechanism, or the      *//*      spatiallly cached (dumb reader) mechanism.                      *//* -------------------------------------------------------------------- */    bUseCaching = !EQUALN(pszReaderName,"SDE",3)                && !EQUALN(pszReaderName,"ORACLE",6);/* -------------------------------------------------------------------- *//*      Is there already a cache for this dataset?  If so, we will      *//*      use it.                                                         *//* -------------------------------------------------------------------- */#ifdef SUPPORT_PERSISTENT_CACHE    OGRFMECacheIndex   oCacheIndex(                            CPLFormFilename(GetTmpDir(), "ogrfmeds", "ind" ) );    CPLXMLNode        *psMatchDS = NULL;    if( bUseCaching && oCacheIndex.Lock() && oCacheIndex.Load() )    {        int bNeedSave = oCacheIndex.ExpireOldCaches( poSession );        psMatchDS = oCacheIndex.FindMatch( pszReaderName, pszDataset,                                            *poUserDirectives );        if( psMatchDS != NULL )        {            oCacheIndex.Reference( psMatchDS );            oCacheIndex.Save();            psMatchDS = CPLCloneXMLTree( psMatchDS );            oCacheIndex.Unlock();        }        else        {            if( bNeedSave )                oCacheIndex.Save();            oCacheIndex.Unlock();        }        if( psMatchDS != NULL )        {            if( InitializeFromXML( psMatchDS ) )            {                CPLDestroyXMLNode( psMatchDS );                ReleaseSession();                return TRUE;            }            CPLDestroyXMLNode( psMatchDS );        }    }#endif /* def SUPPORT_PERSISTENT_CACHE *//* -------------------------------------------------------------------- *//*      Create a reader.                                                *//* -------------------------------------------------------------------- */    IFMEStringArray     *poParms = poSession->createStringArray();    for( i = 0; i < (int) poUserDirectives->entries(); i++ )        CPLDebug( kPROVIDERNAME, "oUserDirectives(%d) = '%s'",                   i, (*poUserDirectives)(i) );    poReader = poSession->createReader(pszReaderName, FME_FALSE,                                        poUserDirectives);    if( poReader == NULL )    {        CPLFMEError( poSession,                     "Failed to create reader of type `%s'.\n",                     pszReaderName );        ReleaseSession();        return FALSE;    }    CPLDebug( kPROVIDERNAME, "%p:reader created.", this );/* -------------------------------------------------------------------- *//*      Now try to open the dataset.                                    *//* -------------------------------------------------------------------- */    err = poReader->open( pszDataset, *poParms );    if( err )    {        CPLFMEError( poSession,                     "Failed to open dataset `%s' with reader of type `%s'.\n",                     pszDataset, pszReaderName );        ReleaseSession();        return FALSE;    }    CPLDebug( kPROVIDERNAME, "%p:reader opened.", this );/* -------------------------------------------------------------------- *//*      There are some circumstances where we want to keep a            *//*      "connection" open for a data source.  Offer this reader for     *//*      connection caching.                                             *//* -------------------------------------------------------------------- */    OfferForConnectionCaching( poReader, pszReaderName,                                pszDataset );/* -------------------------------------------------------------------- *//*      Create a layer for each schema feature.                         *//* -------------------------------------------------------------------- */    FME_Boolean         eEndOfSchema;    while( TRUE )    {        err = poReader->readSchema( *poFMEFeature, eEndOfSchema );        if( err )        {            CPLFMEError( poSession, "IFMEReader::readSchema() failed." );            ReleaseSession();            return FALSE;        }        if( eEndOfSchema == FME_TRUE )            break;        CPLDebug( kPROVIDERNAME, "%p:readSchema() got %s.",                   this, poFMEFeature->getFeatureType() );

⌨️ 快捷键说明

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