Skip to content

Commit

Permalink
[3d] Fix online terrain ignoring project coordinate transform context
Browse files Browse the repository at this point in the history
fixes #40390
  • Loading branch information
nirvn committed Dec 2, 2020
1 parent 63946c8 commit feedc1e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Expand Up @@ -157,6 +157,7 @@ QgsDemHeightMapGenerator::QgsDemHeightMapGenerator( QgsRasterLayer *dtm, const Q
, mResolution( resolution )
, mLastJobId( 0 )
, mDownloader( dtm ? nullptr : new QgsTerrainDownloader( transformContext ) )
, mTransformContext( transformContext )
{
}

Expand Down Expand Up @@ -205,9 +206,9 @@ static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectan
return data;
}

static QByteArray _readOnlineDtm( QgsTerrainDownloader *downloader, const QgsRectangle &extent, int res, const QgsCoordinateReferenceSystem &destCrs )
static QByteArray _readOnlineDtm( QgsTerrainDownloader *downloader, const QgsRectangle &extent, int res, const QgsCoordinateReferenceSystem &destCrs, const QgsCoordinateTransformContext &context )
{
return downloader->getHeightMap( extent, res, destCrs );
return downloader->getHeightMap( extent, res, destCrs, context );
}

int QgsDemHeightMapGenerator::render( const QgsChunkNodeId &nodeId )
Expand All @@ -231,7 +232,7 @@ int QgsDemHeightMapGenerator::render( const QgsChunkNodeId &nodeId )
if ( mDtm )
jd.future = QtConcurrent::run( _readDtmData, mClonedProvider, extent, mResolution, mTilingScheme.crs() );
else
jd.future = QtConcurrent::run( _readOnlineDtm, mDownloader.get(), extent, mResolution, mTilingScheme.crs() );
jd.future = QtConcurrent::run( _readOnlineDtm, mDownloader.get(), extent, mResolution, mTilingScheme.crs(), mTransformContext );

QFutureWatcher<QByteArray> *fw = new QFutureWatcher<QByteArray>( nullptr );
fw->setFuture( jd.future );
Expand Down
3 changes: 3 additions & 0 deletions src/3d/terrain/qgsdemterraintileloader_p.h
Expand Up @@ -35,6 +35,7 @@
#include <QMutex>

#include "qgschunknode_p.h"
#include "qgscoordinatetransformcontext.h"
#include "qgsrectangle.h"
#include "qgsterraintileloader_p.h"
#include "qgstilingscheme.h"
Expand Down Expand Up @@ -138,6 +139,8 @@ class QgsDemHeightMapGenerator : public QObject
mutable QMutex mLazyLoadDtmCoarseDataMutex;
//! used for height queries
QByteArray mDtmCoarseData;

QgsCoordinateTransformContext mTransformContext;
};

/// @endcond
Expand Down
4 changes: 2 additions & 2 deletions src/3d/terrain/qgsterraindownloader.cpp
Expand Up @@ -177,8 +177,8 @@ QByteArray QgsTerrainDownloader::getHeightMap( const QgsRectangle &extentOrig, i
}

// resample to the desired extent + resolution

QgsGdalUtils::resampleSingleBandRaster( hSrcDS.get(), hDstDS.get(), GRA_Bilinear );
QgsGdalUtils::resampleSingleBandRaster( hSrcDS.get(), hDstDS.get(), GRA_Bilinear,
context.calculateCoordinateOperation( mOnlineDtm->crs(), destCrs ).toUtf8().constData() );

QByteArray heightMapOut;
heightMapOut.resize( resOrig * resOrig * sizeof( float ) );
Expand Down
12 changes: 7 additions & 5 deletions src/core/qgsgdalutils.cpp
Expand Up @@ -145,7 +145,7 @@ gdal::dataset_unique_ptr QgsGdalUtils::imageToMemoryDataset( const QImage &image
return hSrcDS;
}

bool QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg )
bool QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg, const char *pszCoordinateOperation )
{
gdal::warp_options_unique_ptr psWarpOptions( GDALCreateWarpOptions() );
psWarpOptions->hSrcDS = hSrcDS;
Expand All @@ -160,10 +160,12 @@ bool QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH h
psWarpOptions->eResampleAlg = resampleAlg;

// Establish reprojection transformer.
psWarpOptions->pTransformerArg =
GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef( hSrcDS ),
hDstDS, GDALGetProjectionRef( hDstDS ),
FALSE, 0.0, 1 );
char **papszOptions = nullptr;
if ( pszCoordinateOperation != nullptr )
papszOptions = CSLSetNameValue( papszOptions, "COORDINATE_OPERATION", pszCoordinateOperation );
psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer2( hSrcDS, hDstDS, papszOptions );
CSLDestroy( papszOptions );

if ( ! psWarpOptions->pTransformerArg )
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsgdalutils.h
Expand Up @@ -66,7 +66,7 @@ class CORE_EXPORT QgsGdalUtils
* \returns TRUE on success
* \since QGIS 3.8
*/
static bool resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg );
static bool resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg, const char *pszCoordinateOperation );

/**
* Resamples a QImage \a image using GDAL resampler.
Expand Down
Expand Up @@ -492,7 +492,7 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
}

// Resample the raster to the final bounds and resolution
if ( ! QgsGdalUtils::resampleSingleBandRaster( tmpDS.get(), dstDS.get(), GDALResampleAlg::GRA_NearestNeighbour ) )
if ( ! QgsGdalUtils::resampleSingleBandRaster( tmpDS.get(), dstDS.get(), GDALResampleAlg::GRA_NearestNeighbour, nullptr ) )
{
const QString lastError = QString::fromUtf8( CPLGetLastErrorMsg() ) ;
QgsMessageLog::logMessage( tr( "Unable to resample and transform destination raster for tiles from %1: %2" )
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsgdalutils.cpp
Expand Up @@ -194,7 +194,7 @@ void TestQgsGdalUtils::testResampleSingleBandRaster()
gdal::dataset_unique_ptr dstDS = QgsGdalUtils::createSingleBandTiffDataset( outputFilename, GDT_Float32, outputExtent, 2, 2, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( dstDS );

QgsGdalUtils::resampleSingleBandRaster( srcDS.get(), dstDS.get(), GRA_NearestNeighbour );
QgsGdalUtils::resampleSingleBandRaster( srcDS.get(), dstDS.get(), GRA_NearestNeighbour, nullptr );
dstDS.reset();

std::unique_ptr<QgsRasterLayer> layer( new QgsRasterLayer( outputFilename, "test", "gdal" ) );
Expand Down

0 comments on commit feedc1e

Please sign in to comment.