Skip to content

Commit

Permalink
Crash when there is just y-part of the vector dataset in gdal (#80)
Browse files Browse the repository at this point in the history
fix #79 Crash when there is just y-part of the vector dataset in gdal
  • Loading branch information
PeterPetrik committed Jan 21, 2019
1 parent adfe803 commit 8d93063
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
60 changes: 60 additions & 0 deletions mdal/frmts/mdal_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,60 @@ void MDAL::DriverGdal::parseRasterBands( const MDAL::GdalDataset *cfGDALDataset
}
}

void MDAL::DriverGdal::fixRasterBands()
{
// go through all bands and find out if both x and y dataset are valid
// if such pair if found, make the group scalar by removal of null band pointer
// this can happen is parseBandInfo() returns false-positive in vector detection
for ( data_hash::iterator band = mBands.begin(); band != mBands.end(); band++ )
{
if ( band->second.empty() )
continue;

// scalars are always ok
bool is_scalar = ( band->second.begin()->second.size() == 1 );
if ( is_scalar )
continue;

// check if we have some null bands
int needs_fix = false;
for ( timestep_map::const_iterator time_step = band->second.begin(); time_step != band->second.end(); time_step++ )
{
std::vector<GDALRasterBandH> raster_bands = time_step->second;

if ( !raster_bands[0] )
{
needs_fix = true;
break;
}
if ( !raster_bands[1] )
{
needs_fix = true;
break;
}
}

// convert this vector to scalar
if ( needs_fix )
{
for ( timestep_map::iterator time_step = band->second.begin(); time_step != band->second.end(); time_step++ )
{
std::vector<GDALRasterBandH> &raster_bands = time_step->second;

if ( !raster_bands[0] )
{
raster_bands[0] = raster_bands[1];
}
// get rid of y-coord
raster_bands.resize( 1 );

// not we should have only 1 band and valid
assert( raster_bands[0] );
}
}
}
}

void MDAL::DriverGdal::addDataToOutput( GDALRasterBandH raster_band, std::shared_ptr<MemoryDataset> tos, bool is_vector, bool is_x )
{
assert( raster_band );
Expand Down Expand Up @@ -552,6 +606,12 @@ std::unique_ptr<MDAL::Mesh> MDAL::DriverGdal::load( const std::string &fileName,
}
}

// Fix consistency of groups
// It can happen that we thought that the
// group is vector based on name, but it could be just coicidence
// or clash in naming
fixRasterBands();

// Create MDAL datasets
addDatasetGroups();
}
Expand Down
1 change: 1 addition & 0 deletions mdal/frmts/mdal_gdal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace MDAL
void addDatasetGroups();
void createMesh();
void parseRasterBands( const GdalDataset *cfGDALDataset );
void fixRasterBands();

std::string mFileName;
const std::string mGdalDriverName; /* GDAL driver name */
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/windows/install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -e

echo "Installing qgis with choco"

choco install qgis --version 3.4.2 -y --verbose
choco install qgis --version 3.4.3 -y --verbose
mv "C:/Program Files/QGIS 3.4" "C:/OSGeo4W64"
Binary file added tests/data/grib/wind_only_u_component.grib
Binary file not shown.
43 changes: 43 additions & 0 deletions tests/test_gdal_grib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,49 @@ TEST( MeshGdalGribTest, VectorFile )
MDAL_CloseMesh( m );
}

TEST( MeshGdalGribTest, ScalarFileWithUComponent )
{
// https://github.com/lutraconsulting/MDAL/issues/79
std::string path = test_file( "/grib/wind_only_u_component.grib" );
MeshH m = MDAL_LoadMesh( path.c_str() );
ASSERT_NE( m, nullptr );
MDAL_Status s = MDAL_LastStatus();
EXPECT_EQ( MDAL_Status::None, s );
ASSERT_EQ( 1, MDAL_M_datasetGroupCount( m ) );

DatasetGroupH g = MDAL_M_datasetGroup( m, 0 );
ASSERT_NE( g, nullptr );

int meta_count = MDAL_G_metadataCount( g );
ASSERT_EQ( 1, meta_count );

const char *name = MDAL_G_name( g );
EXPECT_EQ( std::string( "10 metre wind [m/s]" ), std::string( name ) );

bool scalar = MDAL_G_hasScalarData( g );
EXPECT_EQ( true, scalar );

bool onVertices = MDAL_G_isOnVertices( g );
EXPECT_EQ( true, onVertices );

ASSERT_EQ( 2, MDAL_G_datasetCount( g ) );
DatasetH ds = MDAL_G_dataset( g, 0 );
ASSERT_NE( ds, nullptr );

bool valid = MDAL_D_isValid( ds );
EXPECT_EQ( true, valid );

bool active = getActive( ds, 0 );
EXPECT_EQ( true, active );

int count = MDAL_D_valueCount( ds );
ASSERT_EQ( 115680, count );

double value = getValue( ds, 1600 );
EXPECT_DOUBLE_EQ( -0.818756103515625, value );

MDAL_CloseMesh( m );
}

int main( int argc, char **argv )
{
Expand Down

0 comments on commit 8d93063

Please sign in to comment.