"Fossies" - the Fresh Open Source Software Archive

Member "libgeotiff-1.6.0/bin/listgeo.c" (22 Apr 2020, 9543 Bytes) of package /linux/privat/libgeotiff-1.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "listgeo.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.1_vs_1.6.0.

    1 /*
    2  * listgeo.c -- example client code for LIBGEO geographic
    3  *     TIFF tag support. Dumps info to GeoTIFF metadata file.
    4  *
    5  *  Author: Niles D. Ritter
    6  *
    7  */
    8 
    9 #include "geotiff.h"
   10 #include "xtiffio.h"
   11 #include "geo_normalize.h"
   12 #include "geo_simpletags.h"
   13 #include "geovalues.h"
   14 #include "tiffio.h"
   15 #include "cpl_serv.h"
   16 #include <stdio.h>
   17 
   18 static void WriteTFWFile( GTIF * gtif, const char * tif_filename );
   19 static void GTIFPrintCorners( GTIF *, GTIFDefn *, FILE *, int, int, int, int );
   20 static TIFF *st_setup_test_info();
   21 
   22 void Usage()
   23 {
   24     printf( 
   25         "%s", 
   26         "Usage: listgeo [-d] [-tfw] [-proj4] [-no_norm] [-t tabledir] filename\n"
   27         "\n"
   28         "  -d: report lat/long corners in decimal degrees instead of DMS.\n"
   29         "  -tfw: Generate a .tfw (ESRI TIFF World) file for the target file.\n"
   30         "  -proj4: Report PROJ.4 equivalent projection definition.\n"
   31         "  -no_norm: Don't report 'normalized' parameter values.\n"
   32         "  filename: Name of the GeoTIFF file to report on.\n" );
   33         
   34     exit( 1 );
   35 }
   36 
   37 int main(int argc, char *argv[])
   38 {
   39     char    *fname = NULL;
   40     TIFF    *tif=(TIFF*)0;  /* TIFF-level descriptor */
   41     GTIF    *gtif=(GTIF*)0; /* GeoKey-level descriptor */
   42     int     i, norm_print_flag = 1, proj4_print_flag = 0;
   43     int     tfw_flag = 0, inv_flag = 0, dec_flag = 0;
   44     int         st_test_flag = 0;
   45 
   46     /*
   47      * Handle command line options.
   48      */
   49     for( i = 1; i < argc; i++ )
   50     {
   51         if( strcmp(argv[i],"-no_norm") == 0 )
   52             norm_print_flag = 0;
   53         else if( strcmp(argv[i],"-tfw") == 0 )
   54             tfw_flag = 1;
   55         else if( strcmp(argv[i],"-proj4") == 0 )
   56             proj4_print_flag = 1;
   57         else if( strcmp(argv[i],"-i") == 0 )
   58             inv_flag = 1;
   59         else if( strcmp(argv[i],"-d") == 0 )
   60             dec_flag = 1;
   61         else if( strcmp(argv[i],"-st_test") == 0 )
   62         {
   63             st_test_flag = 1;
   64             norm_print_flag = 0;
   65         }
   66         else if( fname == NULL && argv[i][0] != '-' )
   67             fname = argv[i];
   68         else
   69         {
   70             Usage();
   71         }
   72     }
   73 
   74     if( fname == NULL && !st_test_flag )
   75         Usage();
   76 
   77     /*
   78      * Open the file, read the GeoTIFF information, and print to stdout. 
   79      */
   80 
   81     if( st_test_flag )
   82     {
   83         tif = st_setup_test_info();
   84         gtif = GTIFNewSimpleTags( tif );
   85     }
   86     else
   87     {
   88         tif=XTIFFOpen(fname,"r");
   89         if (!tif) goto failure;
   90     
   91         gtif = GTIFNew(tif);
   92         if (!gtif)
   93         {
   94             fprintf(stderr,"failed in GTIFNew\n");
   95             goto failure;
   96         }
   97     }
   98 
   99     if( tfw_flag )
  100     {
  101         WriteTFWFile( gtif, fname );
  102 
  103         goto Success;
  104     }
  105     
  106     /* dump the GeoTIFF metadata to std out */
  107 
  108     GTIFPrint(gtif,0,0);
  109 
  110     /*
  111      * Capture, and report normalized information if requested.
  112      */
  113 
  114     if( norm_print_flag )
  115     {
  116         GTIFDefn    defn;
  117         
  118         if( GTIFGetDefn( gtif, &defn ) )
  119         {
  120             int     xsize, ysize;
  121             
  122             printf( "\n" );
  123             GTIFPrintDefnEx( gtif, &defn, stdout );
  124 
  125             if( proj4_print_flag )
  126             {
  127                 printf( "\n" );
  128                 printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn));
  129             }
  130             
  131             TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize );
  132             TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize );
  133             GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag );
  134         }
  135 
  136     }
  137 
  138   Success:
  139     GTIFFree(gtif);
  140     if( st_test_flag )
  141         ST_Destroy( (ST_TIFF *) tif );
  142     else
  143         XTIFFClose(tif);
  144     return 0;
  145         
  146   failure:
  147     fprintf(stderr,"failure in listgeo\n");
  148     if (tif) XTIFFClose(tif);
  149     if (gtif) GTIFFree(gtif);
  150     return 1;
  151 }
  152 
  153 /*
  154  * Report the file(s) corner coordinates in projected coordinates, and
  155  * if possible lat/long.
  156  */
  157 
  158 static int GTIFReportACorner( GTIF *gtif, GTIFDefn *defn, FILE * fp_out,
  159                               const char * corner_name,
  160                               double x, double y, int inv_flag, int dec_flag )
  161 
  162 {
  163     double  x_saved, y_saved;
  164 
  165     /* Try to transform the coordinate into PCS space */
  166     if( !GTIFImageToPCS( gtif, &x, &y ) )
  167         return FALSE;
  168     
  169     x_saved = x;
  170     y_saved = y;
  171 
  172     fprintf( fp_out, "%-13s ", corner_name );
  173 
  174     if( defn->Model == ModelTypeGeographic )
  175     {
  176     if (dec_flag) 
  177     {
  178         fprintf( fp_out, "(%.7f,", x );
  179         fprintf( fp_out, "%.7f)\n", y );
  180     } 
  181     else 
  182     {
  183         fprintf( fp_out, "(%s,", GTIFDecToDMS( x, "Long", 2 ) );
  184         fprintf( fp_out, "%s)\n", GTIFDecToDMS( y, "Lat", 2 ) );
  185     }
  186     }
  187     else
  188     {
  189         fprintf( fp_out, "(%12.3f,%12.3f)", x, y );
  190 
  191         if( GTIFProj4ToLatLong( defn, 1, &x, &y ) )
  192         {
  193         if (dec_flag) 
  194         {
  195                 fprintf( fp_out, "  (%.7f,", x );
  196                 fprintf( fp_out, "%.7f)", y );
  197         } 
  198         else 
  199         {
  200                 const char* pszLong = GTIFDecToDMS( x, "Long", 2 );
  201                 if( pszLong[0] == 0 )
  202                 {
  203                     fprintf( fp_out, "  (invalid)" );
  204                 }
  205                 else
  206                 {
  207                     fprintf( fp_out, "  (%s,", pszLong );
  208                     fprintf( fp_out, "%s)", GTIFDecToDMS( y, "Lat", 2 ) );
  209                 }
  210         }
  211         }
  212 
  213         fprintf( fp_out, "\n" );
  214     }
  215 
  216     if( inv_flag && GTIFPCSToImage( gtif, &x_saved, &y_saved ) )
  217     {
  218         fprintf( fp_out, "      inverse (%11.3f,%11.3f)\n", x_saved, y_saved );
  219     }
  220     
  221     return TRUE;
  222 }
  223 
  224 static void GTIFPrintCorners( GTIF *gtif, GTIFDefn *defn, FILE * fp_out,
  225                               int xsize, int ysize, int inv_flag, int dec_flag )
  226 
  227 {
  228     printf( "\nCorner Coordinates:\n" );
  229 
  230     unsigned short raster_type = RasterPixelIsArea;
  231     GTIFKeyGetSHORT(gtif, GTRasterTypeGeoKey, &raster_type, 0, 1);
  232 
  233     double xmin = (raster_type == RasterPixelIsArea) ? 0.0 : -0.5;
  234     double ymin = xmin;
  235     double ymax = ymin + ysize;
  236     double xmax = xmin + xsize;
  237 
  238     if( !GTIFReportACorner( gtif, defn, fp_out,
  239                             "Upper Left", xmin, ymin, inv_flag, dec_flag ) )
  240     {
  241         printf( " ... unable to transform points between pixel/line and PCS space\n" );
  242         return;
  243     }
  244 
  245     GTIFReportACorner( gtif, defn, fp_out, "Lower Left", xmin, ymax,
  246                        inv_flag, dec_flag );
  247     GTIFReportACorner( gtif, defn, fp_out, "Upper Right", xmax, ymin,
  248                        inv_flag, dec_flag );
  249     GTIFReportACorner( gtif, defn, fp_out, "Lower Right", xmax, ymax,
  250                        inv_flag, dec_flag );
  251     GTIFReportACorner( gtif, defn, fp_out, "Center", xmin + xsize/2.0, ymin + ysize/2.0,
  252                        inv_flag, dec_flag );
  253 }
  254 
  255 /*
  256  * Write the defining matrix for this file to a .tfw file with the same
  257  * basename.
  258  */
  259 
  260 static void WriteTFWFile( GTIF * gtif, const char * tif_filename )
  261 
  262 {
  263     char    tfw_filename[1024];
  264     int     i;
  265     double  adfCoeff[6], x, y;
  266     FILE    *fp;
  267 
  268     /*
  269      * form .tfw filename
  270      */
  271     strncpy( tfw_filename, tif_filename, sizeof(tfw_filename)-4 );
  272     for( i = strlen(tfw_filename)-1; i > 0; i-- )
  273     {
  274         if( tfw_filename[i] == '.' )
  275         {
  276             strcpy( tfw_filename + i, ".tfw" );
  277             break;
  278         }
  279     }
  280 
  281     if( i <= 0 )
  282         strcat( tfw_filename, ".tfw" );
  283 
  284     /*
  285      * Compute the coefficients.
  286      */
  287     x = 0.5;
  288     y = 0.5;
  289     if( !GTIFImageToPCS( gtif, &x, &y ) )
  290     {
  291         fprintf( stderr, "Unable to translate image to PCS coordinates.\n" );
  292         return;
  293     }
  294     adfCoeff[4] = x;
  295     adfCoeff[5] = y;
  296 
  297     x = 1.5;
  298     y = 0.5;
  299     if( !GTIFImageToPCS( gtif, &x, &y ) )
  300         return;
  301     adfCoeff[0] = x - adfCoeff[4];
  302     adfCoeff[1] = y - adfCoeff[5];
  303 
  304     x = 0.5;
  305     y = 1.5;
  306     if( !GTIFImageToPCS( gtif, &x, &y ) )
  307         return;
  308     adfCoeff[2] = x - adfCoeff[4];
  309     adfCoeff[3] = y - adfCoeff[5];
  310 
  311     /*
  312      * Write out the coefficients.
  313      */
  314 
  315     fp = fopen( tfw_filename, "wt" );
  316     if( fp == NULL )
  317     {
  318         perror( "fopen" );
  319         fprintf( stderr, "Failed to open TFW file `%s'\n", tfw_filename );
  320         return;
  321     }
  322 
  323     for( i = 0; i < 6; i++ )
  324         fprintf( fp, "%24.10f\n", adfCoeff[i] );
  325 
  326     fclose( fp );
  327 
  328     fprintf( stderr, "World file written to '%s'.\n", tfw_filename); 
  329 }
  330 
  331 /************************************************************************/
  332 /*                         st_setup_test_info()                         */
  333 /*                                                                      */
  334 /*      Setup a ST_TIFF structure for a simulated TIFF file.  This      */
  335 /*      is just a hack to test the ST_ interface.                       */
  336 /************************************************************************/
  337 
  338 static TIFF *st_setup_test_info()
  339 
  340 {
  341     ST_TIFF *st;
  342     double dbl_data[100];
  343     unsigned short  shrt_data[] = 
  344         { 1,1,0,6,1024,0,1,1,1025,0,1,1,1026,34737,17,0,2052,0,1,9001,2054,0,1,9102,3072,0,1,26711 };
  345     char *ascii_data = "UTM    11 S E000|";
  346 
  347     st = ST_Create();
  348 
  349     dbl_data[0] = 60;
  350     dbl_data[1] = 60;
  351     dbl_data[2] = 0;
  352     
  353     ST_SetKey( st, 33550, 3, STT_DOUBLE, dbl_data );
  354 
  355     dbl_data[0] = 0;
  356     dbl_data[1] = 0;
  357     dbl_data[2] = 0;
  358     dbl_data[3] = 440720;
  359     dbl_data[4] = 3751320;
  360     dbl_data[5] = 0;
  361     ST_SetKey( st, 33922, 6, STT_DOUBLE, dbl_data );
  362 
  363     ST_SetKey( st, 34735, sizeof(shrt_data)/2, STT_SHORT, shrt_data );
  364     ST_SetKey( st, 34737, strlen(ascii_data)+1, STT_ASCII, ascii_data );
  365     
  366     return (TIFF *) st;
  367 }