|
| 1 | +/*************************************************************************** |
| 2 | + testqgstilingscheme.cpp |
| 3 | + ---------------------- |
| 4 | + Date : March 2023 |
| 5 | + Copyright : (C) 2023 by Jean Felder |
| 6 | + Email : jean dot felder at oslandia 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 | + |
| 18 | +#include <cmath> |
| 19 | + |
| 20 | +#include "qgscoordinatereferencesystem.h" |
| 21 | +#include "qgstilingscheme.h" |
| 22 | + |
| 23 | +/** |
| 24 | + * \ingroup UnitTests |
| 25 | + * This is a unit test for the vertex tool |
| 26 | + */ |
| 27 | +class TestQgsTilingScheme : public QObject |
| 28 | +{ |
| 29 | + Q_OBJECT |
| 30 | + public: |
| 31 | + TestQgsTilingScheme() = default; |
| 32 | + |
| 33 | + private slots: |
| 34 | + void initTestCase();// will be called before the first testfunction is executed. |
| 35 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 36 | + |
| 37 | + void testTileTransforms(); |
| 38 | + private: |
| 39 | +}; |
| 40 | + |
| 41 | +//runs before all tests |
| 42 | +void TestQgsTilingScheme::initTestCase() |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +//runs after all tests |
| 47 | +void TestQgsTilingScheme::cleanupTestCase() |
| 48 | +{ |
| 49 | +} |
| 50 | + |
| 51 | +void TestQgsTilingScheme::testTileTransforms() |
| 52 | +{ |
| 53 | + const double xMin = 369381.0; |
| 54 | + const double yMin = 6461912.0; |
| 55 | + const double xMax = 388156.0; |
| 56 | + const double yMax = 6518400.0; |
| 57 | + const QgsCoordinateReferenceSystem crs( "EPSG:2154" ); |
| 58 | + const QgsRectangle rect( xMin, yMin, xMax, yMax ); |
| 59 | + QgsTilingScheme tilingScheme( rect, crs ); |
| 60 | + |
| 61 | + const double xMinFour = 376442.0; |
| 62 | + const double yMinTwo = 6465442.5; |
| 63 | + const double xMaxFour = 378207.25; |
| 64 | + const double yMaxTwo = 6467207.75; |
| 65 | + const int z = 5; |
| 66 | + |
| 67 | + const double tileSize = std::max( xMax - xMin, yMax - yMin ) / std::pow( 2, z ); |
| 68 | + QCOMPARE( tileSize, 1765.25 ); |
| 69 | + |
| 70 | + /* |
| 71 | + * Test tileToMap and mapToTile |
| 72 | + */ |
| 73 | + |
| 74 | + QgsPointXY point425 = tilingScheme.tileToMap( 4, 2, z ); |
| 75 | + const QgsPointXY expectedPoint425( xMinFour, yMinTwo ); |
| 76 | + QCOMPARE( point425, expectedPoint425 ); |
| 77 | + |
| 78 | + float x = 0.0f; |
| 79 | + float y = 0.0f; |
| 80 | + tilingScheme.mapToTile( expectedPoint425, z, x, y ); |
| 81 | + QCOMPARE( x, 4.0f ); |
| 82 | + QCOMPARE( y, 2.0f ); |
| 83 | + |
| 84 | + // test (x+1, y+1) tile |
| 85 | + |
| 86 | + QgsPointXY point535 = tilingScheme.tileToMap( 5, 3, z ); |
| 87 | + const QgsPointXY expectedPoint535( xMaxFour, yMaxTwo ); |
| 88 | + QCOMPARE( point535, expectedPoint535 ); |
| 89 | + |
| 90 | + tilingScheme.mapToTile( expectedPoint535, z, x, y ); |
| 91 | + QCOMPARE( x, 5.0f ); |
| 92 | + QCOMPARE( y, 3.0f ); |
| 93 | + |
| 94 | + /* |
| 95 | + * Test tileToExtent and extentToTile |
| 96 | + */ |
| 97 | + |
| 98 | + x = 4.0f; |
| 99 | + y = 2.0f; |
| 100 | + |
| 101 | + |
| 102 | + QgsRectangle rec425 = tilingScheme.tileToExtent( ( int )x, ( int )y, z ); |
| 103 | + QgsRectangle expectedRec425 = QgsRectangle( xMinFour, yMinTwo, xMaxFour, yMaxTwo ); |
| 104 | + QCOMPARE( rec425, expectedRec425 ); |
| 105 | + |
| 106 | + int newX = 0; |
| 107 | + int newY = 0; |
| 108 | + int newZ = 0; |
| 109 | + tilingScheme.extentToTile( expectedRec425, newX, newY, newZ ); |
| 110 | + QCOMPARE( newX, 4 ); |
| 111 | + QCOMPARE( newY, 2 ); |
| 112 | + QCOMPARE( newZ, 5 ); |
| 113 | + |
| 114 | + |
| 115 | + // test (x, y+1) tile |
| 116 | + QgsRectangle rec435 = tilingScheme.tileToExtent( ( int )x, ( int )( y + 1 ), z ); |
| 117 | + QgsRectangle expectedRec435 = QgsRectangle( xMinFour, yMinTwo + tileSize, xMaxFour, yMaxTwo + tileSize ); |
| 118 | + QCOMPARE( rec435, expectedRec435 ); |
| 119 | + tilingScheme.extentToTile( expectedRec435, newX, newY, newZ ); |
| 120 | + QCOMPARE( newX, 4 ); |
| 121 | + QCOMPARE( newY, 3 ); |
| 122 | + QCOMPARE( newZ, 5 ); |
| 123 | + |
| 124 | + |
| 125 | + // test (x+1, y) tile |
| 126 | + QgsRectangle rec525 = tilingScheme.tileToExtent( ( int )( x + 1 ), ( int )y, z ); |
| 127 | + QgsRectangle expectedRec525 = QgsRectangle( xMinFour + tileSize, yMinTwo, xMaxFour + tileSize, yMaxTwo ); |
| 128 | + QCOMPARE( rec525, expectedRec525 ); |
| 129 | + |
| 130 | + tilingScheme.extentToTile( expectedRec525, newX, newY, newZ ); |
| 131 | + QCOMPARE( newX, 5 ); |
| 132 | + QCOMPARE( newY, 2 ); |
| 133 | + QCOMPARE( newZ, 5 ); |
| 134 | + |
| 135 | + |
| 136 | + // test (x, y-1) tile |
| 137 | + QgsRectangle rec415 = tilingScheme.tileToExtent( ( int )x, ( int )( y - 1 ), z ); |
| 138 | + QgsRectangle expectedRec415 = QgsRectangle( xMinFour, yMinTwo - tileSize, xMaxFour, yMaxTwo - tileSize ); |
| 139 | + QCOMPARE( rec415, expectedRec415 ); |
| 140 | + |
| 141 | + tilingScheme.extentToTile( expectedRec415, newX, newY, newZ ); |
| 142 | + QCOMPARE( newX, 4 ); |
| 143 | + QCOMPARE( newY, 1 ); |
| 144 | + QCOMPARE( newZ, 5 ); |
| 145 | + |
| 146 | + |
| 147 | + // test (x-1, y) tile |
| 148 | + QgsRectangle rec325 = tilingScheme.tileToExtent( ( int )( x - 1 ), ( int )y, z ); |
| 149 | + QgsRectangle expectedRec325 = QgsRectangle( xMinFour - tileSize, yMinTwo, xMaxFour - tileSize, yMaxTwo ); |
| 150 | + QCOMPARE( rec325, expectedRec325 ); |
| 151 | + tilingScheme.extentToTile( expectedRec325, newX, newY, newZ ); |
| 152 | + QCOMPARE( newX, 3 ); |
| 153 | + QCOMPARE( newY, 2 ); |
| 154 | + QCOMPARE( newZ, 5 ); |
| 155 | +} |
| 156 | + |
| 157 | +QGSTEST_MAIN( TestQgsTilingScheme ) |
| 158 | +#include "testqgstilingscheme.moc" |
0 commit comments