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
This commit is contained in:
parent
2c2bc8b551
commit
af505d2f0f
3 changed files with 171 additions and 17 deletions
|
@ -1,6 +1,8 @@
|
||||||
/* main.cxx -- main loop
|
/* main.cxx -- main loop
|
||||||
*
|
*
|
||||||
* Written by Curtis Olson, started February 1998.
|
* 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
|
* Copyright (C) 1998, 1999 Curtis L. Olson - http://www.flightgear.org/~curt
|
||||||
*
|
*
|
||||||
|
@ -36,28 +38,148 @@
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include "rawdem.h"
|
#include "rawdem.h"
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define M_ISDIR _S_IFDIR
|
||||||
|
#else
|
||||||
|
#define M_ISDIR __S_IFDIR
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
static void give_help( char * name )
|
||||||
fgRAWDEM raw;
|
{
|
||||||
char basename[256], output_dir[256], hdr_file[256], dem_file[256];
|
printf("Usage: %s [OPTIONS] <input_file_basename> <output_dir>\n", name);
|
||||||
int start_lat, end_lat;
|
printf("Options:\n");
|
||||||
int i;
|
printf(" --min-lat=<degs> - set minimum latitude for output.\n");
|
||||||
// double total;
|
printf(" --max-lat=<degs> - set maximum latitude for output.\n");
|
||||||
// unsigned char buf[2];
|
printf(" --min-lon=<degs> - set minimum longitude for output.\n");
|
||||||
// short int value;
|
printf(" --max-lon=<degs> - set maximum longitude for output.\n");
|
||||||
|
|
||||||
if ( argc != 3 ) {
|
|
||||||
printf("Usage: %s <input_file_basename> <output_dir>\n", argv[0]);
|
|
||||||
exit(-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
static fgRAWDEM raw;
|
||||||
|
static char basename[256], output_dir[256], hdr_file[256], dem_file[256];
|
||||||
|
int start_lat, end_lat;
|
||||||
|
int i;
|
||||||
|
int last_arg = 1;
|
||||||
|
double min_lat, max_lat, min_lon, max_lon;
|
||||||
|
|
||||||
|
#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 */
|
/* get basename */
|
||||||
strcpy(basename, argv[1]);
|
strcpy(basename, argv[last_arg]);
|
||||||
|
|
||||||
/* get output dir */
|
/* 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 */
|
/* generate header file name */
|
||||||
strcpy(hdr_file, basename);
|
strcpy(hdr_file, basename);
|
||||||
|
|
|
@ -85,7 +85,7 @@ int reads(int fd, char *buf, unsigned int len) {
|
||||||
* DEM file */
|
* DEM file */
|
||||||
void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
FILE *hdr;
|
FILE *hdr;
|
||||||
char line[256], key[256], value[256];
|
static char line[256], key[256], value[256];
|
||||||
int i, len, offset;
|
int i, len, offset;
|
||||||
double tmp;
|
double tmp;
|
||||||
|
|
||||||
|
@ -100,7 +100,13 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
/* process each line */
|
/* process each line */
|
||||||
while ( (reads(fileno(hdr), line, 256) != 0) ) {
|
while ( (reads(fileno(hdr), line, 256) != 0) ) {
|
||||||
|
|
||||||
|
len = strlen(line);
|
||||||
|
while(len && ((line[len - 1] == '\n')||(line[len - 1] == '\r'))) {
|
||||||
|
len--;
|
||||||
|
line[len] = 0; // kill EOL characters
|
||||||
|
}
|
||||||
printf("%s ", line);
|
printf("%s ", line);
|
||||||
|
|
||||||
len = strlen(line);
|
len = strlen(line);
|
||||||
|
|
||||||
/* extract key */
|
/* extract key */
|
||||||
|
@ -128,13 +134,17 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
if ( strcmp(key, "BYTEORDER") == 0 ) {
|
if ( strcmp(key, "BYTEORDER") == 0 ) {
|
||||||
if ( strcmp( value, "M" ) == 0 ) {
|
if ( strcmp( value, "M" ) == 0 ) {
|
||||||
raw->big_endian = 1;
|
raw->big_endian = 1;
|
||||||
|
printf( "- set big_endian\n" );
|
||||||
} else {
|
} else {
|
||||||
|
printf( "- unset big_endian (not 'M'!)\n" );
|
||||||
raw->big_endian = 0;
|
raw->big_endian = 0;
|
||||||
}
|
}
|
||||||
} else if ( strcmp(key, "NROWS") == 0 ) {
|
} else if ( strcmp(key, "NROWS") == 0 ) {
|
||||||
raw->nrows = atoi(value);
|
raw->nrows = atoi(value);
|
||||||
|
printf( "- set rows to %d\n", raw->nrows );
|
||||||
} else if ( strcmp(key, "NCOLS") == 0 ) {
|
} else if ( strcmp(key, "NCOLS") == 0 ) {
|
||||||
raw->ncols = atoi(value);
|
raw->ncols = atoi(value);
|
||||||
|
printf( "- set cols to %d\n", raw->ncols );
|
||||||
} else if ( strcmp(key, "ULXMAP") == 0 ) {
|
} else if ( strcmp(key, "ULXMAP") == 0 ) {
|
||||||
tmp = atof(value);
|
tmp = atof(value);
|
||||||
#ifdef HAVE_RINT
|
#ifdef HAVE_RINT
|
||||||
|
@ -142,6 +152,8 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
#else
|
#else
|
||||||
raw->ulxmap = (int)round(tmp * 3600.0); /* convert to arcsec */
|
raw->ulxmap = (int)round(tmp * 3600.0); /* convert to arcsec */
|
||||||
#endif
|
#endif
|
||||||
|
printf( "- set ulxmap to %d arcsecs (%d degrees)\n", raw->ulxmap,
|
||||||
|
raw->ulxmap / 3600);
|
||||||
} else if ( strcmp(key, "ULYMAP") == 0 ) {
|
} else if ( strcmp(key, "ULYMAP") == 0 ) {
|
||||||
tmp = atof(value);
|
tmp = atof(value);
|
||||||
#ifdef HAVE_RINT
|
#ifdef HAVE_RINT
|
||||||
|
@ -149,6 +161,8 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
#else
|
#else
|
||||||
raw->ulymap = (int)round(tmp * 3600.0); /* convert to arcsec */
|
raw->ulymap = (int)round(tmp * 3600.0); /* convert to arcsec */
|
||||||
#endif
|
#endif
|
||||||
|
printf( "- set ulymap to %d arcsecs (%d degrees)\n", raw->ulymap,
|
||||||
|
raw->ulymap / 3600);
|
||||||
} else if ( strcmp(key, "XDIM") == 0 ) {
|
} else if ( strcmp(key, "XDIM") == 0 ) {
|
||||||
tmp = atof(value);
|
tmp = atof(value);
|
||||||
#ifdef HAVE_RINT
|
#ifdef HAVE_RINT
|
||||||
|
@ -156,6 +170,8 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
#else
|
#else
|
||||||
raw->xdim = (int)round(tmp * 3600.0); /* convert to arcsec */
|
raw->xdim = (int)round(tmp * 3600.0); /* convert to arcsec */
|
||||||
#endif
|
#endif
|
||||||
|
printf( "- set xdim to %d arcsecs (%f degrees)\n", raw->xdim,
|
||||||
|
(double)raw->xdim / 3600.0);
|
||||||
} else if ( strcmp(key, "YDIM") == 0 ) {
|
} else if ( strcmp(key, "YDIM") == 0 ) {
|
||||||
tmp = atof(value);
|
tmp = atof(value);
|
||||||
#ifdef HAVE_RINT
|
#ifdef HAVE_RINT
|
||||||
|
@ -163,8 +179,11 @@ void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
|
||||||
#else
|
#else
|
||||||
raw->ydim = (int)round(tmp * 3600.0); /* convert to arcsec */
|
raw->ydim = (int)round(tmp * 3600.0); /* convert to arcsec */
|
||||||
#endif
|
#endif
|
||||||
|
printf( "- set ydim to %d arcsecs (%f degrees)\n", raw->ydim,
|
||||||
|
(double)raw->ydim / 3600.0);
|
||||||
} else {
|
} else {
|
||||||
/* ignore for now */
|
/* 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; }
|
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);
|
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);
|
printf("outfile = %s\n", outfile);
|
||||||
|
|
||||||
if ( (fd = fopen(outfile, "w")) == NULL ) {
|
if ( (fd = fopen(outfile, "w")) == NULL ) {
|
||||||
|
@ -468,7 +496,7 @@ void rawProcessStrip( fgRAWDEM *raw, int lat_degrees, char *path ) {
|
||||||
/* convert to arcsec */
|
/* convert to arcsec */
|
||||||
lat = lat_degrees * 3600;
|
lat = lat_degrees * 3600;
|
||||||
|
|
||||||
printf("Max Latitude = %d arcsec\n", lat);
|
printf("Max Latitude = %d arcsec (%f degs)\n", lat, lat_degrees);
|
||||||
|
|
||||||
/* validity check ... */
|
/* validity check ... */
|
||||||
if ( (lat > raw->rooty) ||
|
if ( (lat > raw->rooty) ||
|
||||||
|
|
|
@ -30,6 +30,8 @@
|
||||||
#define MAX_COLS 7200
|
#define MAX_COLS 7200
|
||||||
#define MAX_COLS_X_2 14400
|
#define MAX_COLS_X_2 14400
|
||||||
|
|
||||||
|
#define BAD_LATLON 12345.0
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
/* header info */
|
/* header info */
|
||||||
int big_endian; /* true if data source is big, false if little */
|
int big_endian; /* true if data source is big, false if little */
|
||||||
|
@ -47,6 +49,8 @@ typedef struct {
|
||||||
/* file ptr */
|
/* file ptr */
|
||||||
int fd; /* Raw DEM file descriptor */
|
int fd; /* Raw DEM file descriptor */
|
||||||
|
|
||||||
|
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
|
/* storage area for a 1 degree high strip of data. Note, for
|
||||||
* convenience this is in y,x order */
|
* convenience this is in y,x order */
|
||||||
short strip[120][MAX_ROWS];
|
short strip[120][MAX_ROWS];
|
||||||
|
|
Loading…
Reference in a new issue