QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
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 "qgs3dutils.h"
21#include "qgsrasterlayer.h"
23
25{
26 delete mHeightMapGenerator;
27}
28
30{
31 mLayer = QgsMapLayerRef( layer );
32 updateGenerator();
33}
34
36{
37 return qobject_cast<QgsRasterLayer *>( mLayer.layer.data() );
38}
39
41{
42 mCrs = crs;
43 mTransformContext = context;
44 updateGenerator();
45}
46
48{
50 cloned->setTerrain( mTerrain );
51 cloned->mCrs = mCrs;
52 cloned->mLayer = mLayer;
53 cloned->mResolution = mResolution;
54 cloned->mSkirtHeight = mSkirtHeight;
55 cloned->mExtent = mExtent;
56 cloned->updateGenerator();
57 return cloned;
58}
59
64
69
70float QgsDemTerrainGenerator::heightAt( double x, double y, const Qgs3DMapSettings &map ) const
71{
72 Q_UNUSED( map )
73 if ( mHeightMapGenerator )
74 return mHeightMapGenerator->heightAt( x, y );
75 else
76 return 0;
77}
78
79void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
80{
81 elem.setAttribute( QStringLiteral( "layer" ), mLayer.layerId );
82 elem.setAttribute( QStringLiteral( "resolution" ), mResolution );
83 elem.setAttribute( QStringLiteral( "skirt-height" ), mSkirtHeight );
84
85 // crs is not read/written - it should be the same as destination crs of the map
86}
87
88void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
89{
90 mLayer = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
91 mResolution = elem.attribute( QStringLiteral( "resolution" ) ).toInt();
92 mSkirtHeight = elem.attribute( QStringLiteral( "skirt-height" ) ).toFloat();
93
94 // crs is not read/written - it should be the same as destination crs of the map
95}
96
98{
99 mLayer = QgsMapLayerRef( project.mapLayer( mLayer.layerId ) );
100 // now that we have the layer, call setExtent() again so we can keep the intersection of mExtent and layer's extent
102}
103
104QgsChunkLoader *QgsDemTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
105{
106 // A bit of a hack to make cloning terrain generator work properly
107 return new QgsDemTerrainTileLoader( mTerrain, node, const_cast<QgsDemTerrainGenerator *>( this ) );
108}
109
111{
112 if ( !mLayer )
113 {
114 // Keep the whole extent for now and setExtent() will be called by again by resolveReferences()
115 mExtent = extent;
116 return;
117 }
118
119 QgsRectangle layerExtent = Qgs3DUtils::tryReprojectExtent2D( mLayer->extent(), mLayer->crs(), mCrs, mTransformContext );
120 // no need to have an mExtent larger than the actual layer's extent
121 mExtent = extent.intersect( layerExtent );
122 updateGenerator();
123}
124
125void QgsDemTerrainGenerator::updateGenerator()
126{
127 QgsRasterLayer *dem = layer();
128 if ( dem )
129 {
131 delete mHeightMapGenerator;
132 mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution, mTransformContext );
133 mIsValid = true;
134 }
135 else
136 {
138 delete mHeightMapGenerator;
139 mHeightMapGenerator = nullptr;
140 mIsValid = false;
141 }
142}
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
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:82
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
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.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
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.
_LayerRef< QgsMapLayer > QgsMapLayerRef
const QgsCoordinateReferenceSystem & crs
QPointer< TYPE > layer
Weak pointer to map layer.
QString layerId
Original layer ID.