ReaderWriterOGR() { supportsExtension("ogr","OGR file reader"); supportsOption("useRandomColorByFeature", "Assign a random color to each feature."); supportsOption("addGroupPerFeature", "Places each feature in a separate group."); oldHandler = CPLSetErrorHandler(CPLOSGErrorHandler); }
rspfGdalFactory* rspfGdalFactory::instance() { if(!theInstance) { theInstance = new rspfGdalFactory; CPLSetErrorHandler((CPLErrorHandler)CPLQuietErrorHandler); GDALAllRegister(); OGRRegisterAll(); } return theInstance; }
SEXP RGDAL_Init(void) { CPLSetErrorHandler((CPLErrorHandler)__errorHandler); GDALAllRegister(); OGRRegisterAll(); return(R_NilValue); }
void CPLPopErrorHandler() { if( psHandlerStack != NULL ) { CPLErrorHandlerNode *psNode = psHandlerStack; psHandlerStack = psNode->psNext; CPLSetErrorHandler( psNode->pfnHandler ); VSIFree( psNode ); } }
void CPLPushErrorHandler( CPLErrorHandler pfnErrorHandler ) { CPLErrorHandlerNode *psNode; psNode = (CPLErrorHandlerNode *) VSIMalloc(sizeof(CPLErrorHandlerNode)); psNode->psNext = psHandlerStack; psNode->pfnHandler = gpfnCPLErrorHandler; psHandlerStack = psNode; CPLSetErrorHandler( pfnErrorHandler ); }
bool OGRGDALImportExtension::initialize() { std::string DirStr; OPENFLUID_GetRunEnvironment("dir.input",DirStr); m_InputDir = QString::fromStdString(DirStr); OPENFLUID_GetRunEnvironment("dir.temp",DirStr); m_TempDir = QString::fromStdString(DirStr); CPLSetErrorHandler(CPLOGRGDALErrorHandler); OGRRegisterAll(); if (OGRGetDriverByName("WFS") == NULL) ui->AddWFSButton->setEnabled(false); return true; }
// Set the ogr handler. void simplet_error_init(){ CPLSetErrorHandler(ogr_error_handler); }
Log::Log() { _level = Log::Info; qInstallMsgHandler(myLoggerFunction); CPLSetErrorHandler(cplErrorHandler); }
int FindSRS( const char *pszInput, OGRSpatialReference &oSRS, int bDebug ) { int bGotSRS = FALSE; VSILFILE *fp = NULL; GDALDataset *poGDALDS = NULL; OGRDataSource *poOGRDS = NULL; OGRLayer *poLayer = NULL; char *pszProjection = NULL; CPLErrorHandler oErrorHandler = NULL; int bIsFile = FALSE; OGRErr eErr = CE_None; /* temporarily supress error messages we may get from xOpen() */ if ( ! bDebug ) oErrorHandler = CPLSetErrorHandler ( CPLQuietErrorHandler ); /* Test if argument is a file */ fp = VSIFOpenL( pszInput, "r" ); if ( fp ) { bIsFile = TRUE; VSIFCloseL( fp ); CPLDebug( "gdalsrsinfo", "argument is a file" ); } /* try to open with GDAL */ CPLDebug( "gdalsrsinfo", "trying to open with GDAL" ); poGDALDS = (GDALDataset *) GDALOpen( pszInput, GA_ReadOnly ); if ( poGDALDS != NULL && poGDALDS->GetProjectionRef( ) != NULL ) { pszProjection = (char *) poGDALDS->GetProjectionRef( ); if( oSRS.importFromWkt( &pszProjection ) == CE_None ) { CPLDebug( "gdalsrsinfo", "got SRS from GDAL" ); bGotSRS = TRUE; } GDALClose( (GDALDatasetH) poGDALDS ); if ( ! bGotSRS ) CPLDebug( "gdalsrsinfo", "did not open with GDAL" ); } #ifdef OGR_ENABLED /* if unsuccessful, try to open with OGR */ if ( ! bGotSRS ) { CPLDebug( "gdalsrsinfo", "trying to open with OGR" ); poOGRDS = OGRSFDriverRegistrar::Open( pszInput, FALSE, NULL ); if( poOGRDS != NULL ) { poLayer = poOGRDS->GetLayer( 0 ); if ( poLayer != NULL ) { OGRSpatialReference *poSRS = poLayer->GetSpatialRef( ); if ( poSRS != NULL ) { CPLDebug( "gdalsrsinfo", "got SRS from OGR" ); bGotSRS = TRUE; OGRSpatialReference* poSRSClone = poSRS->Clone(); oSRS = *poSRSClone; OGRSpatialReference::DestroySpatialReference( poSRSClone ); } } OGRDataSource::DestroyDataSource( poOGRDS ); poOGRDS = NULL; } if ( ! bGotSRS ) CPLDebug( "gdalsrsinfo", "did not open with OGR" ); } #endif // OGR_ENABLED /* Try ESRI file */ if ( ! bGotSRS && bIsFile && (strstr(pszInput,".prj") != NULL) ) { CPLDebug( "gdalsrsinfo", "trying to get SRS from ESRI .prj file [%s]", pszInput ); char **pszTemp; if ( strstr(pszInput,"ESRI::") != NULL ) pszTemp = CSLLoad( pszInput+6 ); else pszTemp = CSLLoad( pszInput ); if( pszTemp ) { eErr = oSRS.importFromESRI( pszTemp ); CSLDestroy( pszTemp ); } else eErr = OGRERR_UNSUPPORTED_SRS; if( eErr != OGRERR_NONE ) { CPLDebug( "gdalsrsinfo", "did not get SRS from ESRI .prj file" ); } else { CPLDebug( "gdalsrsinfo", "got SRS from ESRI .prj file" ); bGotSRS = TRUE; } } /* Last resort, try OSRSetFromUserInput() */ if ( ! bGotSRS ) { CPLDebug( "gdalsrsinfo", "trying to get SRS from user input [%s]", pszInput ); eErr = oSRS.SetFromUserInput( pszInput ); if( eErr != OGRERR_NONE ) { CPLDebug( "gdalsrsinfo", "did not get SRS from user input" ); } else { CPLDebug( "gdalsrsinfo", "got SRS from user input" ); bGotSRS = TRUE; } } /* restore error messages */ if ( ! bDebug ) CPLSetErrorHandler ( oErrorHandler ); return bGotSRS; }
virtual ~ReaderWriterOGR() { CPLSetErrorHandler(oldHandler); }
OGRGDALImportExtension::~OGRGDALImportExtension() { CPLSetErrorHandler(NULL); delete ui; }
// [[Rcpp::export]] void CPL_gdal_init() { CPLSetErrorHandler((CPLErrorHandler)__err_handler); GDALAllRegister(); OGRRegisterAll(); }
int FindSRS( const char *pszInput, OGRSpatialReference &oSRS ) { int bGotSRS = FALSE; VSILFILE *fp = NULL; GDALDataset *poGDALDS = NULL; OGRLayer *poLayer = NULL; const char *pszProjection = NULL; CPLErrorHandler oErrorHandler = NULL; int bIsFile = FALSE; OGRErr eErr = OGRERR_NONE; int bDebug = FALSE; /* temporarily suppress error messages we may get from xOpen() */ bDebug = CSLTestBoolean(CPLGetConfigOption("CPL_DEBUG", "OFF")); if ( ! bDebug ) oErrorHandler = CPLSetErrorHandler ( CPLQuietErrorHandler ); /* Test if argument is a file */ fp = VSIFOpenL( pszInput, "r" ); if ( fp ) { bIsFile = TRUE; VSIFCloseL( fp ); CPLDebug( "gdalsrsinfo", "argument is a file" ); } /* try to open with GDAL */ if( strncmp(pszInput, "http://spatialreference.org/", strlen("http://spatialreference.org/")) != 0 ) { CPLDebug( "gdalsrsinfo", "trying to open with GDAL" ); poGDALDS = (GDALDataset *) GDALOpenEx( pszInput, 0, NULL, NULL, NULL ); } if ( poGDALDS != NULL ) { pszProjection = poGDALDS->GetProjectionRef( ); if( pszProjection != NULL && pszProjection[0] != '\0' ) { char* pszProjectionTmp = (char*) pszProjection; if( oSRS.importFromWkt( &pszProjectionTmp ) == OGRERR_NONE ) { CPLDebug( "gdalsrsinfo", "got SRS from GDAL" ); bGotSRS = TRUE; } } else if( poGDALDS->GetLayerCount() > 0 ) { poLayer = poGDALDS->GetLayer( 0 ); if ( poLayer != NULL ) { OGRSpatialReference *poSRS = poLayer->GetSpatialRef( ); if ( poSRS != NULL ) { CPLDebug( "gdalsrsinfo", "got SRS from OGR" ); bGotSRS = TRUE; OGRSpatialReference* poSRSClone = poSRS->Clone(); oSRS = *poSRSClone; OGRSpatialReference::DestroySpatialReference( poSRSClone ); } } } GDALClose( (GDALDatasetH) poGDALDS ); if ( ! bGotSRS ) CPLDebug( "gdalsrsinfo", "did not open with GDAL" ); } /* Try ESRI file */ if ( ! bGotSRS && bIsFile && (strstr(pszInput,".prj") != NULL) ) { CPLDebug( "gdalsrsinfo", "trying to get SRS from ESRI .prj file [%s]", pszInput ); char **pszTemp; if ( strstr(pszInput,"ESRI::") != NULL ) pszTemp = CSLLoad( pszInput+6 ); else pszTemp = CSLLoad( pszInput ); if( pszTemp ) { eErr = oSRS.importFromESRI( pszTemp ); CSLDestroy( pszTemp ); } else eErr = OGRERR_UNSUPPORTED_SRS; if( eErr != OGRERR_NONE ) { CPLDebug( "gdalsrsinfo", "did not get SRS from ESRI .prj file" ); } else { CPLDebug( "gdalsrsinfo", "got SRS from ESRI .prj file" ); bGotSRS = TRUE; } } /* Last resort, try OSRSetFromUserInput() */ if ( ! bGotSRS ) { CPLDebug( "gdalsrsinfo", "trying to get SRS from user input [%s]", pszInput ); eErr = oSRS.SetFromUserInput( pszInput ); if( eErr != OGRERR_NONE ) { CPLDebug( "gdalsrsinfo", "did not get SRS from user input" ); } else { CPLDebug( "gdalsrsinfo", "got SRS from user input" ); bGotSRS = TRUE; } } /* restore error messages */ if ( ! bDebug ) CPLSetErrorHandler ( oErrorHandler ); return bGotSRS; }
void unset_error_handler(void) { CPLSetErrorHandler((CPLErrorHandler)__err_silent); }
void set_error_handler(void) { CPLSetErrorHandler((CPLErrorHandler)__err_handler); }
int main( int argc, char ** argv ) { int i; int bGotSRS = FALSE; int bPretty = FALSE; int bOutputAll = FALSE; int bValidate = FALSE; const char *pszInput = NULL; const char *pszOutputType = "all"; char *pszOutput = NULL; OGRSpatialReference oSRS; VSILFILE *fp = NULL; GDALDataset *poGDALDS = NULL; OGRDataSource *poOGRDS = NULL; OGRLayer *poLayer = NULL; char *pszProjection = NULL; int bDebug = FALSE; CPLErrorHandler oErrorHandler = NULL; int bIsFile = FALSE; /* Check strict compilation and runtime library version as we use C++ API */ if (! GDAL_CHECK_VERSION(argv[0])) exit(1); /* Must process GDAL_SKIP before GDALAllRegister(), but we can't call */ /* GDALGeneralCmdLineProcessor before it needs the drivers to be registered */ /* for the --format or --formats options */ for( i = 1; i < argc; i++ ) { if( EQUAL(argv[i],"--config") && i + 2 < argc && EQUAL(argv[i + 1], "GDAL_SKIP") ) { CPLSetConfigOption( argv[i+1], argv[i+2] ); i += 2; } } argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); if( argc < 1 ) exit( -argc ); /* -------------------------------------------------------------------- */ /* Parse arguments. */ /* -------------------------------------------------------------------- */ for( i = 1; i < argc; i++ ) { CPLDebug( "gdalsrsinfo", "got arg #%d : [%s]", i, argv[i] ); if( EQUAL(argv[i], "--utility_version") ) { printf("%s was compiled against GDAL %s and is running against GDAL %s\n", argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME")); return 0; } else if( EQUAL(argv[i], "-h") ) Usage(); else if( EQUAL(argv[i], "-o") && i < argc - 1) pszOutputType = argv[++i]; else if( EQUAL(argv[i], "-p") ) bPretty = TRUE; else if( EQUAL(argv[i], "-V") ) bValidate = TRUE; else if( argv[i][0] == '-' ) { CSLDestroy( argv ); Usage(); } else pszInput = argv[i]; } if ( pszInput == NULL ) { CSLDestroy( argv ); Usage(); } /* Register drivers */ GDALAllRegister(); OGRRegisterAll(); /* Search for SRS */ /* temporarily supress error messages we may get from xOpen() */ bDebug = CSLTestBoolean(CPLGetConfigOption("CPL_DEBUG", "OFF")); if ( ! bDebug ) oErrorHandler = CPLSetErrorHandler ( CPLQuietErrorHandler ); /* If argument is a file, try to open it with GDALOpen() and get the projection */ fp = VSIFOpenL( pszInput, "r" ); if ( fp ) { bIsFile = TRUE; VSIFCloseL( fp ); /* try to open with GDAL */ CPLDebug( "gdalsrsinfo", "trying to open with GDAL" ); poGDALDS = (GDALDataset *) GDALOpen( pszInput, GA_ReadOnly ); if ( poGDALDS != NULL && poGDALDS->GetProjectionRef( ) != NULL ) { pszProjection = (char *) poGDALDS->GetProjectionRef( ); if( oSRS.importFromWkt( &pszProjection ) == CE_None ) { CPLDebug( "gdalsrsinfo", "got SRS from GDAL" ); bGotSRS = TRUE; } GDALClose( (GDALDatasetH) poGDALDS ); } if ( ! bGotSRS ) CPLDebug( "gdalsrsinfo", "did not open with GDAL" ); /* if unsuccessful, try to open with OGR */ if ( ! bGotSRS ) { CPLDebug( "gdalsrsinfo", "trying to open with OGR" ); poOGRDS = OGRSFDriverRegistrar::Open( pszInput, FALSE, NULL ); if( poOGRDS != NULL ) { poLayer = poOGRDS->GetLayer( 0 ); if ( poLayer != NULL ) { OGRSpatialReference *poSRS = poLayer->GetSpatialRef( ); if ( poSRS != NULL ) { CPLDebug( "gdalsrsinfo", "got SRS from OGR" ); bGotSRS = TRUE; OGRSpatialReference* poSRSClone = poSRS->Clone(); oSRS = *poSRSClone; OGRSpatialReference::DestroySpatialReference( poSRSClone ); } } OGRDataSource::DestroyDataSource( poOGRDS ); poOGRDS = NULL; } if ( ! bGotSRS ) CPLDebug( "gdalsrsinfo", "did not open with OGR" ); /* OGR_DS_Destroy( hOGRDS ); */ } } /* If didn't get the projection from the file, try OSRSetFromUserInput() */ /* File might not be a dataset, but contain projection info (e.g. .prf files) */ if ( ! bGotSRS ) { CPLDebug( "gdalsrsinfo", "trying to get SRS from user input [%s]", pszInput ); if( oSRS.SetFromUserInput( pszInput ) != OGRERR_NONE ) { CPLDebug( "gdalsrsinfo", "did not get SRS from user input" ); } else { CPLDebug( "gdalsrsinfo", "got SRS from user input" ); bGotSRS = TRUE; } } /* restore error messages */ if ( ! bDebug ) CPLSetErrorHandler ( oErrorHandler ); CPLDebug( "gdalsrsinfo", "bGotSRS: %d bValidate: %d pszOutputType: %s bPretty: %d", bGotSRS, bValidate, pszOutputType, bPretty ); /* Make sure we got a SRS */ if ( ! bGotSRS ) { CPLError( CE_Failure, CPLE_AppDefined, "ERROR - failed to load SRS definition from %s", pszInput ); } else { /* Validate - not well tested!*/ if ( bValidate ) { OGRErr eErr = oSRS.Validate( ); if ( eErr != OGRERR_NONE ) { printf( "\nValidate Fails" ); if ( eErr == OGRERR_CORRUPT_DATA ) printf( " - SRS is not well formed"); else if ( eErr == OGRERR_UNSUPPORTED_SRS ) printf(" - contains non-standard PROJECTION[] values"); printf("\n"); } else printf( "\nValidate Succeeds\n" ); } /* Output */ if ( EQUAL("all", pszOutputType ) ) { bOutputAll = TRUE; bPretty = TRUE; } printf("\n"); if ( bOutputAll || EQUAL("proj4", pszOutputType ) ) { if ( bOutputAll ) printf("PROJ.4 : "); oSRS.exportToProj4( &pszOutput ); printf("\'%s\'\n\n",pszOutput); CPLFree( pszOutput ); } if ( bOutputAll || EQUAL("wkt", pszOutputType ) ) { if ( bOutputAll ) printf("OGC WKT :\n"); if ( bPretty ) oSRS.exportToPrettyWkt( &pszOutput, FALSE ); else oSRS.exportToWkt( &pszOutput ); printf("%s\n\n",pszOutput); CPLFree( pszOutput ); } if ( bOutputAll || EQUAL("wkt_simple", pszOutputType ) ) { if ( bOutputAll ) printf("OGC WKT (simple) :\n"); oSRS.exportToPrettyWkt( &pszOutput, TRUE ); printf("%s\n\n",pszOutput); CPLFree( pszOutput ); } if ( EQUAL("wkt_old", pszOutputType ) ) { if ( bOutputAll ) printf("OGC WKT (old) :\n"); oSRS.StripCTParms( ); if ( bPretty ) oSRS.exportToPrettyWkt( &pszOutput, FALSE ); else oSRS.exportToWkt( &pszOutput ); printf("%s\n\n",pszOutput); CPLFree( pszOutput ); } if ( bOutputAll || EQUAL("wkt_esri", pszOutputType ) ) { if ( bOutputAll ) printf("ESRI WKT :\n"); OGRSpatialReference *poSRS = oSRS.Clone(); poSRS->morphToESRI( ); if ( bPretty ) poSRS->exportToPrettyWkt( &pszOutput, FALSE ); else poSRS->exportToWkt( &pszOutput ); printf("%s\n\n",pszOutput); CPLFree( pszOutput ); OGRSpatialReference::DestroySpatialReference( poSRS ); } /* mapinfo and xml are not output with "all" */ if ( EQUAL("mapinfo", pszOutputType ) ) { if ( bOutputAll ) printf("MAPINFO : "); oSRS.exportToMICoordSys( &pszOutput ); printf("\'%s\'\n\n",pszOutput); CPLFree( pszOutput ); } if ( EQUAL("xml", pszOutputType ) ) { if ( bOutputAll ) printf("XML :\n"); oSRS.exportToXML( &pszOutput, NULL ); printf("%s\n\n",pszOutput); CPLFree( pszOutput ); } } /* cleanup anything left */ GDALDestroyDriverManager(); OGRCleanupAll(); CSLDestroy( argv ); return 0; }
~DisableCplErrors() { // reset to default. CPLSetErrorHandler(oldHandler); }
DisableCplErrors() { oldHandler = CPLSetErrorHandler(emptyErrorHandler); }