Use new SimGear helpers to simplify some code.
This commit is contained in:
parent
99562cfde6
commit
e59d2d3e47
5 changed files with 32 additions and 61 deletions
|
@ -53,24 +53,21 @@ using std::endl;
|
|||
using std::string;
|
||||
|
||||
|
||||
TGHgt::TGHgt( int _res ) {
|
||||
TGHgt::TGHgt( int _res )
|
||||
{
|
||||
hgt_resolution = _res;
|
||||
|
||||
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;
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
|
@ -91,27 +88,22 @@ TGHgt::open ( const SGPath &f ) {
|
|||
if ( file_name.extension() == "zip" ) {
|
||||
// extract the .zip file to /tmp and point the file name
|
||||
// to the extracted file
|
||||
SGPath tmp_dir_path = string( tempnam( 0, "hgt" ) );
|
||||
simgear::Dir tmp_dir(tmp_dir_path);
|
||||
tmp_dir = simgear::Dir::tempDir("hgt");
|
||||
|
||||
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();
|
||||
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() );
|
||||
|
||||
simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
|
||||
BOOST_FOREACH(const SGPath& file, files) {
|
||||
string ext = file.extension();
|
||||
if ( ext == "HGT" || ext == "hgt" ) {
|
||||
string ext = file.lower_extension();
|
||||
if ( ext == "hgt" ) {
|
||||
file_name = file;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
remove_tmp_file = true;
|
||||
remove_file_name = file_name.str();
|
||||
|
||||
cout << "Proceeding with " << file_name.str() << endl;
|
||||
}
|
||||
|
||||
|
@ -148,12 +140,6 @@ TGHgt::open ( const SGPath &f ) {
|
|||
bool
|
||||
TGHgt::close () {
|
||||
gzclose(fd);
|
||||
|
||||
if ( remove_tmp_file ) {
|
||||
unlink( remove_file_name.c_str() );
|
||||
rmdir( remove_file_name.dir().c_str() );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -196,13 +182,4 @@ TGHgt::~TGHgt() {
|
|||
// printf("class TGSrtmBase DEstructor called.\n");
|
||||
delete [] 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() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,12 @@ using std::cout;
|
|||
using std::endl;
|
||||
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
|
||||
// is written out column by column starting at the lower left hand
|
||||
|
|
|
@ -31,11 +31,15 @@
|
|||
#include <simgear/compiler.h>
|
||||
|
||||
#include <simgear/bucket/newbucket.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
|
||||
class TGSrtmBase {
|
||||
|
||||
protected:
|
||||
TGSrtmBase() : remove_tmp_file(false)
|
||||
{}
|
||||
|
||||
~TGSrtmBase();
|
||||
|
||||
// coordinates (in arc seconds) of south west corner
|
||||
double originx, originy;
|
||||
|
@ -47,7 +51,7 @@ protected:
|
|||
double col_step, row_step;
|
||||
|
||||
bool remove_tmp_file;
|
||||
SGPath remove_file_name;
|
||||
simgear::Dir tmp_dir;
|
||||
|
||||
public:
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include <simgear/bucket/newbucket.hxx>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
|
||||
#include <HGT/hgt.hxx>
|
||||
#include <Polygon/point2d.hxx>
|
||||
|
@ -67,8 +68,8 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
|
||||
SGPath sgp( work_dir );
|
||||
sgp.append( "dummy" );
|
||||
sgp.create_dir( 0755 );
|
||||
simgear::Dir workDir(sgp);
|
||||
workDir.create(0755);
|
||||
|
||||
TGHgt hgt(resolution, hgt_name);
|
||||
hgt.load();
|
||||
|
|
|
@ -94,7 +94,6 @@ private:
|
|||
TGSrtmTiff::TGSrtmTiff( const SGPath &file ) {
|
||||
lkind = BottomLeft;
|
||||
tif = 0;
|
||||
remove_tmp_file = false;
|
||||
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 );
|
||||
|
@ -103,7 +102,6 @@ TGSrtmTiff::TGSrtmTiff( const SGPath &file ) {
|
|||
TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) {
|
||||
lkind = lk;
|
||||
tif = 0;
|
||||
remove_tmp_file = false;
|
||||
output_data = 0;
|
||||
if ( lkind == BottomLeft ) {
|
||||
data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE];
|
||||
|
@ -121,15 +119,6 @@ TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) {
|
|||
TGSrtmTiff::~TGSrtmTiff() {
|
||||
delete[] 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 )
|
||||
TIFFClose( tif );
|
||||
}
|
||||
|
@ -160,28 +149,22 @@ bool TGSrtmTiff::open( const SGPath &f ) {
|
|||
int x, y;
|
||||
pos_from_name( file_name.file(), prefix, x, y );
|
||||
if ( ext == "zip" ) {
|
||||
// extract the .zip file to /tmp and point the file name
|
||||
// to the extracted file
|
||||
SGPath tmp_dir_path = string( tempnam( 0, "srtm" ) );
|
||||
simgear::Dir tmp_dir(tmp_dir_path);
|
||||
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();
|
||||
tmp_dir = simgear::Dir::tempDir("srtm");
|
||||
|
||||
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() );
|
||||
|
||||
simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
|
||||
BOOST_FOREACH(const SGPath& file, files) {
|
||||
string ext = file.extension();
|
||||
if ( ext == "TIF" || ext == "tif" ) {
|
||||
string ext = file.lower_extension();
|
||||
if ( (ext == "tif") || (ext == "tiff") ) {
|
||||
file_name = file;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
remove_tmp_file = true;
|
||||
remove_file_name = file_name.str();
|
||||
|
||||
cout << "Proceeding with " << file_name.str() << endl;
|
||||
}
|
||||
|
||||
|
@ -361,8 +344,8 @@ int main(int argc, char **argv) {
|
|||
string work_dir = argv[2];
|
||||
|
||||
SGPath sgp( work_dir );
|
||||
sgp.append( "dummy" );
|
||||
sgp.create_dir( 0755 );
|
||||
simgear::Dir workDir(sgp);
|
||||
workDir.create( 0755 );
|
||||
|
||||
TGSrtmTiff hgt( hgt_name );
|
||||
hgt.load();
|
||||
|
|
Loading…
Reference in a new issue