]> git.mxchange.org Git - flightgear.git/blob - ShapeFile/main.cxx
58ebf52a108ca7981afdc39e5f67ebdfbf8d0046
[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 #include <gfc/gadt_polygon.h>
34 #include <gfc/gdbf.h>
35 #include <gfc/gshapefile.h>
36 #undef E
37 #undef DEG_TO_RAD
38 #undef RAD_TO_DEG
39
40 // include Generic Polygon Clipping Library
41 extern "C" {
42 #include <gpc.h>
43 }
44
45 #include <Include/compiler.h>
46
47 #include STL_STRING
48
49 #include <Debug/logstream.hxx>
50
51 #include <Polygon/index.hxx>
52 #include <Polygon/names.hxx>
53 #include "shape.hxx"
54
55
56 int main( int argc, char **argv ) {
57     gpc_polygon gpc_shape;
58     int i, j;
59
60     fglog().setLogLevels( FG_ALL, FG_DEBUG );
61
62     if ( argc != 3 ) {
63         FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0] 
64                 << " <shape_file> <work_dir>" );
65         exit(-1);
66     }
67
68     FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
69
70     // initialize persistant polygon counter
71     string counter_file = argv[2];
72     counter_file += "/polygon.counter";
73     poly_index_init( counter_file );
74
75     // initialize structure for building gpc polygons
76     shape_utils_init();
77
78     GShapeFile * sf = new GShapeFile( argv[1] );
79     GDBFile *dbf = new GDBFile( argv[1] );
80     string path = argv[2];
81
82     GPolygon shape;
83     double  *coords; // in decimal degrees
84     int n_vertices;
85
86     FG_LOG( FG_GENERAL, FG_INFO, "shape file records = " << sf->numRecords() );
87
88     GShapeFile::ShapeType t = sf->shapeType();
89     if ( t != GShapeFile::av_Polygon ) {
90         FG_LOG( FG_GENERAL, FG_ALERT, "Can't handle non-polygon shape files" );
91         exit(-1);
92     }
93
94     for ( i = 0; i < sf->numRecords(); i++ ) {
95         //fetch i-th record (shape)
96         sf->getShapeRec(i, &shape); 
97         FG_LOG( FG_GENERAL, FG_DEBUG, "Record = " << i << "  rings = " 
98                 << shape.numRings() );
99
100         AreaType area = get_area_type(dbf, i);
101         FG_LOG( FG_GENERAL, FG_DEBUG, "area type = " << get_area_name(area) 
102                 << " (" << (int)area << ")" );
103
104         FG_LOG( FG_GENERAL, FG_INFO, "  record = " << i 
105                 << " ring = " << 0 );
106
107         if ( area == MarshArea ) {
108             // interior of polygon is marsh, holes are water
109
110             // do main outline first
111             init_shape(&gpc_shape);
112             n_vertices = shape.getRing(0, coords);
113             add_to_shape(n_vertices, coords, &gpc_shape);
114             process_shape(path, area, &gpc_shape);
115             free_shape(&gpc_shape);
116
117             // do lakes (individually) next
118             for (  j = 1; j < shape.numRings(); j++ ) {
119                 FG_LOG( FG_GENERAL, FG_INFO, "  record = " << i 
120                         << " ring = " << j );
121                 init_shape(&gpc_shape);
122                 n_vertices = shape.getRing(j, coords);
123                 add_to_shape(n_vertices, coords, &gpc_shape);
124                 process_shape(path, LakeArea, &gpc_shape);
125                 free_shape(&gpc_shape);
126             }
127         } else if ( area == OceanArea ) {
128             // interior of polygon is ocean, holes are islands
129
130             init_shape(&gpc_shape);
131             for (  j = 0; j < shape.numRings(); j++ ) {
132                 n_vertices = shape.getRing(j, coords);
133                 add_to_shape(n_vertices, coords, &gpc_shape);
134             }
135             process_shape(path, area, &gpc_shape);
136             free_shape(&gpc_shape);
137         } else if ( area == LakeArea ) {
138             // interior of polygon is lake, holes are islands
139
140             init_shape(&gpc_shape);
141             for (  j = 0; j < shape.numRings(); j++ ) {
142                 n_vertices = shape.getRing(j, coords);
143                 add_to_shape(n_vertices, coords, &gpc_shape);
144             }
145             process_shape(path, area, &gpc_shape);
146             free_shape(&gpc_shape);
147         } else if ( area == DryLakeArea ) {
148             // interior of polygon is dry lake, holes are islands
149
150             init_shape(&gpc_shape);
151             for (  j = 0; j < shape.numRings(); j++ ) {
152                 n_vertices = shape.getRing(j, coords);
153                 add_to_shape(n_vertices, coords, &gpc_shape);
154             }
155             process_shape(path, area, &gpc_shape);
156             free_shape(&gpc_shape);
157         } else if ( area == IntLakeArea ) {
158             // interior of polygon is intermittent lake, holes are islands
159
160             init_shape(&gpc_shape);
161             for (  j = 0; j < shape.numRings(); j++ ) {
162                 n_vertices = shape.getRing(j, coords);
163                 add_to_shape(n_vertices, coords, &gpc_shape);
164             }
165             process_shape(path, area, &gpc_shape);
166             free_shape(&gpc_shape);
167         } else if ( area == ReservoirArea ) {
168             // interior of polygon is reservoir, holes are islands
169
170             init_shape(&gpc_shape);
171             for (  j = 0; j < shape.numRings(); j++ ) {
172                 n_vertices = shape.getRing(j, coords);
173                 add_to_shape(n_vertices, coords, &gpc_shape);
174             }
175             process_shape(path, area, &gpc_shape);
176             free_shape(&gpc_shape);
177         } else if ( area == IntReservoirArea ) {
178             // interior of polygon is intermittent reservoir, holes are islands
179
180             init_shape(&gpc_shape);
181             for (  j = 0; j < shape.numRings(); j++ ) {
182                 n_vertices = shape.getRing(j, coords);
183                 add_to_shape(n_vertices, coords, &gpc_shape);
184             }
185             process_shape(path, area, &gpc_shape);
186             free_shape(&gpc_shape);
187         } else if ( area == StreamArea ) {
188             // interior of polygon is stream, holes are islands
189
190             init_shape(&gpc_shape);
191             for (  j = 0; j < shape.numRings(); j++ ) {
192                 n_vertices = shape.getRing(j, coords);
193                 add_to_shape(n_vertices, coords, &gpc_shape);
194             }
195             process_shape(path, area, &gpc_shape);
196             free_shape(&gpc_shape);
197         } else if ( area == CanalArea ) {
198             // interior of polygon is canal, holes are islands
199
200             init_shape(&gpc_shape);
201             for (  j = 0; j < shape.numRings(); j++ ) {
202                 n_vertices = shape.getRing(j, coords);
203                 add_to_shape(n_vertices, coords, &gpc_shape);
204             }
205             process_shape(path, area, &gpc_shape);
206             free_shape(&gpc_shape);
207         } else if ( area == GlacierArea ) {
208             // interior of polygon is glacier, holes are dry land
209
210             init_shape(&gpc_shape);
211             for (  j = 0; j < shape.numRings(); j++ ) {
212                 n_vertices = shape.getRing(j, coords);
213                 add_to_shape(n_vertices, coords, &gpc_shape);
214             }
215             process_shape(path, area, &gpc_shape);
216             free_shape(&gpc_shape);
217         } else if ( area == VoidArea ) {
218             // interior is ????
219
220             // skip for now
221             FG_LOG(  FG_GENERAL, FG_ALERT, "Void area ... SKIPPING!" );
222
223             if ( shape.numRings() > 1 ) {
224                 FG_LOG(  FG_GENERAL, FG_ALERT, "  Void area with holes!" );
225                 // exit(-1);
226             }
227
228             init_shape(&gpc_shape);
229             for (  j = 0; j < shape.numRings(); j++ ) {
230                 n_vertices = shape.getRing(j, coords);
231                 add_to_shape(n_vertices, coords, &gpc_shape);
232             }
233             // process_shape(path, area, &gpc_shape);
234             free_shape(&gpc_shape);
235         } else if ( area == NullArea ) {
236             // interior is ????
237
238             // skip for now
239             FG_LOG(  FG_GENERAL, FG_ALERT, "Null area ... SKIPPING!" );
240
241             if ( shape.numRings() > 1 ) {
242                 FG_LOG(  FG_GENERAL, FG_ALERT, "  Null area with holes!" );
243                 // exit(-1);
244             }
245
246             init_shape(&gpc_shape);
247             for (  j = 0; j < shape.numRings(); j++ ) {
248                 n_vertices = shape.getRing(j, coords);
249                 add_to_shape(n_vertices, coords, &gpc_shape);
250             }
251             // process_shape(path, area, &gpc_shape);
252             free_shape(&gpc_shape);
253         } else {
254             FG_LOG(  FG_GENERAL, FG_ALERT, "Uknown area!" );
255             exit(-1);
256         }
257     }
258
259     return 0;
260 }
261
262
263 // $Log$
264 // Revision 1.4  1999/02/25 21:31:05  curt
265 // First working version???
266 //
267 // Revision 1.3  1999/02/23 01:29:04  curt
268 // Additional progress.
269 //
270 // Revision 1.2  1999/02/19 19:05:18  curt
271 // Working on clipping shapes and distributing into buckets.
272 //
273 // Revision 1.1  1999/02/15 19:10:23  curt
274 // Initial revision.
275 //