QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsdemterraingenerator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdemterraingenerator.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 by Martin Dobias
6  Email : wonder dot sk at gmail 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 "qgsdemterraingenerator.h"
17 
19 
20 #include "qgsrasterlayer.h"
21 #include "qgscoordinatetransform.h"
22 
24 {
25  delete mHeightMapGenerator;
26 }
27 
29 {
30  mLayer = QgsMapLayerRef( layer );
31  updateGenerator();
32 }
33 
35 {
36  return qobject_cast<QgsRasterLayer *>( mLayer.layer.data() );
37 }
38 
40 {
41  mCrs = crs;
42  mTransformContext = context;
43  updateGenerator();
44 }
45 
47 {
49  cloned->setTerrain( mTerrain );
50  cloned->mCrs = mCrs;
51  cloned->mLayer = mLayer;
52  cloned->mResolution = mResolution;
53  cloned->mSkirtHeight = mSkirtHeight;
54  cloned->updateGenerator();
55  return cloned;
56 }
57 
59 {
61 }
62 
64 {
65  return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
66 }
67 
68 float QgsDemTerrainGenerator::heightAt( double x, double y, const Qgs3DMapSettings &map ) const
69 {
70  Q_UNUSED( map )
71  if ( mHeightMapGenerator )
72  return mHeightMapGenerator->heightAt( x, y );
73  else
74  return 0;
75 }
76 
77 void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
78 {
79  elem.setAttribute( QStringLiteral( "layer" ), mLayer.layerId );
80  elem.setAttribute( QStringLiteral( "resolution" ), mResolution );
81  elem.setAttribute( QStringLiteral( "skirt-height" ), mSkirtHeight );
82 
83  // crs is not read/written - it should be the same as destination crs of the map
84 }
85 
86 void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
87 {
88  mLayer = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
89  mResolution = elem.attribute( QStringLiteral( "resolution" ) ).toInt();
90  mSkirtHeight = elem.attribute( QStringLiteral( "skirt-height" ) ).toFloat();
91 
92  // crs is not read/written - it should be the same as destination crs of the map
93 }
94 
96 {
97  mLayer = QgsMapLayerRef( project.mapLayer( mLayer.layerId ) );
98  updateGenerator();
99 }
100 
101 QgsChunkLoader *QgsDemTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
102 {
103  // A bit of a hack to make cloning terrain generator work properly
104  return new QgsDemTerrainTileLoader( mTerrain, node, const_cast<QgsDemTerrainGenerator *>( this ) );
105 }
106 
107 void QgsDemTerrainGenerator::updateGenerator()
108 {
109  QgsRasterLayer *dem = layer();
110  if ( dem )
111  {
112  QgsRectangle te = dem->extent();
113  QgsCoordinateTransform terrainToMapTransform( dem->crs(), mCrs, mTransformContext );
114  terrainToMapTransform.setBallparkTransformsAreAppropriate( true );
115  te = terrainToMapTransform.transformBoundingBox( te );
116 
118  delete mHeightMapGenerator;
119  mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution, mTransformContext );
120  mIsValid = true;
121  }
122  else
123  {
125  delete mHeightMapGenerator;
126  mHeightMapGenerator = nullptr;
127  mIsValid = false;
128  }
129 }
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
QgsTerrainGenerator::Dem
@ Dem
Terrain is built from raster layer with digital elevation model.
Definition: qgsterraingenerator.h:58
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
qgsrasterlayer.h
QgsTerrainGenerator::crs
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
Definition: qgsterraingenerator.h:106
QgsDemTerrainGenerator::createChunkLoader
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override
Definition: qgsdemterraingenerator.cpp:101
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsDemTerrainGenerator::readXml
void readXml(const QDomElement &elem) override
Read terrain generator's configuration from XML.
Definition: qgsdemterraingenerator.cpp:86
qgsdemterraingenerator.h
QgsMapLayerRef
_LayerRef< QgsMapLayer > QgsMapLayerRef
Definition: qgsmaplayerref.h:295
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3680
_LayerRef::layer
QPointer< TYPE > layer
Weak pointer to map layer.
Definition: qgsmaplayerref.h:114
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsDemTerrainGenerator::type
Type type() const override
What texture generator implementation is this.
Definition: qgsdemterraingenerator.cpp:58
_LayerRef::layerId
QString layerId
Original layer ID.
Definition: qgsmaplayerref.h:117
QgsTerrainGenerator::mTerrain
QgsTerrainEntity * mTerrain
Definition: qgsterraingenerator.h:122
QgsTerrainGenerator::mTerrainTilingScheme
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
Definition: qgsterraingenerator.h:121
QgsDemTerrainGenerator::layer
QgsRasterLayer * layer() const
Returns raster layer with elevation model to be used for terrain generation.
Definition: qgsdemterraingenerator.cpp:34
QgsDemTerrainGenerator::clone
QgsTerrainGenerator * clone() const override
Makes a copy of the current instance.
Definition: qgsdemterraingenerator.cpp:46
QgsTerrainGenerator
Base class for generators of terrain.
Definition: qgsterraingenerator.h:49
QgsMapLayer::extent
virtual QgsRectangle extent() const
Returns the extent of the layer.
Definition: qgsmaplayer.cpp:305
QgsDemTerrainGenerator::heightAt
float heightAt(double x, double y, const Qgs3DMapSettings &map) const override
Returns height at (x,y) in terrain's CRS.
Definition: qgsdemterraingenerator.cpp:68
qgsdemterraintileloader_p.h
QgsTerrainGenerator::Type
Type
Enumeration of the available terrain generators.
Definition: qgsterraingenerator.h:55
qgscoordinatetransform.h
Qgs3DMapSettings
Definition of the world.
Definition: qgs3dmapsettings.h:57
QgsDemTerrainGenerator::resolveReferences
void resolveReferences(const QgsProject &project) override
After read of XML, resolve references to any layers that have been read as layer IDs.
Definition: qgsdemterraingenerator.cpp:95
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsTilingScheme::tileToExtent
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Definition: qgstilingscheme.cpp:43
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsDemTerrainGenerator
Implementation of terrain generator that uses a raster layer with DEM to build terrain.
Definition: qgsdemterraingenerator.h:41
QgsDemTerrainGenerator::setCrs
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets CRS of the terrain.
Definition: qgsdemterraingenerator.cpp:39
QgsDemTerrainGenerator::setLayer
void setLayer(QgsRasterLayer *layer)
Sets raster layer with elevation model to be used for terrain generation.
Definition: qgsdemterraingenerator.cpp:28
QgsDemTerrainGenerator::~QgsDemTerrainGenerator
~QgsDemTerrainGenerator() override
Definition: qgsdemterraingenerator.cpp:23
QgsDemTerrainGenerator::writeXml
void writeXml(QDomElement &elem) const override
Write terrain generator's configuration to XML.
Definition: qgsdemterraingenerator.cpp:77
QgsDemTerrainGenerator::extent
QgsRectangle extent() const override
extent of the terrain in terrain's CRS
Definition: qgsdemterraingenerator.cpp:63
QgsDemTerrainGenerator::QgsDemTerrainGenerator
QgsDemTerrainGenerator()=default
Constructor for QgsDemTerrainGenerator.
QgsTerrainGenerator::mIsValid
bool mIsValid
Definition: qgsterraingenerator.h:124
QgsTilingScheme
The class encapsulates tiling scheme (just like with WMTS / TMS / XYZ layers).
Definition: qgstilingscheme.h:37
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
QgsTerrainGenerator::setTerrain
void setTerrain(QgsTerrainEntity *t)
Sets terrain entity for the generator (does not transfer ownership)
Definition: qgsterraingenerator.h:64