Skip to content

Commit 775c230

Browse files
committedNov 6, 2022
[elevation profile] Fix raster profile generation against XYZ layers
1 parent 00b1157 commit 775c230

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed
 

‎src/core/raster/qgsrasterlayerprofilegenerator.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,13 @@ bool QgsRasterLayerProfileGenerator::generateProfile( const QgsProfileGeneration
189189
subRegionLeft,
190190
subRegionTop ) : transformedCurve->boundingBox();
191191

192-
if ( mRasterProvider->xSize() == 0 || mRasterProvider->ySize() == 0 )
192+
const bool zeroXYSize = mRasterProvider->xSize() == 0 || mRasterProvider->ySize() == 0;
193+
if ( zeroXYSize )
193194
{
194-
// e.g. XYZ tile source -- this is a rough hack for https://github.com/qgis/QGIS/issues/48806, which results
195-
// in pretty poor curves ;)
196195
const double curveLengthInPixels = sourceCurve->length() / context.mapUnitsPerDistancePixel();
197196
const double conversionFactor = curveLengthInPixels / transformedCurve->length();
198-
subRegionWidth = 2 * conversionFactor * rasterSubRegion.width();
199-
subRegionHeight = 2 * conversionFactor * rasterSubRegion.height();
197+
subRegionWidth = static_cast< int >( std::floor( rasterSubRegion.width() * conversionFactor ) );
198+
subRegionHeight = static_cast< int >( std::floor( rasterSubRegion.height() * conversionFactor ) );
200199
}
201200

202201
// iterate over the raster blocks, throwing away any which don't intersect the profile curve
@@ -248,8 +247,17 @@ bool QgsRasterLayerProfileGenerator::generateProfile( const QgsProfileGeneration
248247
// convert point to a pixel and sample, if it's in this block
249248
if ( blockExtent.contains( *it ) )
250249
{
251-
const int row = std::clamp( static_cast< int >( std::round( ( blockExtent.yMaximum() - it->y() ) / mRasterUnitsPerPixelY ) ), 0, blockRows - 1 );
252-
const int col = std::clamp( static_cast< int >( std::round( ( it->x() - blockExtent.xMinimum() ) / mRasterUnitsPerPixelX ) ), 0, blockColumns - 1 );
250+
int row, col;
251+
if ( zeroXYSize )
252+
{
253+
row = std::clamp( static_cast< int >( std::round( ( blockExtent.yMaximum() - it->y() ) * blockRows / blockExtent.height() ) ), 0, blockRows - 1 );
254+
col = std::clamp( static_cast< int >( std::round( ( it->x() - blockExtent.xMinimum() ) * blockColumns / blockExtent.width() ) ), 0, blockColumns - 1 );
255+
}
256+
else
257+
{
258+
row = std::clamp( static_cast< int >( std::round( ( blockExtent.yMaximum() - it->y() ) / mRasterUnitsPerPixelY ) ), 0, blockRows - 1 );
259+
col = std::clamp( static_cast< int >( std::round( ( it->x() - blockExtent.xMinimum() ) / mRasterUnitsPerPixelX ) ), 0, blockColumns - 1 );
260+
}
253261
double val = block->valueAndNoData( row, col, isNoData );
254262
if ( !isNoData )
255263
{

0 commit comments

Comments
 (0)
Please sign in to comment.