Skip to content

Commit 2ce7812

Browse files
committedMay 25, 2020
PG raster: fix wrong wkb data type conversion
Fix #36689
1 parent 4ddce03 commit 2ce7812

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed
 

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
310310
const int _int { val.toInt( &ok ) };
311311
if ( ! ok )
312312
{
313-
QgsMessageLog::logMessage( tr( "Cannot convert identified value to unsigned long" ), QStringLiteral( "PostGIS" ), Qgis::Warning );
313+
QgsMessageLog::logMessage( tr( "Cannot convert identified value to int" ), QStringLiteral( "PostGIS" ), Qgis::Warning );
314314
return false;
315315
}
316316
std::memcpy( data, &_int, sizeof( int ) );
317317
break;
318318
}
319319
case Qgis::DataType::Int32:
320320
{
321-
const long _long { val.toInt( &ok ) };
321+
const long _long { val.toLong( &ok ) };
322322
if ( ! ok )
323323
{
324324
QgsMessageLog::logMessage( tr( "Cannot convert identified value to long" ), QStringLiteral( "PostGIS" ), Qgis::Warning );

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,57 @@ QVariantMap QgsPostgresRasterUtils::parseWkb( const QByteArray &wkb, int bandNo
7171
{
7272
result[ QStringLiteral( "pxType" ) ] = *reinterpret_cast<const unsigned short int *>( &wkb.constData()[offset] ) & 0x0F;
7373
/*
74-
| 'Bool1' // unsupported
75-
| 'Uint2' // unsupported
76-
| 'Uint4' // unsupported
77-
| 'Int8'
78-
| 'Uint8'
79-
| 'Int16'
80-
| 'Uint16'
81-
| 'Int32'
82-
| 'Uint32'
83-
| 'Float32'
84-
| 'Float64'
74+
| 0 'Bool1' // unsupported
75+
| 1 'Uint2' // unsupported
76+
| 2 'Uint4' // unsupported
77+
| 3 'Int8'
78+
| 4 'Uint8'
79+
| 5 'Int16'
80+
| 6 'Uint16'
81+
| 7 'Int32'
82+
| 8 'Uint32'
83+
!!!!!!!!!!!!!!!!!!!!!!!!!!!
84+
| 9 is missing!!!!
85+
!!!!!!!!!!!!!!!!!!!!!!!!!!!
86+
| 10 'Float32'
87+
| 11 'Float64'
8588
*/
8689
offset++;
8790
int pxSize = 0; // in bytes
8891
switch ( result[ QStringLiteral( "pxType" ) ].toInt() )
8992
{
90-
case 4: // int8
93+
case 3: // int8
9194
pxSize = 1;
9295
result[ QStringLiteral( "nodata" ) ] = *reinterpret_cast<const short int *>( &wkb.constData()[ offset ] );
9396
break;
94-
case 5: // uint8
97+
case 4: // uint8
9598
result[ QStringLiteral( "nodata" ) ] = *reinterpret_cast<const unsigned short int *>( &wkb.constData()[ offset ] );
9699
pxSize = 1;
97100
break;
98-
case 6: // int16
101+
case 5: // int16
99102
result[ QStringLiteral( "nodata" ) ] = *reinterpret_cast<const int *>( &wkb.constData()[ offset ] );
100103
pxSize = 2;
101104
break;
102-
case 7: // uint16
105+
case 6: // uint16
103106
result[ QStringLiteral( "nodata" ) ] = *reinterpret_cast<const unsigned int *>( &wkb.constData()[ offset ] );
104107
pxSize = 2;
105108
break;
106-
case 8: // int32
109+
case 7: // int32
107110
result[ QStringLiteral( "nodata" ) ] = static_cast<long long>( *reinterpret_cast<const long int *>( &wkb.constData()[ offset ] ) );
108111
pxSize = 4;
109112
break;
110-
case 9: // uint32
113+
case 8: // uint32
111114
result[ QStringLiteral( "nodata" ) ] = static_cast<unsigned long long>( *reinterpret_cast<const unsigned long int *>( &wkb.constData()[ offset ] ) );
112115
pxSize = 4;
113116
break;
114-
case 10: // float
117+
118+
// Note: 9 is missing from the specs
119+
120+
case 10: // float 32 bit
115121
result[ QStringLiteral( "nodata" ) ] = *reinterpret_cast<const float *>( &wkb.constData()[ offset ] );
116122
pxSize = 4;
117123
break;
118-
case 11: // double
124+
case 11: // double 64 bit
119125
result[ QStringLiteral( "nodata" ) ] = *reinterpret_cast<const double *>( &wkb.constData()[ offset ] );
120126
pxSize = 8;
121127
break;
@@ -127,7 +133,7 @@ QVariantMap QgsPostgresRasterUtils::parseWkb( const QByteArray &wkb, int bandNo
127133
}
128134
result[ QStringLiteral( "pxSize" ) ] = pxSize;
129135
offset += pxSize; // Init of band data
130-
result[ QStringLiteral( "dataSize" ) ] = static_cast<unsigned int>( pxSize * result[ QStringLiteral( "width" ) ].toInt() * result[ QStringLiteral( "height" ) ].toInt() );
136+
result[ QStringLiteral( "dataSize" ) ] = static_cast<unsigned int>( pxSize ) * result[ QStringLiteral( "width" ) ].toUInt() * result[ QStringLiteral( "height" ) ].toUInt();
131137
};
132138

133139
if ( static_cast<unsigned int>( bandNo ) > nBands )
@@ -143,13 +149,13 @@ QVariantMap QgsPostgresRasterUtils::parseWkb( const QByteArray &wkb, int bandNo
143149
readBandHeader( );
144150
if ( bandNo == 0 || static_cast<unsigned int>( bandNo ) == bandCnt )
145151
{
146-
result[ QStringLiteral( "band%1" ).arg( bandCnt )] = wkb.mid( offset, result[ QStringLiteral( "dataSize" ) ].toInt() );
152+
result[ QStringLiteral( "band%1" ).arg( bandCnt )] = wkb.mid( offset, result[ QStringLiteral( "dataSize" ) ].toUInt() );
147153
}
148154
else
149155
{
150156
// Skip
151157
}
152-
offset += result[ QStringLiteral( "dataSize" ) ].toInt();
158+
offset += result[ QStringLiteral( "dataSize" ) ].toUInt();
153159
}
154160
return result;
155161
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct QgsPostgresRasterUtils
2525
/**
2626
* Parses a \a wkb raster hex and returns information as a variant map
2727
* for a particular \a bandNo or for all bands if bandNo is 0
28+
* See: https://git.osgeo.org/gitea/postgis/postgis/src/branch/master/raster/doc/RFC2-WellKnownBinaryFormat
2829
*/
2930
static QVariantMap parseWkb( const QByteArray &wkb, int bandNo = 0 );
3031

0 commit comments

Comments
 (0)
Please sign in to comment.