QGIS API Documentation  3.2.0-Bonn (bc43194)
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  return mHeightMapGenerator->heightAt( x, y );
70 }
71 
72 void QgsDemTerrainGenerator::writeXml( QDomElement &elem ) const
73 {
74  elem.setAttribute( "layer", mLayer.layerId );
75  elem.setAttribute( "resolution", mResolution );
76  elem.setAttribute( "skirt-height", mSkirtHeight );
77 
78  // crs is not read/written - it should be the same as destination crs of the map
79 }
80 
81 void QgsDemTerrainGenerator::readXml( const QDomElement &elem )
82 {
83  mLayer = QgsMapLayerRef( elem.attribute( "layer" ) );
84  mResolution = elem.attribute( "resolution" ).toInt();
85  mSkirtHeight = elem.attribute( "skirt-height" ).toFloat();
86 
87  // crs is not read/written - it should be the same as destination crs of the map
88 }
89 
91 {
92  mLayer = QgsMapLayerRef( project.mapLayer( mLayer.layerId ) );
93  updateGenerator();
94 }
95 
96 QgsChunkLoader *QgsDemTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
97 {
98  return new QgsDemTerrainTileLoader( mTerrain, node );
99 }
100 
101 void QgsDemTerrainGenerator::updateGenerator()
102 {
103  QgsRasterLayer *dem = layer();
104  if ( dem )
105  {
106  QgsRectangle te = dem->extent();
107  QgsCoordinateTransform terrainToMapTransform( dem->crs(), mCrs, mTransformContext );
108  te = terrainToMapTransform.transformBoundingBox( te );
109 
111  delete mHeightMapGenerator;
112  mHeightMapGenerator = new QgsDemHeightMapGenerator( dem, mTerrainTilingScheme, mResolution );
113  }
114  else
115  {
117  delete mHeightMapGenerator;
118  mHeightMapGenerator = nullptr;
119  }
120 }
A rectangle specified with double values.
Definition: qgsrectangle.h:40
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
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
Reads and writes project states.
Definition: qgsproject.h:85
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.