Skip to content

Commit b340ae7

Browse files
rldhontwonder-sk
authored andcommittedSep 8, 2021
[Bugfix] QgsTileMatrix::tileCenter does not return tile center
Fixing QgsTileMatrix::tileCenter and add a new unit tests.
1 parent 4183c97 commit b340ae7

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed
 

‎src/core/qgstiles.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ QgsRectangle QgsTileMatrix::tileExtent( QgsTileXYZ id ) const
4646

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

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ set(TESTS
193193
testqgsvectorlayerutils.cpp
194194
testqgsvectortilelayer.cpp
195195
testqgsvectortilewriter.cpp
196+
testqgstiles.cpp
196197
testqgsweakrelation.cpp
197198
testqgsziputils.cpp
198199
testqobjectuniqueptr.cpp

‎tests/src/core/testqgstiles.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/***************************************************************************
2+
testqgstiles.cpp
3+
--------------------------------------
4+
Date : August 2021
5+
Copyright : (C) 2021 by René-Luc Dhont
6+
Email : rldhont at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgstest.h"
17+
#include <QObject>
18+
#include <QString>
19+
20+
//qgis includes...
21+
#include "qgsapplication.h"
22+
#include "qgstiles.h"
23+
#include "qgsvectorlayer.h"
24+
25+
/**
26+
* \ingroup UnitTests
27+
* This is a unit test for vector tiles
28+
*/
29+
class TestQgsTiles : public QObject
30+
{
31+
Q_OBJECT
32+
33+
public:
34+
TestQgsTiles() = default;
35+
36+
private:
37+
QString mDataDir;
38+
QString mReport;
39+
40+
private slots:
41+
void initTestCase();// will be called before the first testfunction is executed.
42+
void cleanupTestCase();// will be called after the last testfunction was executed.
43+
void init() {} // will be called before each testfunction is executed.
44+
void cleanup() {} // will be called after every testfunction.
45+
46+
void test_matrixFromWebMercator();
47+
};
48+
49+
50+
void TestQgsTiles::initTestCase()
51+
{
52+
// init QGIS's paths - true means that all path will be inited from prefix
53+
QgsApplication::init();
54+
QgsApplication::initQgis();
55+
QgsApplication::showSettings();
56+
mDataDir = QString( TEST_DATA_DIR ); //defined in CmakeLists.txt
57+
}
58+
59+
void TestQgsTiles::cleanupTestCase()
60+
{
61+
QgsApplication::exitQgis();
62+
}
63+
64+
65+
void TestQgsTiles::test_matrixFromWebMercator()
66+
{
67+
QgsTileMatrix tm = QgsTileMatrix::fromWebMercator( 0 );
68+
QCOMPARE( tm.zoomLevel(), 0 );
69+
QCOMPARE( tm.matrixWidth(), 1 );
70+
QCOMPARE( tm.matrixHeight(), 1 );
71+
QVERIFY( qgsDoubleNear( tm.extent().xMinimum(), -20037508.3427892, 0.0000001 ) );
72+
QVERIFY( qgsDoubleNear( tm.extent().yMinimum(), -20037508.3427892, 0.0000001 ) );
73+
QVERIFY( qgsDoubleNear( tm.extent().xMaximum(), 20037508.3427892, 0.0000001 ) );
74+
QVERIFY( qgsDoubleNear( tm.extent().yMaximum(), 20037508.3427892, 0.0000001 ) );
75+
QVERIFY( qgsDoubleNear( tm.scale(), 559082264.0287178, 0.0000001 ) );
76+
77+
tm = QgsTileMatrix::fromWebMercator( 3 );
78+
QCOMPARE( tm.zoomLevel(), 3 );
79+
QCOMPARE( tm.matrixWidth(), 8 );
80+
QCOMPARE( tm.matrixHeight(), 8 );
81+
QVERIFY( qgsDoubleNear( tm.extent().xMinimum(), -20037508.3427892, 0.0000001 ) );
82+
QVERIFY( qgsDoubleNear( tm.extent().yMinimum(), -20037508.3427892, 0.0000001 ) );
83+
QVERIFY( qgsDoubleNear( tm.extent().xMaximum(), 20037508.3427892, 0.0000001 ) );
84+
QVERIFY( qgsDoubleNear( tm.extent().yMaximum(), 20037508.3427892, 0.0000001 ) );
85+
QVERIFY( qgsDoubleNear( tm.scale(), 559082264.0287178 / 8.0, 0.0000001 ) );
86+
87+
QgsRectangle te = tm.tileExtent( QgsTileXYZ( 3, 3, 3 ) );
88+
QVERIFY( qgsDoubleNear( te.xMinimum(), -5009377.0856973, 0.0000001 ) );
89+
QVERIFY( qgsDoubleNear( te.yMinimum(), 0, 0.0000001 ) );
90+
QVERIFY( qgsDoubleNear( te.xMaximum(), 0, 0.0000001 ) );
91+
QVERIFY( qgsDoubleNear( te.yMaximum(), 5009377.0856973, 0.0000001 ) );
92+
93+
QgsPointXY tc = tm.tileCenter( QgsTileXYZ( 3, 3, 3 ) );
94+
QVERIFY( qgsDoubleNear( tc.x(), te.xMinimum() + te.width() / 2, 0.0000001 ) );
95+
QVERIFY( qgsDoubleNear( tc.x(), -2504688.54284865, 0.0000001 ) );
96+
QVERIFY( qgsDoubleNear( tc.y(), te.yMinimum() + te.height() / 2, 0.0000001 ) );
97+
QVERIFY( qgsDoubleNear( tc.y(), 2504688.54284865, 0.0000001 ) );
98+
99+
QgsVectorLayer *vl = new QgsVectorLayer( mDataDir + "/points.shp", "points", "ogr" );
100+
QgsCoordinateReferenceSystem destCrs( "EPSG:3857" );
101+
QgsCoordinateTransform ct( vl->crs(), destCrs, QgsCoordinateTransformContext() );
102+
QgsRectangle r = ct.transformBoundingBox( vl->extent() );
103+
QgsTileRange tr = tm.tileRangeFromExtent( r );
104+
QVERIFY( tr.isValid() );
105+
QCOMPARE( tr.startColumn(), 1 );
106+
QCOMPARE( tr.endColumn(), 2 );
107+
QCOMPARE( tr.startRow(), 2 );
108+
QCOMPARE( tr.endRow(), 3 );
109+
}
110+
111+
112+
QGSTEST_MAIN( TestQgsTiles )
113+
#include "testqgstiles.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.