Skip to content

Commit

Permalink
[wms] Implement specific sample implementation for tiled layers with …
Browse files Browse the repository at this point in the history
…relevant data types
  • Loading branch information
nirvn committed Nov 5, 2022
1 parent f4890c2 commit 00b1157
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -3137,6 +3137,41 @@ QString QgsWmsProvider::htmlMetadata()
return metadata;
}

double QgsWmsProvider::sample( const QgsPointXY &point, int band, bool *ok, const QgsRectangle &boundingBox, int width, int height, int dpi )
{
if ( ok )
*ok = false;

Qgis::DataType bandDataType = sourceDataType( band );
if ( mSettings.mTiled && mTileMatrixSet &&
( bandDataType != Qgis::DataType::UnknownDataType && bandDataType != Qgis::DataType::ARGB32 && bandDataType != Qgis::DataType::ARGB32_Premultiplied ) )
{
const double maximumNativeResolution = mNativeResolutions.at( 0 );
const double xMin = point.x() - std::fmod( point.x(), maximumNativeResolution );
const double yMin = point.y() - std::fmod( point.y(), maximumNativeResolution );
QgsRectangle rect( xMin, yMin, xMin + maximumNativeResolution, yMin + maximumNativeResolution );


std::unique_ptr<QgsRasterBlock> b( block( band, rect, 1, 1 ) );
if ( b->isValid() )
{
bool isNoData = true;
const double value = b->valueAndNoData( 0, 0, isNoData );
if ( !isNoData )
{
if ( ok )
*ok = true;

return value;
}
}

return std::numeric_limits<double>::quiet_NaN();
}

return QgsRasterDataProvider::sample( point, band, ok, boundingBox, width, height, dpi );
}

QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox, int width, int height, int /*dpi*/ )
{
QgsDebugMsgLevel( QStringLiteral( "format = %1" ).arg( format ), 2 );
Expand Down
1 change: 1 addition & 0 deletions src/providers/wms/qgswmsprovider.h
Expand Up @@ -292,6 +292,7 @@ class QgsWmsProvider final: public QgsRasterDataProvider
int bandCount() const override;
QString htmlMetadata() override;
QgsRasterIdentifyResult identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 ) override;
double sample( const QgsPointXY &point, int band, bool *ok = nullptr, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 ) override;
QString lastErrorTitle() override;
QString lastError() override;
QString lastErrorFormat() override;
Expand Down
17 changes: 17 additions & 0 deletions tests/src/providers/testqgswmsprovider.cpp
Expand Up @@ -167,6 +167,23 @@ class TestQgsWmsProvider: public QgsTest
QVERIFY( imageCheck( "mbtiles_1", mapSettings ) );
}

void testMBTilesSample()
{
QString dataDir( TEST_DATA_DIR );
QUrlQuery uq;
uq.addQueryItem( "type", "mbtiles" );
uq.addQueryItem( "interpretation", "maptilerterrain" );
uq.addQueryItem( "url", QUrl::fromLocalFile( dataDir + "/isle_of_man.mbtiles" ).toString() );

QgsRasterLayer layer( uq.toString(), "isle_of_man", "wms" );
QVERIFY( layer.isValid() );

bool ok = false;
const double value = layer.dataProvider()->sample( QgsPointXY( -496419, 7213350 ), 1, &ok );
QVERIFY( ok );
QCOMPARE( value, 1167617.375 );
}

void testDpiDependentData()
{
QString dataDir( TEST_DATA_DIR );
Expand Down

0 comments on commit 00b1157

Please sign in to comment.