📄 ogrfmedatasource.cpp
字号:
/* -------------------------------------------------------------------- */ for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; CPLFree( papoLayers );/* -------------------------------------------------------------------- *//* If we have a cached instances, decrement the reference count. *//* -------------------------------------------------------------------- */#ifdef SUPPORT_PERSISTENT_CACHE { OGRFMECacheIndex oCacheIndex( CPLFormFilename(GetTmpDir(), "ogrfmeds", "ind" ) ); if( pszReaderName != NULL && nLayers > 0 && bUseCaching && oCacheIndex.Lock() && oCacheIndex.Load() ) { CPLXMLNode *psMatchDS = NULL; psMatchDS = oCacheIndex.FindMatch( pszReaderName, pszDataset, *poUserDirectives ); if( psMatchDS != NULL ) oCacheIndex.Dereference( psMatchDS ); if( oCacheIndex.ExpireOldCaches( poSession ) || psMatchDS != NULL ) oCacheIndex.Save(); oCacheIndex.Unlock(); } }#endif /* def SUPPORT_PERSISTENT_CACHE *//* -------------------------------------------------------------------- *//* Cleanup up various resources. *//* -------------------------------------------------------------------- */ if( poFMEFeature != NULL ) poSession->destroyFeature( poFMEFeature ); if( poUserDirectives != NULL ) poSession->destroyStringArray( poUserDirectives ); if( poReader != NULL ) { if( !IsPartOfConnectionCache( poReader ) ) poSession->destroyReader( poReader ); else CPLDebug( kPROVIDERNAME, "Preserving cached reader on destructor"); } if( poSession != NULL ) { if( --nSharedSessionRefCount == 0 ) {#ifdef SUPPORT_CLEANUP_SESSION#ifdef SUPPORT_INDIRECT_FMEDLL int (*pfnFME_destroySession)(void *); pfnFME_destroySession = (int (*)(void*)) CPLGetSymbol(FMEDLL_NAME, "FME_DestroySession" ); if( pfnFME_destroySession == NULL ) CPLError( CE_Warning, CPLE_AppDefined, "Failed to fetch FME_DestroySession entry point." ); else pfnFME_destroySession( (void *) (&poSession) );#else FME_destroySession( poSession );#endif // def SUPPORT_INDIRECT_FMEDLL poSharedSession = NULL;#else // ndef SUPPORT_CLEANUP_SESSION CPLDebug( kPROVIDERNAME, "no active datasources left, but preserving session." );#endif } } CPLFree( pszName ); CPLFree( pszDataset ); CPLFree( pszReaderName ); ReleaseSession();}/************************************************************************//* PromptForSource() *//************************************************************************/char *OGRFMEDataSource::PromptForSource(){ IFMEDialog *poDialog = NULL; IFMEString *poSourceFormat, *poSourceDSName; char *pszResult = NULL; poSourceFormat = poSession->createString(); poSourceDSName = poSession->createString(); if( poSession->createDialog( poDialog ) != 0 ) return NULL; poUserDirectives->append( "SPATIAL_SETTINGS" ); poUserDirectives->append( "no" ); if( poDialog->sourcePrompt( NULL, NULL, *poSourceFormat, *poSourceDSName, *poUserDirectives ) ) { pszResult = CPLStrdup(CPLSPrintf("%s:%s", poSourceFormat->data(), poSourceDSName->data())); } poSession->destroyString( poSourceFormat ); poSession->destroyString( poSourceDSName ); return pszResult;}/************************************************************************//* ReadFileSource() *//************************************************************************/char *OGRFMEDataSource::ReadFileSource( const char *pszFilename ){ FILE *fp; char **papszLines = NULL; const char *pszLine;/* -------------------------------------------------------------------- *//* Read the definition file. *//* -------------------------------------------------------------------- */ fp = VSIFOpen( pszFilename, "rt" ); if( fp == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to open file %s.", pszFilename ); return NULL; } while( (pszLine = CPLReadLine(fp)) != NULL ) { if( *pszLine != '#' ) papszLines = CSLAddString( papszLines, pszLine ); } VSIFClose( fp );/* -------------------------------------------------------------------- *//* verify minimal requirements. *//* -------------------------------------------------------------------- */ if( CSLCount(papszLines) < 2 ) { CPLError( CE_Failure, CPLE_AppDefined, "Insufficient lines in FME Data Definition file." "At least a readername and data source name is required." ); 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 ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -