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
// 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
# include <config.h>
#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>
#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/strutils.hxx>
#include <simgear/debug/logstream.hxx>
#include "dem.hxx"
@ -84,18 +60,18 @@ bool
TGDem::open ( const string& file ) {
// open input file (or read from stdin)
if ( file == "-" ) {
printf("Loading DEM data file: stdin\n");
SG_LOG(SG_GENERAL, SG_INFO, "Loading DEM data file: stdin");
// fd = stdin;
// fd = gzdopen(STDIN_FILENO, "r");
printf("Not yet ported ...\n");
SG_LOG(SG_GENERAL, SG_INFO, "Not yet ported ...");
return false;
} else {
in = new sg_gzifstream( file );
if ( !(*in) ) {
cout << "Cannot open " << file << endl;
SG_LOG(SG_GENERAL, SG_ALERT, "Cannot open " << file );
return false;
}
cout << "Loading DEM data file: " << file << endl;
SG_LOG(SG_GENERAL, SG_INFO, "Loading DEM data file: " << file );
}
return true;
@ -105,9 +81,8 @@ TGDem::open ( const string& file ) {
// close a DEM file
bool
TGDem::close () {
// the sg_gzifstream doesn't seem to have a close()
delete in;
in->close();
return true;
}
@ -130,7 +105,7 @@ TGDem::next_token() {
int
TGDem::next_int() {
int result;
*in >> result;
return result;
@ -158,7 +133,7 @@ TGDem::next_exp() {
const char* p = token.c_str();
char buf[64];
char* bp = buf;
for ( ; *p != 0; ++p )
{
if ( *p == 'D' )
@ -184,7 +159,7 @@ TGDem::read_a_record() {
in->get(c);
name += c;
}
// clean off the trailing whitespace
name = simgear::strutils::rstrip(name);
cout << " Quad name field: " << name << endl;
@ -309,7 +284,7 @@ TGDem::read_a_record() {
name += c;
}
cout << " Accuracy code = " << inum << "\n";
cout << " column step = " << col_step <<
cout << " column step = " << col_step <<
" row step = " << row_step << "\n";
// dimension of arrays to follow (1)
@ -373,7 +348,7 @@ TGDem::read_b_record( ) {
if ( prof_data > 10000 ) { // meters
prof_data = last;
}
dem_data[cur_col][i] = (float)prof_data;
last = prof_data;
}
@ -408,12 +383,11 @@ TGDem::parse( ) {
return true;
}
// write out the area of data covered by the specified bucket. Data
// is written out column by column starting at the lower left hand
// corner.
bool
TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
TGDem::write_area( const string& root, SGBucket& b ) {
// calculate some boundaries
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;
@ -422,9 +396,10 @@ TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
double max_y = ( b.get_center_lat() + 0.5 * b.get_height() ) * 3600.0;
cout << b << endl;
cout << "width = " << b.get_width() << " height = " << b.get_height()
<< endl;
cout << "width = " << b.get_width() << " height = " << b.get_height()
<< endl;
cout << "min = " << min_x << "," << min_y
<< " max = " << max_x << "," << max_y << endl;
int start_x = (int)((min_x - originx) / col_step);
int span_x = (int)(b.get_width() * 3600.0 / col_step);
@ -438,17 +413,17 @@ TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
// this write_area() routine and feed it buckets that coincide
// well with the underlying grid structure and spacing.
if ( ( min_x < (originx - 0.001) )
|| ( max_x > (originx + cols * col_step + 0.001) )
|| ( min_y < (originy - 0.001) )
|| ( max_y > (originy + rows * row_step + 0.001) ) ) {
cout << " ERROR: bucket at least partially outside DEM data range!" <<
endl;
return false;
if ( ( min_x < originx )
|| ( max_x > originx + cols * col_step )
|| ( min_y < originy )
|| ( max_y > originy + rows * row_step ) ) {
cout << " ERROR: bucket at least partially outside DEM data range!" <<
endl;
return false;
}
// If the area is all ocean, skip it.
if (!has_non_zero_elev(start_x, span_x, start_y, span_y)) {
if ( !has_non_zero_elev(start_x, span_x, start_y, span_y) ) {
cout << "Tile is all zero elevation: skipping" << endl;
return false;
}
@ -460,36 +435,30 @@ TGDem::write_area( const string& root, SGBucket& b, bool compress ) {
sgp.append( "dummy" );
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;
// write the file
FILE *fp;
if ( (fp = fopen(array_file.c_str(), "w")) == NULL ) {
cout << "cannot open " << array_file << " for writing!" << endl;
exit(-1);
gzFile fp;
if ( (fp = gzopen( array_file.c_str(), "wb9" )) == NULL ) {
cout << "ERROR: cannot open " << array_file << " for writing!" << endl;
exit(-1);
}
fprintf( fp, "%d %d\n", (int)min_x, (int)min_y );
fprintf( fp, "%d %f %d %f\n", span_x + 1, col_step,
span_y + 1, row_step );
gzprintf( fp, "%d %d\n", (int)min_x, (int)min_y );
gzprintf( fp, "%d %f %d %f\n", span_x + 1, (int)col_step,
span_y + 1, (int)row_step );
for ( int i = start_x; i <= start_x + span_x; ++i ) {
for ( int j = start_y; j <= start_y + span_y; ++j ) {
fprintf( fp, "%d ", (int)dem_data[i][j] );
}
fprintf( fp, "\n" );
}
fclose(fp);
if ( compress ) {
string command = "gzip --best -f " + array_file;
system( command.c_str() );
for ( int j = start_y; j <= start_y + span_y; ++j ) {
gzprintf( fp, "%d ", (int)dem_data[i][j] );
}
gzprintf( fp, "\n" );
}
gzclose(fp);
return true;
}
TGDem::~TGDem() {
// printf("class TGDem DEstructor called.\n");
delete [] dem_data;

View file

@ -18,16 +18,14 @@
// along with this program; if not, write to the Free Software
// 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
#define _DEM_HXX
#ifndef __cplusplus
#ifndef __cplusplus
# error This library requires C++
#endif
#endif
#ifdef HAVE_CONFIG_H
@ -38,8 +36,6 @@
#include <simgear/bucket/newbucket.hxx>
#include <simgear/misc/sgstream.hxx>
#include <string>
#define DEM_SIZE 1200
#define DEM_SIZE_1 1201
@ -54,13 +50,13 @@ private:
// coordinates (in arc seconds) of south west corner
double originx, originy;
// number of columns and rows
int cols, rows;
// Distance between column and row data points (in arc seconds)
double col_step, row_step;
// pointers to the actual grid data allocated here
float (*dem_data)[DEM_SIZE_1];
float (*output_data)[DEM_SIZE_1];
@ -70,7 +66,7 @@ private:
double dem_x1, dem_y1, dem_x2, dem_y2, dem_x3, dem_y3, dem_x4, dem_y4;
double dem_z1, dem_z2;
int dem_resolution, dem_num_profiles;
// Current "B" Record Information
int prof_col, prof_row;
int prof_num_cols, prof_num_rows;
@ -122,7 +118,7 @@ public:
// write out the area of data covered by the specified bucket.
// Data is written out column by column starting at the lower left
// hand corner.
bool write_area( const std::string& root, SGBucket& b, bool compress );
bool write_area( const std::string& root, SGBucket& b );
// Informational methods
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() );
if ( b_min == b_max ) {
dem.write_area( work_dir, b_min, true );
dem.write_area( work_dir, b_min );
} else {
SGBucket b_cur;
int dx, dy, i, j;
@ -92,7 +92,7 @@ int main(int argc, char **argv) {
for ( j = 0; j <= dy; j++ ) {
for ( i = 0; i <= dx; i++ ) {
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
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#ifdef HAVE_CONFIG_H
# include <config.h>
@ -29,10 +28,6 @@
#include <simgear/compiler.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <simgear/bucket/newbucket.hxx>
#include <simgear/debug/logstream.hxx>
#include <simgear/io/lowlevel.hxx>
@ -297,38 +292,32 @@ void write_bucket(const string& work_dir, SGBucket bucket,
int* buffer,
int min_x, int min_y,
int span_x, int span_y,
int col_step, int row_step,
bool compress = true)
int col_step, int row_step)
{
SGPath path(work_dir);
path.append(bucket.gen_base_path());
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;
if ( (fp = fopen(array_file.c_str(), "w")) == NULL ) {
gzFile fp;
if ( (fp = gzopen(array_file.c_str(), "wb9")) == NULL ) {
SG_LOG(SG_GENERAL, SG_ALERT, "cannot open " << array_file << " for writing!");
exit(-1);
}
fprintf( fp, "%d %d\n", min_x, min_y );
fprintf( fp, "%d %d %d %d\n",
gzprintf( fp, "%d %d\n", min_x, min_y );
gzprintf( fp, "%d %d %d %d\n",
span_x + 1, col_step,
span_y + 1, row_step );
for ( int x = 0; x <= span_x; ++x ) {
for ( int y = 0; y <= span_y; ++y )
fprintf( fp, "%d ", buffer[ y * span_x + x ] );
fprintf( fp, "\n" );
}
fclose(fp);
if ( compress ) {
string command = "gzip --best -f " + array_file;
system( command.c_str() );
gzprintf( fp, "%d ", buffer[ y * span_x + x ] );
gzprintf( fp, "\n" );
}
gzclose(fp);
}
void process_bucket(const string& work_dir, SGBucket bucket,