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