]> git.mxchange.org Git - flightgear.git/blob - ShapeFile/main.cxx
Working on clipping shapes and distributing into buckets.
[flightgear.git] / ShapeFile / main.cxx
1 // main.cxx -- process shapefiles and extract polygon outlines,
2 //             clipping against and sorting them into the revelant
3 //             tiles.
4 //
5 // Written by Curtis Olson, started February 1999.
6 //
7 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24 // (Log is kept at end of this file)
25  
26
27 // Include Geographic Foundation Classes library
28
29 // libgfc.a includes need this bit o' strangeness
30 #if defined ( linux )
31 #  define _LINUX_
32 #endif
33
34 #include <gfc/gadt_polygon.h>
35 #include <gfc/gdbf.h>
36 #include <gfc/gshapefile.h>
37
38 #undef E
39 #undef DEG_TO_RAD
40 #undef RAD_TO_DEG
41
42 // include Generic Polygon Clipping Library
43 extern "C" {
44 #include <gpc.h>
45 }
46
47 #include <Bucket/newbucket.hxx>
48 #include <Debug/logstream.hxx>
49
50
51 class point2d {
52 public:
53     double x, y;
54 };
55
56
57 static void clip_and_write_poly( FGBucket b, int n_vertices, double *coords ) {
58     point2d c, min, max;
59     c.x = b.get_center_lon();
60     c.y = b.get_center_lat();
61     double span = bucket_span(c.y);
62
63     // calculate bucket dimensions
64     if ( (c.y >= -89.0) && (c.y < 89.0) ) {
65         min.x = c.x - span / 2.0;
66         max.x = c.x + span / 2.0;
67         min.y = c.y - FG_HALF_BUCKET_SPAN;
68         max.y = c.y + FG_HALF_BUCKET_SPAN;
69     } else if ( c.y < -89.0) {
70         min.x = -90.0;
71         max.x = -89.0;
72         min.y = -180.0;
73         max.y = 180.0;
74     } else if ( c.y >= 89.0) {
75         min.x = 89.0;
76         max.x = 90.0;
77         min.y = -180.0;
78         max.y = 180.0;
79     } else {
80         FG_LOG ( FG_GENERAL, FG_ALERT, 
81                  "Out of range latitude in clip_and_write_poly() = " << c.y );
82     }
83
84     
85 }
86
87
88 int main( int argc, char **argv ) {
89     point2d min, max;
90
91     fglog().setLogLevels( FG_ALL, FG_DEBUG );
92
93     if ( argc != 2 ) {
94         FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0] << " <shapefile>" );
95         exit(-1);
96     }
97
98     FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
99
100     GShapeFile * sf = new GShapeFile( argv[1] );
101     GDBFile *names = new GDBFile( argv[1] );
102
103     GPolygon shape;
104     double  *coords; // in decimal degrees
105     int n_vertices;
106     double lon, lat;
107
108     FG_LOG( FG_GENERAL, FG_INFO, sf->numRecords() );
109
110     GShapeFile::ShapeType t = sf->shapeType();
111     if ( t != GShapeFile::av_Polygon ) {
112         FG_LOG( FG_GENERAL, FG_ALERT, "Can't handle non-polygon shape files" );
113         exit(-1);
114     }
115
116     for ( int i = 0; i < sf->numRecords(); i++ ) {
117         //fetch i-th record (shape)
118         FG_LOG( FG_GENERAL, FG_DEBUG, names->getRecord( i ) );
119
120         sf->getShapeRec(i, &shape); 
121
122         FG_LOG( FG_GENERAL, FG_DEBUG, "Record = " << i << "  rings = " 
123                 << shape.numRings() );
124
125         for ( int j = 0; j < shape.numRings(); j++ ) {
126             //return j-th branch's coords, # of vertices
127             n_vertices = shape.getRing(j, coords);
128
129             FG_LOG( FG_GENERAL, FG_DEBUG, "  ring " << j << " = " );
130             FG_LOG( FG_GENERAL, FG_INFO, n_vertices );
131
132             // find min/max of this polygon
133             min.x = min.y = 200.0;
134             max.x = max.y = -200.0;
135             for ( int k = 0; k < n_vertices; k++ ) {
136                 if ( coords[k*2+0] < min.x ) { min.x = coords[k*2+0]; }
137                 if ( coords[k*2+1] < min.y ) { min.y = coords[k*2+1]; }
138                 if ( coords[k*2+0] > max.x ) { max.x = coords[k*2+0]; }
139                 if ( coords[k*2+1] > max.y ) { max.y = coords[k*2+1]; }
140             }
141             FG_LOG( FG_GENERAL, FG_INFO, "min = " << min.x << "," << min.y
142                     << " max = " << max.x << "," << max.y );
143
144             // find buckets for min, and max points of convex hull.
145             // note to self: self, you should think about checking for
146             // polygons that span the date line
147             FGBucket b_min(min.x, min.y);
148             FGBucket b_max(max.x, max.y);
149             cout << "Bucket min = " << b_min << endl;
150             cout << "Bucket max = " << b_max << endl;
151             
152             if ( b_min == b_max ) {
153                 clip_and_write_poly( b_min, n_vertices, coords );
154             } else {
155                 FGBucket b_cur;
156                 int dx, dy, i, j;
157
158                 fgBucketDiff(b_min, b_max, &dx, &dy);
159                 cout << "airport spans tile boundaries" << endl;
160                 cout << "  dx = " << dx << "  dy = " << dy << endl;
161
162                 if ( (dx > 1) || (dy > 1) ) {
163                     cout << "somethings really wrong!!!!" << endl;
164                     exit(-1);
165                 }
166
167                 for ( j = 0; j <= dy; j++ ) {
168                     for ( i = 0; i <= dx; i++ ) {
169                         b_cur = fgBucketOffset(min.x, min.y, i, j);
170                         clip_and_write_poly( b_cur, n_vertices, coords );
171                     }
172                 }
173                 // string answer; cin >> answer;
174             }
175
176             for ( int k = 0; k < n_vertices; k++ ) {
177                 lon = coords[k*2+0];
178                 lat = coords[k*2+1];
179                 FG_LOG( FG_GENERAL, FG_INFO, lon << " " << lat );
180             }
181         }
182     }
183
184     return 0;
185 }
186
187 // $Log$
188 // Revision 1.2  1999/02/19 19:05:18  curt
189 // Working on clipping shapes and distributing into buckets.
190 //
191 // Revision 1.1  1999/02/15 19:10:23  curt
192 // Initial revision.
193 //