Skip to content
Permalink
Browse files
Merge pull request #45013 from rldhont/backport-44979-to-queued_ltr_b…
…ackports

[Backport release-3_16][Bugfix] QgsTileMatrix::tileCenter does not return tile center
  • Loading branch information
rldhont committed Sep 10, 2021
2 parents 11fb193 + 2077b48 commit 0abd95c9cc07dd2e4c312b27c2ee5aefea630223
Showing with 116 additions and 2 deletions.
  1. +2 −2 src/core/qgstiles.cpp
  2. +1 −0 tests/src/core/CMakeLists.txt
  3. +113 −0 tests/src/core/testqgstiles.cpp
@@ -46,8 +46,8 @@ QgsRectangle QgsTileMatrix::tileExtent( QgsTileXYZ id ) const

QgsPointXY QgsTileMatrix::tileCenter( QgsTileXYZ id ) const
{
double x = mExtent.xMinimum() + mTileXSpan / 2 * id.column();
double y = mExtent.yMaximum() - mTileYSpan / 2 * id.row();
double x = mExtent.xMinimum() + mTileXSpan * id.column() + mTileXSpan / 2;
double y = mExtent.yMaximum() - mTileYSpan * id.row() - mTileYSpan / 2;
return QgsPointXY( x, y );
}

@@ -250,6 +250,7 @@ set(TESTS
testqgsvectorlayerutils.cpp
testqgsvectortilelayer.cpp
testqgsvectortilewriter.cpp
testqgstiles.cpp
testqgsziputils.cpp
testziplayer.cpp
testqgslayerdefinition.cpp
@@ -0,0 +1,113 @@
/***************************************************************************
testqgstiles.cpp
--------------------------------------
Date : August 2021
Copyright : (C) 2021 by René-Luc Dhont
Email : rldhont 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 "qgstest.h"
#include <QObject>
#include <QString>

//qgis includes...
#include "qgsapplication.h"
#include "qgstiles.h"
#include "qgsvectorlayer.h"

/**
* \ingroup UnitTests
* This is a unit test for vector tiles
*/
class TestQgsTiles : public QObject
{
Q_OBJECT

public:
TestQgsTiles() = default;

private:
QString mDataDir;
QString mReport;

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.

void test_matrixFromWebMercator();
};


void TestQgsTiles::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();
mDataDir = QString( TEST_DATA_DIR ); //defined in CmakeLists.txt
}

void TestQgsTiles::cleanupTestCase()
{
QgsApplication::exitQgis();
}


void TestQgsTiles::test_matrixFromWebMercator()
{
QgsTileMatrix tm = QgsTileMatrix::fromWebMercator( 0 );
QCOMPARE( tm.zoomLevel(), 0 );
QCOMPARE( tm.matrixWidth(), 1 );
QCOMPARE( tm.matrixHeight(), 1 );
QVERIFY( qgsDoubleNear( tm.extent().xMinimum(), -20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.extent().yMinimum(), -20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.extent().xMaximum(), 20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.extent().yMaximum(), 20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.scale(), 559082264.0287178, 0.0000001 ) );

tm = QgsTileMatrix::fromWebMercator( 3 );
QCOMPARE( tm.zoomLevel(), 3 );
QCOMPARE( tm.matrixWidth(), 8 );
QCOMPARE( tm.matrixHeight(), 8 );
QVERIFY( qgsDoubleNear( tm.extent().xMinimum(), -20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.extent().yMinimum(), -20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.extent().xMaximum(), 20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.extent().yMaximum(), 20037508.3427892, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tm.scale(), 559082264.0287178 / 8.0, 0.0000001 ) );

QgsRectangle te = tm.tileExtent( QgsTileXYZ( 3, 3, 3 ) );
QVERIFY( qgsDoubleNear( te.xMinimum(), -5009377.0856973, 0.0000001 ) );
QVERIFY( qgsDoubleNear( te.yMinimum(), 0, 0.0000001 ) );
QVERIFY( qgsDoubleNear( te.xMaximum(), 0, 0.0000001 ) );
QVERIFY( qgsDoubleNear( te.yMaximum(), 5009377.0856973, 0.0000001 ) );

QgsPointXY tc = tm.tileCenter( QgsTileXYZ( 3, 3, 3 ) );
QVERIFY( qgsDoubleNear( tc.x(), te.xMinimum() + te.width() / 2, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tc.x(), -2504688.54284865, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tc.y(), te.yMinimum() + te.height() / 2, 0.0000001 ) );
QVERIFY( qgsDoubleNear( tc.y(), 2504688.54284865, 0.0000001 ) );

QgsVectorLayer *vl = new QgsVectorLayer( mDataDir + "/points.shp", "points", "ogr" );
QgsCoordinateReferenceSystem destCrs( "EPSG:3857" );
QgsCoordinateTransform ct( vl->crs(), destCrs, QgsCoordinateTransformContext() );
QgsRectangle r = ct.transformBoundingBox( vl->extent() );
QgsTileRange tr = tm.tileRangeFromExtent( r );
QVERIFY( tr.isValid() );
QCOMPARE( tr.startColumn(), 1 );
QCOMPARE( tr.endColumn(), 2 );
QCOMPARE( tr.startRow(), 2 );
QCOMPARE( tr.endRow(), 3 );
}


QGSTEST_MAIN( TestQgsTiles )
#include "testqgstiles.moc"

0 comments on commit 0abd95c

Please sign in to comment.