LCOV - code coverage report
Current view: directory - apps - gdalsrsinfo.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 278 172 61.9 %
Date: 2011-12-18 Functions: 7 4 57.1 %

       1                 : /******************************************************************************
       2                 :  * $Id$
       3                 :  *
       4                 :  * Project:  GDAL Utilities
       5                 :  * Purpose:  Commandline application to list info about a given CRS.
       6                 :  *           Outputs a number of formats (WKT, PROJ.4, etc.).
       7                 :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       8                 :  *           Etienne Tourigny, etourigny.dev-at-gmail-dot-com       
       9                 :  *
      10                 :  * ****************************************************************************
      11                 :  * Copyright (c) 1998, Frank Warmerdam
      12                 :  *
      13                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      14                 :  * copy of this software and associated documentation files (the "Software"),
      15                 :  * to deal in the Software without restriction, including without limitation
      16                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      17                 :  * and/or sell copies of the Software, and to permit persons to whom the
      18                 :  * Software is furnished to do so, subject to the following conditions:
      19                 :  *
      20                 :  * The above copyright notice and this permission notice shall be included
      21                 :  * in all copies or substantial portions of the Software.
      22                 :  *
      23                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      24                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      25                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      26                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      27                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      28                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      29                 :  * DEALINGS IN THE SOFTWARE.
      30                 :  ****************************************************************************/
      31                 : 
      32                 : #include "gdal_priv.h"
      33                 : #include "cpl_string.h"
      34                 : #include "ogr_spatialref.h"
      35                 : #include "ogr_api.h"
      36                 : #include "ogrsf_frmts.h"
      37                 : 
      38                 : CPL_CVSID("$Id$");
      39                 : 
      40                 : int FindSRS( const char *pszInput, OGRSpatialReference &oSRS, int bDebug );
      41                 : CPLErr PrintSRS( const OGRSpatialReference &oSRS, 
      42                 :                  const char * pszOutputType, 
      43                 :                  int bPretty, int bPrintSep );
      44                 : void PrintSRSOutputTypes( const OGRSpatialReference &oSRS, 
      45                 :                           const char ** papszOutputTypes );
      46                 : int FindEPSG( const OGRSpatialReference &oSRS );
      47                 : int SearchCSVForWKT( const char *pszFileCSV, const char *pszTarget );
      48                 : 
      49                 : /************************************************************************/
      50                 : /*                               Usage()                                */
      51                 : /************************************************************************/
      52                 : 
      53               0 : void Usage()
      54                 : 
      55                 : {
      56                 :     printf( "\nUsage: gdalsrsinfo [options] srs_def\n"
      57                 :             "\n"
      58                 :             "srs_def may be the filename of a dataset supported by GDAL/OGR "
      59                 :             "from which to extract SRS information\n"
      60                 :             "OR any of the usual GDAL/OGR forms "
      61                 :             "(complete WKT, PROJ.4, EPSG:n or a file containing the SRS)\n"
      62                 :             "\n"          
      63                 :             "Options: \n"
      64                 :             "   [--help-general] [-h]  Show help and exit\n"
      65                 :             "   [-p]                   Pretty-print where applicable (e.g. WKT)\n"
      66                 :             "   [-V]                   Validate SRS\n"
      67                 :             "   [-e]                   Search for EPSG number corresponding to SRS (experimental)\n"
      68                 :             "   [-o out_type]          Output type { default, all, wkt_all,\n"
      69                 :             "                                        proj4, epsg,\n"
      70                 :             "                                        wkt, wkt_simple, wkt_noct, wkt_esri,\n"
      71               0 :             "                                        mapinfo, xml }\n\n" ); 
      72               0 :     exit( 1 );
      73                 : }
      74                 : 
      75                 : 
      76                 : /************************************************************************/
      77                 : /*                                main()                                */
      78                 : /************************************************************************/
      79                 : 
      80              16 : int main( int argc, char ** argv ) 
      81                 : 
      82                 : {
      83                 :     int            i;
      84              16 :     int            bGotSRS = FALSE;
      85              16 :     int            bPretty = FALSE;
      86              16 :     int            bValidate = FALSE;
      87              16 :     int            bDebug = FALSE;
      88              16 :     int            bFindEPSG = FALSE;
      89              16 :     int            nEPSGCode = -1;
      90              16 :     const char     *pszInput = NULL;
      91              16 :     const char     *pszOutputType = "default";
      92              16 :     OGRSpatialReference  oSRS;
      93                 : 
      94                 :     /* Check strict compilation and runtime library version as we use C++ API */
      95              16 :     if (! GDAL_CHECK_VERSION(argv[0]))
      96               0 :         exit(1);
      97                 : 
      98                 :     /* Must process GDAL_SKIP before GDALAllRegister(), but we can't call */
      99                 :     /* GDALGeneralCmdLineProcessor before it needs the drivers to be registered */
     100                 :     /* for the --format or --formats options */
     101              61 :     for( i = 1; i < argc; i++ )
     102                 :     {
     103              45 :         if( EQUAL(argv[i],"--config") && i + 2 < argc && EQUAL(argv[i + 1], "GDAL_SKIP") )
     104                 :         {
     105               0 :             CPLSetConfigOption( argv[i+1], argv[i+2] );
     106                 : 
     107               0 :             i += 2;
     108                 :         }
     109                 :     }
     110                 : 
     111              16 :     argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
     112              16 :     if( argc < 1 )
     113               0 :         exit( -argc );
     114                 : 
     115                 : /* -------------------------------------------------------------------- */
     116                 : /*      Parse arguments.                                                */
     117                 : /* -------------------------------------------------------------------- */
     118              47 :     for( i = 1; i < argc; i++ )
     119                 :     {
     120              32 :         CPLDebug( "gdalsrsinfo", "got arg #%d : [%s]", i, argv[i] );
     121                 : 
     122              32 :         if( EQUAL(argv[i], "--utility_version") )
     123                 :         {
     124                 :             printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
     125               1 :                    argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
     126               1 :             return 0;
     127                 :         }
     128              31 :         else if( EQUAL(argv[i], "-h") )
     129               0 :             Usage();
     130              31 :         else if( EQUAL(argv[i], "-e") )
     131               0 :             bFindEPSG = TRUE;
     132              44 :         else if( EQUAL(argv[i], "-o") && i < argc - 1)
     133              13 :             pszOutputType = argv[++i];
     134              18 :         else if( EQUAL(argv[i], "-p") )
     135               1 :             bPretty = TRUE;
     136              17 :         else if( EQUAL(argv[i], "-V") )
     137               2 :             bValidate = TRUE;
     138              15 :         else if( argv[i][0] == '-' )
     139                 :         {
     140               0 :             CSLDestroy( argv );
     141               0 :             Usage();
     142                 :         }
     143                 :         else  
     144              15 :             pszInput = argv[i];
     145                 :     }
     146                 : 
     147              15 :     if ( pszInput == NULL ) {
     148               0 :         CSLDestroy( argv );
     149               0 :         Usage();
     150                 :     }
     151                 : 
     152                 :     /* Register drivers */
     153              15 :     GDALAllRegister();
     154              15 :     OGRRegisterAll();
     155                 : 
     156                 :     /* Search for SRS */
     157              15 :     bGotSRS = FindSRS( pszInput, oSRS, bDebug );
     158                 : 
     159                 :     CPLDebug( "gdalsrsinfo", 
     160                 :               "bGotSRS: %d bValidate: %d pszOutputType: %s bPretty: %d",
     161              15 :               bGotSRS, bValidate, pszOutputType, bPretty  );
     162                 :       
     163                 : 
     164                 :     /* Make sure we got a SRS */
     165              15 :     if ( ! bGotSRS ) {
     166                 :         CPLError( CE_Failure, CPLE_AppDefined,
     167                 :                   "ERROR - failed to load SRS definition from %s",
     168               1 :                   pszInput );
     169                 :     }
     170                 : 
     171                 :     else {
     172                 : 
     173                 :         /* Find EPSG code - experimental */
     174              14 :         if ( EQUAL(pszOutputType,"epsg") )
     175               0 :             bFindEPSG = TRUE;
     176              14 :         if ( bFindEPSG ) {
     177                 :             CPLError( CE_Warning, CPLE_AppDefined,
     178               0 :                       "EPSG detection is experimental and requires new data files (see bug #4345)" );
     179               0 :             nEPSGCode = FindEPSG( oSRS );
     180                 :             /* If found, replace oSRS based on EPSG code */
     181               0 :             if(nEPSGCode != -1) {
     182                 :                 CPLDebug( "gdalsrsinfo", 
     183               0 :                           "Found EPSG code %d", nEPSGCode );
     184               0 :                 OGRSpatialReference oSRS2;
     185               0 :                 if ( oSRS2.importFromEPSG( nEPSGCode ) == OGRERR_NONE )
     186               0 :                     oSRS = oSRS2;
     187                 :             }
     188                 :         }
     189                 :         /* Validate - not well tested!*/
     190              14 :         if ( bValidate ) {
     191               2 :             OGRErr eErr = oSRS.Validate( );
     192               2 :             if ( eErr != OGRERR_NONE ) {
     193               1 :                 printf( "\nValidate Fails" );
     194               1 :                 if ( eErr == OGRERR_CORRUPT_DATA )
     195               1 :                     printf( " - SRS is not well formed");
     196               0 :                 else if ( eErr == OGRERR_UNSUPPORTED_SRS )
     197               0 :                     printf(" - contains non-standard PROJECTION[] values");
     198               1 :                 printf("\n");
     199                 :             }
     200                 :             else
     201               1 :                 printf( "\nValidate Succeeds\n" );
     202                 :         }
     203                 :         
     204                 :         /* Output */
     205              14 :         if ( EQUAL("default", pszOutputType ) ) {
     206                 :             /* does this work in MSVC? */
     207                 :             const char* papszOutputTypes[] = 
     208               1 :                 { "proj4", "wkt", NULL };      
     209               1 :             if ( bFindEPSG ) 
     210               0 :                 printf("\nEPSG:%d\n",nEPSGCode);
     211               1 :             PrintSRSOutputTypes( oSRS, papszOutputTypes );
     212                 :         }
     213              13 :         else if ( EQUAL("all", pszOutputType ) ) {
     214               0 :             if ( bFindEPSG ) 
     215               0 :                 printf("\nEPSG:%d\n\n",nEPSGCode);
     216                 :             const char* papszOutputTypes[] = 
     217               0 :                 {"proj4","wkt","wkt_simple","wkt_noct","wkt_esri","mapinfo","xml",NULL};
     218               0 :             PrintSRSOutputTypes( oSRS, papszOutputTypes );
     219                 :         }
     220              13 :         else if ( EQUAL("wkt_all", pszOutputType ) ) {
     221                 :             const char* papszOutputTypes[] = 
     222               0 :                 { "wkt", "wkt_simple", "wkt_noct", "wkt_esri", NULL };
     223               0 :             PrintSRSOutputTypes( oSRS, papszOutputTypes );
     224                 :         }
     225                 :         else {
     226              13 :             if ( bPretty )
     227               1 :                 printf( "\n" );
     228              13 :             if ( EQUAL(pszOutputType,"epsg") )
     229               0 :                 printf("EPSG:%d\n",nEPSGCode);
     230                 :             else
     231              13 :                 PrintSRS( oSRS, pszOutputType, bPretty, FALSE );
     232              13 :             if ( bPretty )
     233               1 :                 printf( "\n" );        
     234                 :         }
     235                 : 
     236                 :     }
     237                 : 
     238                 :     /* cleanup anything left */
     239              15 :     GDALDestroyDriverManager();
     240              15 :     OGRCleanupAll();
     241              15 :     CSLDestroy( argv );
     242                 : 
     243              15 :     return 0;
     244                 : }
     245                 : 
     246                 : /************************************************************************/
     247                 : /*                      FindSRS()                                       */
     248                 : /*                                                                      */
     249                 : /*      Search for SRS from pszInput, update oSRS.                      */
     250                 : /************************************************************************/
     251              15 : int FindSRS( const char *pszInput, OGRSpatialReference &oSRS, int bDebug )
     252                 : 
     253                 : {
     254              15 :     int            bGotSRS = FALSE;
     255              15 :     VSILFILE      *fp = NULL;
     256              15 :     GDALDataset   *poGDALDS = NULL; 
     257              15 :     OGRDataSource *poOGRDS = NULL;
     258              15 :     OGRLayer      *poLayer = NULL;
     259              15 :     char           *pszProjection = NULL;
     260              15 :     CPLErrorHandler oErrorHandler = NULL;
     261              15 :     int bIsFile = FALSE;
     262              15 :     OGRErr eErr = CE_None;
     263                 :       
     264                 :     /* temporarily supress error messages we may get from xOpen() */
     265              15 :     if ( ! bDebug )
     266              15 :         oErrorHandler = CPLSetErrorHandler ( CPLQuietErrorHandler );
     267                 : 
     268                 :     /* If argument is a file, try to open it with GDAL and OGROpen() */
     269              15 :     fp = VSIFOpenL( pszInput, "r" );
     270              15 :     if ( fp )  {
     271                 :         
     272               9 :         bIsFile = TRUE;
     273               9 :         VSIFCloseL( fp );
     274                 :         
     275                 :         /* try to open with GDAL */
     276               9 :         CPLDebug( "gdalsrsinfo", "trying to open with GDAL" );
     277               9 :         poGDALDS = (GDALDataset *) GDALOpen( pszInput, GA_ReadOnly );
     278               9 :         if ( poGDALDS != NULL && poGDALDS->GetProjectionRef( ) != NULL ) {
     279               7 :             pszProjection = (char *) poGDALDS->GetProjectionRef( );
     280               7 :             if( oSRS.importFromWkt( &pszProjection ) == CE_None ) {
     281               7 :                 CPLDebug( "gdalsrsinfo", "got SRS from GDAL" );
     282               7 :                 bGotSRS = TRUE;
     283                 :             }
     284               7 :             GDALClose( (GDALDatasetH) poGDALDS );
     285                 :         }
     286               9 :         if ( ! bGotSRS ) 
     287               2 :             CPLDebug( "gdalsrsinfo", "did not open with GDAL" );
     288                 :         
     289                 :         /* if unsuccessful, try to open with OGR */
     290               9 :         if ( ! bGotSRS ) {
     291               2 :             CPLDebug( "gdalsrsinfo", "trying to open with OGR" );
     292               2 :             poOGRDS = OGRSFDriverRegistrar::Open( pszInput, FALSE, NULL );
     293               2 :             if( poOGRDS != NULL ) {
     294               1 :                 poLayer = poOGRDS->GetLayer( 0 );
     295               1 :                 if ( poLayer != NULL ) {
     296               1 :                     OGRSpatialReference *poSRS = poLayer->GetSpatialRef( );
     297               1 :                     if ( poSRS != NULL ) {
     298               1 :                         CPLDebug( "gdalsrsinfo", "got SRS from OGR" );
     299               1 :                         bGotSRS = TRUE;
     300               1 :                         OGRSpatialReference* poSRSClone = poSRS->Clone();
     301               1 :                         oSRS = *poSRSClone;
     302               1 :                         OGRSpatialReference::DestroySpatialReference( poSRSClone );
     303                 :                     }
     304                 :                 }
     305               1 :                 OGRDataSource::DestroyDataSource( poOGRDS );
     306               1 :                 poOGRDS = NULL;
     307                 :             } 
     308               2 :             if ( ! bGotSRS ) 
     309               1 :                 CPLDebug( "gdalsrsinfo", "did not open with OGR" );
     310                 :          }
     311                 :         
     312                 :     }
     313                 :  
     314                 :     /* Try ESRI file */
     315              15 :     if ( ! bGotSRS && (strstr(pszInput,".prj") != NULL) ) {
     316                 :         CPLDebug( "gdalsrsinfo", 
     317               1 :                   "trying to get SRS from ESRI .prj file [%s]", pszInput );
     318                 : 
     319                 :         char **pszTemp;
     320               1 :         if ( strstr(pszInput,"ESRI::") != NULL )
     321               0 :             pszTemp = CSLLoad( pszInput+6 );
     322                 :         else 
     323               1 :             pszTemp = CSLLoad( pszInput );
     324                 : 
     325               1 :         if( pszTemp ) {
     326               1 :             eErr = oSRS.importFromESRI( pszTemp );
     327               1 :             CSLDestroy( pszTemp );
     328                 :         }
     329                 :         else 
     330               0 :             eErr = OGRERR_UNSUPPORTED_SRS;
     331                 : 
     332               1 :         if( eErr != OGRERR_NONE ) {
     333               1 :             CPLDebug( "gdalsrsinfo", "did not get SRS from ESRI .prj file" );
     334                 :         }
     335                 :         else {
     336               0 :             CPLDebug( "gdalsrsinfo", "got SRS from ESRI .prj file" );
     337               0 :             bGotSRS = TRUE;
     338                 :         }
     339                 :     }
     340                 : 
     341                 :     /* Last resort, try OSRSetFromUserInput() */
     342              15 :     if ( ! bGotSRS ) {
     343                 :         CPLDebug( "gdalsrsinfo", 
     344               7 :                   "trying to get SRS from user input [%s]", pszInput );
     345                 : 
     346               7 :         eErr = oSRS.SetFromUserInput( pszInput );
     347                 :  
     348               7 :        if(  eErr != OGRERR_NONE ) {
     349               1 :             CPLDebug( "gdalsrsinfo", "did not get SRS from user input" );
     350                 :         }
     351                 :         else {
     352               6 :             CPLDebug( "gdalsrsinfo", "got SRS from user input" );
     353               6 :             bGotSRS = TRUE;
     354                 :         }
     355                 :     }
     356                 :     
     357                 :   /* restore error messages */
     358              15 :     if ( ! bDebug )
     359              15 :         CPLSetErrorHandler ( oErrorHandler ); 
     360                 : 
     361                 : 
     362              15 :     return bGotSRS;
     363                 : }
     364                 : 
     365                 : 
     366                 : /************************************************************************/
     367                 : /*                      PrintSRS()                                      */
     368                 : /*                                                                      */
     369                 : /*      Print spatial reference in specified format.                    */
     370                 : /************************************************************************/
     371              15 : CPLErr PrintSRS( const OGRSpatialReference &oSRS, 
     372                 :                  const char * pszOutputType, 
     373                 :                  int bPretty, int bPrintSep )
     374                 : 
     375                 : {
     376              15 :     if ( ! pszOutputType || EQUAL(pszOutputType,""))
     377               0 :         return CE_None;
     378                 : 
     379                 :     CPLDebug( "gdalsrsinfo", "PrintSRS( oSRS, %s, %d, %d )\n",
     380              15 :               pszOutputType, bPretty, bPrintSep );
     381                 :     
     382              15 :     char *pszOutput = NULL;
     383                 : 
     384              15 :     if ( EQUAL("proj4", pszOutputType ) ) {
     385               6 :         if ( bPrintSep ) printf( "PROJ.4 : ");
     386               6 :         oSRS.exportToProj4( &pszOutput );
     387               6 :         printf( "\'%s\'\n", pszOutput );
     388                 :     }
     389                 : 
     390               9 :     else if ( EQUAL("wkt", pszOutputType ) ) {
     391               5 :         if ( bPrintSep ) printf("OGC WKT :\n");
     392               5 :         if ( bPretty ) 
     393               2 :             oSRS.exportToPrettyWkt( &pszOutput, FALSE );
     394                 :         else
     395               3 :             oSRS.exportToWkt( &pszOutput );
     396               5 :         printf("%s\n",pszOutput);
     397                 :     }
     398                 :         
     399               4 :     else if (  EQUAL("wkt_simple", pszOutputType ) ) {
     400               1 :         if ( bPrintSep ) printf("OGC WKT (simple) :\n");
     401               1 :         oSRS.exportToPrettyWkt( &pszOutput, TRUE );
     402               1 :         printf("%s\n",pszOutput);
     403                 :     }
     404                 :         
     405               3 :     else if ( EQUAL("wkt_noct", pszOutputType ) ) {
     406               1 :         if (  bPrintSep ) printf("OGC WKT (no CT) :\n");
     407               1 :         OGRSpatialReference *poSRS = oSRS.Clone();
     408               1 :         poSRS->StripCTParms( );
     409               1 :         if ( bPretty ) 
     410               0 :             poSRS->exportToPrettyWkt( &pszOutput, FALSE );
     411                 :         else
     412               1 :             poSRS->exportToWkt( &pszOutput );
     413               1 :         OGRSpatialReference::DestroySpatialReference( poSRS );
     414               1 :         printf("%s\n",pszOutput);
     415                 :     }
     416                 :         
     417               2 :     else if ( EQUAL("wkt_esri", pszOutputType ) ) {
     418               1 :         if ( bPrintSep ) printf("ESRI WKT :\n");
     419               1 :         OGRSpatialReference *poSRS = oSRS.Clone();
     420               1 :         poSRS->morphToESRI( );
     421               1 :         if ( bPretty ) 
     422               0 :             poSRS->exportToPrettyWkt( &pszOutput, FALSE );
     423                 :         else
     424               1 :             poSRS->exportToWkt( &pszOutput );
     425               1 :         OGRSpatialReference::DestroySpatialReference( poSRS );
     426               1 :         printf("%s\n",pszOutput);
     427                 :     }
     428                 :         
     429               1 :     else if ( EQUAL("mapinfo", pszOutputType ) ) {
     430               1 :         if ( bPrintSep ) printf("MAPINFO : ");
     431               1 :         oSRS.exportToMICoordSys( &pszOutput );
     432               1 :         printf("\'%s\'\n",pszOutput);
     433                 :     }
     434                 :         
     435               0 :     else if ( EQUAL("xml", pszOutputType ) ) {
     436               0 :         if ( bPrintSep ) printf("XML :\n");
     437               0 :         oSRS.exportToXML( &pszOutput, NULL );
     438               0 :         printf("%s\n",pszOutput);
     439                 :     }
     440                 : 
     441                 :     else {
     442                 :         CPLError( CE_Failure, CPLE_AppDefined,
     443                 :                   "ERROR - %s output not supported",
     444               0 :                   pszOutputType );
     445               0 :         return CE_Failure;
     446                 :     }
     447                 : 
     448              15 :     CPLFree( pszOutput );
     449                 :     
     450              15 :     return CE_None;
     451                 : }
     452                 : 
     453                 : /************************************************************************/
     454                 : /*                      PrintSRSOutputTypes()                           */
     455                 : /*                                                                      */
     456                 : /*      Print spatial reference in specified formats.                   */
     457                 : /************************************************************************/
     458               1 : void PrintSRSOutputTypes( const OGRSpatialReference &oSRS, 
     459                 :                           const char ** papszOutputTypes )
     460                 :     
     461                 : {
     462               1 :     int nOutputTypes = CSLCount((char**)papszOutputTypes);
     463               1 :     printf( "\n" );
     464               3 :     for ( int i=0; i<nOutputTypes; i++ ) {
     465               2 :         PrintSRS( oSRS, papszOutputTypes[i], TRUE, TRUE );
     466               2 :         printf( "\n" );        
     467                 :     }
     468               1 : }
     469                 : 
     470                 : /************************************************************************/
     471                 : /*                      SearchCSVForWKT()                               */
     472                 : /*                                                                      */
     473                 : /*      Search CSV file for target WKT, return EPSG code (or -1).       */
     474                 : /*      For saving space, the file can be compressed (gz)               */
     475                 : /*      If CSV file is absent are absent the function silently exits    */
     476                 : /************************************************************************/
     477               0 : int SearchCSVForWKT( const char *pszFileCSV, const char *pszTarget )
     478                 : {
     479               0 :     const char *pszFilename = NULL;
     480               0 :     const char *pszWKT = NULL;
     481                 :     char szTemp[1024];
     482               0 :     int nPos = 0;
     483               0 :     const char *pszTemp = NULL;
     484                 : 
     485               0 :     VSILFILE *fp = NULL;
     486               0 :     OGRSpatialReference oSRS;
     487               0 :     int nCode = 0;
     488               0 :     int nFound = -1;
     489                 : 
     490                 :     CPLDebug( "gdalsrsinfo", 
     491                 :               "SearchCSVForWKT()\nfile=%s\nWKT=%s\n",
     492               0 :               pszFileCSV, pszTarget);
     493                 : 
     494                 : /* -------------------------------------------------------------------- */
     495                 : /*      Find and open file.                                             */
     496                 : /* -------------------------------------------------------------------- */
     497                 :     // pszFilename = pszFileCSV;
     498               0 :     pszFilename = CPLFindFile( "gdal", pszFileCSV );
     499               0 :     if( pszFilename == NULL )
     500                 :     {
     501                 :         CPLDebug( "gdalsrsinfo", "could not find support file %s",
     502               0 :                    pszFileCSV );
     503                 :         // return OGRERR_UNSUPPORTED_SRS;
     504               0 :         return -1;
     505                 :     }
     506                 : 
     507                 :     /* support gzipped file */
     508               0 :     if ( strstr( pszFileCSV,".gz") != NULL )
     509               0 :         sprintf( szTemp, "/vsigzip/%s", pszFilename);
     510                 :     else
     511               0 :         sprintf( szTemp, "%s", pszFilename);
     512                 : 
     513                 :     CPLDebug( "gdalsrsinfo", "SearchCSVForWKT() using file %s",
     514               0 :               szTemp );
     515                 : 
     516               0 :     fp = VSIFOpenL( szTemp, "r" );
     517               0 :     if( fp == NULL ) 
     518                 :     {
     519                 :         CPLDebug( "gdalsrsinfo", "could not open support file %s",
     520               0 :                   pszFilename );
     521                 : 
     522                 :         // return OGRERR_UNSUPPORTED_SRS;
     523               0 :         return -1;
     524                 :     }
     525                 : 
     526                 : /* -------------------------------------------------------------------- */
     527                 : /*      Process lines.                                                  */
     528                 : /* -------------------------------------------------------------------- */
     529                 :     const char *pszLine;
     530                 : 
     531               0 :     while( (pszLine = CPLReadLine2L(fp,-1,NULL)) != NULL )
     532                 : 
     533                 :     {
     534                 :         // CPLDebug( "gdalsrsinfo", "read line %s", pszLine );
     535                 : 
     536               0 :         if( pszLine[0] == '#' )
     537               0 :             continue;
     538                 :             /* do nothing */;
     539                 : 
     540                 :         // else if( EQUALN(pszLine,"include ",8) )
     541                 :         // {
     542                 :         //     eErr = importFromDict( pszLine + 8, pszCode );
     543                 :         //     if( eErr != OGRERR_UNSUPPORTED_SRS )
     544                 :         //         break;
     545                 :         // }
     546                 : 
     547                 :         // else if( strstr(pszLine,",") == NULL )
     548                 :         //     /* do nothing */;
     549                 : 
     550               0 :         pszTemp = strstr(pszLine,",");
     551               0 :         if (pszTemp)
     552                 :         {
     553               0 :             nPos = pszTemp - pszLine;
     554                 : 
     555               0 :             if ( nPos == 0 )
     556               0 :                 continue;
     557                 :             
     558               0 :             strncpy( szTemp, pszLine, nPos );
     559               0 :             szTemp[nPos] = '\0';
     560               0 :             nCode = atoi(szTemp);
     561                 : 
     562               0 :             pszWKT = (char *) pszLine + nPos +1;
     563                 : 
     564                 :             // CPLDebug( "gdalsrsinfo", 
     565                 :             //           "code=%d\nWKT=\n[%s]\ntarget=\n[%s]\n",
     566                 :             //           nCode,pszWKT, pszTarget );
     567                 : 
     568               0 :             if ( EQUAL(pszTarget,pszWKT) )
     569                 :             {
     570               0 :                 nFound = nCode;
     571                 :                 CPLDebug( "gdalsrsinfo", "found EPSG:%d\n"
     572                 :                           "current=%s\ntarget= %s\n",
     573               0 :                           nCode, pszWKT, pszTarget );
     574               0 :                 break;
     575                 :             }
     576                 :         }
     577                 :     }
     578                 :     
     579               0 :     VSIFCloseL( fp );
     580                 : 
     581               0 :     return nFound;
     582                 : 
     583                 : }
     584                 : 
     585                 : /* TODO 
     586                 :    - search for well-known values (AutoIdentifyEPSG()) 
     587                 : 
     588                 :    - should we search .override.csv files?
     589                 : 
     590                 :    - fix precision differences (namely in degree: 17 vs 15) so we can use epsg_ogc_simple
     591                 : target:
     592                 :  orig: GEOGCS["SAD69",DATUM["D_South_American_1969",SPHEROID["GRS_1967_Modified",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
     593                 :  ESRI: GEOGCS["SAD69",DATUM["D_South_American_1969",SPHEROID["GRS_1967_Truncated",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
     594                 :  OGC:  GEOGCS["SAD69",DATUM["South_American_Datum_1969",SPHEROID["GRS_1967_Modified",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
     595                 : database:
     596                 :  ESRI: GEOGCS["SAD69",DATUM["D_South_American_1969",SPHEROID["GRS_1967_Truncated",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
     597                 :  OGC:  GEOGCS["SAD69",DATUM["South_American_Datum_1969",SPHEROID["GRS 1967 Modified",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]
     598                 : */
     599                 : 
     600                 : /************************************************************************/
     601                 : /*                      FindEPSG()                                      */
     602                 : /*                                                                      */
     603                 : /*      Return EPSG code corresponding to spatial reference (or -1)     */
     604                 : /************************************************************************/
     605               0 : int FindEPSG( const OGRSpatialReference &oSRS )
     606                 : {
     607               0 :     char *pszWKT = NULL;
     608               0 :     char *pszESRI = NULL;
     609               0 :     int nFound = -1;
     610               0 :     OGRSpatialReference *poSRS = NULL;
     611                 :  
     612               0 :     poSRS = oSRS.Clone();
     613               0 :     poSRS->StripCTParms();
     614               0 :     poSRS->exportToWkt( &pszWKT );
     615               0 :     OGRSpatialReference::DestroySpatialReference( poSRS );
     616                 : 
     617               0 :     poSRS = oSRS.Clone();
     618               0 :     poSRS->morphToESRI( );
     619               0 :     poSRS->exportToWkt( &pszESRI );
     620               0 :     OGRSpatialReference::DestroySpatialReference( poSRS );
     621                 : 
     622                 :     CPLDebug( "gdalsrsinfo", "FindEPSG()\nWKT (OGC)= %s\nWKT (ESRI)=%s",
     623               0 :               pszWKT,pszESRI );
     624                 : 
     625                 :     /* search for EPSG code in epsg_*.wkt.gz files */
     626                 :     /* using ESRI WKT for now, as it seems to work best */
     627               0 :     nFound = SearchCSVForWKT( "epsg_esri.wkt.gz", pszESRI );
     628               0 :     if ( nFound == -1 )
     629               0 :         nFound = SearchCSVForWKT( "epsg_ogc_simple.wkt.gz", pszESRI );
     630               0 :     if ( nFound == -1 )
     631               0 :         nFound = SearchCSVForWKT( "epsg_ogc.wkt.gz", pszESRI );
     632                 : 
     633                 :     
     634               0 :     CPLFree( pszWKT );
     635               0 :     CPLFree( pszESRI );    
     636                 :     
     637               0 :     return nFound;
     638                 : }

Generated by: LCOV version 1.7