Beispiel #1
0
int main(int argc, char *argv[])
{
    char	*fname = NULL;
    char        *outfile = NULL;
    TIFF 	*tif=(TIFF*)0;  /* TIFF-level descriptor */
    GTIF	*gtif=(GTIF*)0; /* GeoKey-level descriptor */
    int		i, j, norm_print_flag = 0, proj4_print_flag = 0;
    int		tfw_flag = 0, inv_flag = 0, dec_flag = 1;
    int         st_test_flag = 0;
    size_t npixels;
    int16* elevations;
    int x, y, skip;
    GTIFDefn	defn;
    FILE* out;

    /*
     * Handle command line options.
     */
    for( i = 1; i < argc; i++ )
    {
        if( fname == NULL && argv[i][0] != '-' )
            fname = argv[i];
        else
        {
            Usage();
        }
    }
    if( fname == NULL)
        Usage();

    skip = 5;

    /*
     * Open the file, read the GeoTIFF information, and print to stdout. 
     */
    int flength = strlen(fname);
    outfile = (char *) malloc(flength);
    strncpy(outfile,fname,flength-4);
    strcat(outfile, ".asc\0");
    out = fopen(outfile, "w");
    printf("Writing to %s\n\n",outfile);
    free(outfile);

    tif=XTIFFOpen(fname,"r");
    if (!tif) goto failure;
    
    gtif = GTIFNew(tif);
    if (!gtif)
      {
	fprintf(stderr,"failed in GTIFNew\n");
	goto failure;
      }
	
    /* dump the GeoTIFF metadata to std out */

    GTIFPrint(gtif,0,0);

    if( GTIFGetDefn( gtif, &defn ) )
      {
	uint32		xsize, ysize;
        
	printf( "\n" );
	GTIFPrintDefn( &defn, stdout );
	
	if( proj4_print_flag )
	  {
	    printf( "\n" );
	    printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn));
	  }
	int count, orient;
	double* data;
	TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize );
	TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize );
	TIFFGetField( tif, TIFFTAG_GEOPIXELSCALE, &count, &data);
	TIFFGetField( tif, TIFFTAG_ORIENTATION, &orient );
	printf("Orientation:%d\n",orient);

	GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag );
	const char* project = "ASTERGDEM";
	const char* yymmdd = "090629";
	double originx, originy;
	GTIFImageToPCS( gtif, &originx, &originy);
	int lat = (int)originy*1000;
	int lon = (int)originx*1000;
	int xmin = 0;
	int ymin = 0;
	int nx = xsize/skip + 1;
	int ny = ysize/skip + 1;
	int dx = 30 * skip;
	int dy = 30 * skip;
	int16 elev = 0;
	fprintf(out, "%12s%12s%7d%7d%7d%7d%7d%7d%7d%7d\n",
	       project, yymmdd, lat, lon, xmin, ymin, nx, ny, dx,dy);
	npixels = xsize * ysize;
	int16* buf;
	tsample_t sample;
	int nbytes;
	nbytes = TIFFScanlineSize(tif);
	buf = (int16*) _TIFFmalloc(nbytes);
	elevations = (int16*) _TIFFmalloc(npixels * sizeof (int16));
	if (elevations != NULL) {
	    for( y = 0; y < ysize; y++ )
	      {
		uint32 row = ysize - 1 - y;
		TIFFReadScanline(tif, buf, row, sample);
		for( x = 0; x < xsize; x++ ) 
		  {
		    elevations[y*xsize + x] = buf[x];
		  }
	      }	    
	  _TIFFfree(buf);
	}
	for( y = 0; y < ysize; y+=skip ) {
	  for( x = 0; x < xsize; x+=skip ) {
	    fprintf(out, "%6d", elevations[y*xsize + x]);
	  }
	  fprintf(out, "\n");
	}
	_TIFFfree(elevations);

      }
    
  Success:
    GTIFFree(gtif);
    if( st_test_flag )
        ST_Destroy( (ST_TIFF *) tif );
    else
        XTIFFClose(tif);
    GTIFDeaccessCSV();
    return 0;
		
  failure:
    fprintf(stderr,"failure in listgeo\n");
    if (tif) XTIFFClose(tif);
    if (gtif) GTIFFree(gtif);
    GTIFDeaccessCSV();
    return 1;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
    char	*fname = NULL;
    TIFF 	*tif=(TIFF*)0;  /* TIFF-level descriptor */
    GTIF	*gtif=(GTIF*)0; /* GeoKey-level descriptor */
    int		i, norm_print_flag = 1, proj4_print_flag = 0;
    int		tfw_flag = 0, inv_flag = 0, dec_flag = 0;
    int         st_test_flag = 0;

    /*
     * Handle command line options.
     */
    for( i = 1; i < argc; i++ )
    {
        if( strcmp(argv[i],"-no_norm") == 0 )
            norm_print_flag = 0;
        else if( strcmp(argv[i],"-t") == 0 )
        {
            CSVDirName = argv[++i];
            SetCSVFilenameHook( CSVFileOverride );
        }
        else if( strcmp(argv[i],"-tfw") == 0 )
            tfw_flag = 1;
        else if( strcmp(argv[i],"-proj4") == 0 )
            proj4_print_flag = 1;
        else if( strcmp(argv[i],"-i") == 0 )
            inv_flag = 1;
        else if( strcmp(argv[i],"-d") == 0 )
            dec_flag = 1;
        else if( strcmp(argv[i],"-st_test") == 0 )
        {
            st_test_flag = 1;
            norm_print_flag = 0;
        }
        else if( fname == NULL && argv[i][0] != '-' )
            fname = argv[i];
        else
        {
            Usage();
        }
    }

    if( fname == NULL && !st_test_flag )
        Usage();

    /*
     * Open the file, read the GeoTIFF information, and print to stdout. 
     */

    if( st_test_flag )
    {
        tif = st_setup_test_info();
        gtif = GTIFNewSimpleTags( tif );
    }
    else
    {
        tif=XTIFFOpen(fname,"r");
        if (!tif) goto failure;
	
        gtif = GTIFNew(tif);
        if (!gtif)
        {
            fprintf(stderr,"failed in GTIFNew\n");
            goto failure;
        }
    }

    if( tfw_flag )
    {
        WriteTFWFile( gtif, fname );

        goto Success;
    }
	
    /* dump the GeoTIFF metadata to std out */

    GTIFPrint(gtif,0,0);

    /*
     * Capture, and report normalized information if requested.
     */

    if( norm_print_flag )
    {
        GTIFDefn	defn;
        
        if( GTIFGetDefn( gtif, &defn ) )
        {
            int		xsize, ysize;
            
            printf( "\n" );
            GTIFPrintDefn( &defn, stdout );

            if( proj4_print_flag )
            {
                printf( "\n" );
                printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn));
            }
            
            TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize );
            TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize );
            GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag );
        }

    }

  Success:
    GTIFFree(gtif);
    if( st_test_flag )
        ST_Destroy( (ST_TIFF *) tif );
    else
        XTIFFClose(tif);
    GTIFDeaccessCSV();
    return 0;
		
  failure:
    fprintf(stderr,"failure in listgeo\n");
    if (tif) XTIFFClose(tif);
    if (gtif) GTIFFree(gtif);
    GTIFDeaccessCSV();
    return 1;
}
Beispiel #3
0
bool DEM::readDem(char* fname) 
{

    demFilename = fname;
    TIFF 	*tif=(TIFF*)0;  /* TIFF-level descriptor */
    GTIF	*gtif=(GTIF*)0; /* GeoKey-level descriptor */
    int	proj4_print_flag = 0;
    int	inv_flag = 0, dec_flag = 1;
    int      st_test_flag = 0;
    GTIFDefn	defn;

    /*
     * Open the file, read the GeoTIFF information, and print some info to stdout. 
     */
	
    tif=XTIFFOpen(fname,"r");
    if (!tif) goto failure;
    
    gtif = GTIFNew(tif);
    if (!gtif)
	{
		fprintf(stderr,"failed in GTIFNew\n");
		goto failure;
	}
	
    /* dump the GeoTIFF metadata to std out */
	
    GTIFPrint(gtif,0,0);
	
    if( GTIFGetDefn( gtif, &defn ) )
	{
		printf( "\n" );
		GTIFPrintDefn( &defn, stdout );
		
		if( proj4_print_flag )
		{
			printf( "\n" );
			printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn));
		}
		int count, orient;
		double* data;
		TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize );
		TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize );
		TIFFGetField( tif, TIFFTAG_GEOPIXELSCALE, &count, &data);
		TIFFGetField( tif, TIFFTAG_ORIENTATION, &orient );
		printf("Orientation:%d\n",orient);
		
		GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag );
		double originx = 0.0;
		double originy = ysize;
		GTIFImageToPCS( gtif, &originx, &originy);
		refLat = originy;
		refLon = originx;
		npixels = xsize * ysize;
		int16* buf;
		tsample_t sample;
		int nbytes;
		nbytes = TIFFScanlineSize(tif);
		buf = (int16*) _TIFFmalloc(nbytes);
		elevations = (int16*) _TIFFmalloc(npixels * sizeof (int16));
		if (elevations != NULL) {
			for(unsigned int y = 0; y < ysize; y++ )
			{
				uint32 row = ysize - 1 - y;
				TIFFReadScanline(tif, buf, row, sample);
				for(unsigned int x = 0; x < xsize; x++ ) 
				{
					elevations[y*xsize + x] = buf[x];
				}
			}	    
			_TIFFfree(buf);
		}
		_TIFFfree(elevations);
		
	}
    

    GTIFFree(gtif);
    if( st_test_flag )
        ST_Destroy( (ST_TIFF *) tif );
    else
        XTIFFClose(tif);
    GTIFDeaccessCSV();
    return true;
	
failure:
    fprintf(stderr,"failure in listgeo\n");
    if (tif) XTIFFClose(tif);
    if (gtif) GTIFFree(gtif);
    GTIFDeaccessCSV();
    return false;
}