Skip to content

Commit

Permalink
[3d] Reproject DEM for terrain if the DEM has different CRS (fixes #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Nov 29, 2017
1 parent 62543eb commit ebde3be
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/3d/qgs3dmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ void Qgs3DMapSettings::readXml( const QDomElement &elem, const QgsReadWriteConte
QString terrainGenType = elemTerrainGenerator.attribute( "type" );
if ( terrainGenType == "dem" )
{
mTerrainGenerator.reset( new QgsDemTerrainGenerator );
QgsDemTerrainGenerator *demTerrainGenerator = new QgsDemTerrainGenerator;
demTerrainGenerator->setCrs( mCrs );
mTerrainGenerator.reset( demTerrainGenerator );
}
else if ( terrainGenType == "quantized-mesh" )
{
Expand Down
17 changes: 16 additions & 1 deletion src/3d/terrain/qgsdemterraingenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ QgsRasterLayer *QgsDemTerrainGenerator::layer() const
return qobject_cast<QgsRasterLayer *>( mLayer.layer.data() );
}

void QgsDemTerrainGenerator::setCrs( const QgsCoordinateReferenceSystem &crs )
{
mCrs = crs;
updateGenerator();
}

QgsTerrainGenerator *QgsDemTerrainGenerator::clone() const
{
QgsDemTerrainGenerator *cloned = new QgsDemTerrainGenerator;
cloned->mCrs = mCrs;
cloned->mLayer = mLayer;
cloned->mResolution = mResolution;
cloned->mSkirtHeight = mSkirtHeight;
Expand Down Expand Up @@ -66,13 +73,17 @@ void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
elem.setAttribute( "layer", mLayer.layerId );
elem.setAttribute( "resolution", mResolution );
elem.setAttribute( "skirt-height", mSkirtHeight );

// crs is not read/written - it should be the same as destination crs of the map
}

void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
{
mLayer = QgsMapLayerRef( elem.attribute( "layer" ) );
mResolution = elem.attribute( "resolution" ).toInt();
mSkirtHeight = elem.attribute( "skirt-height" ).toFloat();

// crs is not read/written - it should be the same as destination crs of the map
}

void QgsDemTerrainGenerator::resolveReferences( const QgsProject &project )
Expand All @@ -91,7 +102,11 @@ void QgsDemTerrainGenerator::updateGenerator()
QgsRasterLayer *dem = layer();
if ( dem )
{
mTerrainTilingScheme = QgsTilingScheme( dem->extent(), dem->crs() );
QgsRectangle te = dem->extent();
QgsCoordinateTransform terrainToMapTransform( dem->crs(), mCrs );
te = terrainToMapTransform.transformBoundingBox( te );

mTerrainTilingScheme = QgsTilingScheme( te, mCrs );
delete mHeightMapGenerator;
mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution );
}
Expand Down
4 changes: 4 additions & 0 deletions src/3d/terrain/qgsdemterraingenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class _3D_EXPORT QgsDemTerrainGenerator : public QgsTerrainGenerator
//! Returns raster layer with elevation model to be used for terrain generation
QgsRasterLayer *layer() const;

//! Sets CRS of the terrain
void setCrs( const QgsCoordinateReferenceSystem &crs );

//! Sets resolution of the generator (how many elevation samples on one side of a terrain tile)
void setResolution( int resolution ) { mResolution = resolution; updateGenerator(); }
//! Returns resolution of the generator (how many elevation samples on one side of a terrain tile)
Expand Down Expand Up @@ -73,6 +76,7 @@ class _3D_EXPORT QgsDemTerrainGenerator : public QgsTerrainGenerator

QgsDemHeightMapGenerator *mHeightMapGenerator = nullptr;

QgsCoordinateReferenceSystem mCrs;
//! source layer for heights
QgsMapLayerRef mLayer;
//! how many vertices to place on one side of the tile
Expand Down
16 changes: 13 additions & 3 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void QgsDemTerrainTileLoader::onHeightMapReady( int jobId, const QByteArray &hei
// ---------------------

#include <qgsrasterlayer.h>
#include <qgsrasterprojector.h>
#include <QtConcurrent/QtConcurrentRun>
#include <QFutureWatcher>

Expand All @@ -132,13 +133,22 @@ QgsDemHeightMapGenerator::~QgsDemHeightMapGenerator()

#include <QElapsedTimer>

static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectangle &extent, int res )
static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectangle &extent, int res, const QgsCoordinateReferenceSystem &destCrs )
{
QElapsedTimer t;
t.start();

// TODO: use feedback object? (but GDAL currently does not support cancelation anyway)
QgsRasterBlock *block = provider->block( 1, extent, res, res );
QgsRasterInterface *input = provider;
std::unique_ptr<QgsRasterProjector> projector;
if ( provider->crs() != destCrs )
{
projector.reset( new QgsRasterProjector );
projector->setCrs( provider->crs(), destCrs );
projector->setInput( provider );
input = projector.get();
}
QgsRasterBlock *block = input->block( 1, extent, res, res );

QByteArray data;
if ( block )
Expand Down Expand Up @@ -168,7 +178,7 @@ int QgsDemHeightMapGenerator::render( int x, int y, int z )
jd.extent = extent;
jd.timer.start();
// make a clone of the data provider so it is safe to use in worker thread
jd.future = QtConcurrent::run( _readDtmData, mClonedProvider, extent, mResolution );
jd.future = QtConcurrent::run( _readDtmData, mClonedProvider, extent, mResolution, mTilingScheme.crs() );

QFutureWatcher<QByteArray> *fw = new QFutureWatcher<QByteArray>;
fw->setFuture( jd.future );
Expand Down
2 changes: 2 additions & 0 deletions src/app/3d/qgs3dmapconfigwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void Qgs3DMapConfigWidget::apply()
if ( tGenNeedsUpdate )
{
QgsDemTerrainGenerator *demTerrainGen = new QgsDemTerrainGenerator;
demTerrainGen->setCrs( mMap->crs() );
demTerrainGen->setLayer( demLayer );
demTerrainGen->setResolution( spinTerrainResolution->value() );
demTerrainGen->setSkirtHeight( spinTerrainSkirtHeight->value() );
Expand Down Expand Up @@ -139,6 +140,7 @@ void Qgs3DMapConfigWidget::updateMaxZoomLevel()
if ( demLayer )
{
QgsDemTerrainGenerator *demTerrainGen = new QgsDemTerrainGenerator;
demTerrainGen->setCrs( mMap->crs() );
demTerrainGen->setLayer( demLayer );
demTerrainGen->setResolution( spinTerrainResolution->value() );
tGen.reset( demTerrainGen );
Expand Down

0 comments on commit ebde3be

Please sign in to comment.