QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsdemterrainsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdemterrainsettings.cpp
3 --------------------------------------
4 Date : August 2024
5 Copyright : (C) 2024 by Nyall Dawson
6 Email : nyall dot dawson 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#include "qgsrasterlayer.h"
19#include "qgs3drendercontext.h"
20
25
30
32{
33 return QStringLiteral( "dem" );
34}
35
36void QgsDemTerrainSettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
37{
38 if ( element.hasAttribute( QStringLiteral( "layer" ) ) )
39 {
40 mLayer = QgsMapLayerRef( element.attribute( QStringLiteral( "layer" ) ) );
41 mResolution = element.attribute( QStringLiteral( "resolution" ) ).toInt();
42 mSkirtHeight = element.attribute( QStringLiteral( "skirt-height" ) ).toDouble();
43 }
44 else
45 {
46 // restore old project
47 const QDomElement elemTerrainGenerator = element.firstChildElement( QStringLiteral( "generator" ) );
48 mLayer = QgsMapLayerRef( elemTerrainGenerator.attribute( QStringLiteral( "layer" ) ) );
49 mResolution = elemTerrainGenerator.attribute( QStringLiteral( "resolution" ) ).toInt();
50 mSkirtHeight = elemTerrainGenerator.attribute( QStringLiteral( "skirt-height" ) ).toDouble();
51 }
52
53 readCommonProperties( element, context );
54}
55
56void QgsDemTerrainSettings::writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
57{
58 element.setAttribute( QStringLiteral( "layer" ), mLayer.layerId );
59 element.setAttribute( QStringLiteral( "resolution" ), mResolution );
60 element.setAttribute( QStringLiteral( "skirt-height" ), mSkirtHeight );
61 writeCommonProperties( element, context );
62}
63
65{
66 mLayer.resolve( project );
67}
68
70{
71 const QgsDemTerrainSettings *otherTerrain = dynamic_cast<const QgsDemTerrainSettings *>( other );
72 if ( !otherTerrain )
73 return false;
74
75 if ( !equalsCommon( other ) )
76 return false;
77
78 return mResolution == otherTerrain->mResolution
79 && qgsDoubleNear( mSkirtHeight, otherTerrain->mSkirtHeight )
80 && mLayer.layerId == otherTerrain->mLayer.layerId;
81}
82
83std::unique_ptr<QgsTerrainGenerator> QgsDemTerrainSettings::createTerrainGenerator( const Qgs3DRenderContext &context ) const
84{
85 std::unique_ptr<QgsDemTerrainGenerator> generator = std::make_unique<QgsDemTerrainGenerator>();
86 generator->setCrs( context.crs(), context.transformContext() );
87 generator->setExtent( context.extent() );
88 generator->setLayer( layer() );
89 generator->setResolution( mResolution );
90 generator->setSkirtHeight( static_cast<float>( mSkirtHeight ) );
91 return generator;
92}
93
98
100{
101 return qobject_cast<QgsRasterLayer *>( mLayer.layer.data() );
102}
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system used in the 3D scene.
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene's CRS.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
void writeCommonProperties(QDomElement &element, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
bool equalsCommon(const QgsAbstractTerrainSettings *other) const
Returns true if common base class settings from other match this object.
void readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common properties from the base class from the given DOM element.
QgsDemTerrainSettings * clone() const final
Returns a copy of the terrain settings.
bool equals(const QgsAbstractTerrainSettings *other) const final
Returns true if this settings is exactly equal to another other settings.
QString type() const final
Returns the unique type name for the terrain generator.
static QgsAbstractTerrainSettings * create()
Creates a new instance of a QgsDemTerrainSettings object.
QgsRasterLayer * layer() const
Returns the raster layer with elevation model to be used for terrain generation.
void resolveReferences(const QgsProject *project) final
After reading settings from XML, resolves references to any layers in a project that have been read a...
void writeXml(QDomElement &element, const QgsReadWriteContext &context) const final
Writes settings to a DOM element.
void setLayer(QgsRasterLayer *layer)
Sets the raster layer with elevation model to be used for terrain generation.
void readXml(const QDomElement &element, const QgsReadWriteContext &context) final
Reads settings from a DOM element.
std::unique_ptr< QgsTerrainGenerator > createTerrainGenerator(const Qgs3DRenderContext &context) const override
Creates a new instance of a terrain generator which matches the terrain settings.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Represents a raster layer.
The class is used as a container of context for various read/write operations on other objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6024
_LayerRef< QgsMapLayer > QgsMapLayerRef
QPointer< TYPE > layer
Weak pointer to map layer.
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
QString layerId
Original layer ID.