QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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->mCrs = mCrs;
49  cloned->mLayer = mLayer;
50  cloned->mResolution = mResolution;
51  cloned->mSkirtHeight = mSkirtHeight;
52  cloned->updateGenerator();
53  return cloned;
54 }
55 
57 {
59 }
60 
62 {
63  return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
64 }
65 
66 float QgsDemTerrainGenerator::heightAt( double x, double y, const Qgs3DMapSettings &map ) const
67 {
68  Q_UNUSED( map )
69  if ( mHeightMapGenerator )
70  return mHeightMapGenerator->heightAt( x, y );
71  else
72  return 0;
73 }
74 
75 void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
76 {
77  elem.setAttribute( QStringLiteral( "layer" ), mLayer.layerId );
78  elem.setAttribute( QStringLiteral( "resolution" ), mResolution );
79  elem.setAttribute( QStringLiteral( "skirt-height" ), mSkirtHeight );
80 
81  // crs is not read/written - it should be the same as destination crs of the map
82 }
83 
84 void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
85 {
86  mLayer = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
87  mResolution = elem.attribute( QStringLiteral( "resolution" ) ).toInt();
88  mSkirtHeight = elem.attribute( QStringLiteral( "skirt-height" ) ).toFloat();
89 
90  // crs is not read/written - it should be the same as destination crs of the map
91 }
92 
94 {
95  mLayer = QgsMapLayerRef( project.mapLayer( mLayer.layerId ) );
96  updateGenerator();
97 }
98 
99 QgsChunkLoader *QgsDemTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
100 {
101  return new QgsDemTerrainTileLoader( mTerrain, node );
102 }
103 
104 void QgsDemTerrainGenerator::updateGenerator()
105 {
106  QgsRasterLayer *dem = layer();
107  if ( dem )
108  {
109  QgsRectangle te = dem->extent();
110  QgsCoordinateTransform terrainToMapTransform( dem->crs(), mCrs, mTransformContext );
111  te = terrainToMapTransform.transformBoundingBox( te );
112 
114  delete mHeightMapGenerator;
115  mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution, mTransformContext );
116  mIsValid = true;
117  }
118  else
119  {
121  delete mHeightMapGenerator;
122  mHeightMapGenerator = nullptr;
123  mIsValid = false;
124  }
125 }
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:88
QgsTerrainGenerator::Dem
@ Dem
Terrain is built from raster layer with digital elevation model.
Definition: qgsterraingenerator.h:55
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
qgsrasterlayer.h
QgsTerrainGenerator::crs
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
Definition: qgsterraingenerator.h:100
QgsDemTerrainGenerator::createChunkLoader
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override
Definition: qgsdemterraingenerator.cpp:99
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:84
qgsdemterraingenerator.h
QgsMapLayerRef
_LayerRef< QgsMapLayer > QgsMapLayerRef
Definition: qgsmaplayerref.h:294
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3124
_LayerRef::layer
QPointer< TYPE > layer
Weak pointer to map layer.
Definition: qgsmaplayerref.h:113
QgsRectangle
Definition: qgsrectangle.h:41
QgsProject
Definition: qgsproject.h:92
QgsDemTerrainGenerator::type
Type type() const override
What texture generator implementation is this.
Definition: qgsdemterraingenerator.cpp:56
_LayerRef::layerId
QString layerId
Original layer ID.
Definition: qgsmaplayerref.h:116
QgsTerrainGenerator::mTerrain
QgsTerrainEntity * mTerrain
Definition: qgsterraingenerator.h:107
QgsTerrainGenerator::mTerrainTilingScheme
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
Definition: qgsterraingenerator.h:106
QgsDemTerrainGenerator::layer
QgsRasterLayer * layer() const
Returns raster layer with elevation model to be used for terrain generation.
Definition: qgsdemterraingenerator.cpp:33
QgsDemTerrainGenerator::clone
QgsTerrainGenerator * clone() const override
Makes a copy of the current instance.
Definition: qgsdemterraingenerator.cpp:45
QgsTerrainGenerator
Definition: qgsterraingenerator.h:47
QgsMapLayer::extent
virtual QgsRectangle extent() const
Returns the extent of the layer.
Definition: qgsmaplayer.cpp:197
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:66
qgsdemterraintileloader_p.h
QgsTerrainGenerator::Type
Type
Enumeration of the available terrain generators.
Definition: qgsterraingenerator.h:52
Qgs3DMapSettings
Definition: qgs3dmapsettings.h:51
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:93
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsTilingScheme::tileToExtent
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Definition: qgstilingscheme.cpp:42
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsDemTerrainGenerator
Definition: qgsdemterraingenerator.h:41
QgsDemTerrainGenerator::setCrs
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets CRS of the terrain.
Definition: qgsdemterraingenerator.cpp:38
QgsDemTerrainGenerator::setLayer
void setLayer(QgsRasterLayer *layer)
Sets raster layer with elevation model to be used for terrain generation.
Definition: qgsdemterraingenerator.cpp:27
QgsDemTerrainGenerator::~QgsDemTerrainGenerator
~QgsDemTerrainGenerator() override
Definition: qgsdemterraingenerator.cpp:22
QgsDemTerrainGenerator::writeXml
void writeXml(QDomElement &elem) const override
Write terrain generator's configuration to XML.
Definition: qgsdemterraingenerator.cpp:75
QgsDemTerrainGenerator::extent
QgsRectangle extent() const override
extent of the terrain in terrain's CRS
Definition: qgsdemterraingenerator.cpp:61
QgsDemTerrainGenerator::QgsDemTerrainGenerator
QgsDemTerrainGenerator()=default
Constructor for QgsDemTerrainGenerator.
QgsTerrainGenerator::mIsValid
bool mIsValid
Definition: qgsterraingenerator.h:109
QgsTilingScheme
Definition: qgstilingscheme.h:35
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52