Detect and handle errors in loading udt

This commit is contained in:
Gareth Evans
2017-03-04 20:14:41 +00:00
parent f3029bb3cf
commit 4109c6488f
4 changed files with 131 additions and 114 deletions

223
inputs.cc
View File

@@ -1537,7 +1537,7 @@ int LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat)
return 0;
}
void LoadUDT(char *filename)
int LoadUDT(char *filename)
{
/* This function reads a file containing User-Defined Terrain
features for their addition to the digital elevation model
@@ -1552,13 +1552,83 @@ void LoadUDT(char *filename)
double latitude, longitude, height, tempheight;
FILE *fd1 = NULL, *fd2 = NULL;
strcpy(tempname, "/tmp/XXXXXX\0");
strcpy(tempname, "/tmp/XXXXXX");
fd1 = fopen(filename, "r");
if( (fd1 = fopen(filename, "r")) == NULL )
return errno;
if (fd1 != NULL) {
fd = mkstemp(tempname);
fd2 = fopen(tempname, "w");
if( (fd = mkstemp(tempname)) == -1 )
return errno;
if( (fd2 = fdopen(fd,"w")) == NULL ){
fclose(fd1);
close(fd);
return errno;
}
s = fgets(input, 78, fd1);
pointer = strchr(input, ';');
if (pointer != NULL)
*pointer = 0;
while (feof(fd1) == 0) {
/* Parse line for latitude, longitude, height */
for (x = 0, y = 0, z = 0;
x < 78 && input[x] != 0 && z < 3; x++) {
if (input[x] != ',' && y < 78) {
str[z][y] = input[x];
y++;
}
else {
str[z][y] = 0;
z++;
y = 0;
}
}
latitude = ReadBearing(str[0]);
longitude = ReadBearing(str[1]);
if (longitude < 0.0)
longitude += 360;
/* Remove <CR> and/or <LF> from antenna height string */
for (i = 0;
str[2][i] != 13 && str[2][i] != 10
&& str[2][i] != 0; i++) ;
str[2][i] = 0;
/* The terrain feature may be expressed in either
feet or meters. If the letter 'M' or 'm' is
discovered in the string, then this is an
indication that the value given is expressed
in meters. Otherwise the height is interpreted
as being expressed in feet. */
for (i = 0;
str[2][i] != 'M' && str[2][i] != 'm'
&& str[2][i] != 0 && i < 48; i++) ;
if (str[2][i] == 'M' || str[2][i] == 'm') {
str[2][i] = 0;
height = rint(atof(str[2]));
}
else {
str[2][i] = 0;
height = rint(METERS_PER_FOOT * atof(str[2]));
}
if (height > 0.0)
fprintf(fd2, "%d, %d, %f\n",
(int)rint(latitude / dpp),
(int)rint(longitude / dpp), height);
s = fgets(input, 78, fd1);
@@ -1566,125 +1636,64 @@ void LoadUDT(char *filename)
if (pointer != NULL)
*pointer = 0;
}
while (feof(fd1) == 0) {
/* Parse line for latitude, longitude, height */
fclose(fd1);
fclose(fd2);
for (x = 0, y = 0, z = 0;
x < 78 && input[x] != 0 && z < 3; x++) {
if (input[x] != ',' && y < 78) {
str[z][y] = input[x];
y++;
}
if( (fd1 = fopen(tempname, "r")) == NULL )
return errno;
else {
str[z][y] = 0;
z++;
y = 0;
}
}
if( (fd2 = fopen(tempname, "r")) == NULL ){
fclose(fd1);
return errno;
}
latitude = ReadBearing(str[0]);
longitude = ReadBearing(str[1]);
y = 0;
if (longitude < 0.0)
longitude += 360;
n = fscanf(fd1, "%d, %d, %lf", &xpix, &ypix, &height);
/* Remove <CR> and/or <LF> from antenna height string */
do {
x = 0;
z = 0;
for (i = 0;
str[2][i] != 13 && str[2][i] != 10
&& str[2][i] != 0; i++) ;
n = fscanf(fd2, "%d, %d, %lf", &tempxpix, &tempypix,
&tempheight);
str[2][i] = 0;
do {
if (x > y && xpix == tempxpix
&& ypix == tempypix) {
z = 1; /* Dupe! */
/* The terrain feature may be expressed in either
feet or meters. If the letter 'M' or 'm' is
discovered in the string, then this is an
indication that the value given is expressed
in meters. Otherwise the height is interpreted
as being expressed in feet. */
for (i = 0;
str[2][i] != 'M' && str[2][i] != 'm'
&& str[2][i] != 0 && i < 48; i++) ;
if (str[2][i] == 'M' || str[2][i] == 'm') {
str[2][i] = 0;
height = rint(atof(str[2]));
if (tempheight > height)
height = tempheight;
}
else {
str[2][i] = 0;
height = rint(METERS_PER_FOOT * atof(str[2]));
n = fscanf(fd2, "%d, %d, %lf",
&tempxpix, &tempypix,
&tempheight);
x++;
}
if (height > 0.0)
fprintf(fd2, "%d, %d, %f\n",
(int)rint(latitude / dpp),
(int)rint(longitude / dpp), height);
} while (feof(fd2) == 0 && z == 0);
s = fgets(input, 78, fd1);
pointer = strchr(input, ';');
if (pointer != NULL)
*pointer = 0;
}
fclose(fd1);
fclose(fd2);
close(fd);
fd1 = fopen(tempname, "r");
fd2 = fopen(tempname, "r");
y = 0;
if (z == 0)
/* No duplicate found */
//fprintf(stderr,"%lf, %lf \n",xpix*dpp, ypix*dpp);
fflush(stderr);
AddElevation(xpix * dpp, ypix * dpp, height, 1);
fflush(stderr);
n = fscanf(fd1, "%d, %d, %lf", &xpix, &ypix, &height);
y++;
do {
x = 0;
z = 0;
rewind(fd2);
n = fscanf(fd2, "%d, %d, %lf", &tempxpix, &tempypix,
&tempheight);
do {
if (x > y && xpix == tempxpix
&& ypix == tempypix) {
z = 1; /* Dupe! */
if (tempheight > height)
height = tempheight;
}
else {
n = fscanf(fd2, "%d, %d, %lf",
&tempxpix, &tempypix,
&tempheight);
x++;
}
} while (feof(fd2) == 0 && z == 0);
if (z == 0)
/* No duplicate found */
//fprintf(stderr,"%lf, %lf \n",xpix*dpp, ypix*dpp);
fflush(stderr);
AddElevation(xpix * dpp, ypix * dpp, height, 1);
fflush(stderr);
n = fscanf(fd1, "%d, %d, %lf", &xpix, &ypix, &height);
y++;
rewind(fd2);
} while (feof(fd1) == 0);
fclose(fd1);
fclose(fd2);
unlink(tempname);
}
} while (feof(fd1) == 0);
fclose(fd1);
fclose(fd2);
unlink(tempname);
return 0;
}

View File

@@ -10,7 +10,7 @@ int LoadSignalColors(struct site xmtr);
int LoadLossColors(struct site xmtr);
int LoadDBMColors(struct site xmtr);
int LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat);
void LoadUDT(char *filename);
int LoadUDT(char *filename);
int loadLIDAR(char *filename);
int loadClutter(char *filename, double radius, struct site tx);

18
main.cc
View File

@@ -28,6 +28,7 @@ double version = 3.02;
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "common.h"
#include "inputs.hh"
@@ -1049,8 +1050,8 @@ int main(int argc, char *argv[])
unsigned char LRmap = 0, txsites = 0, topomap = 0, geo = 0, kml =
0, area_mode = 0, max_txsites, ngs = 0;
char mapfile[255], udt_file[255], ano_filename[255], lidar_tiles[4096], clutter_file[255];
char *az_filename, *el_filename;
char mapfile[255], ano_filename[255], lidar_tiles[4096], clutter_file[255];
char *az_filename, *el_filename, *udt_file = NULL;
double altitude = 0.0, altitudeLR = 0.0, tx_range = 0.0,
rx_range = 0.0, deg_range = 0.0, deg_limit = 0.0, deg_range_lon;
@@ -1142,7 +1143,7 @@ int main(int argc, char *argv[])
forced_erp = -1.0;
forced_freq = 0.0;
sdf_path[0] = 0;
udt_file[0] = 0;
udt_file = NULL;
path.length = 0;
max_txsites = 30;
fzone_clearance = 0.6;
@@ -1488,10 +1489,14 @@ int main(int argc, char *argv[])
}
/*UDT*/ if (strcmp(argv[x], "-udt") == 0) {
/*UDT*/
if (strcmp(argv[x], "-udt") == 0) {
z = x + 1;
if (z <= y && argv[z][0]) {
udt_file = (char*) calloc(PATH_MAX+1, sizeof(char));
if( udt_file == NULL )
return ENOMEM;
strncpy(udt_file, argv[z], 253);
}
}
@@ -1813,7 +1818,10 @@ int main(int argc, char *argv[])
mpi = ippd-1;
// User defined clutter file
LoadUDT(udt_file);
if( udt_file != NULL && (result = LoadUDT(udt_file)) != 0 ){
fprintf(stderr, "Error loading clutter file\n");
return result;
}
// Enrich with Clutter
if(strlen(clutter_file) > 1){

View File

@@ -507,7 +507,7 @@ int DoSigStr(char *filename, unsigned char geo, unsigned char kml,
fclose(fd);
fd = NULL;
}
return 0;
}
void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,