Additional progress.
This commit is contained in:
parent
d4950f5e70
commit
1a7c97601d
6 changed files with 659 additions and 96 deletions
|
@ -1,6 +1,6 @@
|
||||||
bin_PROGRAMS = shape-decode
|
bin_PROGRAMS = shape-decode
|
||||||
|
|
||||||
shape_decode_SOURCES = main.cxx
|
shape_decode_SOURCES = names.cxx names.hxx main.cxx shape.cxx shape.hxx
|
||||||
|
|
||||||
shape_decode_LDADD = $(top_builddir)/Lib/Debug/libDebug.a \
|
shape_decode_LDADD = $(top_builddir)/Lib/Debug/libDebug.a \
|
||||||
$(top_builddir)/Lib/Bucket/libBucket.a \
|
$(top_builddir)/Lib/Bucket/libBucket.a \
|
||||||
|
|
|
@ -30,11 +30,9 @@
|
||||||
#if defined ( linux )
|
#if defined ( linux )
|
||||||
# define _LINUX_
|
# define _LINUX_
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <gfc/gadt_polygon.h>
|
#include <gfc/gadt_polygon.h>
|
||||||
#include <gfc/gdbf.h>
|
#include <gfc/gdbf.h>
|
||||||
#include <gfc/gshapefile.h>
|
#include <gfc/gshapefile.h>
|
||||||
|
|
||||||
#undef E
|
#undef E
|
||||||
#undef DEG_TO_RAD
|
#undef DEG_TO_RAD
|
||||||
#undef RAD_TO_DEG
|
#undef RAD_TO_DEG
|
||||||
|
@ -44,68 +42,42 @@ extern "C" {
|
||||||
#include <gpc.h>
|
#include <gpc.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Bucket/newbucket.hxx>
|
#include <Include/compiler.h>
|
||||||
|
|
||||||
|
#include STL_STRING
|
||||||
|
|
||||||
#include <Debug/logstream.hxx>
|
#include <Debug/logstream.hxx>
|
||||||
|
|
||||||
|
#include "names.hxx"
|
||||||
class point2d {
|
#include "shape.hxx"
|
||||||
public:
|
|
||||||
double x, y;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
static void clip_and_write_poly( FGBucket b, int n_vertices, double *coords ) {
|
|
||||||
point2d c, min, max;
|
|
||||||
c.x = b.get_center_lon();
|
|
||||||
c.y = b.get_center_lat();
|
|
||||||
double span = bucket_span(c.y);
|
|
||||||
|
|
||||||
// calculate bucket dimensions
|
|
||||||
if ( (c.y >= -89.0) && (c.y < 89.0) ) {
|
|
||||||
min.x = c.x - span / 2.0;
|
|
||||||
max.x = c.x + span / 2.0;
|
|
||||||
min.y = c.y - FG_HALF_BUCKET_SPAN;
|
|
||||||
max.y = c.y + FG_HALF_BUCKET_SPAN;
|
|
||||||
} else if ( c.y < -89.0) {
|
|
||||||
min.x = -90.0;
|
|
||||||
max.x = -89.0;
|
|
||||||
min.y = -180.0;
|
|
||||||
max.y = 180.0;
|
|
||||||
} else if ( c.y >= 89.0) {
|
|
||||||
min.x = 89.0;
|
|
||||||
max.x = 90.0;
|
|
||||||
min.y = -180.0;
|
|
||||||
max.y = 180.0;
|
|
||||||
} else {
|
|
||||||
FG_LOG ( FG_GENERAL, FG_ALERT,
|
|
||||||
"Out of range latitude in clip_and_write_poly() = " << c.y );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main( int argc, char **argv ) {
|
int main( int argc, char **argv ) {
|
||||||
point2d min, max;
|
gpc_polygon gpc_shape;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
fglog().setLogLevels( FG_ALL, FG_DEBUG );
|
fglog().setLogLevels( FG_ALL, FG_DEBUG );
|
||||||
|
|
||||||
if ( argc != 2 ) {
|
if ( argc != 3 ) {
|
||||||
FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0] << " <shapefile>" );
|
FG_LOG( FG_GENERAL, FG_ALERT, "Usage: " << argv[0]
|
||||||
|
<< " <shapefile> <workdir>" );
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
|
FG_LOG( FG_GENERAL, FG_DEBUG, "Opening " << argv[1] << " for reading." );
|
||||||
|
|
||||||
|
// initialize structure for building gpc polygons
|
||||||
|
shape_utils_init();
|
||||||
|
|
||||||
GShapeFile * sf = new GShapeFile( argv[1] );
|
GShapeFile * sf = new GShapeFile( argv[1] );
|
||||||
GDBFile *names = new GDBFile( argv[1] );
|
GDBFile *dbf = new GDBFile( argv[1] );
|
||||||
|
string path = argv[2];
|
||||||
|
|
||||||
GPolygon shape;
|
GPolygon shape;
|
||||||
double *coords; // in decimal degrees
|
double *coords; // in decimal degrees
|
||||||
int n_vertices;
|
int n_vertices;
|
||||||
double lon, lat;
|
|
||||||
|
|
||||||
FG_LOG( FG_GENERAL, FG_INFO, sf->numRecords() );
|
FG_LOG( FG_GENERAL, FG_INFO, "shape file records = " << sf->numRecords() );
|
||||||
|
|
||||||
GShapeFile::ShapeType t = sf->shapeType();
|
GShapeFile::ShapeType t = sf->shapeType();
|
||||||
if ( t != GShapeFile::av_Polygon ) {
|
if ( t != GShapeFile::av_Polygon ) {
|
||||||
|
@ -113,78 +85,178 @@ int main( int argc, char **argv ) {
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int i = 0; i < sf->numRecords(); i++ ) {
|
for ( i = 16473; i < sf->numRecords(); i++ ) {
|
||||||
//fetch i-th record (shape)
|
//fetch i-th record (shape)
|
||||||
FG_LOG( FG_GENERAL, FG_DEBUG, names->getRecord( i ) );
|
|
||||||
|
|
||||||
sf->getShapeRec(i, &shape);
|
sf->getShapeRec(i, &shape);
|
||||||
|
|
||||||
FG_LOG( FG_GENERAL, FG_DEBUG, "Record = " << i << " rings = "
|
FG_LOG( FG_GENERAL, FG_DEBUG, "Record = " << i << " rings = "
|
||||||
<< shape.numRings() );
|
<< shape.numRings() );
|
||||||
|
|
||||||
for ( int j = 0; j < shape.numRings(); j++ ) {
|
AreaType area = get_area_type(dbf, i);
|
||||||
//return j-th branch's coords, # of vertices
|
FG_LOG( FG_GENERAL, FG_DEBUG, "area type = " << (int)area );
|
||||||
n_vertices = shape.getRing(j, coords);
|
|
||||||
|
|
||||||
FG_LOG( FG_GENERAL, FG_DEBUG, " ring " << j << " = " );
|
FG_LOG( FG_GENERAL, FG_INFO, " record = " << i
|
||||||
FG_LOG( FG_GENERAL, FG_INFO, n_vertices );
|
<< " ring = " << 0 );
|
||||||
|
|
||||||
// find min/max of this polygon
|
if ( area == MarshArea ) {
|
||||||
min.x = min.y = 200.0;
|
// interior of polygon is marsh, holes are water
|
||||||
max.x = max.y = -200.0;
|
|
||||||
for ( int k = 0; k < n_vertices; k++ ) {
|
// do main outline first
|
||||||
if ( coords[k*2+0] < min.x ) { min.x = coords[k*2+0]; }
|
init_shape(&gpc_shape);
|
||||||
if ( coords[k*2+1] < min.y ) { min.y = coords[k*2+1]; }
|
n_vertices = shape.getRing(0, coords);
|
||||||
if ( coords[k*2+0] > max.x ) { max.x = coords[k*2+0]; }
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
if ( coords[k*2+1] > max.y ) { max.y = coords[k*2+1]; }
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
|
||||||
|
// do lakes (individually) next
|
||||||
|
for ( j = 1; j < shape.numRings(); j++ ) {
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO, " record = " << i
|
||||||
|
<< " ring = " << j );
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
process_shape(path, LakeArea, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
}
|
}
|
||||||
FG_LOG( FG_GENERAL, FG_INFO, "min = " << min.x << "," << min.y
|
} else if ( area == OceanArea ) {
|
||||||
<< " max = " << max.x << "," << max.y );
|
// interior of polygon is ocean, holes are islands
|
||||||
|
|
||||||
// find buckets for min, and max points of convex hull.
|
init_shape(&gpc_shape);
|
||||||
// note to self: self, you should think about checking for
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
// polygons that span the date line
|
n_vertices = shape.getRing(j, coords);
|
||||||
FGBucket b_min(min.x, min.y);
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
FGBucket b_max(max.x, max.y);
|
}
|
||||||
cout << "Bucket min = " << b_min << endl;
|
process_shape(path, area, &gpc_shape);
|
||||||
cout << "Bucket max = " << b_max << endl;
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == LakeArea ) {
|
||||||
if ( b_min == b_max ) {
|
// interior of polygon is lake, holes are islands
|
||||||
clip_and_write_poly( b_min, n_vertices, coords );
|
|
||||||
} else {
|
|
||||||
FGBucket b_cur;
|
|
||||||
int dx, dy, i, j;
|
|
||||||
|
|
||||||
fgBucketDiff(b_min, b_max, &dx, &dy);
|
init_shape(&gpc_shape);
|
||||||
cout << "airport spans tile boundaries" << endl;
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
cout << " dx = " << dx << " dy = " << dy << endl;
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == DryLakeArea ) {
|
||||||
|
// interior of polygon is dry lake, holes are islands
|
||||||
|
|
||||||
if ( (dx > 1) || (dy > 1) ) {
|
init_shape(&gpc_shape);
|
||||||
cout << "somethings really wrong!!!!" << endl;
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
exit(-1);
|
n_vertices = shape.getRing(j, coords);
|
||||||
}
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == IntLakeArea ) {
|
||||||
|
// interior of polygon is intermittent lake, holes are islands
|
||||||
|
|
||||||
for ( j = 0; j <= dy; j++ ) {
|
init_shape(&gpc_shape);
|
||||||
for ( i = 0; i <= dx; i++ ) {
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
b_cur = fgBucketOffset(min.x, min.y, i, j);
|
n_vertices = shape.getRing(j, coords);
|
||||||
clip_and_write_poly( b_cur, n_vertices, coords );
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
}
|
}
|
||||||
}
|
process_shape(path, area, &gpc_shape);
|
||||||
// string answer; cin >> answer;
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == ReservoirArea ) {
|
||||||
|
// interior of polygon is reservoir, holes are islands
|
||||||
|
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == IntReservoirArea ) {
|
||||||
|
// interior of polygon is intermittent reservoir, holes are islands
|
||||||
|
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == StreamArea ) {
|
||||||
|
// interior of polygon is stream, holes are islands
|
||||||
|
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == CanalArea ) {
|
||||||
|
// interior of polygon is canal, holes are islands
|
||||||
|
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == GlacierArea ) {
|
||||||
|
// interior of polygon is glacier, holes are dry land
|
||||||
|
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == VoidArea ) {
|
||||||
|
// interior is ????
|
||||||
|
|
||||||
|
// skip for now
|
||||||
|
FG_LOG( FG_GENERAL, FG_ALERT, "Void area ... SKIPPING!" );
|
||||||
|
|
||||||
|
if ( shape.numRings() > 1 ) {
|
||||||
|
FG_LOG( FG_GENERAL, FG_ALERT, " Void area with holes!" );
|
||||||
|
// exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int k = 0; k < n_vertices; k++ ) {
|
init_shape(&gpc_shape);
|
||||||
lon = coords[k*2+0];
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
lat = coords[k*2+1];
|
n_vertices = shape.getRing(j, coords);
|
||||||
FG_LOG( FG_GENERAL, FG_INFO, lon << " " << lat );
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
}
|
}
|
||||||
|
// process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else if ( area == NullArea ) {
|
||||||
|
// interior is ????
|
||||||
|
|
||||||
|
// skip for now
|
||||||
|
FG_LOG( FG_GENERAL, FG_ALERT, "Null area ... SKIPPING!" );
|
||||||
|
|
||||||
|
if ( shape.numRings() > 1 ) {
|
||||||
|
FG_LOG( FG_GENERAL, FG_ALERT, " Null area with holes!" );
|
||||||
|
// exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
init_shape(&gpc_shape);
|
||||||
|
for ( j = 0; j < shape.numRings(); j++ ) {
|
||||||
|
n_vertices = shape.getRing(j, coords);
|
||||||
|
add_to_shape(n_vertices, coords, &gpc_shape);
|
||||||
|
}
|
||||||
|
// process_shape(path, area, &gpc_shape);
|
||||||
|
free_shape(&gpc_shape);
|
||||||
|
} else {
|
||||||
|
FG_LOG( FG_GENERAL, FG_ALERT, "Uknown area!" );
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// $Log$
|
// $Log$
|
||||||
|
// Revision 1.3 1999/02/23 01:29:04 curt
|
||||||
|
// Additional progress.
|
||||||
|
//
|
||||||
// Revision 1.2 1999/02/19 19:05:18 curt
|
// Revision 1.2 1999/02/19 19:05:18 curt
|
||||||
// Working on clipping shapes and distributing into buckets.
|
// Working on clipping shapes and distributing into buckets.
|
||||||
//
|
//
|
||||||
|
|
104
ShapeFile/names.cxx
Normal file
104
ShapeFile/names.cxx
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
// names.cxx -- process shapefiles names
|
||||||
|
//
|
||||||
|
// Written by Curtis Olson, started February 1999.
|
||||||
|
//
|
||||||
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
// (Log is kept at end of this file)
|
||||||
|
|
||||||
|
#include <Include/compiler.h>
|
||||||
|
|
||||||
|
#include STL_STRING
|
||||||
|
|
||||||
|
#include "names.hxx"
|
||||||
|
|
||||||
|
|
||||||
|
// return the type of the shapefile record
|
||||||
|
AreaType get_area_type(GDBFile *dbf, int rec) {
|
||||||
|
GDBFieldDesc *fdesc[128]; // 128 is an arbitrary number here
|
||||||
|
GDBFValue *fields; //an array of field values
|
||||||
|
char* dbf_rec; //a record containing all the fields
|
||||||
|
|
||||||
|
// grab the meta-information for all the fields
|
||||||
|
// this applies to all the records in the DBF file.
|
||||||
|
// for ( int i = 0; i < dbf->numFields(); i++ ) {
|
||||||
|
// fdesc[i] = dbf->getFieldDesc(i);
|
||||||
|
// cout << i << ") " << fdesc[i]->name << endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// this is the whole name record
|
||||||
|
dbf_rec = dbf->getRecord( rec );
|
||||||
|
|
||||||
|
// parse it into individual fields
|
||||||
|
if ( dbf_rec ) {
|
||||||
|
fields = dbf->recordDeform( dbf_rec );
|
||||||
|
}
|
||||||
|
|
||||||
|
string area = fields[4].str_v;
|
||||||
|
// strip leading spaces
|
||||||
|
while ( area[0] == ' ' ) {
|
||||||
|
area = area.substr(1, area.length() - 1);
|
||||||
|
}
|
||||||
|
// strip trailing spaces
|
||||||
|
while ( area[area.length() - 1] == ' ' ) {
|
||||||
|
area = area.substr(0, area.length() - 1);
|
||||||
|
}
|
||||||
|
// strip other junk encountered
|
||||||
|
while ( (int)area[area.length() - 1] == 9 ) {
|
||||||
|
area = area.substr(0, area.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( area == "Swamp or Marsh" ) {
|
||||||
|
return MarshArea;
|
||||||
|
} else if ( area == "Bay Estuary or Ocean" ) {
|
||||||
|
return OceanArea;
|
||||||
|
} else if ( area == "Lake" ) {
|
||||||
|
return LakeArea;
|
||||||
|
} else if ( area == "Lake Dry" ) {
|
||||||
|
return DryLakeArea;
|
||||||
|
} else if ( area == "Lake Intermittent" ) {
|
||||||
|
return IntLakeArea;
|
||||||
|
} else if ( area == "Reservoir" ) {
|
||||||
|
return ReservoirArea;
|
||||||
|
} else if ( area == "Reservoir Intermittent" ) {
|
||||||
|
return IntReservoirArea;
|
||||||
|
} else if ( area == "Stream" ) {
|
||||||
|
return StreamArea;
|
||||||
|
} else if ( area == "Canal" ) {
|
||||||
|
return CanalArea;
|
||||||
|
} else if ( area == "Glacier" ) {
|
||||||
|
return GlacierArea;
|
||||||
|
} else if ( area == "Void Area" ) {
|
||||||
|
return VoidArea;
|
||||||
|
} else if ( area == "Null" ) {
|
||||||
|
return NullArea;
|
||||||
|
} else {
|
||||||
|
cout << "unknown area = '" << area << "'" << endl;
|
||||||
|
// cout << "area = " << area << endl;
|
||||||
|
for ( int i = 0; i < area.length(); i++ ) {
|
||||||
|
cout << i << ") " << (int)area[i] << endl;
|
||||||
|
}
|
||||||
|
return UnknownArea;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// $Log$
|
||||||
|
// Revision 1.1 1999/02/23 01:29:05 curt
|
||||||
|
// Additional progress.
|
||||||
|
//
|
68
ShapeFile/names.hxx
Normal file
68
ShapeFile/names.hxx
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
// names.hxx -- process shapefiles names
|
||||||
|
//
|
||||||
|
// Written by Curtis Olson, started February 1999.
|
||||||
|
//
|
||||||
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
// (Log is kept at end of this file)
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _NAMES_HXX
|
||||||
|
#define _NAMES_HXX
|
||||||
|
|
||||||
|
|
||||||
|
// libgfc.a includes need this bit o' strangeness
|
||||||
|
#if defined ( linux )
|
||||||
|
# define _LINUX_
|
||||||
|
#endif
|
||||||
|
#include <gfc/gadt_polygon.h>
|
||||||
|
#include <gfc/gdbf.h>
|
||||||
|
#undef E
|
||||||
|
#undef DEG_TO_RAD
|
||||||
|
#undef RAD_TO_DEG
|
||||||
|
|
||||||
|
|
||||||
|
// Posible shape file types
|
||||||
|
enum AreaType {
|
||||||
|
MarshArea = 0,
|
||||||
|
OceanArea = 1,
|
||||||
|
LakeArea = 2,
|
||||||
|
DryLakeArea = 3,
|
||||||
|
IntLakeArea = 4,
|
||||||
|
ReservoirArea = 5,
|
||||||
|
IntReservoirArea = 6,
|
||||||
|
StreamArea = 7,
|
||||||
|
CanalArea = 8,
|
||||||
|
GlacierArea = 9,
|
||||||
|
VoidArea = 9997,
|
||||||
|
NullArea = 9998,
|
||||||
|
UnknownArea = 9999
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// return the type of the shapefile record
|
||||||
|
AreaType get_area_type(GDBFile *dbf, int rec);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _NAMES_HXX
|
||||||
|
|
||||||
|
|
||||||
|
// $Log$
|
||||||
|
// Revision 1.1 1999/02/23 01:29:05 curt
|
||||||
|
// Additional progress.
|
||||||
|
//
|
258
ShapeFile/shape.cxx
Normal file
258
ShapeFile/shape.cxx
Normal file
|
@ -0,0 +1,258 @@
|
||||||
|
// shape.cxx -- shape/gpc utils
|
||||||
|
//
|
||||||
|
// Written by Curtis Olson, started February 1999.
|
||||||
|
//
|
||||||
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
// (Log is kept at end of this file)
|
||||||
|
|
||||||
|
|
||||||
|
#include <Include/compiler.h>
|
||||||
|
|
||||||
|
#include STL_STRING
|
||||||
|
|
||||||
|
#include <Bucket/newbucket.hxx>
|
||||||
|
#include <Debug/logstream.hxx>
|
||||||
|
|
||||||
|
#include "names.hxx"
|
||||||
|
#include "shape.hxx"
|
||||||
|
|
||||||
|
|
||||||
|
#define FG_MAX_VERTICES 100000
|
||||||
|
static gpc_vertex_list v_list;
|
||||||
|
|
||||||
|
|
||||||
|
class point2d {
|
||||||
|
public:
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void clip_and_write_poly( string root, AreaType area,
|
||||||
|
FGBucket b, gpc_polygon *shape ) {
|
||||||
|
point2d c, min, max;
|
||||||
|
c.x = b.get_center_lon();
|
||||||
|
c.y = b.get_center_lat();
|
||||||
|
double span = bucket_span(c.y);
|
||||||
|
gpc_polygon base, result;
|
||||||
|
char tmp[256];
|
||||||
|
|
||||||
|
// calculate bucket dimensions
|
||||||
|
if ( (c.y >= -89.0) && (c.y < 89.0) ) {
|
||||||
|
min.x = c.x - span / 2.0;
|
||||||
|
max.x = c.x + span / 2.0;
|
||||||
|
min.y = c.y - FG_HALF_BUCKET_SPAN;
|
||||||
|
max.y = c.y + FG_HALF_BUCKET_SPAN;
|
||||||
|
} else if ( c.y < -89.0) {
|
||||||
|
min.x = -90.0;
|
||||||
|
max.x = -89.0;
|
||||||
|
min.y = -180.0;
|
||||||
|
max.y = 180.0;
|
||||||
|
} else if ( c.y >= 89.0) {
|
||||||
|
min.x = 89.0;
|
||||||
|
max.x = 90.0;
|
||||||
|
min.y = -180.0;
|
||||||
|
max.y = 180.0;
|
||||||
|
} else {
|
||||||
|
FG_LOG ( FG_GENERAL, FG_ALERT,
|
||||||
|
"Out of range latitude in clip_and_write_poly() = " << c.y );
|
||||||
|
}
|
||||||
|
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO, " (" << min.x << "," << min.y << ") ("
|
||||||
|
<< max.x << "," << max.y << ")" );
|
||||||
|
|
||||||
|
// set up clipping tile
|
||||||
|
v_list.vertex[0].x = min.x;
|
||||||
|
v_list.vertex[0].y = min.y;
|
||||||
|
|
||||||
|
v_list.vertex[1].x = max.x;
|
||||||
|
v_list.vertex[1].y = min.y;
|
||||||
|
|
||||||
|
v_list.vertex[2].x = max.x;
|
||||||
|
v_list.vertex[2].y = max.y;
|
||||||
|
|
||||||
|
v_list.vertex[3].x = min.x;
|
||||||
|
v_list.vertex[3].y = max.y;
|
||||||
|
|
||||||
|
v_list.num_vertices = 4;
|
||||||
|
|
||||||
|
base.num_contours = 0;
|
||||||
|
base.contour = NULL;
|
||||||
|
gpc_add_contour( &base, &v_list );
|
||||||
|
|
||||||
|
// FG_LOG( FG_GENERAL, FG_DEBUG, "base = 4 vertices" );
|
||||||
|
|
||||||
|
/*
|
||||||
|
FILE *bfp= fopen("base", "w");
|
||||||
|
gpc_write_polygon(bfp, &base);
|
||||||
|
fclose(bfp);
|
||||||
|
*/
|
||||||
|
|
||||||
|
gpc_polygon_clip(GPC_INT, &base, shape, &result);
|
||||||
|
|
||||||
|
if ( result.num_contours > 0 ) {
|
||||||
|
long int index = b.gen_index();
|
||||||
|
string path = root + "/Scenery/" + b.gen_base_path();
|
||||||
|
string command = "mkdir -p " + path;
|
||||||
|
system( command.c_str() );
|
||||||
|
|
||||||
|
sprintf(tmp, "%ld", index);
|
||||||
|
string polyfile = path + "/" + tmp;
|
||||||
|
|
||||||
|
if ( area == MarshArea ) {
|
||||||
|
polyfile += ".marsh";
|
||||||
|
} else if ( area == OceanArea ) {
|
||||||
|
polyfile += ".ocean";
|
||||||
|
} else if ( area == LakeArea ) {
|
||||||
|
polyfile += ".lake";
|
||||||
|
} else if ( area == DryLakeArea ) {
|
||||||
|
polyfile += ".drylake";
|
||||||
|
} else if ( area == IntLakeArea ) {
|
||||||
|
polyfile += ".intlake";
|
||||||
|
} else if ( area == ReservoirArea ) {
|
||||||
|
polyfile += ".reservoir";
|
||||||
|
} else if ( area == IntReservoirArea ) {
|
||||||
|
polyfile += ".intreservoir";
|
||||||
|
} else if ( area == StreamArea ) {
|
||||||
|
polyfile += ".stream";
|
||||||
|
} else if ( area == CanalArea ) {
|
||||||
|
polyfile += ".canal";
|
||||||
|
} else if ( area == GlacierArea ) {
|
||||||
|
polyfile += ".glacier";
|
||||||
|
} else {
|
||||||
|
cout << "unknown area type in clip_and_write_poly()!" << endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *rfp= fopen(polyfile.c_str(), "w");
|
||||||
|
gpc_write_polygon(rfp, &result);
|
||||||
|
fclose(rfp);
|
||||||
|
}
|
||||||
|
|
||||||
|
gpc_free_polygon(&base);
|
||||||
|
gpc_free_polygon(&result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize structure we use to create polygons for the gpc library
|
||||||
|
bool shape_utils_init() {
|
||||||
|
v_list.num_vertices = 0;
|
||||||
|
v_list.vertex = new gpc_vertex[FG_MAX_VERTICES];;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// initialize a gpc_polygon
|
||||||
|
void init_shape(gpc_polygon *shape) {
|
||||||
|
shape->num_contours = 0;
|
||||||
|
shape->contour = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// make a gpc_polygon
|
||||||
|
void add_to_shape(int count, double *coords, gpc_polygon *shape) {
|
||||||
|
|
||||||
|
for ( int i = 0; i < count; i++ ) {
|
||||||
|
v_list.vertex[i].x = coords[i*2+0];
|
||||||
|
v_list.vertex[i].y = coords[i*2+1];
|
||||||
|
}
|
||||||
|
|
||||||
|
v_list.num_vertices = count;
|
||||||
|
gpc_add_contour( shape, &v_list );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// process shape (write polygon to all intersecting tiles)
|
||||||
|
void process_shape(string path, AreaType area, gpc_polygon *gpc_shape) {
|
||||||
|
point2d min, max;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
min.x = min.y = 200.0;
|
||||||
|
max.x = max.y = -200.0;
|
||||||
|
|
||||||
|
// find min/max of polygon
|
||||||
|
for ( i = 0; i < gpc_shape->num_contours; i++ ) {
|
||||||
|
for ( j = 0; j < gpc_shape->contour[i].num_vertices; j++ ) {
|
||||||
|
double x = gpc_shape->contour[i].vertex[j].x;
|
||||||
|
double y = gpc_shape->contour[i].vertex[j].y;
|
||||||
|
|
||||||
|
if ( x < min.x ) { min.x = x; }
|
||||||
|
if ( y < min.y ) { min.y = y; }
|
||||||
|
if ( x > max.x ) { max.x = x; }
|
||||||
|
if ( y > max.y ) { max.y = y; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
FILE *sfp= fopen("shape", "w");
|
||||||
|
gpc_write_polygon(sfp, gpc_shape);
|
||||||
|
fclose(sfp);
|
||||||
|
exit(-1);
|
||||||
|
*/
|
||||||
|
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO, " min = " << min.x << "," << min.y
|
||||||
|
<< " max = " << max.x << "," << max.y );
|
||||||
|
|
||||||
|
// find buckets for min, and max points of convex hull.
|
||||||
|
// note to self: self, you should think about checking for
|
||||||
|
// polygons that span the date line
|
||||||
|
FGBucket b_min(min.x, min.y);
|
||||||
|
FGBucket b_max(max.x, max.y);
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO, " Bucket min = " << b_min );
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO, " Bucket max = " << b_max );
|
||||||
|
|
||||||
|
if ( b_min == b_max ) {
|
||||||
|
clip_and_write_poly( path, area, b_min, gpc_shape );
|
||||||
|
} else {
|
||||||
|
FGBucket b_cur;
|
||||||
|
int dx, dy, i, j;
|
||||||
|
|
||||||
|
fgBucketDiff(b_min, b_max, &dx, &dy);
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO,
|
||||||
|
" polygon spans tile boundaries" );
|
||||||
|
FG_LOG( FG_GENERAL, FG_INFO, " dx = " << dx
|
||||||
|
<< " dy = " << dy );
|
||||||
|
|
||||||
|
if ( (dx > 100) || (dy > 100) ) {
|
||||||
|
FG_LOG( FG_GENERAL, FG_ALERT,
|
||||||
|
"somethings really wrong!!!!" );
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( j = 0; j <= dy; j++ ) {
|
||||||
|
for ( i = 0; i <= dx; i++ ) {
|
||||||
|
b_cur = fgBucketOffset(min.x, min.y, i, j);
|
||||||
|
clip_and_write_poly( path, area, b_cur, gpc_shape );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// string answer; cin >> answer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// free a gpc_polygon
|
||||||
|
void free_shape(gpc_polygon *shape) {
|
||||||
|
gpc_free_polygon(shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// $Log$
|
||||||
|
// Revision 1.1 1999/02/23 01:29:06 curt
|
||||||
|
// Additional progress.
|
||||||
|
//
|
61
ShapeFile/shape.hxx
Normal file
61
ShapeFile/shape.hxx
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
// shape.hxx -- shape/gpc utils
|
||||||
|
//
|
||||||
|
// Written by Curtis Olson, started February 1999.
|
||||||
|
//
|
||||||
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
// (Log is kept at end of this file)
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _SHAPE_HXX
|
||||||
|
#define _SHAPE_HXX
|
||||||
|
|
||||||
|
|
||||||
|
// include Generic Polygon Clipping Library
|
||||||
|
extern "C" {
|
||||||
|
#include <gpc.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "names.hxx"
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize structure we use to create polygons for the gpc library
|
||||||
|
// this must be called once from main for any program that uses this library
|
||||||
|
bool shape_utils_init();
|
||||||
|
|
||||||
|
|
||||||
|
// initialize a gpc_polygon
|
||||||
|
void init_shape(gpc_polygon *shape);
|
||||||
|
|
||||||
|
// make a gpc_polygon
|
||||||
|
void add_to_shape(int count, double *coords, gpc_polygon *shape);
|
||||||
|
|
||||||
|
// process shape (write polygon to all intersecting tiles)
|
||||||
|
void process_shape(string path, AreaType area, gpc_polygon *gpc_shape);
|
||||||
|
|
||||||
|
// free a gpc_polygon
|
||||||
|
void free_shape(gpc_polygon *shape);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _SHAPE_HXX
|
||||||
|
|
||||||
|
|
||||||
|
// $Log$
|
||||||
|
// Revision 1.1 1999/02/23 01:29:06 curt
|
||||||
|
// Additional progress.
|
||||||
|
//
|
Loading…
Reference in a new issue