Skip to content

Commit

Permalink
Add unit test for mbtiles, copyright header, project exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 17, 2020
1 parent 4dc26a3 commit 9f7c6d9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/providers/wms/qgsmbtilesreader.cpp
@@ -1,3 +1,18 @@
/***************************************************************************
qgsmbtilesreader.cpp
--------------------------------------
Date : January 2020
Copyright : (C) 2020 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmbtilesreader.h"

#include "qgslogger.h"
Expand Down
15 changes: 15 additions & 0 deletions src/providers/wms/qgsmbtilesreader.h
@@ -1,3 +1,18 @@
/***************************************************************************
qgsmbtilesreader.h
--------------------------------------
Date : January 2020
Copyright : (C) 2020 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSMBTILESREADER_H
#define QGSMBTILESREADER_H

Expand Down
13 changes: 11 additions & 2 deletions src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -1363,10 +1363,19 @@ bool QgsWmsProvider::setupMBTilesCapabilities( const QString &uri )
QgsRectangle sourceExtentWgs84 = mbtilesReader.extent();
if ( !sourceExtentWgs84.isNull() )
{
QgsPointXY customTopLeft, customBottomRight;
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QgsCoordinateReferenceSystem( mSettings.mCrsId ),
transformContext() );
QgsPointXY customTopLeft = ct.transform( QgsPointXY( sourceExtentWgs84.xMinimum(), sourceExtentWgs84.yMaximum() ) );
QgsPointXY customBottomRight = ct.transform( QgsPointXY( sourceExtentWgs84.xMaximum(), sourceExtentWgs84.yMinimum() ) );
try
{
customTopLeft = ct.transform( QgsPointXY( sourceExtentWgs84.xMinimum(), sourceExtentWgs84.yMaximum() ) );
customBottomRight = ct.transform( QgsPointXY( sourceExtentWgs84.xMaximum(), sourceExtentWgs84.yMinimum() ) );
}
catch ( const QgsCsException & )
{
QgsDebugMsg( QStringLiteral( "Failed to reproject extent from MBTiles metadata" ) );
return false;
}
sourceExtent = QgsRectangle( customTopLeft.x(), customBottomRight.y(), customBottomRight.x(), customTopLeft.y() );
}

Expand Down
54 changes: 54 additions & 0 deletions tests/src/providers/testqgswmsprovider.cpp
Expand Up @@ -17,6 +17,8 @@
#include "qgstest.h"
#include <qgswmsprovider.h>
#include <qgsapplication.h>
#include <qgsmultirenderchecker.h>
#include <qgsrasterlayer.h>

/**
* \ingroup UnitTests
Expand All @@ -41,11 +43,22 @@ class TestQgsWmsProvider: public QObject

mCapabilities = new QgsWmsCapabilities();
QVERIFY( mCapabilities->parseResponse( content, config ) );

mReport += QLatin1String( "<h1>WMS Provider Tests</h1>\n" );
}

//runs after all tests
void cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}

delete mCapabilities;
QgsApplication::exitQgis();
}
Expand Down Expand Up @@ -87,8 +100,49 @@ class TestQgsWmsProvider: public QObject
"STYLES=&FORMAT=&TRANSPARENT=TRUE" ) );
}

void testMBTiles()
{
QString dataDir( TEST_DATA_DIR );
QUrlQuery uq;
uq.addQueryItem( "type", "mbtiles" );
uq.addQueryItem( "url", QUrl::fromLocalFile( dataDir + "/isle_of_man_xxx_invalid.mbtiles" ).toString() );

// check first that we do not accept invalid mbtiles paths
QgsRasterLayer layerInvalid( uq.toString(), "invalid", "wms" );
//QgsWmsProvider providerInvalid( uq.toString(), QgsDataProvider::ProviderOptions() );
QVERIFY( !layerInvalid.isValid() );

uq.addQueryItem( "url", QUrl::fromLocalFile( dataDir + "/isle_of_man.mbtiles" ).toString() );
QgsRasterLayer layer( uq.toString(), "isle_of_man", "wms" );
QVERIFY( layer.isValid() );

QVERIFY( imageCheck( "mbtiles_1", &layer, layer.extent() ) );
}


bool imageCheck( const QString &testType, QgsMapLayer *layer, const QgsRectangle &extent )
{
//use the QgsRenderChecker test utility class to
//ensure the rendered output matches our control image
QgsMapSettings mapSettings;
mapSettings.setLayers( QList<QgsMapLayer *>() << layer );
mapSettings.setExtent( extent );
mapSettings.setOutputSize( QSize( 400, 400 ) );
mapSettings.setOutputDpi( 96 );
QgsMultiRenderChecker myChecker;
myChecker.setControlPathPrefix( QStringLiteral( "wmsprovider" ) );
myChecker.setControlName( "expected_" + testType );
myChecker.setMapSettings( mapSettings );
myChecker.setColorTolerance( 20 );
bool myResultFlag = myChecker.runTest( testType, 500 );
mReport += myChecker.report();
return myResultFlag;
}

private:
QgsWmsCapabilities *mCapabilities = nullptr;

QString mReport;
};

QGSTEST_MAIN( TestQgsWmsProvider )
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/testdata/isle_of_man.mbtiles
Binary file not shown.

0 comments on commit 9f7c6d9

Please sign in to comment.