QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 }
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:79
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:101
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.