Skip to content

Commit e9b58cc

Browse files
committedJan 15, 2020
PG raster: make wkb parser more flexible
1 parent f290007 commit e9b58cc

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed
 

‎src/providers/postgres/raster/qgspostgresrastershareddata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,5 @@ QgsPostgresRasterSharedData::Tile::Tile( const QgsPostgresRasterSharedData::Tile
261261

262262
QByteArray QgsPostgresRasterSharedData::Tile::bandData( int bandNo ) const
263263
{
264-
return QgsPostgresRasterUtils::parseWkb( data, bandNo )[ QStringLiteral( "data" )].toByteArray();
264+
return QgsPostgresRasterUtils::parseWkb( data, bandNo )[ QStringLiteral( "band%1" ).arg( bandNo )].toByteArray();
265265
}

‎src/providers/postgres/raster/qgspostgresrasterutils.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* *
1515
***************************************************************************/
1616

17+
1718
#include "qgspostgresrasterutils.h"
1819
#include "qgsmessagelog.h"
1920

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

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

140-
// Read first band
141-
readBandHeader( );
142-
// Read other bands
143-
for ( int i = 2; i <= bandNo; ++i )
142+
// Read bands (all bands if bandNo is 0)
143+
for ( unsigned int bandCnt = 1; bandCnt <= ( bandNo == 0 ? nBands : static_cast<unsigned int>( bandNo ) ); ++bandCnt )
144144
{
145-
offset += result[ QStringLiteral( "dataSize" ) ].toInt();
146145
readBandHeader( );
146+
if ( bandNo == 0 || static_cast<unsigned int>( bandNo ) == bandCnt )
147+
{
148+
result[ QStringLiteral( "band%1" ).arg( bandCnt )] = wkb.mid( offset, result[ QStringLiteral( "dataSize" ) ].toInt() );
149+
}
150+
else
151+
{
152+
// Skip
153+
}
154+
offset += result[ QStringLiteral( "dataSize" ) ].toInt();
147155
}
148-
149-
// Check we did the math good
150-
Q_ASSERT( result[ QStringLiteral( "dataSize" ) ].toInt() == result[ QStringLiteral( "width" ) ].toInt() * result[ QStringLiteral( "height" ) ].toInt() * result[ QStringLiteral( "pxSize" ) ].toInt( ) );
151-
152-
result[ QStringLiteral( "data" )] = wkb.mid( offset, result[ QStringLiteral( "dataSize" ) ].toInt() );
153-
154156
return result;
155157
}

‎src/providers/postgres/raster/qgspostgresrasterutils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
struct QgsPostgresRasterUtils
2323
{
2424

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

2831
};
2932

0 commit comments

Comments
 (0)
Please sign in to comment.