| 1 |
/** |
| 2 |
* Command line program to parse the world airports database as provided by: |
| 3 |
* http://ourairports.com |
| 4 |
* |
| 5 |
* Data downloaded 2017-11-01. The following files are required by this |
| 6 |
* program and must be stored in a single directory whose path must be indicated |
| 7 |
* on the command line: airports.csv, navaids.csv, runways.csv. |
| 8 |
* |
| 9 |
* Writes to standard output the generated scenery file with RWY and NAV records |
| 10 |
* only. For a detailed description of the command line options, use --help. |
| 11 |
* |
| 12 |
* Unfortunately, ILS antennas are missing from that database, so only RWY and |
| 13 |
* NAV records are generated. |
| 14 |
* |
| 15 |
* The only purpose of the airports file is to get the average elevation of its |
| 16 |
* runways, as the ACM does not support (or, it behaves badly) close runways at |
| 17 |
* different elevation (see the reference manual for more about this issue). |
| 18 |
* |
| 19 |
* @file |
| 20 |
* @author Umberto Salsi <[email protected]> |
| 21 |
* @version $Date: 2017/11/16 17:05:25 $ |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <math.h> |
| 27 |
|
| 28 |
// LINKER_OPTIONS -lm |
| 29 |
|
| 30 |
#include "../../src/util/error.h" |
| 31 |
#include "../../src/util/hashtable.h" |
| 32 |
#include "../../src/util/memory.h" |
| 33 |
#include "../../src/util/reader.h" |
| 34 |
#include "../../src/util/units.h" |
| 35 |
#include "../../src/dis/dis/earth.h" |
| 36 |
|
| 37 |
#define MAX_FIELDS 99 |
| 38 |
|
| 39 |
|
| 40 |
/* |
| 41 |
* Range of latitude and longitude of the items we are interested to extract. |
| 42 |
* The value must be greater or equal to the minimum, and strictly less than the |
| 43 |
* maximum. |
| 44 |
* For runways, the base end is compared. |
| 45 |
* For ILS, the position of the localized antenna is compared. |
| 46 |
* For NDB and VOR with associated DME, the location of the NDB or VOR antenna |
| 47 |
* is compared. |
| 48 |
*/ |
| 49 |
static double lat_min = units_DEGtoRAD(-90); |
| 50 |
static double lat_max = units_DEGtoRAD(91); |
| 51 |
static double lon_min = units_DEGtoRAD(-180); |
| 52 |
static double lon_max = units_DEGtoRAD(181); |
| 53 |
|
| 54 |
|
| 55 |
static long parseLong(char *s, long *l) |
| 56 |
{ |
| 57 |
char *tail; |
| 58 |
*l = strtol(s, &tail, 10); |
| 59 |
return tail != s && *tail == 0; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
static int parseDouble(char *s, double *d) |
| 64 |
{ |
| 65 |
char *tail; |
| 66 |
*d = strtod(s, &tail); |
| 67 |
return tail != s && *tail == 0 && isfinite(*d); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
static int foundExpectdHeaderFields(reader_Type *in, char **exp_fields, int got_fields_number, char **got_fields) |
| 72 |
{ |
| 73 |
int err = 0; |
| 74 |
int i = 0; |
| 75 |
while(exp_fields[i] != NULL){ |
| 76 |
if( i >= got_fields_number ){ |
| 77 |
fprintf(stderr, "%s:%d: missing expected fields: ", reader_getPath(in), reader_getLineNumber(in)); |
| 78 |
do { |
| 79 |
fprintf(stderr, " %d)%s", i+1, exp_fields[i]); |
| 80 |
i++; |
| 81 |
} while(exp_fields[i] != NULL); |
| 82 |
fprintf(stderr, "\n"); |
| 83 |
return 0; |
| 84 |
} |
| 85 |
if( strcmp(exp_fields[i], got_fields[i]) != 0 ){ |
| 86 |
fprintf(stderr, "%s:%d: expected field no. %d \"%s\" but got \"%s\"\n", reader_getPath(in), reader_getLineNumber(in), i+1, exp_fields[i], got_fields[i]); |
| 87 |
err++; |
| 88 |
} |
| 89 |
i++; |
| 90 |
} |
| 91 |
if( i < got_fields_number ){ |
| 92 |
fprintf(stderr, "%s:%d: unexpected further fields: ", reader_getPath(in), reader_getLineNumber(in)); |
| 93 |
do { |
| 94 |
fprintf(stderr, " %d)%s", i+1, got_fields[i]); |
| 95 |
i++; |
| 96 |
} while(i < got_fields_number); |
| 97 |
fprintf(stderr, "\n"); |
| 98 |
} |
| 99 |
return err == 0; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
typedef struct { |
| 104 |
char *pk; // airport primary key of the data base |
| 105 |
int line_no; // line no. in the airports file (for duplicates error reporting) |
| 106 |
int elevation; // average elevation assumed for all the runways of this airport (ft) |
| 107 |
int isAvailable; // if open, operational, fixed-wings airport |
| 108 |
} Airport; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Maps airport's pk to the allocated Airport. |
| 113 |
*/ |
| 114 |
static hashtable_Type *airports; |
| 115 |
|
| 116 |
|
| 117 |
static int parseAirports(char *path) |
| 118 |
{ |
| 119 |
airports = hashtable_new(hashtable_getHashOfString, hashtable_equalStrings); |
| 120 |
reader_Type *in = reader_new(path); |
| 121 |
char line[999]; |
| 122 |
while( reader_getLine(in, line, sizeof(line)) ){ |
| 123 |
int argc; |
| 124 |
char *argv[MAX_FIELDS]; |
| 125 |
if( ! reader_splitDoubleQuotedCommaSeparated(line, &argc, argv, MAX_FIELDS) ){ |
| 126 |
fprintf(stderr, "%s:%d: more than %d fields found\n", |
| 127 |
reader_getPath(in), reader_getLineNumber(in), MAX_FIELDS); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
if( reader_getLineNumber(in) == 1 ){ |
| 131 |
if( ! foundExpectdHeaderFields(in, |
| 132 |
(char *[]){ |
| 133 |
/*00*/ "id", |
| 134 |
/*01*/ "ident", |
| 135 |
/*02*/ "type", |
| 136 |
/*03*/ "name", |
| 137 |
/*04*/ "latitude_deg", |
| 138 |
/*05*/ "longitude_deg", |
| 139 |
/*06*/ "elevation_ft", |
| 140 |
/*07*/ "continent", |
| 141 |
/*08*/ "iso_country", |
| 142 |
/*09*/ "iso_region", |
| 143 |
/*10*/ "municipality", |
| 144 |
/*11*/ "scheduled_service", |
| 145 |
/*12*/ "gps_code", |
| 146 |
/*13*/ "iata_code", |
| 147 |
/*14*/ "local_code", |
| 148 |
/*15*/ "home_link", |
| 149 |
/*16*/ "wikipedia_link", |
| 150 |
/*17*/ "keywords", NULL |
| 151 |
}, argc, argv) ) |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
// At |
| 156 |
if( argc < 7 ){ |
| 157 |
fprintf(stderr, "%s:%d: less than 7 fields in this record, so no elevation fro this airport\n", reader_getPath(in), |
| 158 |
reader_getLineNumber(in)); |
| 159 |
} |
| 160 |
|
| 161 |
// Empty id are silently ignored: |
| 162 |
char *id = argv[0]; |
| 163 |
if( strlen(id) == 0 ) |
| 164 |
continue; |
| 165 |
|
| 166 |
// Invalid elevation values assumed zero: |
| 167 |
double elevation; |
| 168 |
if( ! parseDouble(argv[6], &elevation) ) |
| 169 |
elevation = 0.0; |
| 170 |
// Deeper known airfield in this DB is the Bar Yehuda Airfield 31.33N 35.39E |
| 171 |
// at -1266 ft under the WGS-84 ellipsoid (roughly, under the sea level)! |
| 172 |
// So, -1300 seems a safe lower limit to check for; +24000 definitely too hight! |
| 173 |
if( !(-1300 <= elevation && elevation <= 24000) ){ |
| 174 |
fprintf(stderr, "%s:%d: airport elevation out of range: %g ft -- ignoring\n", |
| 175 |
reader_getPath(in), reader_getLineNumber(in), elevation); |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
Airport *airport = hashtable_get(airports, id); |
| 180 |
if( airport != NULL ){ |
| 181 |
fprintf(stderr, "%s:%d: duplicated airport pk %s in line %d\n", reader_getPath(in), |
| 182 |
reader_getLineNumber(in), id, airport->line_no); |
| 183 |
} |
| 184 |
|
| 185 |
// Check if type of airport is operational, fixed-wings; |
| 186 |
int isAvailable = 1; |
| 187 |
if( strcmp(argv[2], "closed") == 0 |
| 188 |
|| strcmp(argv[2], "heliport") == 0 |
| 189 |
|| strcmp(argv[2], "balloonport") == 0 |
| 190 |
){ |
| 191 |
isAvailable = 0; |
| 192 |
} else if( strcmp(argv[2], "small_airport") == 0 |
| 193 |
|| strcmp(argv[2], "medium_airport") == 0 |
| 194 |
|| strcmp(argv[2], "large_airport") == 0 |
| 195 |
|| strcmp(argv[2], "seaplane_base") == 0 |
| 196 |
){ |
| 197 |
isAvailable = 1; |
| 198 |
} else { |
| 199 |
isAvailable = 0; |
| 200 |
fprintf(stderr, "%s:%d: unknown airport type: %s -- ignoring\n", |
| 201 |
reader_getPath(in), reader_getLineNumber(in), argv[2]); |
| 202 |
} |
| 203 |
|
| 204 |
airport = memory_allocate(sizeof(Airport), NULL); |
| 205 |
airport->pk = memory_strdup(id); |
| 206 |
airport->line_no = reader_getLineNumber(in); |
| 207 |
airport->elevation = (int) round(elevation); |
| 208 |
airport->isAvailable = isAvailable; |
| 209 |
hashtable_put(airports, airport->pk, airport); |
| 210 |
|
| 211 |
/* |
| 212 |
if( reader_getLineNumber(in) <= 30 ){ |
| 213 |
printf("Line no. %d:\n", reader_getLineNumber(in)); |
| 214 |
int i; |
| 215 |
for(i = 0; i < argc; i++){ |
| 216 |
printf(" %d)%s", i, argv[i]); |
| 217 |
} |
| 218 |
printf("\n"); |
| 219 |
} |
| 220 |
*/ |
| 221 |
} |
| 222 |
if( reader_getError(in) != NULL ){ |
| 223 |
fprintf(stderr, "%s:%d: %s\n", reader_getPath(in), |
| 224 |
reader_getLineNumber(in), reader_getError(in)); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
memory_dispose(in); |
| 228 |
|
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
static int parseRunways(char *path) |
| 234 |
{ |
| 235 |
reader_Type *in = reader_new(path); |
| 236 |
char line[999]; |
| 237 |
while( reader_getLine(in, line, sizeof(line)) ){ |
| 238 |
int argc; |
| 239 |
char *argv[MAX_FIELDS]; |
| 240 |
if( ! reader_splitDoubleQuotedCommaSeparated(line, &argc, argv, MAX_FIELDS) ){ |
| 241 |
fprintf(stderr, "%s:%d: more than %d fields found\n", |
| 242 |
reader_getPath(in), reader_getLineNumber(in), MAX_FIELDS); |
| 243 |
return 0; |
| 244 |
} |
| 245 |
if( reader_getLineNumber(in) == 1 ){ |
| 246 |
if( ! foundExpectdHeaderFields(in, |
| 247 |
(char *[]){ |
| 248 |
/*00*/ "id", |
| 249 |
/*01*/ "airport_ref", |
| 250 |
/*02*/ "airport_ident", |
| 251 |
/*03*/ "length_ft", |
| 252 |
/*04*/ "width_ft", |
| 253 |
/*05*/ "surface", |
| 254 |
/*06*/ "lighted", |
| 255 |
/*07*/ "closed", |
| 256 |
/*08*/ "le_ident", |
| 257 |
/*09*/ "le_latitude_deg", |
| 258 |
/*10*/ "le_longitude_deg", |
| 259 |
/*11*/ "le_elevation_ft", |
| 260 |
/*12*/ "le_heading_degT", |
| 261 |
/*13*/ "le_displaced_threshold_ft", |
| 262 |
/*14*/ "he_ident", |
| 263 |
/*15*/ "he_latitude_deg", |
| 264 |
/*16*/ "he_longitude_deg", |
| 265 |
/*17*/ "he_elevation_ft", |
| 266 |
/*18*/ "he_heading_degT", |
| 267 |
/*19*/ "he_displaced_threshold_ft", |
| 268 |
NULL |
| 269 |
}, argc, argv) ) |
| 270 |
return 0; |
| 271 |
} else { |
| 272 |
if( argc < 17 ){ |
| 273 |
fprintf(stderr, "%s:%d: missing fields -- ignoring\n", |
| 274 |
reader_getPath(in), reader_getLineNumber(in)); |
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
// Most lat/lon are missing, we may safely silently ignore. |
| 280 |
if( argv[9][0] == 0 || argv[10][0] == 0 |
| 281 |
|| argv[15][0] == 0 || argv[16][0] == 0 ) |
| 282 |
continue; |
| 283 |
|
| 284 |
double end1_lat; |
| 285 |
if( ! parseDouble(argv[9], &end1_lat) || !(-90 <= end1_lat && end1_lat <= 90) ){ |
| 286 |
fprintf(stderr, "%s:%d: invalid runway latitude: %s -- ignoring\n", |
| 287 |
reader_getPath(in), reader_getLineNumber(in), argv[9]); |
| 288 |
continue; |
| 289 |
} |
| 290 |
double end1_lon; |
| 291 |
if( ! parseDouble(argv[10], &end1_lon) || !(-180 <= end1_lon && end1_lon <= 180) ){ |
| 292 |
fprintf(stderr, "%s:%d: invalid runway longitude: %s -- ignoring\n", |
| 293 |
reader_getPath(in), reader_getLineNumber(in), argv[10]); |
| 294 |
continue; |
| 295 |
} |
| 296 |
double end2_lat; |
| 297 |
if( ! parseDouble(argv[15], &end2_lat) || !(-90 <= end2_lat && end2_lat <= 90) ){ |
| 298 |
fprintf(stderr, "%s:%d: invalid runway latitude: %s -- ignoring\n", |
| 299 |
reader_getPath(in), reader_getLineNumber(in), argv[15]); |
| 300 |
continue; |
| 301 |
} |
| 302 |
double end2_lon; |
| 303 |
if( ! parseDouble(argv[16], &end2_lon) || !(-180 <= end2_lon && end2_lon <= 180) ){ |
| 304 |
fprintf(stderr, "%s:%d: invalid runway longitude: %s -- ignoring\n", |
| 305 |
reader_getPath(in), reader_getLineNumber(in), argv[16]); |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
end1_lat = units_DEGtoRAD(end1_lat); |
| 310 |
end1_lon = units_DEGtoRAD(end1_lon); |
| 311 |
end2_lat = units_DEGtoRAD(end2_lat); |
| 312 |
end2_lon = units_DEGtoRAD(end2_lon); |
| 313 |
|
| 314 |
// Detects N/S and W/E mismatches (there are some): |
| 315 |
if( fabs(end1_lat - end2_lat) > 1 ){ |
| 316 |
fprintf(stderr, "%s:%d: runway ends latitudes too far -- ignoring\n", |
| 317 |
reader_getPath(in), reader_getLineNumber(in)); |
| 318 |
continue; |
| 319 |
} |
| 320 |
if( fabs(end1_lon - end2_lon) * cos(end1_lat) > 1 ){ |
| 321 |
fprintf(stderr, "%s:%d: runway ends longitudes too far -- ignoring\n", |
| 322 |
reader_getPath(in), reader_getLineNumber(in)); |
| 323 |
continue; |
| 324 |
} |
| 325 |
|
| 326 |
// Ignore runway if middle point lies outside the range: |
| 327 |
double center_lat = (end1_lat + end2_lat)/2; |
| 328 |
double center_lon = (end1_lon + end2_lon)/2; |
| 329 |
if( !(lat_min <= center_lat && center_lat < lat_max |
| 330 |
&& lon_min <= center_lon && center_lon < lon_max) ) |
| 331 |
continue; |
| 332 |
|
| 333 |
char *surface = argv[5]; |
| 334 |
// ACCEPT THESE SURFACES: |
| 335 |
if( memory_startsWith(surface, "CONC") |
| 336 |
|| memory_startsWith(surface, "CON") |
| 337 |
|| memory_startsWith(surface, "ASP") |
| 338 |
|| memory_startsWith(surface, "Asphalt") |
| 339 |
|| strcmp(surface, "asphalt") == 0 |
| 340 |
|| strcmp(surface, "concrete") == 0 |
| 341 |
|| strcmp(surface, "BIT") == 0 // bitumen? |
| 342 |
|| strcmp(surface, "GRE") == 0 // ? |
| 343 |
|| strcmp(surface, "GRS") == 0 // ? |
| 344 |
|| strcmp(surface, "PEM") == 0 // ? |
| 345 |
){ |
| 346 |
|
| 347 |
// SILENTLY REJECT THESE SURFACES: |
| 348 |
} else if( memory_startsWith(surface, "WATER") |
| 349 |
|| memory_startsWith(surface, "TURF") |
| 350 |
|| memory_startsWith(surface, "Turf") |
| 351 |
|| memory_startsWith(surface, "DIRT") |
| 352 |
|| strcmp(surface, "GRASS") == 0 |
| 353 |
|| memory_startsWith(surface, "Grass") |
| 354 |
|| strcmp(surface, "Gravel") == 0 |
| 355 |
|| memory_startsWith(surface, "GRAVEL") |
| 356 |
|| memory_startsWith(surface, "GRVL") |
| 357 |
|| memory_startsWith(surface, "GVL") |
| 358 |
|| strcmp(surface, "MATS") == 0 |
| 359 |
){ |
| 360 |
continue; |
| 361 |
|
| 362 |
// ERROR ON ANY OTHER UNEXPECTED SURFACE TYPE: |
| 363 |
} else { |
| 364 |
fprintf(stderr, "%s:%d: unknown runway surface: %s -- ignoring\n", |
| 365 |
reader_getPath(in), reader_getLineNumber(in), surface); |
| 366 |
continue; |
| 367 |
} |
| 368 |
|
| 369 |
char *airport_ident = argv[2]; |
| 370 |
if( strlen(airport_ident) < 3 ){ |
| 371 |
fprintf(stderr, "%s:%d: airport code too short: %s -- ignoring\n", |
| 372 |
reader_getPath(in), reader_getLineNumber(in), airport_ident); |
| 373 |
continue; |
| 374 |
} |
| 375 |
if( strlen(airport_ident) > 4 ){ |
| 376 |
fprintf(stderr, "%s:%d: airport code too long: %s -- ignoring\n", |
| 377 |
reader_getPath(in), reader_getLineNumber(in), airport_ident); |
| 378 |
continue; |
| 379 |
} |
| 380 |
char *end1_id = argv[8]; |
| 381 |
if( strlen(end1_id) < 2 || strlen(end1_id) > 3 ){ |
| 382 |
fprintf(stderr, "%s:%d: invalid runway ID length: %s -- ignoring\n", |
| 383 |
reader_getPath(in), reader_getLineNumber(in), end1_id); |
| 384 |
continue; |
| 385 |
} |
| 386 |
char *end2_id = argv[14]; |
| 387 |
if( strlen(end2_id) < 2 || strlen(end2_id) > 3 ){ |
| 388 |
fprintf(stderr, "%s:%d: invalid runway ID length: %s -- ignoring\n", |
| 389 |
reader_getPath(in), reader_getLineNumber(in), end2_id); |
| 390 |
continue; |
| 391 |
} |
| 392 |
char stripe[8]; |
| 393 |
snprintf(stripe, sizeof(stripe), "%s/%s", end1_id, end2_id); |
| 394 |
|
| 395 |
int elevation = 0; |
| 396 |
char *airport_pk = argv[1]; |
| 397 |
Airport *airport = hashtable_get(airports, airport_pk); |
| 398 |
if( airport == NULL ){ |
| 399 |
fprintf(stderr, "%s:%d: airport pk %s not found in the airports file\n", |
| 400 |
reader_getPath(in), reader_getLineNumber(in), airport_pk); |
| 401 |
continue; |
| 402 |
} else if( ! airport->isAvailable ){ |
| 403 |
continue; |
| 404 |
} else { |
| 405 |
elevation = airport->elevation; |
| 406 |
} |
| 407 |
|
| 408 |
double length; |
| 409 |
if( ! parseDouble(argv[3], &length) ){ |
| 410 |
fprintf(stderr, "%s:%d: invalid runway length: %s -- ignoring\n", |
| 411 |
reader_getPath(in), reader_getLineNumber(in), argv[3]); |
| 412 |
continue; |
| 413 |
} |
| 414 |
double width; |
| 415 |
if( ! parseDouble(argv[4], &width) ){ |
| 416 |
fprintf(stderr, "%s:%d: invalid runway width: %s -- ignoring\n", |
| 417 |
reader_getPath(in), reader_getLineNumber(in), argv[4]); |
| 418 |
continue; |
| 419 |
} |
| 420 |
|
| 421 |
char lat1[99], lon1[99], lat2[99], lon2[99]; |
| 422 |
earth_latitudeToString(lat1, sizeof(lat1), end1_lat, earth_LLM_DMS); |
| 423 |
earth_longitudeToString(lon1, sizeof(lon1), end1_lon, earth_LLM_DMS); |
| 424 |
earth_latitudeToString(lat2, sizeof(lat2), end2_lat, earth_LLM_DMS); |
| 425 |
earth_longitudeToString(lon2, sizeof(lon2), end2_lon, earth_LLM_DMS); |
| 426 |
printf("RWY %-4s %-7s %4d %5.0f %3.0f %s %s %s %s\n", |
| 427 |
airport_ident, stripe, elevation, length, width, lat1, lon1, lat2, lon2); |
| 428 |
|
| 429 |
} |
| 430 |
/* |
| 431 |
if( reader_getLineNumber(in) <= 90 ){ |
| 432 |
printf("Line no. %d:\n", reader_getLineNumber(in)); |
| 433 |
int i; |
| 434 |
for(i = 0; i < argc; i++){ |
| 435 |
printf("/ *%02d* / \"%s\"\n", i, argv[i]); |
| 436 |
} |
| 437 |
} |
| 438 |
*/ |
| 439 |
} |
| 440 |
if( reader_getError(in) != NULL ){ |
| 441 |
fprintf(stderr, "%s:%d: %s\n", reader_getPath(in), |
| 442 |
reader_getLineNumber(in), reader_getError(in)); |
| 443 |
return 0; |
| 444 |
} |
| 445 |
memory_dispose(in); |
| 446 |
|
| 447 |
return 1; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
static int parseNavaids(char *path) |
| 452 |
{ |
| 453 |
reader_Type *in = reader_new(path); |
| 454 |
char line[999]; |
| 455 |
while( reader_getLine(in, line, sizeof(line)) ){ |
| 456 |
int argc; |
| 457 |
char *argv[MAX_FIELDS]; |
| 458 |
if( ! reader_splitDoubleQuotedCommaSeparated(line, &argc, argv, MAX_FIELDS) ){ |
| 459 |
fprintf(stderr, "%s:%d: more than %d fields found\n", |
| 460 |
reader_getPath(in), reader_getLineNumber(in), MAX_FIELDS); |
| 461 |
return 0; |
| 462 |
} |
| 463 |
if( reader_getLineNumber(in) == 1 ){ |
| 464 |
if( ! foundExpectdHeaderFields(in, |
| 465 |
(char *[]){ |
| 466 |
/*00*/ "id", |
| 467 |
/*01*/ "filename", |
| 468 |
/*02*/ "ident", |
| 469 |
/*03*/ "name", |
| 470 |
/*04*/ "type", |
| 471 |
/*05*/ "frequency_khz", |
| 472 |
/*06*/ "latitude_deg", |
| 473 |
/*07*/ "longitude_deg", |
| 474 |
/*08*/ "elevation_ft", |
| 475 |
/*09*/ "iso_country", |
| 476 |
/*10*/ "dme_frequency_khz", |
| 477 |
/*11*/ "dme_channel", |
| 478 |
/*12*/ "dme_latitude_deg", |
| 479 |
/*13*/ "dme_longitude_deg", |
| 480 |
/*14*/ "dme_elevation_ft", |
| 481 |
/*15*/ "slaved_variation_deg", |
| 482 |
/*16*/ "magnetic_variation_deg", |
| 483 |
/*17*/ "usageType", |
| 484 |
/*18*/ "power", |
| 485 |
/*19*/ "associated_airport", |
| 486 |
NULL |
| 487 |
}, argc, argv) ) |
| 488 |
return 0; |
| 489 |
} else { |
| 490 |
if( argc < 14 ){ |
| 491 |
fprintf(stderr, "%s:%d: missing fields -- ignoring\n", |
| 492 |
reader_getPath(in), reader_getLineNumber(in)); |
| 493 |
continue; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
// Most lat/lon are missing, we may safely silently ignore. |
| 498 |
if( argv[6][0] == 0 || argv[7][0] == 0 ) |
| 499 |
continue; |
| 500 |
|
| 501 |
double lat; |
| 502 |
if( ! parseDouble(argv[6], &lat) || !(-90 <= lat && lat <= 90) ){ |
| 503 |
fprintf(stderr, "%s:%d: invalid latitude: %s -- ignoring\n", |
| 504 |
reader_getPath(in), reader_getLineNumber(in), argv[6]); |
| 505 |
continue; |
| 506 |
} |
| 507 |
double lon; |
| 508 |
if( ! parseDouble(argv[7], &lon) || !(-180 <= lon && lon <= 180) ){ |
| 509 |
fprintf(stderr, "%s:%d: invalid longitude: %s -- ignoring\n", |
| 510 |
reader_getPath(in), reader_getLineNumber(in), argv[7]); |
| 511 |
continue; |
| 512 |
} |
| 513 |
|
| 514 |
lat = units_DEGtoRAD(lat); |
| 515 |
lon = units_DEGtoRAD(lon); |
| 516 |
|
| 517 |
if( !(lat_min <= lat && lat < lat_max |
| 518 |
&& lon_min <= lon && lon < lon_max) ) |
| 519 |
continue; |
| 520 |
|
| 521 |
int mhz = 0; // 0=KHz, 1=MHz |
| 522 |
char *type = argv[4]; |
| 523 |
|
| 524 |
// ACCEPT THESE TYPES: |
| 525 |
if( strcmp(type, "VORTAC") == 0 |
| 526 |
|| strcmp(type, "TACAN") == 0 |
| 527 |
|| strcmp(type, "DME") == 0 |
| 528 |
|| strcmp(type, "VOR") == 0 |
| 529 |
){ |
| 530 |
mhz = 1; |
| 531 |
|
| 532 |
} else if( strcmp(type, "NDB") == 0 ){ |
| 533 |
mhz = 0; |
| 534 |
|
| 535 |
} else if( strcmp(type, "NDB-DME") == 0 ){ |
| 536 |
// No support for NDB/DME under ACM, sorry. |
| 537 |
type = "NDB"; |
| 538 |
mhz = 0; |
| 539 |
|
| 540 |
} else if( strcmp(type, "NDB-DME") == 0 ){ |
| 541 |
// No support for NDB/DME under ACM, sorry. |
| 542 |
type = "NDB"; |
| 543 |
mhz = 0; |
| 544 |
|
| 545 |
} else if( strcmp(type, "VOR-DME") == 0 ){ |
| 546 |
type = "VOR/DME"; |
| 547 |
mhz = 1; |
| 548 |
|
| 549 |
// SILENTLY REJECT THESE TYPES: |
| 550 |
} else if( memory_startsWith(type, "xxxxx") |
| 551 |
){ |
| 552 |
continue; |
| 553 |
|
| 554 |
// ERROR ON ANY OTHER UNEXPECTED NAVAID TYPE: |
| 555 |
} else { |
| 556 |
fprintf(stderr, "%s:%d: unknown NAVAID type: %s -- ignoring\n", |
| 557 |
reader_getPath(in), reader_getLineNumber(in), type); |
| 558 |
continue; |
| 559 |
} |
| 560 |
|
| 561 |
char *name = argv[2]; |
| 562 |
if( strlen(name) < 2 ){ |
| 563 |
fprintf(stderr, "%s:%d: NAVAID name too short: %s -- ignoring\n", |
| 564 |
reader_getPath(in), reader_getLineNumber(in), name); |
| 565 |
continue; |
| 566 |
} |
| 567 |
if( strlen(name) > 4 ){ |
| 568 |
fprintf(stderr, "%s:%d: NAVAID name too long: %s -- ignoring\n", |
| 569 |
reader_getPath(in), reader_getLineNumber(in), name); |
| 570 |
continue; |
| 571 |
} |
| 572 |
|
| 573 |
long freq_khz; |
| 574 |
if( ! parseLong(argv[5], &freq_khz) ){ |
| 575 |
fprintf(stderr, "%s:%d: failed to parse NAVAID frequency as an integral number of KHz: %s -- ignoring\n", |
| 576 |
reader_getPath(in), reader_getLineNumber(in), argv[5]); |
| 577 |
continue; |
| 578 |
} |
| 579 |
char freq[9]; |
| 580 |
if( mhz ){ |
| 581 |
if( !(108000 <= freq_khz && freq_khz <= 117950) ){ |
| 582 |
fprintf(stderr, "%s:%d: DME or VOR frequency out of the range [108000,117950] KHz: %ld -- ignoring\n", |
| 583 |
reader_getPath(in), reader_getLineNumber(in), freq_khz); |
| 584 |
continue; |
| 585 |
} |
| 586 |
if( (freq_khz % 50) != 0 ){ |
| 587 |
fprintf(stderr, "%s:%d: invalid DME or VOR frequency, expected multiple of 50 KHz: %ld -- ignoring\n", |
| 588 |
reader_getPath(in), reader_getLineNumber(in), freq_khz); |
| 589 |
continue; |
| 590 |
} |
| 591 |
snprintf(freq, sizeof(freq), "%.2f", freq_khz / 1000.0); |
| 592 |
|
| 593 |
} else { |
| 594 |
if( !(200 <= freq_khz && freq_khz <= 529) ){ |
| 595 |
fprintf(stderr, "%s:%d: NDB frequency out of the range [200,529] KHz: %ld -- ignoring\n", |
| 596 |
reader_getPath(in), reader_getLineNumber(in), freq_khz); |
| 597 |
continue; |
| 598 |
} |
| 599 |
snprintf(freq, sizeof(freq), "%ld", freq_khz); |
| 600 |
} |
| 601 |
|
| 602 |
double elevation; |
| 603 |
if( argv[8][0] == 0 ){ |
| 604 |
// Most altitudes are blank. Silently set zero. |
| 605 |
elevation = 0; |
| 606 |
} else if( ! parseDouble(argv[8], &elevation) ){ |
| 607 |
fprintf(stderr, "%s:%d: cannot parse NAVAID altitude: %s -- assuming zero\n", |
| 608 |
reader_getPath(in), reader_getLineNumber(in), argv[8]); |
| 609 |
elevation = 0; |
| 610 |
} |
| 611 |
|
| 612 |
char lat_s[99], lon_s[99]; |
| 613 |
earth_latitudeToString(lat_s, sizeof(lat_s), lat, earth_LLM_DMS); |
| 614 |
earth_longitudeToString(lon_s, sizeof(lon_s), lon, earth_LLM_DMS); |
| 615 |
printf("NAV %-4s %-7s %s %s %4.0f %6s -\n", |
| 616 |
name, type, lat_s, lon_s, elevation, freq); |
| 617 |
|
| 618 |
} |
| 619 |
/* |
| 620 |
if( reader_getLineNumber(in) <= 9 ){ |
| 621 |
printf("Line no. %d:\n", reader_getLineNumber(in)); |
| 622 |
int i; |
| 623 |
for(i = 0; i < argc; i++){ |
| 624 |
printf(" %d)%s", i, argv[i]); |
| 625 |
} |
| 626 |
printf("\n"); |
| 627 |
} |
| 628 |
*/ |
| 629 |
} |
| 630 |
if( reader_getError(in) != NULL ){ |
| 631 |
fprintf(stderr, "%s:%d: %s\n", reader_getPath(in), |
| 632 |
reader_getLineNumber(in), reader_getError(in)); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
memory_dispose(in); |
| 636 |
|
| 637 |
return 1; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
static void help() |
| 642 |
{ |
| 643 |
puts( |
| 644 |
"This program parses the ourairports.com data base in its textual format and generates\n" |
| 645 |
"an ACM scenery. The ourairports-com data base can be downloaded from\n" |
| 646 |
"https://ourairports.com\n" |
| 647 |
"Download the airports.csv, runways.csv and navaids.csv files in a folder,\n" |
| 648 |
"then start this program with the following command line parameters:\n" |
| 649 |
"\n" |
| 650 |
" --lat-min DEG Minimum latitude, default 90S.\n" |
| 651 |
" --lat-max DEG Maximum latitude, default 90N.\n" |
| 652 |
" --lon-min DEG Minimum longitude, default 180W.\n" |
| 653 |
" --lon-max DEG maximum longitude, default 180E.\n" |
| 654 |
" --src-dir PATH Path to the directory of the data base CSV files,\n" |
| 655 |
" default . (current directory).\n" |
| 656 |
"\n" |
| 657 |
"The program collects all the runways and NAVAID in the given range of\n" |
| 658 |
"latitude and longitude and writes the scenery on standard output; errors and\n" |
| 659 |
"warnings are sent to standard error. Only the RWY and NAV records\n" |
| 660 |
"are generated. You may want to add the TEAM1_LOC, TEAM2_LOC and GROUND_COLOR\n" |
| 661 |
"records to complete the scenery; ILS must be added by hand, sorry.\n" |
| 662 |
); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
int main(int argc, char** argv) |
| 667 |
{ |
| 668 |
error_init(argv[0]); |
| 669 |
char *src_dir = "."; |
| 670 |
|
| 671 |
int i = 1; |
| 672 |
while( i < argc ){ |
| 673 |
char *a = argv[i]; |
| 674 |
char *v = i < argc? argv[i+1] : NULL; |
| 675 |
if( strcmp(a, "--help") == 0 || strcmp(a, "-h") == 0 ){ |
| 676 |
help(); |
| 677 |
return 0; |
| 678 |
} else if( strcmp(a, "--lat-min") == 0 && v != NULL ){ |
| 679 |
if( ! earth_parseLatitude(v, &lat_min) ) |
| 680 |
error_external("invalid latitude: %s", v); |
| 681 |
i += 2; |
| 682 |
} else if( strcmp(a, "--lat-max") == 0 && v != NULL ){ |
| 683 |
if( ! earth_parseLatitude(v, &lat_max) ) |
| 684 |
error_external("invalid latitude: %s", v); |
| 685 |
i += 2; |
| 686 |
} else if( strcmp(a, "--lon-min") == 0 && v != NULL ){ |
| 687 |
if( ! earth_parseLongitude(v, &lon_min) ) |
| 688 |
error_external("invalid longitude: %s", v); |
| 689 |
i += 2; |
| 690 |
} else if( strcmp(a, "--lon-max") == 0 && v != NULL ){ |
| 691 |
if( ! earth_parseLongitude(v, &lon_max) ) |
| 692 |
error_external("invalid longitude: %s", v); |
| 693 |
i += 2; |
| 694 |
} else if( strcmp(a, "--src-dir") == 0 && v != NULL ){ |
| 695 |
src_dir = v; |
| 696 |
i += 2; |
| 697 |
} else { |
| 698 |
error_external("unknown option/value: %s", a); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
if( !(lat_min < lat_max && lon_min < lon_max) ) |
| 703 |
error_external("invalid latitude [%g,%g[ DEG or longitude [%g,%g[ DEG ranges", units_RADtoDEG(lat_min), units_RADtoDEG(lat_max), units_RADtoDEG(lon_min), units_RADtoDEG(lon_max)); |
| 704 |
|
| 705 |
char path[999]; |
| 706 |
|
| 707 |
snprintf(path, sizeof(path), "%s/airports.csv", src_dir); |
| 708 |
if( ! parseAirports(path) ) |
| 709 |
return 1; |
| 710 |
|
| 711 |
snprintf(path, sizeof(path), "%s/runways.csv", src_dir); |
| 712 |
if( ! parseRunways(path) ) |
| 713 |
return 1; |
| 714 |
|
| 715 |
snprintf(path, sizeof(path), "%s/navaids.csv", src_dir); |
| 716 |
if( ! parseNavaids(path) ) |
| 717 |
return 1; |
| 718 |
|
| 719 |
return 0; |
| 720 |
} |
| 721 |
|
| 722 |
|