Skip to content

Commit

Permalink
PG raster: make wkb parser more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jan 15, 2020
1 parent f290007 commit e9b58cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
Expand Up @@ -261,5 +261,5 @@ QgsPostgresRasterSharedData::Tile::Tile( const QgsPostgresRasterSharedData::Tile

QByteArray QgsPostgresRasterSharedData::Tile::bandData( int bandNo ) const
{
return QgsPostgresRasterUtils::parseWkb( data, bandNo )[ QStringLiteral( "data" )].toByteArray();
return QgsPostgresRasterUtils::parseWkb( data, bandNo )[ QStringLiteral( "band%1" ).arg( bandNo )].toByteArray();
}
24 changes: 13 additions & 11 deletions src/providers/postgres/raster/qgspostgresrasterutils.cpp
Expand Up @@ -14,6 +14,7 @@
* *
***************************************************************************/


#include "qgspostgresrasterutils.h"
#include "qgsmessagelog.h"

Expand Down Expand Up @@ -121,6 +122,7 @@ QVariantMap QgsPostgresRasterUtils::parseWkb( const QByteArray &wkb, int bandNo
pxSize = 8;
break;
default:
result[ QStringLiteral( "nodata" ) ] = std::numeric_limits<double>::min();
QgsMessageLog::logMessage( QStringLiteral( "Unsupported pixel type: %1" )
.arg( result[ QStringLiteral( "pxType" ) ].toInt() ), QStringLiteral( "PostGIS" ), Qgis::Critical );

Expand All @@ -137,19 +139,19 @@ QVariantMap QgsPostgresRasterUtils::parseWkb( const QByteArray &wkb, int bandNo
return result;
}

// Read first band
readBandHeader( );
// Read other bands
for ( int i = 2; i <= bandNo; ++i )
// Read bands (all bands if bandNo is 0)
for ( unsigned int bandCnt = 1; bandCnt <= ( bandNo == 0 ? nBands : static_cast<unsigned int>( bandNo ) ); ++bandCnt )
{
offset += result[ QStringLiteral( "dataSize" ) ].toInt();
readBandHeader( );
if ( bandNo == 0 || static_cast<unsigned int>( bandNo ) == bandCnt )
{
result[ QStringLiteral( "band%1" ).arg( bandCnt )] = wkb.mid( offset, result[ QStringLiteral( "dataSize" ) ].toInt() );
}
else
{
// Skip
}
offset += result[ QStringLiteral( "dataSize" ) ].toInt();
}

// Check we did the math good
Q_ASSERT( result[ QStringLiteral( "dataSize" ) ].toInt() == result[ QStringLiteral( "width" ) ].toInt() * result[ QStringLiteral( "height" ) ].toInt() * result[ QStringLiteral( "pxSize" ) ].toInt( ) );

result[ QStringLiteral( "data" )] = wkb.mid( offset, result[ QStringLiteral( "dataSize" ) ].toInt() );

return result;
}
7 changes: 5 additions & 2 deletions src/providers/postgres/raster/qgspostgresrasterutils.h
Expand Up @@ -22,8 +22,11 @@
struct QgsPostgresRasterUtils
{

//! Parses a WKB raster hex and returns information as a variant map
static QVariantMap parseWkb( const QByteArray &wkb, int bandNo = 1 );
/**
* Parses a \a wkb raster hex and returns information as a variant map
* for a particular \a bandNo or for all bands if bandNo is 0
*/
static QVariantMap parseWkb( const QByteArray &wkb, int bandNo = 0 );

};

Expand Down

0 comments on commit e9b58cc

Please sign in to comment.