1
0
Fork 0

Use new SimGear helpers to simplify some code.

This commit is contained in:
James Turner 2011-10-14 19:28:01 +01:00 committed by Christian Schmitt
parent 99562cfde6
commit e59d2d3e47
5 changed files with 32 additions and 61 deletions

View file

@ -53,24 +53,21 @@ using std::endl;
using std::string; using std::string;
TGHgt::TGHgt( int _res ) { TGHgt::TGHgt( int _res )
{
hgt_resolution = _res; hgt_resolution = _res;
data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
remove_tmp_file = false;
} }
TGHgt::TGHgt( int _res, const SGPath &file ) { TGHgt::TGHgt( int _res, const SGPath &file )
{
hgt_resolution = _res; hgt_resolution = _res;
data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
remove_tmp_file = false;
TGHgt::open( file ); TGHgt::open( file );
} }
@ -91,27 +88,22 @@ TGHgt::open ( const SGPath &f ) {
if ( file_name.extension() == "zip" ) { if ( file_name.extension() == "zip" ) {
// extract the .zip file to /tmp and point the file name // extract the .zip file to /tmp and point the file name
// to the extracted file // to the extracted file
SGPath tmp_dir_path = string( tempnam( 0, "hgt" ) ); tmp_dir = simgear::Dir::tempDir("hgt");
simgear::Dir tmp_dir(tmp_dir_path);
SGPath dummy = tmp_dir.file( "dummy" ); cout << "Extracting " << file_name.str() << " to " << tmp_dir.path().str() << endl;
dummy.create_dir( 0700 ); string command = "unzip -d \"" + tmp_dir.path().str() + "\" " + file_name.base();
cout << "Extracting " << file_name.str() << " to " << tmp_dir_path.str() << endl;
string command = "unzip -d \"" + tmp_dir_path.str() + "\" " + file_name.base();
system( command.c_str() ); system( command.c_str() );
simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT); simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
BOOST_FOREACH(const SGPath& file, files) { BOOST_FOREACH(const SGPath& file, files) {
string ext = file.extension(); string ext = file.lower_extension();
if ( ext == "HGT" || ext == "hgt" ) { if ( ext == "hgt" ) {
file_name = file; file_name = file;
break; break;
} }
} }
remove_tmp_file = true; remove_tmp_file = true;
remove_file_name = file_name.str();
cout << "Proceeding with " << file_name.str() << endl; cout << "Proceeding with " << file_name.str() << endl;
} }
@ -148,12 +140,6 @@ TGHgt::open ( const SGPath &f ) {
bool bool
TGHgt::close () { TGHgt::close () {
gzclose(fd); gzclose(fd);
if ( remove_tmp_file ) {
unlink( remove_file_name.c_str() );
rmdir( remove_file_name.dir().c_str() );
}
return true; return true;
} }
@ -196,13 +182,4 @@ TGHgt::~TGHgt() {
// printf("class TGSrtmBase DEstructor called.\n"); // printf("class TGSrtmBase DEstructor called.\n");
delete [] data; delete [] data;
delete [] output_data; delete [] output_data;
if ( remove_tmp_file ) {
simgear::Dir dir(remove_file_name.dir());
simgear::PathList files = dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
BOOST_FOREACH(const SGPath& file, files) {
unlink( file.c_str() );
}
rmdir( remove_file_name.dir().c_str() );
}
} }

View file

@ -37,6 +37,12 @@ using std::cout;
using std::endl; using std::endl;
using std::string; using std::string;
TGSrtmBase::~TGSrtmBase()
{
if (remove_tmp_file) {
tmp_dir.remove(true /*recursive*/);
}
}
// write out the area of data covered by the specified bucket. Data // write out the area of data covered by the specified bucket. Data
// is written out column by column starting at the lower left hand // is written out column by column starting at the lower left hand

View file

@ -31,11 +31,15 @@
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/bucket/newbucket.hxx> #include <simgear/bucket/newbucket.hxx>
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_dir.hxx>
class TGSrtmBase { class TGSrtmBase {
protected: protected:
TGSrtmBase() : remove_tmp_file(false)
{}
~TGSrtmBase();
// coordinates (in arc seconds) of south west corner // coordinates (in arc seconds) of south west corner
double originx, originy; double originx, originy;
@ -47,7 +51,7 @@ protected:
double col_step, row_step; double col_step, row_step;
bool remove_tmp_file; bool remove_tmp_file;
SGPath remove_file_name; simgear::Dir tmp_dir;
public: public:

View file

@ -33,6 +33,7 @@
#include <simgear/bucket/newbucket.hxx> #include <simgear/bucket/newbucket.hxx>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/misc/sg_dir.hxx>
#include <HGT/hgt.hxx> #include <HGT/hgt.hxx>
#include <Polygon/point2d.hxx> #include <Polygon/point2d.hxx>
@ -67,8 +68,8 @@ int main(int argc, char **argv) {
} }
SGPath sgp( work_dir ); SGPath sgp( work_dir );
sgp.append( "dummy" ); simgear::Dir workDir(sgp);
sgp.create_dir( 0755 ); workDir.create(0755);
TGHgt hgt(resolution, hgt_name); TGHgt hgt(resolution, hgt_name);
hgt.load(); hgt.load();

View file

@ -94,7 +94,6 @@ private:
TGSrtmTiff::TGSrtmTiff( const SGPath &file ) { TGSrtmTiff::TGSrtmTiff( const SGPath &file ) {
lkind = BottomLeft; lkind = BottomLeft;
tif = 0; tif = 0;
remove_tmp_file = false;
data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
opened = TGSrtmTiff::open( file ); opened = TGSrtmTiff::open( file );
@ -103,7 +102,6 @@ TGSrtmTiff::TGSrtmTiff( const SGPath &file ) {
TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) { TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) {
lkind = lk; lkind = lk;
tif = 0; tif = 0;
remove_tmp_file = false;
output_data = 0; output_data = 0;
if ( lkind == BottomLeft ) { if ( lkind == BottomLeft ) {
data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
@ -121,15 +119,6 @@ TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) {
TGSrtmTiff::~TGSrtmTiff() { TGSrtmTiff::~TGSrtmTiff() {
delete[] data; delete[] data;
delete[] output_data; delete[] output_data;
if ( remove_tmp_file ) {
simgear::Dir dir(remove_file_name.dir());
simgear::PathList files = dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
BOOST_FOREACH(const SGPath& file, files) {
unlink( file.c_str() );
}
rmdir( remove_file_name.dir().c_str() );
}
if ( tif ) if ( tif )
TIFFClose( tif ); TIFFClose( tif );
} }
@ -160,28 +149,22 @@ bool TGSrtmTiff::open( const SGPath &f ) {
int x, y; int x, y;
pos_from_name( file_name.file(), prefix, x, y ); pos_from_name( file_name.file(), prefix, x, y );
if ( ext == "zip" ) { if ( ext == "zip" ) {
// extract the .zip file to /tmp and point the file name tmp_dir = simgear::Dir::tempDir("srtm");
// to the extracted file
SGPath tmp_dir_path = string( tempnam( 0, "srtm" ) ); cout << "Extracting " << file_name.str() << " to " << tmp_dir.path().str() << endl;
simgear::Dir tmp_dir(tmp_dir_path); string command = "unzip -d \"" + tmp_dir.path().str() + "\" " + file_name.base();
SGPath dummy = tmp_dir.file( "dummy" );
dummy.create_dir( 0700 );
cout << "Extracting " << file_name.str() << " to " << tmp_dir_path.str() << endl;
string command = "unzip -d \"" + tmp_dir_path.str() + "\" " + file_name.base();
system( command.c_str() ); system( command.c_str() );
simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT); simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
BOOST_FOREACH(const SGPath& file, files) { BOOST_FOREACH(const SGPath& file, files) {
string ext = file.extension(); string ext = file.lower_extension();
if ( ext == "TIF" || ext == "tif" ) { if ( (ext == "tif") || (ext == "tiff") ) {
file_name = file; file_name = file;
break; break;
} }
} }
remove_tmp_file = true; remove_tmp_file = true;
remove_file_name = file_name.str();
cout << "Proceeding with " << file_name.str() << endl; cout << "Proceeding with " << file_name.str() << endl;
} }
@ -361,8 +344,8 @@ int main(int argc, char **argv) {
string work_dir = argv[2]; string work_dir = argv[2];
SGPath sgp( work_dir ); SGPath sgp( work_dir );
sgp.append( "dummy" ); simgear::Dir workDir(sgp);
sgp.create_dir( 0755 ); workDir.create( 0755 );
TGSrtmTiff hgt( hgt_name ); TGSrtmTiff hgt( hgt_name );
hgt.load(); hgt.load();