QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
17
19
20#include "qgsrasterlayer.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->mExtent = mExtent;
55 cloned->updateGenerator();
56 return cloned;
57}
58
60{
62}
63
65{
66 return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
67}
68
69float QgsDemTerrainGenerator::heightAt( double x, double y, const Qgs3DMapSettings &map ) const
70{
71 Q_UNUSED( map )
72 if ( mHeightMapGenerator )
73 return mHeightMapGenerator->heightAt( x, y );
74 else
75 return 0;
76}
77
78void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
79{
80 elem.setAttribute( QStringLiteral( "layer" ), mLayer.layerId );
81 elem.setAttribute( QStringLiteral( "resolution" ), mResolution );
82 elem.setAttribute( QStringLiteral( "skirt-height" ), mSkirtHeight );
83
84 // crs is not read/written - it should be the same as destination crs of the map
85}
86
87void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
88{
89 mLayer = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
90 mResolution = elem.attribute( QStringLiteral( "resolution" ) ).toInt();
91 mSkirtHeight = elem.attribute( QStringLiteral( "skirt-height" ) ).toFloat();
92
93 // crs is not read/written - it should be the same as destination crs of the map
94}
95
97{
98 mLayer = QgsMapLayerRef( project.mapLayer( mLayer.layerId ) );
99 // now that we have the layer, call setExtent() again so we can keep the intersection of mExtent and layer's extent
101}
102
103QgsChunkLoader *QgsDemTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
104{
105 // A bit of a hack to make cloning terrain generator work properly
106 return new QgsDemTerrainTileLoader( mTerrain, node, const_cast<QgsDemTerrainGenerator *>( this ) );
107}
108
110{
111 if ( !mLayer )
112 {
113 // Keep the whole extent for now and setExtent() will be called by again by resolveReferences()
114 mExtent = extent;
115 return;
116 }
117
118 QgsRectangle layerExtent = mLayer->extent();
119 if ( mCrs != mLayer->crs() )
120 {
121 QgsCoordinateTransform ct( mLayer->crs(), mCrs, mTransformContext );
123 try
124 {
125 layerExtent = ct.transformBoundingBox( layerExtent );
126 }
127 catch ( const QgsCsException & )
128 {
129 QgsDebugMsg( QStringLiteral( "Transformation of layer extent to terrain crs failed." ) );
130 }
131 }
132 // no need to have an mExtent larger than the actual layer's extent
133 mExtent = extent.intersect( layerExtent );
134 updateGenerator();
135}
136
137void QgsDemTerrainGenerator::updateGenerator()
138{
139 QgsRasterLayer *dem = layer();
140 if ( dem )
141 {
143 delete mHeightMapGenerator;
144 mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution, mTransformContext );
145 mIsValid = true;
146 }
147 else
148 {
150 delete mHeightMapGenerator;
151 mHeightMapGenerator = nullptr;
152 mIsValid = false;
153 }
154}
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.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
float heightAt(double x, double y, const Qgs3DMapSettings &map) const override
Returns height at (x,y) in terrain's CRS.
QgsRectangle rootChunkExtent() const override
extent of the terrain's root chunk 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.
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 setExtent(const QgsRectangle &extent) override
sets the extent of the terrain in terrain's CRS
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:105
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
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:333
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.
virtual QgsRectangle extent() const
extent of the terrain in terrain's CRS, might be non-square and smaller than rootChunkExtent()
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.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
_LayerRef< QgsMapLayer > QgsMapLayerRef
const QgsCoordinateReferenceSystem & crs
QPointer< TYPE > layer
Weak pointer to map layer.
QString layerId
Original layer ID.