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
33 #include <gfc/gadt_polygon.h>
35 #include <gfc/gshapefile.h>
40 // include Generic Polygon Clipping Library
45 #include <Include/compiler.h>
49 #include <Debug/logstream.hxx>
51 #include <Polygon/index.hxx>
52 #include <Polygon/names.hxx>
56 int main( int argc, char **argv ) {
57 gpc_polygon gpc_shape;
60 fglog().setLogLevels( FG_ALL, FG_DEBUG );
63 FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0]
64 << " <shape_file> <work_dir>" );
68 FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
70 // initialize persistant polygon counter
71 string counter_file = argv[2];
72 counter_file += "/polygon.counter";
73 poly_index_init( counter_file );
75 // initialize structure for building gpc polygons
78 GShapeFile * sf = new GShapeFile( argv[1] );
79 GDBFile *dbf = new GDBFile( argv[1] );
80 string path = argv[2];
83 double *coords; // in decimal degrees
86 FG_LOG( FG_GENERAL, FG_INFO, "shape file records = " << sf->numRecords() );
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" );
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() );
100 AreaType area = get_area_type(dbf, i);
101 FG_LOG( FG_GENERAL, FG_DEBUG, "area type = " << get_area_name(area)
102 << " (" << (int)area << ")" );
104 FG_LOG( FG_GENERAL, FG_INFO, " record = " << i
105 << " ring = " << 0 );
107 if ( area == MarshArea ) {
108 // interior of polygon is marsh, holes are water
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);
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);
127 } else if ( area == OceanArea ) {
128 // interior of polygon is ocean, holes are islands
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);
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
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);
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
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);
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
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);
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
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);
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
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);
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
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);
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
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);
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
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);
215 process_shape(path, area, &gpc_shape);
216 free_shape(&gpc_shape);
217 } else if ( area == VoidArea ) {
221 FG_LOG( FG_GENERAL, FG_ALERT, "Void area ... SKIPPING!" );
223 if ( shape.numRings() > 1 ) {
224 FG_LOG( FG_GENERAL, FG_ALERT, " Void area with holes!" );
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);
233 // process_shape(path, area, &gpc_shape);
234 free_shape(&gpc_shape);
235 } else if ( area == NullArea ) {
239 FG_LOG( FG_GENERAL, FG_ALERT, "Null area ... SKIPPING!" );
241 if ( shape.numRings() > 1 ) {
242 FG_LOG( FG_GENERAL, FG_ALERT, " Null area with holes!" );
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);
251 // process_shape(path, area, &gpc_shape);
252 free_shape(&gpc_shape);
254 FG_LOG( FG_GENERAL, FG_ALERT, "Uknown area!" );
264 // Revision 1.4 1999/02/25 21:31:05 curt
265 // First working version???
267 // Revision 1.3 1999/02/23 01:29:04 curt
268 // Additional progress.
270 // Revision 1.2 1999/02/19 19:05:18 curt
271 // Working on clipping shapes and distributing into buckets.
273 // Revision 1.1 1999/02/15 19:10:23 curt