QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
23 {
24  delete mHeightMapGenerator;
25 }
26 
28 {
29  mLayer = QgsMapLayerRef( layer );
30  updateGenerator();
31 }
32 
34 {
35  return qobject_cast<QgsRasterLayer *>( mLayer.layer.data() );
36 }
37 
39 {
40  mCrs = crs;
41  mTransformContext = context;
42  updateGenerator();
43 }
44 
46 {
48  cloned->setTerrain( mTerrain );
49  cloned->mCrs = mCrs;
50  cloned->mLayer = mLayer;
51  cloned->mResolution = mResolution;
52  cloned->mSkirtHeight = mSkirtHeight;
53  cloned->updateGenerator();
54  return cloned;
55 }
56 
58 {
60 }
61 
63 {
64  return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
65 }
66 
67 float QgsDemTerrainGenerator::heightAt( double x, double y, const Qgs3DMapSettings &map ) const
68 {
69  Q_UNUSED( map )
70  if ( mHeightMapGenerator )
71  return mHeightMapGenerator->heightAt( x, y );
72  else
73  return 0;
74 }
75 
76 void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
77 {
78  elem.setAttribute( QStringLiteral( "layer" ), mLayer.layerId );
79  elem.setAttribute( QStringLiteral( "resolution" ), mResolution );
80  elem.setAttribute( QStringLiteral( "skirt-height" ), mSkirtHeight );
81 
82  // crs is not read/written - it should be the same as destination crs of the map
83 }
84 
85 void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
86 {
87  mLayer = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
88  mResolution = elem.attribute( QStringLiteral( "resolution" ) ).toInt();
89  mSkirtHeight = elem.attribute( QStringLiteral( "skirt-height" ) ).toFloat();
90 
91  // crs is not read/written - it should be the same as destination crs of the map
92 }
93 
95 {
96  mLayer = QgsMapLayerRef( project.mapLayer( mLayer.layerId ) );
97  updateGenerator();
98 }
99 
100 QgsChunkLoader *QgsDemTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
101 {
102  // A bit of a hack to make cloning terrain generator work properly
103  return new QgsDemTerrainTileLoader( mTerrain, node, const_cast<QgsDemTerrainGenerator *>( this ) );
104 }
105 
106 void QgsDemTerrainGenerator::updateGenerator()
107 {
108  QgsRasterLayer *dem = layer();
109  if ( dem )
110  {
111  QgsRectangle te = dem->extent();
112  QgsCoordinateTransform terrainToMapTransform( dem->crs(), mCrs, mTransformContext );
113  te = terrainToMapTransform.transformBoundingBox( te );
114 
116  delete mHeightMapGenerator;
117  mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution, mTransformContext );
118  mIsValid = true;
119  }
120  else
121  {
123  delete mHeightMapGenerator;
124  mHeightMapGenerator = nullptr;
125  mIsValid = false;
126  }
127 }
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
float heightAt(double x, double y, const Qgs3DMapSettings &map) const override
Returns height at (x,y) in terrain's CRS.
Type type() const override
What texture generator implementation is this.
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets CRS of the terrain.
void readXml(const QDomElement &elem) override
Read terrain generator's configuration from XML.
QgsTerrainGenerator * clone() const override
Makes a copy of the current instance.
QgsRectangle extent() const override
extent of the terrain in terrain's CRS
void resolveReferences(const QgsProject &project) override
After read of XML, resolve references to any layers that have been read as layer IDs.
QgsRasterLayer * layer() const
Returns raster layer with elevation model to be used for terrain generation.
void writeXml(QDomElement &elem) const override
Write terrain generator's configuration to XML.
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override
void setLayer(QgsRasterLayer *layer)
Sets raster layer with elevation model to be used for terrain generation.
QgsDemTerrainGenerator()=default
Constructor for QgsDemTerrainGenerator.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:76
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Represents a raster layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Type
Enumeration of the available terrain generators.
@ Dem
Terrain is built from raster layer with digital elevation model.
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
QgsTerrainEntity * mTerrain
void setTerrain(QgsTerrainEntity *t)
Sets terrain entity for the generator (does not transfer ownership)
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
_LayerRef< QgsMapLayer > QgsMapLayerRef
const QgsCoordinateReferenceSystem & crs
QPointer< TYPE > layer
Weak pointer to map layer.
QString layerId
Original layer ID.