From af505d2f0f0c99450701c2073a1853eff0cfd7e3 Mon Sep 17 00:00:00 2001 From: Frederic Bouvier Date: Sat, 28 Mar 2009 14:42:41 +0100 Subject: [PATCH] Geoff McLane: allow limits, max/min lat/lon to be added to raw2ascii Frederic Bouvier: _set_output_format is only available in VS2005 and later --- src/Prep/DemRaw2ascii/main.c | 144 ++++++++++++++++++++++++++++++--- src/Prep/DemRaw2ascii/rawdem.c | 38 +++++++-- src/Prep/DemRaw2ascii/rawdem.h | 6 +- 3 files changed, 171 insertions(+), 17 deletions(-) diff --git a/src/Prep/DemRaw2ascii/main.c b/src/Prep/DemRaw2ascii/main.c index 2d509b4c..27829df5 100644 --- a/src/Prep/DemRaw2ascii/main.c +++ b/src/Prep/DemRaw2ascii/main.c @@ -1,6 +1,8 @@ /* main.cxx -- main loop * * Written by Curtis Olson, started February 1998. + * Modified by Geoff McLane, March, 2009 + * to add min, max, lon, lat, to limit the output of ASCII DEM files. * * Copyright (C) 1998, 1999 Curtis L. Olson - http://www.flightgear.org/~curt * @@ -36,29 +38,149 @@ # include #endif +#include +#include #include "rawdem.h" +#ifdef _MSC_VER +#define M_ISDIR _S_IFDIR +#else +#define M_ISDIR __S_IFDIR +#endif + +static void give_help( char * name ) +{ + printf("Usage: %s [OPTIONS] \n", name); + printf("Options:\n"); + printf(" --min-lat= - set minimum latitude for output.\n"); + printf(" --max-lat= - set maximum latitude for output.\n"); + printf(" --min-lon= - set minimum longitude for output.\n"); + printf(" --max-lon= - set maximum longitude for output.\n"); +} + +static int is_file_or_directory( char * cur_item ) +{ + struct stat buf; + if( stat( cur_item, &buf ) == 0 ) { + if ( buf.st_mode & M_ISDIR ) + return 1; // is directory + else + return 2; // is file + } + return 0; +} int main(int argc, char **argv) { - fgRAWDEM raw; - char basename[256], output_dir[256], hdr_file[256], dem_file[256]; + static fgRAWDEM raw; + static char basename[256], output_dir[256], hdr_file[256], dem_file[256]; int start_lat, end_lat; int i; - // double total; - // unsigned char buf[2]; - // short int value; + int last_arg = 1; + double min_lat, max_lat, min_lon, max_lon; - if ( argc != 3 ) { - printf("Usage: %s \n", argv[0]); - exit(-1); +#if defined( _MSC_VER ) && _MSC_VER >= 1400 // set 2-ditit exponent - defaults to 3 - Only available for VS2005 + _set_output_format( _TWO_DIGIT_EXPONENT ); +#endif // _MSC_VER + + min_lat = max_lat = min_lon = max_lon = BAD_LATLON; + for( i = 1; i < argc; i++ ) + { + char * arg = argv[i]; + if (*arg == '-') { + // option + if((strcmp(arg,"-h") == 0)|| + (strcmp(arg,"--help") == 0)) { + give_help( argv[0] ); + exit(0); + } else if( strncmp(arg,"--min-lon=", 10) == 0 ) { + min_lon = atof( &arg[10] ); + } else if( strncmp(arg,"--max-lon=", 10) == 0 ) { + max_lon = atof( &arg[10] ); + } else if( strncmp(arg,"--min-lat=", 10) == 0 ) { + min_lat = atof( &arg[10] ); + } else if( strncmp(arg,"--max-lat=", 10) == 0 ) { + max_lat = atof( &arg[10] ); + } else { + printf( "ERROR: Unknown argument [%s]. Use -h for help.\n", arg ); + exit(1); + } + last_arg = i + 1; + } else + break; } + if ( (argc - last_arg) != 2 ) { + give_help( argv[0] ); + exit(1); + } + if(( min_lat != BAD_LATLON )&& + (( min_lat < -90.0 )||( min_lat > 90.0 ))) { + printf( "ERROR: Bad min-lat [%f]!\n", min_lat ); + exit(1); + } + if(( max_lat != BAD_LATLON )&& + (( max_lat < -90.0 )||( max_lat > 90.0 ))) { + printf( "ERROR: Bad max-lat [%f]!\n", max_lat ); + exit(1); + } + if(( min_lon != BAD_LATLON )&& + (( min_lon < -180.0 )||( min_lon > 180.0 ))) { + printf( "ERROR: Bad min-lon [%f]!\n", min_lon ); + exit(1); + } + if(( max_lon != BAD_LATLON )&& + (( max_lon < -180.0 )||( max_lon > 180.0 ))) { + printf( "ERROR: Bad max-lon [%f]!\n", max_lon ); + exit(1); + } + if(( min_lat != BAD_LATLON )&& + ( max_lat != BAD_LATLON )&& + ( min_lat > max_lat )) + { + printf( "ERROR: Bad min-lat [%f] NOT less than max-lat [%f]!\n", min_lat, max_lat ); + exit(1); + } + if(( min_lon != BAD_LATLON )&& + ( max_lon != BAD_LATLON )&& + ( min_lon > max_lon )) + { + printf( "ERROR: Bad min-lon [%f] NOT less than max-lon [%f]!\n", min_lon, max_lon ); + exit(1); + } + + if(( min_lat != BAD_LATLON )|| + ( max_lat != BAD_LATLON )|| + ( min_lon != BAD_LATLON )|| + ( max_lon != BAD_LATLON )) + { + printf( "Limited to " ); + if( min_lat != BAD_LATLON ) + printf( "min lat [%f] ", min_lat ); + if( max_lat != BAD_LATLON ) + printf( "max lat [%f] ", max_lat ); + if( min_lon != BAD_LATLON ) + printf( "min lon [%f] ", min_lon ); + if( max_lon != BAD_LATLON ) + printf( "max lon [%f] ", max_lon ); + printf( "\n" ); + } + + /* set any mins and max */ + raw.min_lat = min_lat; + raw.min_lon = min_lon; + raw.max_lat = max_lat; + raw.max_lon = max_lon; + /* get basename */ - strcpy(basename, argv[1]); + strcpy(basename, argv[last_arg]); /* get output dir */ - strcpy(output_dir, argv[2]); - + strcpy(output_dir, argv[last_arg+1]); + if ( is_file_or_directory( output_dir ) != 1 ) { + printf( "ERROR: Ouput directory [%s], does not exist!\n", output_dir ); + exit(1); + } + /* generate header file name */ strcpy(hdr_file, basename); strcat(hdr_file, ".HDR"); diff --git a/src/Prep/DemRaw2ascii/rawdem.c b/src/Prep/DemRaw2ascii/rawdem.c index d17f99f0..4a7da74d 100644 --- a/src/Prep/DemRaw2ascii/rawdem.c +++ b/src/Prep/DemRaw2ascii/rawdem.c @@ -85,7 +85,7 @@ int reads(int fd, char *buf, unsigned int len) { * DEM file */ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { FILE *hdr; - char line[256], key[256], value[256]; + static char line[256], key[256], value[256]; int i, len, offset; double tmp; @@ -100,7 +100,13 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { /* process each line */ while ( (reads(fileno(hdr), line, 256) != 0) ) { - printf("%s", line); + len = strlen(line); + while(len && ((line[len - 1] == '\n')||(line[len - 1] == '\r'))) { + len--; + line[len] = 0; // kill EOL characters + } + printf("%s ", line); + len = strlen(line); /* extract key */ @@ -128,13 +134,17 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { if ( strcmp(key, "BYTEORDER") == 0 ) { if ( strcmp( value, "M" ) == 0 ) { raw->big_endian = 1; - } else { + printf( "- set big_endian\n" ); + } else { + printf( "- unset big_endian (not 'M'!)\n" ); raw->big_endian = 0; } } else if ( strcmp(key, "NROWS") == 0 ) { raw->nrows = atoi(value); + printf( "- set rows to %d\n", raw->nrows ); } else if ( strcmp(key, "NCOLS") == 0 ) { raw->ncols = atoi(value); + printf( "- set cols to %d\n", raw->ncols ); } else if ( strcmp(key, "ULXMAP") == 0 ) { tmp = atof(value); #ifdef HAVE_RINT @@ -142,6 +152,8 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { #else raw->ulxmap = (int)round(tmp * 3600.0); /* convert to arcsec */ #endif + printf( "- set ulxmap to %d arcsecs (%d degrees)\n", raw->ulxmap, + raw->ulxmap / 3600); } else if ( strcmp(key, "ULYMAP") == 0 ) { tmp = atof(value); #ifdef HAVE_RINT @@ -149,6 +161,8 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { #else raw->ulymap = (int)round(tmp * 3600.0); /* convert to arcsec */ #endif + printf( "- set ulymap to %d arcsecs (%d degrees)\n", raw->ulymap, + raw->ulymap / 3600); } else if ( strcmp(key, "XDIM") == 0 ) { tmp = atof(value); #ifdef HAVE_RINT @@ -156,6 +170,8 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { #else raw->xdim = (int)round(tmp * 3600.0); /* convert to arcsec */ #endif + printf( "- set xdim to %d arcsecs (%f degrees)\n", raw->xdim, + (double)raw->xdim / 3600.0); } else if ( strcmp(key, "YDIM") == 0 ) { tmp = atof(value); #ifdef HAVE_RINT @@ -163,8 +179,11 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { #else raw->ydim = (int)round(tmp * 3600.0); /* convert to arcsec */ #endif + printf( "- set ydim to %d arcsecs (%f degrees)\n", raw->ydim, + (double)raw->ydim / 3600.0); } else { /* ignore for now */ + printf( "- ignore for now\n" ); } } @@ -258,7 +277,7 @@ void rawReadNextRow( fgRAWDEM *raw, int index ) { if ( value > raw->max ) { raw->max = value; } } - printf( " so far, min = %d max = %d\n", raw->min, raw->max ); + /* printf( " so far, min = %d max = %d\n", raw->min, raw->max ); */ } @@ -331,6 +350,15 @@ void rawDumpAsciiDEM( fgRAWDEM *raw, char *path, int ilon, int ilat ) { sprintf(outfile, "%s/%c%03d%c%02d.dem", path, lon_sign, lon, lat_sign, lat); + if((( raw->min_lat != BAD_LATLON)&&( (double)ilat < raw->min_lat ))|| + (( raw->max_lat != BAD_LATLON)&&( (double)ilat > raw->max_lat ))|| + (( raw->min_lon != BAD_LATLON)&&( (double)ilon < raw->min_lon ))|| + (( raw->max_lon != BAD_LATLON)&&( (double)ilon > raw->max_lon ))) + { + printf("outfile = %s not written. Outside range\n", outfile); + return; + } + printf("outfile = %s\n", outfile); if ( (fd = fopen(outfile, "w")) == NULL ) { @@ -468,7 +496,7 @@ void rawProcessStrip( fgRAWDEM *raw, int lat_degrees, char *path ) { /* convert to arcsec */ lat = lat_degrees * 3600; - printf("Max Latitude = %d arcsec\n", lat); + printf("Max Latitude = %d arcsec (%f degs)\n", lat, lat_degrees); /* validity check ... */ if ( (lat > raw->rooty) || diff --git a/src/Prep/DemRaw2ascii/rawdem.h b/src/Prep/DemRaw2ascii/rawdem.h index 8da2a70b..d615dbb1 100644 --- a/src/Prep/DemRaw2ascii/rawdem.h +++ b/src/Prep/DemRaw2ascii/rawdem.h @@ -30,6 +30,8 @@ #define MAX_COLS 7200 #define MAX_COLS_X_2 14400 +#define BAD_LATLON 12345.0 + typedef struct { /* header info */ int big_endian; /* true if data source is big, false if little */ @@ -47,7 +49,9 @@ typedef struct { /* file ptr */ int fd; /* Raw DEM file descriptor */ - /* storage area for a 1 degree high strip of data. Note, for + double min_lat, max_lat, min_lon, max_lon; /* some limits, if any */ + + /* storage area for a 1 degree high strip of data. Note, for * convenience this is in y,x order */ short strip[120][MAX_ROWS];