ogrfmedatasource.cpp

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

CPP
1,716
字号
                         papszTokens[i], papszTokens[i+1] );            }        }    }/* -------------------------------------------------------------------- *//*      Do we already have a reader cached for this definition?         *//* -------------------------------------------------------------------- */    for( i = 0; i < nCachedConnectionCount; i++ )    {        if( strcmp(szDefinition, pasCachedConnections[i].pszDefinition) == 0 )            return;    }        /* -------------------------------------------------------------------- *//*      Added this reader to the cache.                                 *//* -------------------------------------------------------------------- */    CPLDebug( kPROVIDERNAME,               "Caching IFMEUniversalReader to maintain connection.\n"              "ReaderType=%s, Definition=%s",               pszReaderType, szDefinition );    nCachedConnectionCount++;    pasCachedConnections = (CachedConnection *)         CPLRealloc(pasCachedConnections,                    sizeof(CachedConnection) * nCachedConnectionCount);        pasCachedConnections[nCachedConnectionCount-1].poReader = poReader;    pasCachedConnections[nCachedConnectionCount-1].pszReaderType =         CPLStrdup(pszReaderType);    pasCachedConnections[nCachedConnectionCount-1].pszDefinition =         CPLStrdup(szDefinition);}/************************************************************************//*                      IsPartOfConnectionCache()                       *//*                                                                      *//*      I this reader being used to maintain a connection cache?        *//************************************************************************/int OGRFMEDataSource::IsPartOfConnectionCache( IFMEUniversalReader *poReader ){    int            i;        for( i = 0; i < nCachedConnectionCount; i++ )        if( poReader == pasCachedConnections[i].poReader )            return TRUE;    return FALSE;}/************************************************************************//*                           AcquireSession()                           *//*                                                                      *//*      Get unique ownership of the FME session for this thread.        *//************************************************************************/IFMESession *OGRFMEDataSource::AcquireSession(){    FME_MsgNum          err;/* -------------------------------------------------------------------- *//*      Create session mutex if we don't already have one.              *//* -------------------------------------------------------------------- */    if( hSessionMutex == NULL )    {        hSessionMutex = CPLCreateMutex();        CPLDebug( kPROVIDERNAME, "%p:Creating FME session, mutex=%d.",                   this, hSessionMutex );    }/* -------------------------------------------------------------------- *//*      Try to acquire ownership of the session, even if the session    *//*      doesn't yet exist.                                              *//* -------------------------------------------------------------------- */    else    {#ifdef DEBUG_MUTEX        CPLDebug( kPROVIDERNAME, "%p:Wait for session mutex.", this );#endif        if( !CPLAcquireMutex( hSessionMutex, 5.0 ) )        {            CPLDebug( kPROVIDERNAME, "%p:Failed to acquire session mutex in 5s.",                       this );        }#ifdef DEBUG_MUTEX        else            CPLDebug( kPROVIDERNAME, "%p:Got session mutex.", this );#endif    }/* -------------------------------------------------------------------- *//*      If the session doesn't exist, create it now.                    *//* -------------------------------------------------------------------- */    if( poSharedSession == NULL )    {#ifdef SUPPORT_INDIRECT_FMEDLL        FME_MsgNum (*pfnFME_CreateSession)( void * );        pfnFME_CreateSession = (FME_MsgNum (*)(void*))             CPLGetSymbol( FMEDLL_NAME, "FME_CreateSession" );        if( pfnFME_CreateSession == NULL )        {            CPLReleaseMutex( hSessionMutex );            CPLDebug( kPROVIDERNAME, "Unable to load FME_CreateSession from %s, skipping FME Driver.", FMEDLL_NAME );            return NULL;        }        err = pfnFME_CreateSession( (void *) (&poSharedSession) );#else        err = FME_createSession(poSharedSession);#endif        if( err )        {            poSharedSession = NULL;            CPLReleaseMutex( hSessionMutex );            CPLError( CE_Failure, CPLE_AppDefined,                      "Failed to create FMESession." );            return NULL;        }        // Dale Nov 26 '01 -- Set up to log "badnews" from FME        // to help track down problems        IFMEStringArray *poSessionDirectives =             poSharedSession->createStringArray();        if( poSessionDirectives == NULL )        {            err = 1;            CPLError( CE_Warning, CPLE_AppDefined,                       "Something has gone wonky with createStringArray() on the IFMESession.\n"                      "Is it possible you built with gcc 3.2 on Linux?  This seems problematic." );        }        else        {            poSessionDirectives->append("FME_DEBUG");            poSessionDirectives->append("BADNEWS");                        err = poSharedSession->init( poSessionDirectives );            poSharedSession->destroyStringArray( poSessionDirectives );            if( err )            {                CPLError( CE_Warning, CPLE_AppDefined,                          "Failed to initialize FMESession.\n%s",                          poSharedSession->getLastErrorMsg());            }        }        if( err )        {#ifdef SUPPORT_INDIRECT_FMEDLL            int (*pfnFME_destroySession)(void *);            pfnFME_destroySession = (int (*)(void*))                 CPLGetSymbol(FMEDLL_NAME, "FME_DestroySession" );            if( pfnFME_destroySession != NULL )                pfnFME_destroySession( (void *) (&poSharedSession) );#else            FME_destroySession( poSharedSession );#endif // def SUPPORT_INDIRECT_FMEDLL            poSharedSession = NULL;            CPLReleaseMutex( hSessionMutex );            return NULL;        }    }    return poSharedSession;}/************************************************************************//*                           ReleaseSession()                           *//*                                                                      *//*      Release the lock on the FME session.                            *//************************************************************************/void OGRFMEDataSource::ReleaseSession(){#ifdef DEBUG_MUTEX    CPLDebug( kPROVIDERNAME, "%p:Release session mutex.", this );#endif    CPLReleaseMutex( hSessionMutex );}/************************************************************************//*                           SerializeToXML()                           *//*                                                                      *//*      Convert the information about this datasource, and it's         *//*      layers into an XML format that can be stored in the             *//*      persistent feature cache index.                                 *//************************************************************************/CPLXMLNode *OGRFMEDataSource::SerializeToXML(){    CPLXMLNode      *psDS;    CPLAssert( bUseCaching );/* -------------------------------------------------------------------- *//*      Setup data source information.                                  *//* -------------------------------------------------------------------- */    psDS = CPLCreateXMLNode( NULL, CXT_Element, "DataSource" );    CPLCreateXMLElementAndValue( psDS, "Driver", pszReaderName );    CPLCreateXMLElementAndValue( psDS, "DSName", pszDataset );    CPLCreateXMLElementAndValue( psDS, "RefCount", "0" );    CPLCreateXMLElementAndValue( psDS, "CreationTime", "0" );    CPLCreateXMLElementAndValue( psDS, "LastUseTime", "0" );/* -------------------------------------------------------------------- *//*      Append all the FME user directives in force.                    *//* -------------------------------------------------------------------- */    CPLXMLNode *psUD;    psUD = CPLCreateXMLNode( psDS, CXT_Element, "UserDirectives" );    for( int i = 0; i < (int) poUserDirectives->entries(); i++ )        CPLCreateXMLElementAndValue( psUD, "Directive",                                      (*poUserDirectives)(i) );/* -------------------------------------------------------------------- *//*      Now append all the layer information.                           *//* -------------------------------------------------------------------- */    for( int iLayer = 0; iLayer < nLayers; iLayer++ )    {        OGRFMELayerCached * poLayer = (OGRFMELayerCached *) papoLayers[iLayer];        CPLXMLNode *psLayer;        psLayer = poLayer->SerializeToXML();        CPLAddXMLChild( psDS, psLayer );    }    return psDS;}/************************************************************************//*                         InitializeFromXML()                          *//************************************************************************/int OGRFMEDataSource::InitializeFromXML( CPLXMLNode *psDS ){    CPLAssert( bUseCaching );/* -------------------------------------------------------------------- *//*      Loop over layers, instantiating from the cached data.           *//* -------------------------------------------------------------------- */    CPLXMLNode *psLayerN;    for( psLayerN = psDS->psChild; psLayerN != NULL;          psLayerN = psLayerN->psNext )    {        OGRFMELayerCached *poNewLayer;        if( !EQUAL(psLayerN->pszValue,"OGRLayer") )            continue;        poNewLayer = new OGRFMELayerCached( this );/* -------------------------------------------------------------------- *//*      Initialize the layer from the XML.                              *//* -------------------------------------------------------------------- */        if( !poNewLayer->InitializeFromXML( psLayerN ) )        {            // this is *not* proper cleanup            CPLAssert( FALSE );            nLayers = 0;            return FALSE;        }/* -------------------------------------------------------------------- *//*      Assign the spatial index.  We should really change this to      *//*      check if it succeeds!                                           *//* -------------------------------------------------------------------- */        poNewLayer->AssignIndex(             CPLGetXMLValue( psLayerN, "SpatialCacheName",                             "<missing cachename>" ),            NULL, NULL );/* -------------------------------------------------------------------- *//*      Add the layer to the layer list.                                *//* -------------------------------------------------------------------- */        papoLayers = (OGRFMELayer **)            CPLRealloc(papoLayers, sizeof(void*) * ++nLayers );        papoLayers[nLayers-1] = poNewLayer;    }    return TRUE;}/************************************************************************//*                         FME2OGRSpatialRef()                          *//*                                                                      *//*      Translate an FME coordinate system into an                      *//*      OGRSpatialReference using the coordinate system manager         *//*      getCoordSysAsOGCDef() method.  We assume the session has        *//*      already been acquired.                                          *//************************************************************************/OGRSpatialReference *OGRFMEDataSource::FME2OGRSpatialRef( const char *pszCoordsys ){    IFMEString *poOGCDef;    poOGCDef = poSession->createString();    poSession->coordSysManager()->getCoordSysAsOGCDef(         pszCoordsys, *poOGCDef );    char *pszWKT = (char *) poOGCDef->data();    OGRSpatialReference oSRS;    if( oSRS.importFromWkt( &pszWKT ) == OGRERR_NONE )    {        poSession->destroyString( poOGCDef );        return oSRS.Clone();    }    else    {        poSession->destroyString( poOGCDef );        return NULL;    }}

⌨️ 快捷键说明

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