25using namespace Qt::StringLiterals;
31static constexpr double PIXELS_TO_M = 2.8 / 10000.0;
35 constexpr double z0xMin = -20037508.3427892;
36 constexpr double z0yMax = 20037508.3427892;
44 double z0xMin = z0TopLeftPoint.
x();
45 double z0yMax = z0TopLeftPoint.
y();
46 double z0xMax = z0xMin + z0MatrixWidth * z0Dimension;
47 double z0yMin = z0yMax - z0MatrixHeight * z0Dimension;
50 constexpr double TILE_SIZE = 256.0;
53 const double scaleDenom0 = ( z0Dimension / TILE_SIZE ) * ( unitToMeters / PIXELS_TO_M );
55 int numTiles =
static_cast<int>( pow( 2,
zoomLevel ) );
60 tm.mMatrixWidth = z0MatrixWidth * numTiles;
61 tm.mMatrixHeight = z0MatrixHeight * numTiles;
62 tm.mTileXSpan = ( z0xMax - z0xMin ) / tm.mMatrixWidth;
63 tm.mTileYSpan = ( z0yMax - z0yMin ) / tm.mMatrixHeight;
64 tm.mExtent =
QgsRectangle( z0xMin, z0yMin, z0xMax, z0yMax );
65 tm.mScaleDenom = scaleDenom0 / pow( 2,
zoomLevel );
72 int numTiles =
static_cast<int>( pow( 2,
zoomLevel ) );
74 int aNumTiles =
static_cast<int>( pow( 2, aZoomLevel ) );
78 tm.mCrs = tileMatrix.
crs();
80 tm.mMatrixWidth = aMatrixWidth * numTiles / aNumTiles;
81 tm.mMatrixHeight = aMatrixHeight * numTiles / aNumTiles;
82 tm.mTileXSpan = aExtent.
width() / tm.mMatrixWidth;
83 tm.mTileYSpan = aExtent.
height() / tm.mMatrixHeight;
85 tm.mScaleDenom = tileMatrix.
scale() * pow( 2, aZoomLevel ) / pow( 2,
zoomLevel );
91 double xMin = mExtent.
xMinimum() + mTileXSpan *
id.column();
92 double xMax = xMin + mTileXSpan;
93 double yMax = mExtent.yMaximum() - mTileYSpan *
id.row();
94 double yMin = yMax - mTileYSpan;
100 double x = mExtent.xMinimum() + mTileXSpan *
id.column() + mTileXSpan / 2;
101 double y = mExtent.yMaximum() - mTileYSpan *
id.row() - mTileYSpan / 2;
107 double x0 = std::clamp( r.
xMinimum(), mExtent.xMinimum(), mExtent.xMaximum() );
108 double y0 = std::clamp( r.
yMinimum(), mExtent.yMinimum(), mExtent.yMaximum() );
109 double x1 = std::clamp( r.
xMaximum(), mExtent.xMinimum(), mExtent.xMaximum() );
110 double y1 = std::clamp( r.
yMaximum(), mExtent.yMinimum(), mExtent.yMaximum() );
111 if ( x0 >= x1 || y0 >= y1 )
114 double tileX1 = ( x0 - mExtent.xMinimum() ) / mTileXSpan;
115 double tileX2 = ( x1 - mExtent.xMinimum() ) / mTileXSpan;
116 double tileY1 = ( mExtent.yMaximum() - y1 ) / mTileYSpan;
117 double tileY2 = ( mExtent.yMaximum() - y0 ) / mTileYSpan;
119 QgsDebugMsgLevel( u
"Tile range of edges [%1,%2] - [%3,%4]"_s.arg( tileX1 ).arg( tileY1 ).arg( tileX2 ).arg( tileY2 ), 2 );
122 int startColumn = std::clamp(
static_cast<int>( floor( tileX1 ) ), 0, mMatrixWidth - 1 );
123 int endColumn = std::clamp(
static_cast<int>( floor( tileX2 ) ), 0, mMatrixWidth - 1 );
124 int startRow = std::clamp(
static_cast<int>( floor( tileY1 ) ), 0, mMatrixHeight - 1 );
125 int endRow = std::clamp(
static_cast<int>( floor( tileY2 ) ), 0, mMatrixHeight - 1 );
126 return QgsTileRange( startColumn, endColumn, startRow, endRow );
131 double dx = mapPoint.
x() - mExtent.xMinimum();
132 double dy = mExtent.yMaximum() - mapPoint.
y();
133 return QPointF( dx / mTileXSpan, dy / mTileYSpan );
192 if ( res == -1 || it->zoomLevel() < res )
193 res = it->zoomLevel();
203 if ( res == -1 || it->zoomLevel() > res )
204 res = it->zoomLevel();
241 double scaleUnder = 0;
242 double scaleOver = 0;
259 if ( it->scale() > scale && ( zoomUnder == -1 || zoomUnder < it->zoomLevel() ) )
261 zoomUnder = it->zoomLevel();
262 scaleUnder = it->scale();
264 if ( it->scale() < scale && ( zoomOver == -1 || zoomOver > it->zoomLevel() ) )
266 zoomOver = it->zoomLevel();
267 scaleOver = it->scale();
280 if ( scaleOver < scale && scale < scaleUnder )
282 return ( scaleUnder - scale ) / ( scaleUnder - scaleOver ) * ( zoomOver - zoomUnder ) + zoomUnder;
284 scaleUnder = scaleOver;
285 zoomUnder = zoomOver;
286 scaleOver = scaleOver / 2;
291 return ( scaleUnder - scale ) / ( scaleUnder - scaleOver ) * ( zoomOver - zoomUnder ) + zoomUnder;
300 tileZoom =
static_cast<int>( round(
scaleToZoom( scale ) ) );
303 tileZoom =
static_cast<int>( floor(
scaleToZoom( scale ) ) );
316 const double mapDpi = context.
scaleFactor() * 25.4;
331 constexpr double REFERENCE_DPI = 0.0254 / PIXELS_TO_M;
332 const double tileScale = actualMapScale * REFERENCE_DPI / mapDpi;
341 constexpr double METERS_PER_DEGREE = M_PI / 180.0 * 6378137;
342 constexpr double INCHES_PER_METER = 39.370078;
343 const double mapWidthInches = mapExtent.
width() * METERS_PER_DEGREE * INCHES_PER_METER;
345 double scale = mapWidthInches * mapDpi /
static_cast< double >( mapSize.width() );
356 return actualMapScale;
368 auto readMatrixFromElement = [](
const QDomElement &matrixElement ) ->
QgsTileMatrix {
370 matrix.mZoomLevel = matrixElement.attribute( u
"zoomLevel"_s ).toInt();
371 matrix.mMatrixWidth = matrixElement.attribute( u
"matrixWidth"_s ).toInt();
372 matrix.mMatrixHeight = matrixElement.attribute( u
"matrixHeight"_s ).toInt();
374 matrixElement.attribute( u
"xMin"_s ).toDouble(), matrixElement.attribute( u
"yMin"_s ).toDouble(), matrixElement.attribute( u
"xMax"_s ).toDouble(), matrixElement.attribute( u
"yMax"_s ).toDouble()
377 matrix.mScaleDenom = matrixElement.attribute( u
"scale"_s ).toDouble();
378 matrix.mTileXSpan = matrixElement.attribute( u
"tileXSpan"_s ).toDouble();
379 matrix.mTileYSpan = matrixElement.attribute( u
"tileYSpan"_s ).toDouble();
380 matrix.mCrs.
readXml( matrixElement );
384 const QDomNodeList children = element.childNodes();
385 for (
int i = 0; i < children.size(); i++ )
387 const QDomElement matrixElement = children.at( i ).toElement();
388 if ( matrixElement.tagName() ==
"rootMatrix"_L1 )
391 QgsTileMatrix matrix = readMatrixFromElement( matrixElement );
398 const QDomElement rootElement = element.firstChildElement( u
"rootMatrix"_s );
399 if ( !rootElement.isNull() )
401 mRootMatrix = readMatrixFromElement( rootElement );
409 QDomElement setElement = document.createElement( u
"matrixSet"_s );
412 auto writeMatrixToElement = [&document](
const QgsTileMatrix &matrix, QDomElement &matrixElement ) {
413 matrixElement.setAttribute( u
"zoomLevel"_s, matrix.
zoomLevel() );
414 matrixElement.setAttribute( u
"matrixWidth"_s, matrix.
matrixWidth() );
415 matrixElement.setAttribute( u
"matrixHeight"_s, matrix.
matrixHeight() );
423 matrixElement.setAttribute( u
"tileXSpan"_s,
qgsDoubleToString( matrix.mTileXSpan ) );
424 matrixElement.setAttribute( u
"tileYSpan"_s,
qgsDoubleToString( matrix.mTileYSpan ) );
431 QDomElement matrixElement = document.createElement( u
"matrix"_s );
432 writeMatrixToElement( *it, matrixElement );
433 setElement.appendChild( matrixElement );
436 QDomElement rootElement = document.createElement( u
"rootMatrix"_s );
438 setElement.appendChild( rootElement );
445 QVector<QgsTileXYZ> tiles;
448 for (
int tileRow = range.
startRow(); tileRow <= range.
endRow(); ++tileRow )
452 QgsTileXYZ tile( tileColumn, tileRow, zoomLevel );
462 if ( !tiles.contains( replacement ) )
463 tiles.append( replacement );
@ Esri
No scale doubling, always rounds down when matching to available tile levels.
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
TileAvailability
Possible availability states for a tile within a tile matrix.
@ UseLowerZoomLevelTile
Tile is not available at the requested zoom level, it should be replaced by a tile from a lower zoom ...
@ NotAvailable
Tile is not available within the matrix, e.g. there is no content for the tile.
@ AvailableNoChildren
Tile is available within the matrix, and is known to have no children (ie no higher zoom level tiles ...
@ Available
Tile is available within the matrix.
Represents a coordinate reference system (CRS).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
double rendererScale() const
Returns the renderer map scale.
QSize outputSize() const
Returns the size of the resulting rendered image, in pixels.
QgsRectangle mapExtent() const
Returns the original extent of the map being rendered.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Writes the set to an XML element.
Qgis::ScaleToTileZoomLevelMethod mScaleToTileZoomMethod
void addGoogleCrs84QuadTiles(int minimumZoom=0, int maximumZoom=14)
Adds tile matrices corresponding to the standard web mercator/GoogleCRS84Quad setup.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system associated with the tiles.
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
double scaleForRenderContext(const QgsRenderContext &context) const
Calculates the correct scale to use for the tiles when rendered using the specified render context.
int minimumZoom() const
Returns the minimum zoom level for tiles present in the set.
double scaleToZoom(double scale) const
Calculates a fractional zoom level given a map scale denominator.
QMap< int, QgsTileMatrix > mTileMatrices
QVector< QgsTileXYZ > tilesInRange(QgsTileRange range, int zoomLevel) const
Returns a list of tiles in the given tile range.
int maximumZoom() const
Returns the maximum zoom level for tiles present in the set.
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
Qgis::TileAvailability tileAvailability(QgsTileXYZ id) const
Returns the availability of the given tile in this matrix.
QgsTileMatrix rootMatrix() const
Returns the root tile matrix (usually corresponding to zoom level 0).
virtual bool readXml(const QDomElement &element, QgsReadWriteContext &context)
Reads the set from an XML element.
void addMatrix(const QgsTileMatrix &matrix)
Adds a matrix to the set.
double calculateTileScaleForMap(double actualMapScale, const QgsCoordinateReferenceSystem &mapCrs, const QgsRectangle &mapExtent, const QSize mapSize, const double mapDpi) const
Calculates the correct scale to use for the tiles when rendered using the specified map properties.
void dropMatricesOutsideZoomRange(int minimumZoom, int maximumZoom)
Deletes any existing matrices which fall outside the zoom range specified by minimumZoom to maximumZo...
bool isEmpty() const
Returns true if the matrix set is empty.
QgsTileMatrix mRootMatrix
void setRootMatrix(const QgsTileMatrix &matrix)
Sets the root tile matrix (usually corresponding to zoom level 0).
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
int scaleToZoomLevel(double scale, bool clamp=true) const
Finds the best fitting (integer) zoom level given a map scale denominator.
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
QgsRectangle tileExtent(QgsTileXYZ id) const
Returns extent of the given tile in this matrix.
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent) const
Returns tile range that fully covers the given extent.
static QgsTileMatrix fromWebMercator(int zoomLevel)
Returns a tile matrix for the usual web mercator.
QgsRectangle extent() const
Returns extent of the tile matrix.
int matrixWidth() const
Returns number of columns of the tile matrix.
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
double scale() const
Returns scale denominator of the tile matrix.
QgsPointXY tileCenter(QgsTileXYZ id) const
Returns center of the given tile in this matrix.
int matrixHeight() const
Returns number of rows of the tile matrix.
static QgsTileMatrix fromTileMatrix(int zoomLevel, const QgsTileMatrix &tileMatrix)
Returns a tile matrix based on another one.
int zoomLevel() const
Returns the zoom level of the tile matrix.
static QgsTileMatrix fromCustomDef(int zoomLevel, const QgsCoordinateReferenceSystem &crs, const QgsPointXY &z0TopLeftPoint, double z0Dimension, int z0MatrixWidth=1, int z0MatrixHeight=1)
Returns a tile matrix for a specific CRS, top left point, zoom level 0 dimension in CRS units.
A range of tiles in a tile matrix.
int endColumn() const
Returns index of the last column in the range.
int endRow() const
Returns index of the last row in the range.
int startRow() const
Returns index of the first row in the range.
int startColumn() const
Returns index of the first column in the range.
Stores coordinates of a tile in a tile matrix set.
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define BUILTIN_UNREACHABLE
#define QgsDebugMsgLevel(str, level)