1
0
Fork 0

Get rid of a system calls to gzip in demchop and gdalchop

This commit is contained in:
Christian Schmitt 2012-11-08 12:55:02 +01:00
parent 619bc7f1f7
commit da8003b1e3
4 changed files with 55 additions and 101 deletions

View file

@ -18,41 +18,17 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// //
// $Id: dem.cxx,v 1.20 2004-11-19 22:25:50 curt Exp $
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include <config.h> # include <config.h>
#endif #endif
#include <simgear/compiler.h>
#include <ctype.h> // isspace()
#include <stdlib.h> // atoi()
#include <math.h> // rint()
#include <stdio.h>
#include <string.h>
#include <iostream> #include <iostream>
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h> // stat()
#endif
#ifdef SG_HAVE_STD_INCLUDES
# include <cerrno>
#else
# include <errno.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h> // stat()
#endif
#include <simgear/constants.h>
#include <simgear/misc/sgstream.hxx>
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/debug/logstream.hxx>
#include "dem.hxx" #include "dem.hxx"
@ -84,18 +60,18 @@ bool
TGDem::open ( const string& file ) { TGDem::open ( const string& file ) {
// open input file (or read from stdin) // open input file (or read from stdin)
if ( file == "-" ) { if ( file == "-" ) {
printf("Loading DEM data file: stdin\n"); SG_LOG(SG_GENERAL, SG_INFO, "Loading DEM data file: stdin");
// fd = stdin; // fd = stdin;
// fd = gzdopen(STDIN_FILENO, "r"); // fd = gzdopen(STDIN_FILENO, "r");
printf("Not yet ported ...\n"); SG_LOG(SG_GENERAL, SG_INFO, "Not yet ported ...");
return false; return false;
} else { } else {
in = new sg_gzifstream( file ); in = new sg_gzifstream( file );
if ( !(*in) ) { if ( !(*in) ) {
cout << "Cannot open " << file << endl; SG_LOG(SG_GENERAL, SG_ALERT, "Cannot open " << file );
return false; return false;
} }
cout << "Loading DEM data file: " << file << endl; SG_LOG(SG_GENERAL, SG_INFO, "Loading DEM data file: " << file );
} }
return true; return true;
@ -105,9 +81,8 @@ TGDem::open ( const string& file ) {
// close a DEM file // close a DEM file
bool bool
TGDem::close () { TGDem::close () {
// the sg_gzifstream doesn't seem to have a close()
delete in; in->close();
return true; return true;
} }
@ -408,12 +383,11 @@ TGDem::parse( ) {
return true; return true;
} }
// 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
// corner. // corner.
bool bool
TGDem::write_area( const string& root, SGBucket& b, bool compress ) { TGDem::write_area( const string& root, SGBucket& b ) {
// calculate some boundaries // calculate some boundaries
double min_x = ( b.get_center_lon() - 0.5 * b.get_width() ) * 3600.0; double min_x = ( b.get_center_lon() - 0.5 * b.get_width() ) * 3600.0;
double max_x = ( b.get_center_lon() + 0.5 * b.get_width() ) * 3600.0; double max_x = ( b.get_center_lon() + 0.5 * b.get_width() ) * 3600.0;
@ -424,7 +398,8 @@ TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
cout << b << endl; cout << b << endl;
cout << "width = " << b.get_width() << " height = " << b.get_height() cout << "width = " << b.get_width() << " height = " << b.get_height()
<< endl; << endl;
cout << "min = " << min_x << "," << min_y
<< " max = " << max_x << "," << max_y << endl;
int start_x = (int)((min_x - originx) / col_step); int start_x = (int)((min_x - originx) / col_step);
int span_x = (int)(b.get_width() * 3600.0 / col_step); int span_x = (int)(b.get_width() * 3600.0 / col_step);
@ -438,10 +413,10 @@ TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
// this write_area() routine and feed it buckets that coincide // this write_area() routine and feed it buckets that coincide
// well with the underlying grid structure and spacing. // well with the underlying grid structure and spacing.
if ( ( min_x < (originx - 0.001) ) if ( ( min_x < originx )
|| ( max_x > (originx + cols * col_step + 0.001) ) || ( max_x > originx + cols * col_step )
|| ( min_y < (originy - 0.001) ) || ( min_y < originy )
|| ( max_y > (originy + rows * row_step + 0.001) ) ) { || ( max_y > originy + rows * row_step ) ) {
cout << " ERROR: bucket at least partially outside DEM data range!" << cout << " ERROR: bucket at least partially outside DEM data range!" <<
endl; endl;
return false; return false;
@ -460,36 +435,30 @@ TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
sgp.append( "dummy" ); sgp.append( "dummy" );
sgp.create_dir( 0755 ); sgp.create_dir( 0755 );
string array_file = path + "/" + b.gen_index_str() + ".arr"; string array_file = path + "/" + b.gen_index_str() + ".arr.gz";
cout << "array_file = " << array_file << endl; cout << "array_file = " << array_file << endl;
// write the file // write the file
FILE *fp; gzFile fp;
if ( (fp = fopen(array_file.c_str(), "w")) == NULL ) { if ( (fp = gzopen( array_file.c_str(), "wb9" )) == NULL ) {
cout << "cannot open " << array_file << " for writing!" << endl; cout << "ERROR: cannot open " << array_file << " for writing!" << endl;
exit(-1); exit(-1);
} }
fprintf( fp, "%d %d\n", (int)min_x, (int)min_y ); gzprintf( fp, "%d %d\n", (int)min_x, (int)min_y );
fprintf( fp, "%d %f %d %f\n", span_x + 1, col_step, gzprintf( fp, "%d %f %d %f\n", span_x + 1, (int)col_step,
span_y + 1, row_step ); span_y + 1, (int)row_step );
for ( int i = start_x; i <= start_x + span_x; ++i ) { for ( int i = start_x; i <= start_x + span_x; ++i ) {
for ( int j = start_y; j <= start_y + span_y; ++j ) { for ( int j = start_y; j <= start_y + span_y; ++j ) {
fprintf( fp, "%d ", (int)dem_data[i][j] ); gzprintf( fp, "%d ", (int)dem_data[i][j] );
} }
fprintf( fp, "\n" ); gzprintf( fp, "\n" );
}
fclose(fp);
if ( compress ) {
string command = "gzip --best -f " + array_file;
system( command.c_str() );
} }
gzclose(fp);
return true; return true;
} }
TGDem::~TGDem() { TGDem::~TGDem() {
// printf("class TGDem DEstructor called.\n"); // printf("class TGDem DEstructor called.\n");
delete [] dem_data; delete [] dem_data;

View file

@ -18,8 +18,6 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// //
// $Id: dem.hxx,v 1.11 2004-11-19 22:25:50 curt Exp $
#ifndef _DEM_HXX #ifndef _DEM_HXX
#define _DEM_HXX #define _DEM_HXX
@ -38,8 +36,6 @@
#include <simgear/bucket/newbucket.hxx> #include <simgear/bucket/newbucket.hxx>
#include <simgear/misc/sgstream.hxx> #include <simgear/misc/sgstream.hxx>
#include <string>
#define DEM_SIZE 1200 #define DEM_SIZE 1200
#define DEM_SIZE_1 1201 #define DEM_SIZE_1 1201
@ -122,7 +118,7 @@ public:
// write out the area of data covered by the specified bucket. // write out the area of data covered by the specified bucket.
// Data is written out column by column starting at the lower left // Data is written out column by column starting at the lower left
// hand corner. // hand corner.
bool write_area( const std::string& root, SGBucket& b, bool compress ); bool write_area( const std::string& root, SGBucket& b );
// Informational methods // Informational methods
inline double get_originx() const { return originx; } inline double get_originx() const { return originx; }

View file

@ -75,7 +75,7 @@ int main(int argc, char **argv) {
SGBucket b_max( max.x(), max.y() ); SGBucket b_max( max.x(), max.y() );
if ( b_min == b_max ) { if ( b_min == b_max ) {
dem.write_area( work_dir, b_min, true ); dem.write_area( work_dir, b_min );
} else { } else {
SGBucket b_cur; SGBucket b_cur;
int dx, dy, i, j; int dx, dy, i, j;
@ -92,7 +92,7 @@ int main(int argc, char **argv) {
for ( j = 0; j <= dy; j++ ) { for ( j = 0; j <= dy; j++ ) {
for ( i = 0; i <= dx; i++ ) { for ( i = 0; i <= dx; i++ ) {
b_cur = sgBucketOffset(min.x(), min.y(), i, j); b_cur = sgBucketOffset(min.x(), min.y(), i, j);
dem.write_area( work_dir, b_cur, true ); dem.write_area( work_dir, b_cur );
} }
} }
} }

View file

@ -21,7 +21,6 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// //
// $Id$
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include <config.h> # include <config.h>
@ -29,10 +28,6 @@
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <simgear/bucket/newbucket.hxx> #include <simgear/bucket/newbucket.hxx>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
#include <simgear/io/lowlevel.hxx> #include <simgear/io/lowlevel.hxx>
@ -297,38 +292,32 @@ void write_bucket(const string& work_dir, SGBucket bucket,
int* buffer, int* buffer,
int min_x, int min_y, int min_x, int min_y,
int span_x, int span_y, int span_x, int span_y,
int col_step, int row_step, int col_step, int row_step)
bool compress = true)
{ {
SGPath path(work_dir); SGPath path(work_dir);
path.append(bucket.gen_base_path()); path.append(bucket.gen_base_path());
path.create_dir( 0755 ); path.create_dir( 0755 );
string array_file = path.str() + "/" + bucket.gen_index_str() + ".arr"; string array_file = path.str() + "/" + bucket.gen_index_str() + ".arr.gz";
FILE *fp; gzFile fp;
if ( (fp = fopen(array_file.c_str(), "w")) == NULL ) { if ( (fp = gzopen(array_file.c_str(), "wb9")) == NULL ) {
SG_LOG(SG_GENERAL, SG_ALERT, "cannot open " << array_file << " for writing!"); SG_LOG(SG_GENERAL, SG_ALERT, "cannot open " << array_file << " for writing!");
exit(-1); exit(-1);
} }
fprintf( fp, "%d %d\n", min_x, min_y ); gzprintf( fp, "%d %d\n", min_x, min_y );
fprintf( fp, "%d %d %d %d\n", gzprintf( fp, "%d %d %d %d\n",
span_x + 1, col_step, span_x + 1, col_step,
span_y + 1, row_step ); span_y + 1, row_step );
for ( int x = 0; x <= span_x; ++x ) { for ( int x = 0; x <= span_x; ++x ) {
for ( int y = 0; y <= span_y; ++y ) for ( int y = 0; y <= span_y; ++y )
fprintf( fp, "%d ", buffer[ y * span_x + x ] ); gzprintf( fp, "%d ", buffer[ y * span_x + x ] );
fprintf( fp, "\n" ); gzprintf( fp, "\n" );
}
fclose(fp);
if ( compress ) {
string command = "gzip --best -f " + array_file;
system( command.c_str() );
} }
gzclose(fp);
} }
void process_bucket(const string& work_dir, SGBucket bucket, void process_bucket(const string& work_dir, SGBucket bucket,