-/************************************************************\
- ** Created originally by Jonathan Naylor, G4KLX **
- ** Later embellished by John Magliacane, KD2BD to **
- ** detect and handle voids found in the SRTM data **
- ************************************************************
- ** Compile like this: **
- ** cc -Wall -O3 -s -lbz2 srtm2sdf.c -o srtm2sdf **
- ** Last modification: 18-Mar-2006 **
-\************************************************************/
+/**************************************************************\
+ ** Created originally by Jonathan Naylor, G4KLX **
+ ** Later embellished by John Magliacane, KD2BD to **
+ ** detect and handle voids found in the SRTM data **
+ ** and to handle SRTM data in .BIL as well as .HGT format. **
+ **************************************************************
+ ** Compile like this: **
+ ** cc -Wall -O3 -s -lbz2 srtm2sdf.c -o srtm2sdf **
+ ** Last modification: 23-Sep-2007 **
+\**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define BZBUFFER 65536
-char sdf_filename[25], sdf_path[255], replacement_flag, opened=0;
-int srtm[1201][1201], usgs[1200][1200], max_north, max_west,
- min_north, min_west, merge=0, min_elevation, bzerror;
+char sdf_filename[25], sdf_path[255], replacement_flag, opened=0,
+ hgt=0, bil=0;
+
+int srtm[1201][1201], usgs[1200][1200], max_north, max_west,
+ min_north, min_west, merge=0, min_elevation, bzerror;
int ReadSRTM(char *filename)
{
- int x, y, infile, byte, bytes_read;
+ int x, y, infile, byte=0, bytes_read;
unsigned char error, buffer[2];
- char north[3], west[4], *base=NULL;
+ char north[3], west[4], *base=NULL, blw_filename[255];
+ double cell_size, deg_north, deg_west;
+ FILE *fd=NULL;
if (strstr(filename, ".zip")!=NULL)
{
}
- if (strstr(filename, ".hgt")==NULL)
+ if (strstr(filename, ".tgz")!=NULL)
+ {
+ fprintf(stderr, "*** Error: \"%s\" must be uncompressed\n",filename);
+ return -1;
+
+ }
+
+ if ((strstr(filename, ".hgt")==NULL) && (strstr(filename, ".bil")==NULL))
{
- fprintf(stderr, "*** Error: \"%s\" does not have the correct extension (.hgt)\n",filename);
+ fprintf(stderr, "*** Error: \"%s\" does not have the correct extension (.hgt or .bil)\n",filename);
return -1;
}
+ if (strstr(filename, ".hgt")!=NULL)
+ hgt=1;
+
+ if (strstr(filename, ".bil")!=NULL)
+ bil=1;
+
base=strrchr(filename, '/');
-
+
if (base==NULL)
base=filename;
else
base+=1;
- north[0]=base[1];
- north[1]=base[2];
- north[2]=0;
+ if (hgt)
+ {
+ /* We obtain coordinates from the base of the .HGT filename */
+
+ north[0]=base[1];
+ north[1]=base[2];
+ north[2]=0;
- west[0]=base[4];
- west[1]=base[5];
- west[2]=base[6];
- west[3]=0;
+ west[0]=base[4];
+ west[1]=base[5];
+ west[2]=base[6];
+ west[3]=0;
- if ((base[0]!='N' && base[0]!='S') || (base[3]!='W' && base[3]!='E'))
- {
- fprintf(stderr, "*** Error: \"%s\" doesn't look like a valid SRTM filename.\n", filename);
- return -1;
+ if ((base[0]!='N' && base[0]!='S') || (base[3]!='W' && base[3]!='E'))
+ {
+ fprintf(stderr, "*** Error: \"%s\" doesn't look like a valid .hgt SRTM filename.\n", filename);
+ return -1;
+ }
+
+ max_west=atoi(west);
+
+ if (base[3]=='E')
+ max_west=360-max_west;
+
+ min_west=max_west-1;
+
+ if (max_west==360)
+ max_west=0;
+
+ if (base[0]=='N')
+ min_north=atoi(north);
+ else
+ min_north=-atoi(north);
+
+ max_north=min_north+1;
}
- max_west=atoi(west);
+ if (bil)
+ {
+ /* We obtain .BIL file coordinates
+ from the corresponding .BLW file */
- if (base[3]=='E')
- max_west=360-max_west;
+ strncpy(blw_filename,filename,250);
+ x=strlen(filename);
- min_west=max_west-1;
+ if (x>3)
+ {
+ blw_filename[x-2]='l';
+ blw_filename[x-1]='w';
+ blw_filename[x]=0;
- if (max_west==360)
- max_west=0;
+ fd=fopen(blw_filename,"rb");
- if (base[0]=='N')
- min_north=atoi(north);
- else
- min_north=-atoi(north);
+ if (fd!=NULL)
+ {
+ fscanf(fd,"%lf",&cell_size);
+
+ if ((cell_size<0.0008) || (cell_size>0.0009))
+ {
+ printf("\n*** .BIL file's cell size is incompatible with SPLAT!!\n");
+ exit(1);
+ }
+
+ fscanf(fd,"%lf",°_west);
+ fscanf(fd,"%lf",°_west);
+ fscanf(fd,"%lf",°_west);
+
+ fscanf(fd,"%lf",°_west);
+
+ fscanf(fd,"%lf",°_north);
+
+ fclose(fd);
+ }
+
+ min_north=(int)(deg_north);
+ max_north=min_north+1;
+
+ if (deg_west<0.0)
+ deg_west=-deg_west;
+ else
+ deg_west=360.0-deg_west;
+
+ min_west=(int)(deg_west);
+
+ if (min_west==360)
+ min_west=0;
- max_north=min_north+1;
+ max_west=min_west+1;
+ }
+ }
infile=open(filename, O_RDONLY);
if (bytes_read==2)
{
- byte=buffer[1]+(buffer[0]<<8);
+ if (bil)
+ {
+ /* "little-endian" structure */
+
+ byte=buffer[0]+(buffer[1]<<8);
- if (buffer[0]&128)
- byte-=0x10000;
+ if (buffer[1]&128)
+ byte-=0x10000;
+ }
+
+ if (hgt)
+ {
+ /* "big-endian" structure */
+
+ byte=buffer[1]+(buffer[0]<<8);
+
+ if (buffer[0]&128)
+ byte-=0x10000;
+ }
/* Flag problem elevations here */
+ if (byte<-32768)
+ byte=-32768;
+
+ if (byte>32767)
+ byte=32767;
+
if (byte<=min_elevation)
replacement_flag=1;
if (error)
{
fprintf(stderr,"\n*** Error: Premature EOF detected while reading \"%s\"! :-(\n",filename);
- return -1;
}
close(infile);
{
sscanf(argv[z],"%d",&min_elevation);
- if (min_elevation<-32768)
+ if (min_elevation<-32767)
min_elevation=0;
}
}