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>
55 int main( int argc, char **argv ) {
56 gpc_polygon gpc_shape;
59 fglog().setLogLevels( FG_ALL, FG_DEBUG );
62 FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0]
63 << " <shapefile> <workdir>" );
67 FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
69 // initialize structure for building gpc polygons
72 GShapeFile * sf = new GShapeFile( argv[1] );
73 GDBFile *dbf = new GDBFile( argv[1] );
74 string path = argv[2];
77 double *coords; // in decimal degrees
80 FG_LOG( FG_GENERAL, FG_INFO, "shape file records = " << sf->numRecords() );
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" );
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() );
94 AreaType area = get_area_type(dbf, i);
95 FG_LOG( FG_GENERAL, FG_DEBUG, "area type = " << (int)area );
97 FG_LOG( FG_GENERAL, FG_INFO, " record = " << i
100 if ( area == MarshArea ) {
101 // interior of polygon is marsh, holes are water
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);
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);
120 } else if ( area == OceanArea ) {
121 // interior of polygon is ocean, holes are islands
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);
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
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);
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
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);
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
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);
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
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);
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
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);
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
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);
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
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);
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
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);
208 process_shape(path, area, &gpc_shape);
209 free_shape(&gpc_shape);
210 } else if ( area == VoidArea ) {
214 FG_LOG( FG_GENERAL, FG_ALERT, "Void area ... SKIPPING!" );
216 if ( shape.numRings() > 1 ) {
217 FG_LOG( FG_GENERAL, FG_ALERT, " Void area with holes!" );
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);
226 // process_shape(path, area, &gpc_shape);
227 free_shape(&gpc_shape);
228 } else if ( area == NullArea ) {
232 FG_LOG( FG_GENERAL, FG_ALERT, "Null area ... SKIPPING!" );
234 if ( shape.numRings() > 1 ) {
235 FG_LOG( FG_GENERAL, FG_ALERT, " Null area with holes!" );
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);
244 // process_shape(path, area, &gpc_shape);
245 free_shape(&gpc_shape);
247 FG_LOG( FG_GENERAL, FG_ALERT, "Uknown area!" );
257 // Revision 1.3 1999/02/23 01:29:04 curt
258 // Additional progress.
260 // Revision 1.2 1999/02/19 19:05:18 curt
261 // Working on clipping shapes and distributing into buckets.
263 // Revision 1.1 1999/02/15 19:10:23 curt