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