QGIS API Documentation  3.6.0-Noosa (5873452)
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 );
116  }
117  else
118  {
120  delete mHeightMapGenerator;
121  mHeightMapGenerator = nullptr;
122  }
123 }
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsTerrainGenerator * clone() const override
Makes a copy of the current instance.
_LayerRef< QgsMapLayer > QgsMapLayerRef
QgsDemTerrainGenerator()=default
Constructor for QgsDemTerrainGenerator.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
float heightAt(double x, double y, const Qgs3DMapSettings &map) const override
Returns height at (x,y) in terrain&#39;s CRS.
3 Definition of the world
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString layerId
Original layer ID.
Terrain is built from raster layer with digital elevation model.
void setLayer(QgsRasterLayer *layer)
Sets raster layer with elevation model to be used for terrain generation.
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets CRS of the terrain.
QPointer< TYPE > layer
Weak pointer to map layer.
void writeXml(QDomElement &elem) const override
Write terrain generator&#39;s configuration to XML.
QgsRectangle extent() const override
extent of the terrain in terrain&#39;s CRS
QgsTerrainEntity * mTerrain
Reads and writes project states.
Definition: qgsproject.h:89
Contains information about the context in which a coordinate transform is executed.
void readXml(const QDomElement &elem) override
Read terrain generator&#39;s configuration from XML.
Type
Enumeration of the available terrain generators.
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
3 Base class for generators of terrain.
3 Implementation of terrain generator that uses a raster layer with DEM to build terrain.
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override
QgsRasterLayer * layer() const
Returns raster layer with elevation model to be used for terrain generation.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
void resolveReferences(const QgsProject &project) override
After read of XML, resolve references to any layers that have been read as layer IDs.
3 The class encapsulates tiling scheme (just like with WMTS / TMS / XYZ layers).
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Type type() const override
What texture generator implementation is this.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:71