]> git.mxchange.org Git - flightgear.git/blob - ShapeFile/shape.cxx
Additional progress.
[flightgear.git] / 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 "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, 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 tmp[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 );
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 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(tmp, "%ld", index);
115         string polyfile = path + "/" + tmp;
116
117         if ( area == MarshArea ) {
118             polyfile += ".marsh";
119         } else if ( area == OceanArea ) {
120             polyfile += ".ocean";
121         } else if ( area == LakeArea ) {
122             polyfile += ".lake";
123         } else if ( area == DryLakeArea ) {
124             polyfile += ".drylake";
125         } else if ( area == IntLakeArea ) {
126             polyfile += ".intlake";
127         } else if ( area == ReservoirArea ) {
128             polyfile += ".reservoir";
129         } else if ( area == IntReservoirArea ) {
130             polyfile += ".intreservoir";
131         } else if ( area == StreamArea ) {
132             polyfile += ".stream";
133         } else if ( area == CanalArea ) {
134             polyfile += ".canal";
135         } else if ( area == GlacierArea ) {
136             polyfile += ".glacier";
137         } else {
138             cout << "unknown area type in clip_and_write_poly()!" << endl;
139             exit(-1);
140         }
141         
142         FILE *rfp= fopen(polyfile.c_str(), "w");
143         gpc_write_polygon(rfp, &result);
144         fclose(rfp);
145     }
146
147     gpc_free_polygon(&base);
148     gpc_free_polygon(&result);
149 }
150
151
152 // Initialize structure we use to create polygons for the gpc library
153 bool shape_utils_init() {
154     v_list.num_vertices = 0;
155     v_list.vertex = new gpc_vertex[FG_MAX_VERTICES];;
156
157     return true;
158 }
159
160
161 // initialize a gpc_polygon
162 void init_shape(gpc_polygon *shape) {
163     shape->num_contours = 0;
164     shape->contour = NULL;
165 }
166
167
168 // make a gpc_polygon
169 void add_to_shape(int count, double *coords, gpc_polygon *shape) {
170     
171     for ( int i = 0; i < count; i++ ) {
172         v_list.vertex[i].x = coords[i*2+0];
173         v_list.vertex[i].y = coords[i*2+1];
174     }
175
176     v_list.num_vertices = count;
177     gpc_add_contour( shape, &v_list );
178 }
179
180
181 // process shape (write polygon to all intersecting tiles)
182 void process_shape(string path, AreaType area, gpc_polygon *gpc_shape) {
183     point2d min, max;
184     int i, j;
185
186     min.x = min.y = 200.0;
187     max.x = max.y = -200.0;
188
189     // find min/max of polygon
190     for ( i = 0; i < gpc_shape->num_contours; i++ ) {
191         for ( j = 0; j < gpc_shape->contour[i].num_vertices; j++ ) {
192             double x = gpc_shape->contour[i].vertex[j].x;
193             double y = gpc_shape->contour[i].vertex[j].y;
194
195             if ( x < min.x ) { min.x = x; }
196             if ( y < min.y ) { min.y = y; }
197             if ( x > max.x ) { max.x = x; }
198             if ( y > max.y ) { max.y = y; }
199         }
200     }
201
202     /*
203     FILE *sfp= fopen("shape", "w");
204     gpc_write_polygon(sfp, gpc_shape);
205     fclose(sfp);
206     exit(-1);
207     */
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, 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, 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
255 // $Log$
256 // Revision 1.1  1999/02/23 01:29:06  curt
257 // Additional progress.
258 //