]> git.mxchange.org Git - flightgear.git/blob - ShapeFile/main.cxx
Don't crash when work directory doesn't exist ... create it.
[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     // make work directory
71     string work_dir = argv[2];
72     string command = "mkdir -p " + work_dir;
73     system( command.c_str() );
74
75     // initialize persistant polygon counter
76     string counter_file = work_dir + "/polygon.counter";
77     poly_index_init( counter_file );
78
79     // initialize structure for building gpc polygons
80     shape_utils_init();
81
82     GShapeFile * sf = new GShapeFile( argv[1] );
83     GDBFile *dbf = new GDBFile( argv[1] );
84     string path = argv[2];
85
86     GPolygon shape;
87     double  *coords; // in decimal degrees
88     int n_vertices;
89
90     FG_LOG( FG_GENERAL, FG_INFO, "shape file records = " << sf->numRecords() );
91
92     GShapeFile::ShapeType t = sf->shapeType();
93     if ( t != GShapeFile::av_Polygon ) {
94         FG_LOG( FG_GENERAL, FG_ALERT, "Can't handle non-polygon shape files" );
95         exit(-1);
96     }
97
98     for ( i = 0; i < sf->numRecords(); i++ ) {
99         //fetch i-th record (shape)
100         sf->getShapeRec(i, &shape); 
101         FG_LOG( FG_GENERAL, FG_DEBUG, "Record = " << i << "  rings = " 
102                 << shape.numRings() );
103
104         AreaType area = get_shapefile_type(dbf, i);
105         FG_LOG( FG_GENERAL, FG_DEBUG, "area type = " << get_area_name(area) 
106                 << " (" << (int)area << ")" );
107
108         FG_LOG( FG_GENERAL, FG_INFO, "  record = " << i 
109                 << " ring = " << 0 );
110
111         if ( area == MarshArea ) {
112             // interior of polygon is marsh, holes are water
113
114             // do main outline first
115             init_shape(&gpc_shape);
116             n_vertices = shape.getRing(0, coords);
117             add_to_shape(n_vertices, coords, &gpc_shape);
118             process_shape(path, area, &gpc_shape);
119             free_shape(&gpc_shape);
120
121             // do lakes (individually) next
122             for (  j = 1; j < shape.numRings(); j++ ) {
123                 FG_LOG( FG_GENERAL, FG_INFO, "  record = " << i 
124                         << " ring = " << j );
125                 init_shape(&gpc_shape);
126                 n_vertices = shape.getRing(j, coords);
127                 add_to_shape(n_vertices, coords, &gpc_shape);
128                 process_shape(path, LakeArea, &gpc_shape);
129                 free_shape(&gpc_shape);
130             }
131         } else if ( area == OceanArea ) {
132             // interior of polygon is ocean, holes are islands
133
134             init_shape(&gpc_shape);
135             for (  j = 0; j < shape.numRings(); j++ ) {
136                 n_vertices = shape.getRing(j, coords);
137                 add_to_shape(n_vertices, coords, &gpc_shape);
138             }
139             process_shape(path, area, &gpc_shape);
140             free_shape(&gpc_shape);
141         } else if ( area == LakeArea ) {
142             // interior of polygon is lake, holes are islands
143
144             init_shape(&gpc_shape);
145             for (  j = 0; j < shape.numRings(); j++ ) {
146                 n_vertices = shape.getRing(j, coords);
147                 add_to_shape(n_vertices, coords, &gpc_shape);
148             }
149             process_shape(path, area, &gpc_shape);
150             free_shape(&gpc_shape);
151         } else if ( area == DryLakeArea ) {
152             // interior of polygon is dry lake, holes are islands
153
154             init_shape(&gpc_shape);
155             for (  j = 0; j < shape.numRings(); j++ ) {
156                 n_vertices = shape.getRing(j, coords);
157                 add_to_shape(n_vertices, coords, &gpc_shape);
158             }
159             process_shape(path, area, &gpc_shape);
160             free_shape(&gpc_shape);
161         } else if ( area == IntLakeArea ) {
162             // interior of polygon is intermittent lake, holes are islands
163
164             init_shape(&gpc_shape);
165             for (  j = 0; j < shape.numRings(); j++ ) {
166                 n_vertices = shape.getRing(j, coords);
167                 add_to_shape(n_vertices, coords, &gpc_shape);
168             }
169             process_shape(path, area, &gpc_shape);
170             free_shape(&gpc_shape);
171         } else if ( area == ReservoirArea ) {
172             // interior of polygon is reservoir, holes are islands
173
174             init_shape(&gpc_shape);
175             for (  j = 0; j < shape.numRings(); j++ ) {
176                 n_vertices = shape.getRing(j, coords);
177                 add_to_shape(n_vertices, coords, &gpc_shape);
178             }
179             process_shape(path, area, &gpc_shape);
180             free_shape(&gpc_shape);
181         } else if ( area == IntReservoirArea ) {
182             // interior of polygon is intermittent reservoir, holes are islands
183
184             init_shape(&gpc_shape);
185             for (  j = 0; j < shape.numRings(); j++ ) {
186                 n_vertices = shape.getRing(j, coords);
187                 add_to_shape(n_vertices, coords, &gpc_shape);
188             }
189             process_shape(path, area, &gpc_shape);
190             free_shape(&gpc_shape);
191         } else if ( area == StreamArea ) {
192             // interior of polygon is stream, holes are islands
193
194             init_shape(&gpc_shape);
195             for (  j = 0; j < shape.numRings(); j++ ) {
196                 n_vertices = shape.getRing(j, coords);
197                 add_to_shape(n_vertices, coords, &gpc_shape);
198             }
199             process_shape(path, area, &gpc_shape);
200             free_shape(&gpc_shape);
201         } else if ( area == CanalArea ) {
202             // interior of polygon is canal, holes are islands
203
204             init_shape(&gpc_shape);
205             for (  j = 0; j < shape.numRings(); j++ ) {
206                 n_vertices = shape.getRing(j, coords);
207                 add_to_shape(n_vertices, coords, &gpc_shape);
208             }
209             process_shape(path, area, &gpc_shape);
210             free_shape(&gpc_shape);
211         } else if ( area == GlacierArea ) {
212             // interior of polygon is glacier, holes are dry land
213
214             init_shape(&gpc_shape);
215             for (  j = 0; j < shape.numRings(); j++ ) {
216                 n_vertices = shape.getRing(j, coords);
217                 add_to_shape(n_vertices, coords, &gpc_shape);
218             }
219             process_shape(path, area, &gpc_shape);
220             free_shape(&gpc_shape);
221         } else if ( area == VoidArea ) {
222             // interior is ????
223
224             // skip for now
225             FG_LOG(  FG_GENERAL, FG_ALERT, "Void area ... SKIPPING!" );
226
227             if ( shape.numRings() > 1 ) {
228                 FG_LOG(  FG_GENERAL, FG_ALERT, "  Void area with holes!" );
229                 // exit(-1);
230             }
231
232             init_shape(&gpc_shape);
233             for (  j = 0; j < shape.numRings(); j++ ) {
234                 n_vertices = shape.getRing(j, coords);
235                 add_to_shape(n_vertices, coords, &gpc_shape);
236             }
237             // process_shape(path, area, &gpc_shape);
238             free_shape(&gpc_shape);
239         } else if ( area == NullArea ) {
240             // interior is ????
241
242             // skip for now
243             FG_LOG(  FG_GENERAL, FG_ALERT, "Null area ... SKIPPING!" );
244
245             if ( shape.numRings() > 1 ) {
246                 FG_LOG(  FG_GENERAL, FG_ALERT, "  Null area with holes!" );
247                 // exit(-1);
248             }
249
250             init_shape(&gpc_shape);
251             for (  j = 0; j < shape.numRings(); j++ ) {
252                 n_vertices = shape.getRing(j, coords);
253                 add_to_shape(n_vertices, coords, &gpc_shape);
254             }
255             // process_shape(path, area, &gpc_shape);
256             free_shape(&gpc_shape);
257         } else {
258             FG_LOG(  FG_GENERAL, FG_ALERT, "Uknown area!" );
259             exit(-1);
260         }
261     }
262
263     return 0;
264 }
265
266
267 // $Log$
268 // Revision 1.6  1999/03/02 01:04:28  curt
269 // Don't crash when work directory doesn't exist ... create it.
270 //
271 // Revision 1.5  1999/03/01 15:36:28  curt
272 // Tweaked a function call name in "names.hxx".
273 //
274 // Revision 1.4  1999/02/25 21:31:05  curt
275 // First working version???
276 //
277 // Revision 1.3  1999/02/23 01:29:04  curt
278 // Additional progress.
279 //
280 // Revision 1.2  1999/02/19 19:05:18  curt
281 // Working on clipping shapes and distributing into buckets.
282 //
283 // Revision 1.1  1999/02/15 19:10:23  curt
284 // Initial revision.
285 //