]> git.mxchange.org Git - flightgear.git/blob - Tools/Prep/ShapeFile/shape.cxx
Initial revision
[flightgear.git] / Tools / Prep / ShapeFile / shape.cxx
1 // shape.cxx -- shape/gpc utils
2 //
3 // Written by Curtis Olson, started February 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
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 #include <Include/compiler.h>
26
27 #include STL_STRING
28
29 #include <Bucket/newbucket.hxx>
30 #include <Debug/logstream.hxx>
31
32 #include <Polygon/index.hxx>
33 #include <Polygon/names.hxx>
34 #include "shape.hxx"
35
36
37 #define FG_MAX_VERTICES 100000
38 static gpc_vertex_list v_list;
39
40
41 class point2d {
42 public:
43     double x, y;
44 };
45
46
47 static void clip_and_write_poly( string root, long int p_index, AreaType area, 
48                                  FGBucket b, gpc_polygon *shape ) {
49     point2d c, min, max;
50     c.x = b.get_center_lon();
51     c.y = b.get_center_lat();
52     double span = bucket_span(c.y);
53     gpc_polygon base, result;
54     char tile_name[256], poly_index[256];
55
56     // calculate bucket dimensions
57     if ( (c.y >= -89.0) && (c.y < 89.0) ) {
58         min.x = c.x - span / 2.0;
59         max.x = c.x + span / 2.0;
60         min.y = c.y - FG_HALF_BUCKET_SPAN;
61         max.y = c.y + FG_HALF_BUCKET_SPAN;
62     } else if ( c.y < -89.0) {
63         min.x = -90.0;
64         max.x = -89.0;
65         min.y = -180.0;
66         max.y = 180.0;
67     } else if ( c.y >= 89.0) {
68         min.x = 89.0;
69         max.x = 90.0;
70         min.y = -180.0;
71         max.y = 180.0;
72     } else {
73         FG_LOG ( FG_GENERAL, FG_ALERT, 
74                  "Out of range latitude in clip_and_write_poly() = " << c.y );
75     }
76
77     FG_LOG( FG_GENERAL, FG_INFO, "  (" << min.x << "," << min.y << ") ("
78             << max.x << "," << max.y << ")" );
79
80     // set up clipping tile
81     v_list.vertex[0].x = min.x;
82     v_list.vertex[0].y = min.y;
83
84     v_list.vertex[1].x = max.x;
85     v_list.vertex[1].y = min.y;
86
87     v_list.vertex[2].x = max.x;
88     v_list.vertex[2].y = max.y;
89
90     v_list.vertex[3].x = min.x;
91     v_list.vertex[3].y = max.y;
92
93     v_list.num_vertices = 4;
94
95     base.num_contours = 0;
96     base.contour = NULL;
97     gpc_add_contour( &base, &v_list );
98
99     // FG_LOG( FG_GENERAL, FG_DEBUG, "base = 4 vertices" );
100
101     /*
102     FILE *bfp= fopen("base", "w");
103     gpc_write_polygon(bfp, &base);
104     fclose(bfp);
105     */
106
107     gpc_polygon_clip(GPC_INT, &base, shape, &result);
108
109     if ( result.num_contours > 0 ) {
110         long int t_index = b.gen_index();
111         string path = root + "/Scenery/" + b.gen_base_path();
112         string command = "mkdir -p " + path;
113         system( command.c_str() );
114
115         sprintf( tile_name, "%ld", t_index );
116         string polyfile = path + "/" + tile_name;
117
118         sprintf( poly_index, "%ld", p_index );
119         polyfile += ".";
120         polyfile += poly_index;
121
122         string poly_type = get_area_name( area );
123         if ( poly_type == "Unknown" ) {
124             cout << "unknown area type in clip_and_write_poly()!" << endl;
125             exit(-1);
126         }
127         
128         FILE *rfp= fopen( polyfile.c_str(), "w" );
129         fprintf( rfp, "%s\n", poly_type.c_str() );
130         gpc_write_polygon( rfp, &result );
131         fclose( rfp );
132     }
133
134     gpc_free_polygon(&base);
135     gpc_free_polygon(&result);
136 }
137
138
139 // Initialize structure we use to create polygons for the gpc library
140 bool shape_utils_init() {
141     v_list.num_vertices = 0;
142     v_list.vertex = new gpc_vertex[FG_MAX_VERTICES];;
143
144     return true;
145 }
146
147
148 // initialize a gpc_polygon
149 void init_shape(gpc_polygon *shape) {
150     shape->num_contours = 0;
151     shape->contour = NULL;
152 }
153
154
155 // make a gpc_polygon
156 void add_to_shape(int count, double *coords, gpc_polygon *shape) {
157     
158     for ( int i = 0; i < count; i++ ) {
159         v_list.vertex[i].x = coords[i*2+0];
160         v_list.vertex[i].y = coords[i*2+1];
161     }
162
163     v_list.num_vertices = count;
164     gpc_add_contour( shape, &v_list );
165 }
166
167
168 // process shape (write polygon to all intersecting tiles)
169 void process_shape(string path, AreaType area, gpc_polygon *gpc_shape) {
170     point2d min, max;
171     long int index;
172     int i, j;
173
174     min.x = min.y = 200.0;
175     max.x = max.y = -200.0;
176
177     // find min/max of polygon
178     for ( i = 0; i < gpc_shape->num_contours; i++ ) {
179         for ( j = 0; j < gpc_shape->contour[i].num_vertices; j++ ) {
180             double x = gpc_shape->contour[i].vertex[j].x;
181             double y = gpc_shape->contour[i].vertex[j].y;
182
183             if ( x < min.x ) { min.x = x; }
184             if ( y < min.y ) { min.y = y; }
185             if ( x > max.x ) { max.x = x; }
186             if ( y > max.y ) { max.y = y; }
187         }
188     }
189
190     /*
191     FILE *sfp= fopen("shape", "w");
192     gpc_write_polygon(sfp, gpc_shape);
193     fclose(sfp);
194     exit(-1);
195     */
196         
197     // get next polygon index
198     index = poly_index_next();
199
200     FG_LOG( FG_GENERAL, FG_INFO, "  min = " << min.x << "," << min.y
201             << " max = " << max.x << "," << max.y );
202
203     // find buckets for min, and max points of convex hull.
204     // note to self: self, you should think about checking for
205     // polygons that span the date line
206     FGBucket b_min(min.x, min.y);
207     FGBucket b_max(max.x, max.y);
208     FG_LOG( FG_GENERAL, FG_INFO, "  Bucket min = " << b_min );
209     FG_LOG( FG_GENERAL, FG_INFO, "  Bucket max = " << b_max );
210             
211     if ( b_min == b_max ) {
212         clip_and_write_poly( path, index, area, b_min, gpc_shape );
213     } else {
214         FGBucket b_cur;
215         int dx, dy, i, j;
216             
217         fgBucketDiff(b_min, b_max, &dx, &dy);
218         FG_LOG( FG_GENERAL, FG_INFO, 
219                 "  polygon spans tile boundaries" );
220         FG_LOG( FG_GENERAL, FG_INFO, "  dx = " << dx 
221                 << "  dy = " << dy );
222
223         if ( (dx > 100) || (dy > 100) ) {
224             FG_LOG( FG_GENERAL, FG_ALERT, 
225                     "somethings really wrong!!!!" );
226             exit(-1);
227         }
228
229         for ( j = 0; j <= dy; j++ ) {
230             for ( i = 0; i <= dx; i++ ) {
231                 b_cur = fgBucketOffset(min.x, min.y, i, j);
232                 clip_and_write_poly( path, index, area, b_cur, gpc_shape );
233             }
234         }
235         // string answer; cin >> answer;
236     }
237 }
238
239
240 // free a gpc_polygon
241 void free_shape(gpc_polygon *shape) {
242     gpc_free_polygon(shape);
243 }
244
245
246 // $Log$
247 // Revision 1.1  1999/04/05 21:32:42  curt
248 // Initial revision
249 //
250 // Revision 1.3  1999/03/19 00:27:41  curt
251 // Use long int for index instead of just int.
252 //
253 // Revision 1.2  1999/02/25 21:31:08  curt
254 // First working version???
255 //
256 // Revision 1.1  1999/02/23 01:29:06  curt
257 // Additional progress.
258 //