1 // main.cxx -- process shapefiles and extract polygon outlines,
2 // clipping against and sorting them into the revelant
5 // Written by Curtis Olson, started February 1999.
7 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 // (Log is kept at end of this file)
27 // Include Geographic Foundation Classes library
29 // libgfc.a includes need this bit o' strangeness
34 #include <gfc/gadt_polygon.h>
36 #include <gfc/gshapefile.h>
42 // include Generic Polygon Clipping Library
47 #include <Bucket/newbucket.hxx>
48 #include <Debug/logstream.hxx>
57 static void clip_and_write_poly( FGBucket b, int n_vertices, double *coords ) {
59 c.x = b.get_center_lon();
60 c.y = b.get_center_lat();
61 double span = bucket_span(c.y);
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) {
74 } else if ( c.y >= 89.0) {
80 FG_LOG ( FG_GENERAL, FG_ALERT,
81 "Out of range latitude in clip_and_write_poly() = " << c.y );
88 int main( int argc, char **argv ) {
91 fglog().setLogLevels( FG_ALL, FG_DEBUG );
94 FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0] << " <shapefile>" );
98 FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
100 GShapeFile * sf = new GShapeFile( argv[1] );
101 GDBFile *names = new GDBFile( argv[1] );
104 double *coords; // in decimal degrees
108 FG_LOG( FG_GENERAL, FG_INFO, sf->numRecords() );
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" );
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 ) );
120 sf->getShapeRec(i, &shape);
122 FG_LOG( FG_GENERAL, FG_DEBUG, "Record = " << i << " rings = "
123 << shape.numRings() );
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);
129 FG_LOG( FG_GENERAL, FG_DEBUG, " ring " << j << " = " );
130 FG_LOG( FG_GENERAL, FG_INFO, n_vertices );
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]; }
141 FG_LOG( FG_GENERAL, FG_INFO, "min = " << min.x << "," << min.y
142 << " max = " << max.x << "," << max.y );
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;
152 if ( b_min == b_max ) {
153 clip_and_write_poly( b_min, n_vertices, coords );
158 fgBucketDiff(b_min, b_max, &dx, &dy);
159 cout << "airport spans tile boundaries" << endl;
160 cout << " dx = " << dx << " dy = " << dy << endl;
162 if ( (dx > 1) || (dy > 1) ) {
163 cout << "somethings really wrong!!!!" << endl;
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 );
173 // string answer; cin >> answer;
176 for ( int k = 0; k < n_vertices; k++ ) {
179 FG_LOG( FG_GENERAL, FG_INFO, lon << " " << lat );
188 // Revision 1.2 1999/02/19 19:05:18 curt
189 // Working on clipping shapes and distributing into buckets.
191 // Revision 1.1 1999/02/15 19:10:23 curt