]> git.mxchange.org Git - flightgear.git/blob - GenAirports/main.cxx
9a42f9021d7ccefda0bb08ebf3f5f2979e285258
[flightgear.git] / GenAirports / main.cxx
1 // main.cxx -- main loop
2 //
3 // Written by Curtis Olson, started March 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23 //
24
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33
34 #include <list>
35 #include <stdio.h>
36 #include <string.h>
37 #include <string>
38
39 #include <Bucket/bucketutils.h>
40 #include <Include/fg_constants.h>
41 #include <Include/fg_zlib.h>
42
43 #include "area.hxx"
44 #include "convex_hull.hxx"
45
46
47 // process and airport + runway list
48 void process_airport( string last_airport, list < string > & runway_list ) {
49     list < point2d > rwy_list, apt_list;
50     list < point2d > :: iterator current;                          
51     list < point2d > :: iterator last;                    
52
53     string line_str;
54     double lon, lat;
55     int len, width, hdg, label_hdg, elev;
56     char codes[10];
57     char side;
58
59     printf( "(apt) %s", last_airport.c_str() );
60
61     while ( runway_list.size() ) {
62         line_str = runway_list.front();
63         runway_list.pop_front();
64         printf( "%s", line_str.c_str() );
65
66         sscanf( line_str.c_str(), "%lf %lf %d %d %d %s %d %c %d\n",
67                 &lon, &lat, &len, &width, &hdg, codes, &label_hdg, 
68                 &side, &elev );
69
70         rwy_list = gen_runway_area( lon, lat, (double)hdg * DEG_TO_RAD, 
71                                     (double)len * FEET_TO_METER,
72                                     (double)width * FEET_TO_METER );
73
74         // add rwy_list to apt_list
75         current = rwy_list.begin();
76         last = rwy_list.end();
77         while ( current != last ) {
78             apt_list.push_back(*current);
79             current++;
80         }
81     }
82
83     printf("Final results in degrees\n");
84     current = apt_list.begin();
85     last = apt_list.end();
86     while ( current != last ) {
87         // printf( "(%.4f, %.4f)\n", 
88         printf( "%.5f %.5f\n", current->lon, current->lat );
89         current++;
90     }
91     printf("\n");
92
93     convex_hull(apt_list);
94 }
95
96
97 // reads the apt_full file and extracts and processes the individual
98 // airport records
99 int main( int argc, char **argv ) {
100     list < string > runway_list;
101     string apt_path, gz_apt_path;
102     string airport, last_airport;
103     fgFile f;
104     char line[256];
105     /*
106     fgBUCKET b;
107     point2d nodes[4];
108     char base[256], path[256], command[256], file[256], exfile[256];
109     double lon, lat, elevation, heading;
110     double length, width;
111     long int index;
112     */
113
114     if ( argc != 3 ) {
115         printf("Usage %s <apt_file> <work dir>\n", argv[0]);
116         exit(0);
117     }
118
119     apt_path = argv[1];
120     gz_apt_path = apt_path + ".gz";
121
122     // first try "path.gz"
123     if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
124         // next try "path"
125         if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
126             printf( "Cannot open file: %s\n", apt_path.c_str());
127         }
128     }
129
130     while ( fggets(f, line, 250) != NULL ) {
131         // printf("%s", line);
132         if ( strlen(line) == 0 ) {
133             // empty, skip
134         } else if ( line[0] == '#' ) {
135             // comment, skip
136         } else if ( line[0] == '\t' ) {
137             // runway entry
138             runway_list.push_back(line);
139         } else {
140             // start of airport record
141             airport = line;
142
143             if ( last_airport.length() ) {
144                 // process previous record
145                 process_airport(last_airport, runway_list);
146             }
147
148             last_airport = airport;
149         }
150     }
151
152     if ( last_airport.length() ) {
153         // process previous record
154         process_airport(last_airport, runway_list);
155     }
156
157     fgclose(f);
158
159     return(1);
160 }
161
162
163 #if 0
164     // P13 (Globe, AZ)
165     // lon = -110.6642442;
166     // lat = 33.3528903;
167     // heading = 102.0 * DEG_TO_RAD;
168     // length = 1769;
169     // width = 23;
170
171     // KANE
172     lon = -93.2113889;
173     lat = 45.145;
174     elevation = 912 * FEET_TO_METER;
175     heading = 270.0 * DEG_TO_RAD;
176     length = 1220;
177     width = 23;
178
179     gen_runway_area( lon * DEG_TO_RAD, lat * DEG_TO_RAD, 
180                      heading, length, width, nodes, &count );
181
182     fgBucketFind(lon, lat, &b);
183     printf( "Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
184             b.lon, b.lat, b.x, b.y);
185
186     index = fgBucketGenIndex(&b);
187     fgBucketGenBasePath(&b, base);
188     sprintf(path, "%s/Scenery/%s", argv[1], base);
189     sprintf(command, "mkdir -p %s\n", path);
190     system(command);
191     
192     sprintf(exfile, "%s/%ld.node.ex", path, index);
193     sprintf(file, "%s/%ld.poly", path, index);
194     printf( "extra node file = %s\n", exfile);
195     printf( "poly file = %s\n", file);
196
197     // output extra nodes
198     if ( (fd = fopen(exfile, "w")) == NULL ) {
199         printf("Cannot open file: %s\n", exfile);
200         exit(-1);
201     }
202
203     fprintf(fd, "%d 2 0 0\n", count);
204     for ( i = 0; i < count; i++ ) {
205         fprintf( fd, "%d %.2f %.2f %.2f\n", i + 1, 
206                  nodes[i].lon * RAD_TO_ARCSEC, nodes[i].lat * RAD_TO_ARCSEC, 
207                  elevation);
208     }
209     fclose(fd);
210
211     // output poly
212     if ( (fd = fopen(file, "w")) == NULL ) {
213         printf("Cannot open file: %s\n", file);
214         exit(-1);
215     }
216
217     // output empty node list
218     fprintf(fd, "0 2 0 0\n");
219
220     // output segments
221     fprintf(fd, "%d 0\n", count);
222     for ( i = 0; i < count - 1; i++ ) {
223         fprintf( fd, "%d %d %d\n", i + 1, i + 1, i + 2 );
224     }
225     fprintf( fd, "%d %d %d\n", count, count, 1 );
226
227     // output hole center
228     fprintf( fd, "1\n");
229     fprintf( fd, "1 %.2f %.2f\n", lon * 3600.0, lat * 3600);
230
231     fclose(fd);
232
233 #endif
234
235
236 // $Log: main.c,v
237 //